List of usage examples for com.mongodb.client.gridfs GridFSUploadStream write
@Override public abstract void write(byte[] b, int off, int len);
From source file:UnitTest3.java
License:Open Source License
public static void testgridfs3() throws FileNotFoundException, IOException { long time1;//from ww w . java 2s. c o m long time2; long time3; long time4; int n = 0; // "/home/anees/workspace/testdata/in/App3.class" String fileName = "makezip4.rar"; MongoClient mongo = new MongoClient("localhost", 27017); //DB db = mongo.getDB("JFileDB"); MongoDatabase db = mongo.getDatabase("JFileDB"); GridFSBucket gridFSBucket = GridFSBuckets.create(db); // Get the input stream time1 = System.currentTimeMillis(); InputStream streamToUploadFrom = new FileInputStream("/home/anees/workspace/testdata/in/" + fileName); // Create some custom options GridFSUploadOptions options = new GridFSUploadOptions().chunkSizeBytes(1024 * 1024) .metadata(new Document("type", "class")); byte data[] = new byte[64 * 1024 * 1024]; GridFSUploadStream uploadStream = gridFSBucket.openUploadStream(fileName, options); while ((n = streamToUploadFrom.read(data)) > 0) { uploadStream.write(data, 0, n); } uploadStream.close(); time2 = System.currentTimeMillis(); time3 = System.currentTimeMillis(); Date date = new Date(); FileOutputStream streamToDownloadTo = new FileOutputStream( "/home/anees/workspace/testdata/out/" + fileName + "_" + date.toString()); GridFSDownloadByNameOptions downloadOptions = new GridFSDownloadByNameOptions().revision(-1); GridFSDownloadStream downloadStream = gridFSBucket.openDownloadStreamByName(fileName, downloadOptions); long fileLength = downloadStream.getGridFSFile().getLength(); byte[] bytesToWriteTo = new byte[64 * 1024 * 1024]; while ((n = downloadStream.read(bytesToWriteTo)) > 0) { streamToDownloadTo.write(bytesToWriteTo, 0, n); } downloadStream.close(); time4 = System.currentTimeMillis(); System.out.println("The fileId of the uploaded file is: " + uploadStream.getFileId().toHexString()); System.out.println("Upload time taken : time2 - time1 : " + (time2 - time1)); System.out.println("Download time taken : time4 - time3 : " + (time4 - time3)); streamToDownloadTo.close(); streamToUploadFrom.close(); mongo.close(); }
From source file:com.dilmus.dilshad.scabi.db.DBackFile.java
License:Open Source License
public long put(String fileName, String fullFilePath, String type, String contentType) throws IOException, DScabiException, ParseException { long time1;// ww w . j a v a 2 s . c om long time2; int n = 0; long total = 0; // Get the input stream time1 = System.currentTimeMillis(); InputStream fromStream = new FileInputStream(fullFilePath); byte data[] = new byte[m_bufferSize]; GridFSUploadStream uploadStream = m_gridFSBucket.openUploadStream(fileName, m_options); while ((n = fromStream.read(data)) > 0) { uploadStream.write(data, 0, n); total = total + n; } uploadStream.close(); fromStream.close(); updateMetaData(fileName, uploadStream.getFileId(), type, contentType); time2 = System.currentTimeMillis(); log.debug("put() The fileId of the uploaded file is: " + uploadStream.getFileId().toHexString()); log.debug("put() Upload time taken : time2 - time1 : " + (time2 - time1)); return total; }
From source file:com.dilmus.dilshad.scabi.db.DBackFile.java
License:Open Source License
public long put(String fileName, InputStream fromStream, String type, String contentType) throws IOException, DScabiException, ParseException { long time1;//from w w w.ja va2 s. c o m long time2; int n = 0; long total = 0; time1 = System.currentTimeMillis(); byte data[] = new byte[m_bufferSize]; GridFSUploadStream uploadStream = m_gridFSBucket.openUploadStream(fileName, m_options); while ((n = fromStream.read(data)) > 0) { uploadStream.write(data, 0, n); total = total + n; } uploadStream.close(); fromStream.close(); updateMetaData(fileName, uploadStream.getFileId(), type, contentType); time2 = System.currentTimeMillis(); log.debug("put() The fileId of the uploaded file is: " + uploadStream.getFileId().toHexString()); log.debug("put() Upload time taken : time2 - time1 : " + (time2 - time1)); return total; }