/** * Finds one file matching the given objectId. Equivalent to findOne(objectId). * * @param objectId the objectId of the file stored on a server * @return a gridfs file * @throws com.mongodb.MongoException if the operation fails */ public GridFSDBFile find(final ObjectId objectId) { return findOne(objectId); }
/** * Creates a file entry. After calling this method, you have to call {@link com.mongodb.gridfs.GridFSInputFile#save()}. * * @param in an inputstream containing the file's data * @param filename the file name as stored in the db * @return a gridfs input file */ public GridFSInputFile createFile(final InputStream in, final String filename) { return new GridFSInputFile(this, in, filename); }
/** * Returns an InputStream from which data can be read. * * @return the input stream */ public InputStream getInputStream() { return new GridFSInputStream(); }
/** * Creates a file entry. After calling this method, you have to call {@link com.mongodb.gridfs.GridFSInputFile#save()}. * * @param in an inputstream containing the file's data * @return a gridfs input file */ public GridFSInputFile createFile(final InputStream in) { return createFile(in, null); }
/** * Calls {@link GridFSInputFile#save(long)} with the existing chunk size. * * @throws MongoException if there's a problem saving the file. */ @Override public void save() { save(chunkSize); }
/** * Removes all files matching the given query. * * @param query filter to apply * @throws com.mongodb.MongoException if the operation fails */ public void remove(final DBObject query) { if (query == null) { throw new IllegalArgumentException("query can not be null"); } for (final GridFSDBFile f : find(query)) { f.remove(); } }
/** * Finds a list of files matching the given query. * * @param query the filter to apply * @return list of gridfs files * @throws com.mongodb.MongoException if the operation fails */ public List<GridFSDBFile> find(final DBObject query) { return find(query, null); }
GridFSInputStream() { this.numberOfChunks = numChunks(); }
@Override public int read() { byte[] b = new byte[1]; int res = read(b); if (res < 0) { return -1; } return b[0] & 0xFF; }
/** * Saves all data into chunks from configured {@link java.io.InputStream} input stream to GridFS. * * @return Number of the next chunk. * @throws IOException on problems reading the new entry's {@link java.io.InputStream}. * @throws MongoException if there's a failure * @see com.mongodb.gridfs.GridFSInputFile#saveChunks(long) */ public int saveChunks() throws IOException { return saveChunks(chunkSize); }
@Override public void write(final int b) throws IOException { byte[] byteArray = new byte[1]; byteArray[0] = (byte) (b & 0xff); write(byteArray, 0, 1); }
/** * After retrieving this {@link java.io.OutputStream}, this object will be capable of accepting successively written data to the output * stream. To completely persist this GridFS object, you must finally call the {@link java.io.OutputStream#close()} method on the output * stream. Note that calling the save() and saveChunks() methods will throw Exceptions once you obtained the OutputStream. * * @return Writable stream object. */ public OutputStream getOutputStream() { if (outputStream == null) { outputStream = new GridFSOutputStream(); } return outputStream; }
/** * Creates a file entry. After calling this method, you have to call {@link com.mongodb.gridfs.GridFSInputFile#save()}. * * @param in an inputstream containing the file's data * @param closeStreamOnPersist indicate the passed in input stream should be closed once the data chunk persisted * @return a gridfs input file */ public GridFSInputFile createFile(final InputStream in, final boolean closeStreamOnPersist) { return createFile(in, null, closeStreamOnPersist); }
/** * Finds a list of files matching the given filename. * * @param filename the filename to look for * @return list of gridfs files * @throws com.mongodb.MongoException if the operation fails */ public List<GridFSDBFile> find(final String filename) { return find(new BasicDBObject("filename", filename)); }
/** * Creates a file entry. * * @param filename the file name as stored in the db * @return a gridfs input file * @see GridFS#createFile() */ public GridFSInputFile createFile(final String filename) { return new GridFSInputFile(this, filename); }
@Override public int read(final byte[] b) { return read(b, 0, b.length); }
/** * Creates a file entry. After calling this method, you have to call {@link com.mongodb.gridfs.GridFSInputFile#save()}. * * @param data the file's data * @return a gridfs input file */ public GridFSInputFile createFile(final byte[] data) { return createFile(new ByteArrayInputStream(data), true); }
/** * This method creates an empty {@link GridFSInputFile} instance. On this instance an {@link java.io.OutputStream} can be obtained using * the {@link GridFSInputFile#getOutputStream()} method. You can still call {@link GridFSInputFile#setContentType(String)} and {@link * GridFSInputFile#setFilename(String)}. The file will be completely written and closed after calling the {@link * java.io.OutputStream#close()} method on the output stream. * * @return GridFS file handle instance. */ public GridFSInputFile createFile() { return new GridFSInputFile(this); }
/** * Creates a file entry. After calling this method, you have to call {@link com.mongodb.gridfs.GridFSInputFile#save()}. * * @param in an inputstream containing the file's data * @param filename the file name as stored in the db * @param closeStreamOnPersist indicate the passed in input stream should be closed once the data chunk persisted * @return a gridfs input file */ public GridFSInputFile createFile(final InputStream in, final String filename, final boolean closeStreamOnPersist) { return new GridFSInputFile(this, in, filename, closeStreamOnPersist); }