Skip to content

Commit

Permalink
Merge pull request #51 from greenlaw110/master
Browse files Browse the repository at this point in the history
Add close on persist option on GridFSInputFile

This breaks the current API, but I will fix.
  • Loading branch information
Ryan committed Oct 31, 2011
2 parents b36fd44 + 420284c commit 614db51
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
29 changes: 27 additions & 2 deletions src/main/com/mongodb/gridfs/GridFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public void remove( DBObject query ){
* @return
*/
public GridFSInputFile createFile( byte[] data ){
return createFile( new ByteArrayInputStream( data ) );
return createFile( new ByteArrayInputStream( data ), true );
}


Expand All @@ -245,7 +245,7 @@ public GridFSInputFile createFile( byte[] data ){
*/
public GridFSInputFile createFile( File f )
throws IOException {
return createFile( new FileInputStream( f ) , f.getName() );
return createFile( new FileInputStream( f ) , f.getName(), true );
}

/**
Expand All @@ -258,6 +258,18 @@ public GridFSInputFile createFile( InputStream in ){
return createFile( in , null );
}

/**
* creates a file entry.
* after calling this method, you have to call save() on the GridFSInputFile file
* @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
*/
public GridFSInputFile createFile( InputStream in, boolean closeStreamOnPersist ){
return createFile( in , null, closeStreamOnPersist );
}

/**
* creates a file entry.
* After calling this method, you have to call save() on the GridFSInputFile file
Expand All @@ -269,6 +281,19 @@ public GridFSInputFile createFile( InputStream in , String filename ){
return new GridFSInputFile( this , in , filename );
}

/**
* creates a file entry.
* After calling this method, you have to call save() on the GridFSInputFile file
* @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
*/
public GridFSInputFile createFile( InputStream in , String filename, boolean closeStreamOnPersist ){
return new GridFSInputFile( this , in , filename, closeStreamOnPersist );
}

/**
* @see {@link GridFS#createFile()} on how to use this method
* @param filename the file name as stored in the db
Expand Down
28 changes: 27 additions & 1 deletion src/main/com/mongodb/gridfs/GridFSInputFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ public class GridFSInputFile extends GridFSFile {
* Stream used for reading data from.
* @param filename
* Name of the file to be created.
* @param closeStreamOnPersist
indicate the passed in input stream should be closed once the data chunk persisted
*/
GridFSInputFile( GridFS fs , InputStream in , String filename ) {
GridFSInputFile( GridFS fs , InputStream in , String filename, boolean closeStreamOnPersist ) {
_fs = fs;
_in = in;
_filename = filename;
_closeStreamOnPersist = closeStreamOnPersist;

_id = new ObjectId();
_chunkSize = GridFS.DEFAULT_CHUNKSIZE;
Expand All @@ -66,6 +69,21 @@ public class GridFSInputFile extends GridFSFile {
_buffer = new byte[(int) _chunkSize];
}

/**
* Default constructor setting the GridFS file name and providing an input
* stream containing data to be written to the file.
*
* @param fs
* The GridFS connection handle.
* @param in
* Stream used for reading data from.
* @param filename
* Name of the file to be created.
*/
GridFSInputFile( GridFS fs , InputStream in , String filename ) {
this( fs, in, filename, false);
}

/**
* Constructor that only provides a file name, but does not rely on the
* presence of an {@link java.io.InputStream}. An
Expand Down Expand Up @@ -289,10 +307,18 @@ private void _finishData() {
_messageDigester = null;
_length = _totalBytes;
_savedChunks = true;
try {
if (null != _in && _closeStreamOnPersist) {
_in.close();
}
} catch (IOException e) {
//ignore
}
}
}

private final InputStream _in;
private boolean _closeStreamOnPersist;
private boolean _savedChunks = false;
private byte[] _buffer = null;
private int _currentChunkNumber = 0;
Expand Down

1 comment on commit 614db51

@nzrgit
Copy link
Contributor

@nzrgit nzrgit commented on 614db51 Oct 31, 2011

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for tracking... this was: JAVA-462

Please sign in to comment.