Search in sources :

Example 21 with MongoGridFSException

use of com.mongodb.MongoGridFSException in project mongo-java-driver by mongodb.

the class SyncGridFSBucket method inputStreamToFlux.

@SuppressWarnings("BlockingMethodInNonBlockingContext")
private Flux<ByteBuffer> inputStreamToFlux(final InputStream source, final GridFSUploadOptions options) {
    List<ByteBuffer> byteBuffers = new ArrayList<>();
    int chunkSize = options.getChunkSizeBytes() == null ? wrapped.getChunkSizeBytes() : options.getChunkSizeBytes();
    byte[] buffer = new byte[chunkSize];
    try {
        int len;
        while ((len = source.read(buffer)) != -1) {
            byteBuffers.add(ByteBuffer.wrap(buffer, 0, len));
            buffer = new byte[chunkSize];
        }
        return Flux.fromIterable(byteBuffers);
    } catch (IOException e) {
        throw new MongoGridFSException("IOException when reading from the InputStream", e);
    }
}
Also used : MongoGridFSException(com.mongodb.MongoGridFSException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 22 with MongoGridFSException

use of com.mongodb.MongoGridFSException in project mongo-java-driver by mongodb.

the class SyncGridFSBucket method toOutputStream.

private void toOutputStream(final GridFSDownloadPublisher downloadPublisher, final OutputStream destination) {
    Flux.from(downloadPublisher).toStream().forEach(byteBuffer -> {
        try {
            byte[] bytes = new byte[byteBuffer.remaining()];
            byteBuffer.get(bytes);
            destination.write(bytes);
        } catch (IOException e) {
            throw new MongoGridFSException("IOException when reading from the OutputStream", e);
        }
    });
}
Also used : MongoGridFSException(com.mongodb.MongoGridFSException) IOException(java.io.IOException)

Aggregations

MongoGridFSException (com.mongodb.MongoGridFSException)22 BsonDocument (org.bson.BsonDocument)11 GridFSFile (com.mongodb.client.gridfs.model.GridFSFile)6 IOException (java.io.IOException)6 Document (org.bson.Document)6 BsonString (org.bson.BsonString)5 DeleteResult (com.mongodb.client.result.DeleteResult)4 SingleResultCallback (com.mongodb.async.SingleResultCallback)3 UpdateResult (com.mongodb.client.result.UpdateResult)3 ByteBuffer (java.nio.ByteBuffer)3 BsonValue (org.bson.BsonValue)3 Binary (org.bson.types.Binary)3 Assertions.notNull (com.mongodb.assertions.Assertions.notNull)2 Nullable (com.mongodb.lang.Nullable)2 ClientSession (com.mongodb.reactivestreams.client.ClientSession)2 FindPublisher (com.mongodb.reactivestreams.client.FindPublisher)2 MongoCollection (com.mongodb.reactivestreams.client.MongoCollection)2 String.format (java.lang.String.format)2 BsonArray (org.bson.BsonArray)2 BsonObjectId (org.bson.BsonObjectId)2