private void executeUploadFromStream(@Nullable final ClientSession clientSession, final BsonValue id, final String filename, final InputStream source, final GridFSUploadOptions options) { GridFSUploadStream uploadStream = createGridFSUploadStream(clientSession, id, filename, options); Integer chunkSizeBytes = options.getChunkSizeBytes(); int chunkSize = chunkSizeBytes == null ? this.chunkSizeBytes : chunkSizeBytes; byte[] buffer = new byte[chunkSize]; int len; try { while ((len = source.read(buffer)) != -1) { uploadStream.write(buffer, 0, len); } uploadStream.close(); } catch (IOException e) { uploadStream.abort(); throw new MongoGridFSException("IOException when reading from the InputStream", e); } }
/** * Create a file in GridFS with the given filename and write * some random data to it. * @param filename the name of the file to create * @param size the number of random bytes to write * @param vertx the Vert.x instance * @param handler a handler that will be called when the file * has been written */ private void prepareData(String filename, int size, Vertx vertx, Handler<AsyncResult<String>> handler) { vertx.<String>executeBlocking(f -> { try (MongoClient client = new MongoClient(mongoConnector.serverAddress)) { MongoDatabase db = client.getDatabase(MongoDBTestConnector.MONGODB_DBNAME); GridFSBucket gridFS = GridFSBuckets.create(db); try (GridFSUploadStream os = gridFS.openUploadStream(filename)) { for (int i = 0; i < size; ++i) { os.write((byte)(i & 0xFF)); } } } f.complete(filename); }, handler); }
private void executeUploadFromStream(@Nullable final ClientSession clientSession, final BsonValue id, final String filename, final InputStream source, final GridFSUploadOptions options) { GridFSUploadStream uploadStream = createGridFSUploadStream(clientSession, id, filename, options); Integer chunkSizeBytes = options.getChunkSizeBytes(); int chunkSize = chunkSizeBytes == null ? this.chunkSizeBytes : chunkSizeBytes; byte[] buffer = new byte[chunkSize]; int len; try { while ((len = source.read(buffer)) != -1) { uploadStream.write(buffer, 0, len); } uploadStream.close(); } catch (IOException e) { uploadStream.abort(); throw new MongoGridFSException("IOException when reading from the InputStream", e); } }
private void executeUploadFromStream(@Nullable final ClientSession clientSession, final BsonValue id, final String filename, final InputStream source, final GridFSUploadOptions options) { GridFSUploadStream uploadStream = createGridFSUploadStream(clientSession, id, filename, options); Integer chunkSizeBytes = options.getChunkSizeBytes(); int chunkSize = chunkSizeBytes == null ? this.chunkSizeBytes : chunkSizeBytes; byte[] buffer = new byte[chunkSize]; int len; try { while ((len = source.read(buffer)) != -1) { uploadStream.write(buffer, 0, len); } uploadStream.close(); } catch (IOException e) { uploadStream.abort(); throw new MongoGridFSException("IOException when reading from the InputStream", e); } }