Package com.mongodb.gridfs
Class GridFSInputFile
java.lang.Object
com.mongodb.gridfs.GridFSFile
com.mongodb.gridfs.GridFSInputFile
- All Implemented Interfaces:
DBObject
,BSONObject
This class represents a GridFS file to be written to the database. Operations include:
- Writing data obtained from an InputStream
- Getting an OutputStream to stream the data out to
- MongoDB documentation
- GridFS
-
Constructor Summary
ModifierConstructorDescriptionprotected
GridFSInputFile
(GridFS gridFS) Minimal constructor that does not rely on the presence of anInputStream
.protected
GridFSInputFile
(GridFS gridFS, InputStream inputStream, String filename) Default constructor setting the GridFS file name and providing an input stream containing data to be written to the file.protected
GridFSInputFile
(GridFS gridFS, InputStream inputStream, String filename, boolean closeStreamOnPersist) Default constructor setting the GridFS file name and providing an input stream containing data to be written to the file.protected
GridFSInputFile
(GridFS gridFS, String filename) Constructor that only provides a file name, but does not rely on the presence of anInputStream
. -
Method Summary
Modifier and TypeMethodDescriptionprotected DBObject
createChunk
(Object id, int currentChunkNumber, byte[] writeBuffer) Creates a new chunk of this file.After retrieving thisOutputStream
, this object will be capable of accepting successively written data to the output stream.void
save()
Callssave(long)
with the existing chunk size.void
save
(long chunkSize) This method first calls saveChunks(long) if the file data has not been saved yet.int
Saves all data into chunks from configuredInputStream
input stream to GridFS.int
saveChunks
(long chunkSize) Saves all data into chunks from configuredInputStream
input stream to GridFS.void
setChunkSize
(long chunkSize) Set the chunk size.void
setContentType
(String contentType) Sets the content type (MIME type) on the GridFS entry.void
setFilename
(String filename) Sets the file name on the GridFS entry.void
Sets the ID of this GridFS file.Methods inherited from class com.mongodb.gridfs.GridFSFile
containsField, get, getAliases, getChunkSize, getContentType, getFilename, getGridFS, getId, getLength, getMetaData, getUploadDate, isPartialObject, keySet, markAsPartialObject, numChunks, put, putAll, putAll, removeField, setGridFS, setMetaData, toMap, toString
-
Constructor Details
-
GridFSInputFile
protected GridFSInputFile(GridFS gridFS, @Nullable InputStream inputStream, @Nullable String filename, boolean closeStreamOnPersist) Default constructor setting the GridFS file name and providing an input stream containing data to be written to the file.- Parameters:
gridFS
- The GridFS connection handle.inputStream
- Stream used for reading data from.filename
- Name of the file to be created.closeStreamOnPersist
- indicate the passed in input stream should be closed once the data chunk persisted
-
GridFSInputFile
protected GridFSInputFile(GridFS gridFS, @Nullable InputStream inputStream, @Nullable String filename) Default constructor setting the GridFS file name and providing an input stream containing data to be written to the file.- Parameters:
gridFS
- The GridFS connection handle.inputStream
- Stream used for reading data from.filename
- Name of the file to be created.
-
GridFSInputFile
Constructor that only provides a file name, but does not rely on the presence of anInputStream
. AnOutputStream
can later be obtained for writing using thegetOutputStream()
method.- Parameters:
gridFS
- The GridFS connection handle.filename
- Name of the file to be created.
-
GridFSInputFile
Minimal constructor that does not rely on the presence of anInputStream
. AnOutputStream
can later be obtained for writing using thegetOutputStream()
method.- Parameters:
gridFS
- The GridFS connection handle.
-
-
Method Details
-
setId
Sets the ID of this GridFS file.- Parameters:
id
- the file's ID.
-
setFilename
Sets the file name on the GridFS entry.- Parameters:
filename
- File name.
-
setContentType
Sets the content type (MIME type) on the GridFS entry.- Parameters:
contentType
- Content type.
-
setChunkSize
public void setChunkSize(long chunkSize) Set the chunk size. This must be called before saving any data.- Parameters:
chunkSize
- The size in bytes.
-
save
public void save()Callssave(long)
with the existing chunk size.- Overrides:
save
in classGridFSFile
- Throws:
MongoException
- if there's a problem saving the file.
-
save
public void save(long chunkSize) This method first calls saveChunks(long) if the file data has not been saved yet. Then it persists the file entry to GridFS.- Parameters:
chunkSize
- Size of chunks for file in bytes.- Throws:
MongoException
- if there's a problem saving the file.
-
saveChunks
Saves all data into chunks from configuredInputStream
input stream to GridFS.- Returns:
- Number of the next chunk.
- Throws:
IOException
- on problems reading the new entry'sInputStream
.MongoException
- if there's a failure- See Also:
-
saveChunks
Saves all data into chunks from configuredInputStream
input stream to GridFS. A non-default chunk size can be specified. This method does NOT save the file object itself, one must call save() to do so.- Parameters:
chunkSize
- Size of chunks for file in bytes.- Returns:
- Number of the next chunk.
- Throws:
IOException
- on problems reading the new entry'sInputStream
.MongoException
- if there's a failure
-
getOutputStream
After retrieving thisOutputStream
, this object will be capable of accepting successively written data to the output stream. To completely persist this GridFS object, you must finally call theOutputStream.close()
method on the output stream. Note that calling the save() and saveChunks() methods will throw Exceptions once you obtained the OutputStream.- Returns:
- Writable stream object.
-
createChunk
Creates a new chunk of this file. Can be over-ridden, if input files need to be split into chunks using a different mechanism.- Parameters:
id
- the file IDcurrentChunkNumber
- the unique id for this chunkwriteBuffer
- the byte array containing the data for this chunk- Returns:
- a DBObject representing this chunk.
-