com.mongodb.gridfs
Class GridFSInputFile

java.lang.Object
  extended by com.mongodb.gridfs.GridFSFile
      extended by com.mongodb.gridfs.GridFSInputFile
All Implemented Interfaces:
DBObject, BSONObject

public class GridFSInputFile
extends GridFSFile

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

Author:
Eliot Horowitz and Guy K. Kloss

Field Summary
 
Fields inherited from class com.mongodb.gridfs.GridFSFile
_fs
 
Constructor Summary
protected GridFSInputFile(GridFS fs)
          Minimal constructor that does not rely on the presence of an InputStream.
protected GridFSInputFile(GridFS fs, InputStream in, 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 fs, InputStream in, 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 fs, String filename)
          Constructor that only provides a file name, but does not rely on the presence of an InputStream.
 
Method Summary
protected  DBObject createChunk(Object id, int currentChunkNumber, byte[] writeBuffer)
           
 OutputStream getOutputStream()
          After retrieving this OutputStream, this object will be capable of accepting successively written data to the output stream.
 void save()
          calls save(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 saveChunks()
           
 int saveChunks(long chunkSize)
          Saves all data into chunks from configured InputStream input stream to GridFS.
 void setChunkSize(long chunkSize)
          Set the chunk size.
 void setContentType(String ct)
          Sets the content type (MIME type) on the GridFS entry.
 void setFilename(String fn)
          Sets the file name on the GridFS entry.
 void setId(Object id)
           
 
Methods inherited from class com.mongodb.gridfs.GridFSFile
containsField, containsKey, get, getAliases, getChunkSize, getContentType, getFilename, getId, getLength, getMD5, getMetaData, getUploadDate, isPartialObject, keySet, markAsPartialObject, numChunks, put, putAll, putAll, removeField, setGridFS, setMetaData, toMap, toString, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

GridFSInputFile

protected GridFSInputFile(GridFS fs,
                          InputStream in,
                          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:
fs - The GridFS connection handle.
in - 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 fs,
                          InputStream in,
                          String filename)
Default constructor setting the GridFS file name and providing an input stream containing data to be written to the file.

Parameters:
fs - The GridFS connection handle.
in - Stream used for reading data from.
filename - Name of the file to be created.

GridFSInputFile

protected GridFSInputFile(GridFS fs,
                          String filename)
Constructor that only provides a file name, but does not rely on the presence of an InputStream. An OutputStream can later be obtained for writing using the getOutputStream() method.

Parameters:
fs - The GridFS connection handle.
filename - Name of the file to be created.

GridFSInputFile

protected GridFSInputFile(GridFS fs)
Minimal constructor that does not rely on the presence of an InputStream. An OutputStream can later be obtained for writing using the getOutputStream() method.

Parameters:
fs - The GridFS connection handle.
Method Detail

setId

public void setId(Object id)

setFilename

public void setFilename(String fn)
Sets the file name on the GridFS entry.

Parameters:
fn - File name.

setContentType

public void setContentType(String ct)
Sets the content type (MIME type) on the GridFS entry.

Parameters:
ct - 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()
calls save(long) with the existing chunk size

Overrides:
save in class GridFSFile
Throws:
MongoException

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

saveChunks

public int saveChunks()
               throws IOException
Returns:
Number of the next chunk.
Throws:
IOException - on problems reading the new entry's InputStream.
MongoException
See Also:
saveChunks(long)

saveChunks

public int saveChunks(long chunkSize)
               throws IOException
Saves all data into chunks from configured InputStream 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's InputStream.
MongoException

getOutputStream

public OutputStream getOutputStream()
After retrieving this 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 OutputStream.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

protected DBObject createChunk(Object id,
                               int currentChunkNumber,
                               byte[] writeBuffer)