Skip to content

Commit e19ec50

Browse files
committed
Added try/finally block to method com.mongodb.gridfs.GridFSDBFile.writeTo( File f ) which closes the
an OutputStream instantiated at the beginning of the method. Properly closing the output stream addresses a potential memory leak.
1 parent 17a6ba6 commit e19ec50

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,15 @@ public long writeTo( String filename ) throws IOException {
6464
* @throws IOException
6565
*/
6666
public long writeTo( File f ) throws IOException {
67-
return writeTo( new FileOutputStream( f ) );
67+
68+
FileOutputStream out = null;
69+
try{
70+
out = new FileOutputStream( f );
71+
return writeTo( out);
72+
}finally{
73+
if(out != null)
74+
out.close();
75+
}
6876
}
6977

7078
/**
@@ -74,12 +82,12 @@ public long writeTo( File f ) throws IOException {
7482
* @throws IOException
7583
*/
7684
public long writeTo( OutputStream out )
77-
throws IOException {
78-
final int nc = numChunks();
79-
for ( int i=0; i<nc; i++ ){
80-
out.write( getChunk( i ) );
81-
}
82-
return _length;
85+
throws IOException {
86+
final int nc = numChunks();
87+
for ( int i=0; i<nc; i++ ){
88+
out.write( getChunk( i ) );
89+
}
90+
return _length;
8391
}
8492

8593
byte[] getChunk( int i ){

0 commit comments

Comments
 (0)