Skip to content

Commit 614db51

Browse files
author
Ryan
committed
Merge pull request #51 from greenlaw110/master
Add close on persist option on GridFSInputFile This breaks the current API, but I will fix.
2 parents b36fd44 + 420284c commit 614db51

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

src/main/com/mongodb/gridfs/GridFS.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public void remove( DBObject query ){
232232
* @return
233233
*/
234234
public GridFSInputFile createFile( byte[] data ){
235-
return createFile( new ByteArrayInputStream( data ) );
235+
return createFile( new ByteArrayInputStream( data ), true );
236236
}
237237

238238

@@ -245,7 +245,7 @@ public GridFSInputFile createFile( byte[] data ){
245245
*/
246246
public GridFSInputFile createFile( File f )
247247
throws IOException {
248-
return createFile( new FileInputStream( f ) , f.getName() );
248+
return createFile( new FileInputStream( f ) , f.getName(), true );
249249
}
250250

251251
/**
@@ -258,6 +258,18 @@ public GridFSInputFile createFile( InputStream in ){
258258
return createFile( in , null );
259259
}
260260

261+
/**
262+
* creates a file entry.
263+
* after calling this method, you have to call save() on the GridFSInputFile file
264+
* @param in an inputstream containing the file's data
265+
* @param closeStreamOnPersist indicate the passed in input stream should be closed
266+
* once the data chunk persisted
267+
* @return
268+
*/
269+
public GridFSInputFile createFile( InputStream in, boolean closeStreamOnPersist ){
270+
return createFile( in , null, closeStreamOnPersist );
271+
}
272+
261273
/**
262274
* creates a file entry.
263275
* After calling this method, you have to call save() on the GridFSInputFile file
@@ -269,6 +281,19 @@ public GridFSInputFile createFile( InputStream in , String filename ){
269281
return new GridFSInputFile( this , in , filename );
270282
}
271283

284+
/**
285+
* creates a file entry.
286+
* After calling this method, you have to call save() on the GridFSInputFile file
287+
* @param in an inputstream containing the file's data
288+
* @param filename the file name as stored in the db
289+
* @param closeStreamOnPersist indicate the passed in input stream should be closed
290+
* once the data chunk persisted
291+
* @return
292+
*/
293+
public GridFSInputFile createFile( InputStream in , String filename, boolean closeStreamOnPersist ){
294+
return new GridFSInputFile( this , in , filename, closeStreamOnPersist );
295+
}
296+
272297
/**
273298
* @see {@link GridFS#createFile()} on how to use this method
274299
* @param filename the file name as stored in the db

src/main/com/mongodb/gridfs/GridFSInputFile.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,14 @@ public class GridFSInputFile extends GridFSFile {
5252
* Stream used for reading data from.
5353
* @param filename
5454
* Name of the file to be created.
55+
* @param closeStreamOnPersist
56+
indicate the passed in input stream should be closed once the data chunk persisted
5557
*/
56-
GridFSInputFile( GridFS fs , InputStream in , String filename ) {
58+
GridFSInputFile( GridFS fs , InputStream in , String filename, boolean closeStreamOnPersist ) {
5759
_fs = fs;
5860
_in = in;
5961
_filename = filename;
62+
_closeStreamOnPersist = closeStreamOnPersist;
6063

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

72+
/**
73+
* Default constructor setting the GridFS file name and providing an input
74+
* stream containing data to be written to the file.
75+
*
76+
* @param fs
77+
* The GridFS connection handle.
78+
* @param in
79+
* Stream used for reading data from.
80+
* @param filename
81+
* Name of the file to be created.
82+
*/
83+
GridFSInputFile( GridFS fs , InputStream in , String filename ) {
84+
this( fs, in, filename, false);
85+
}
86+
6987
/**
7088
* Constructor that only provides a file name, but does not rely on the
7189
* presence of an {@link java.io.InputStream}. An
@@ -289,10 +307,18 @@ private void _finishData() {
289307
_messageDigester = null;
290308
_length = _totalBytes;
291309
_savedChunks = true;
310+
try {
311+
if (null != _in && _closeStreamOnPersist) {
312+
_in.close();
313+
}
314+
} catch (IOException e) {
315+
//ignore
316+
}
292317
}
293318
}
294319

295320
private final InputStream _in;
321+
private boolean _closeStreamOnPersist;
296322
private boolean _savedChunks = false;
297323
private byte[] _buffer = null;
298324
private int _currentChunkNumber = 0;

0 commit comments

Comments
 (0)