Class GridFSInputFile

java.lang.Object
com.mongodb.gridfs.GridFSFile
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 to
MongoDB documentation
GridFS
  • Constructor Details

    • GridFSInputFile

      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.
      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, InputStream inputStream, 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

      protected GridFSInputFile(GridFS gridFS, 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:
      gridFS - The GridFS connection handle.
      filename - Name of the file to be created.
    • GridFSInputFile

      protected GridFSInputFile(GridFS gridFS)
      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:
      gridFS - The GridFS connection handle.
  • Method Details

    • setId

      public void setId(Object id)
      Sets the ID of this GridFS file.
      Parameters:
      id - the file's ID.
    • setFilename

      public void setFilename(String filename)
      Sets the file name on the GridFS entry.
      Parameters:
      filename - File name.
    • setContentType

      public void setContentType(String contentType)
      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()
      Calls save(long) with the existing chunk size.
      Overrides:
      save in class GridFSFile
      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

      public int saveChunks() throws IOException
      Saves all data into chunks from configured InputStream input stream to GridFS.
      Returns:
      Number of the next chunk.
      Throws:
      IOException - on problems reading the new entry's InputStream.
      MongoException - if there's a failure
      See Also:
    • 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 - if there's a failure
    • 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)
      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 ID
      currentChunkNumber - the unique id for this chunk
      writeBuffer - the byte array containing the data for this chunk
      Returns:
      a DBObject representing this chunk.