Skip to content

Commit 2b6b69c

Browse files
committed
Fixed GridFS.createFile bug
The underlying call handles the closure of the stream correctly JAVA-1813
1 parent 0747ed7 commit 2b6b69c

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

config/findbugs-exclude.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
<Bug pattern="RV_RETURN_VALUE_IGNORED"/> <!-- Deliberately ignoring return value of CountDownLatch.await -->
2020
</Class>
2121
</Match>
22+
23+
<Match>
24+
<Class name="com.mongodb.gridfs.GridFS" />
25+
<Method name="createFile" params="java.io.File" /> <!-- The underlying call to GridFSInputFile closes the file -->
26+
<Bug pattern="OBL_UNSATISFIED_OBLIGATION"/>
27+
</Match>
2228
<Match>
2329
<Class name="com.mongodb.DocumentCodec">
2430
<Bug pattern="NM_SAME_SIMPLE_NAME_AS_SUPERCLASS"/>

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,7 @@ public GridFSInputFile createFile(final byte[] data) {
330330
* @throws IOException if there are problems reading {@code file}
331331
*/
332332
public GridFSInputFile createFile(final File file) throws IOException {
333-
FileInputStream fileInputStream = new FileInputStream(file);
334-
try {
335-
return createFile(fileInputStream, file.getName(), true);
336-
} finally {
337-
fileInputStream.close();
338-
}
333+
return createFile(new FileInputStream(file), file.getName(), true);
339334
}
340335

341336
/**

driver/src/test/functional/com/mongodb/gridfs/GridFSTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@
2828

2929
import java.io.ByteArrayInputStream;
3030
import java.io.ByteArrayOutputStream;
31+
import java.io.File;
3132
import java.io.IOException;
3233
import java.io.InputStream;
3334
import java.io.OutputStream;
35+
import java.net.URI;
36+
import java.nio.file.Files;
37+
import java.nio.file.Paths;
3438

3539
import static java.nio.charset.Charset.defaultCharset;
3640
import static org.junit.Assert.assertArrayEquals;
@@ -114,6 +118,20 @@ public void testOutStreamBigAligned() throws Exception {
114118
testOutStream(s);
115119
}
116120

121+
@Test
122+
public void testCreateFileWithFile() throws Exception {
123+
URI fileURI = GridFSTest.class.getResource("/GridFS/GridFSTestFile.txt").toURI();
124+
GridFSInputFile in = gridFS.createFile(new File(fileURI));
125+
in.save();
126+
127+
GridFSDBFile out = gridFS.findOne(new BasicDBObject("_id", in.getId()));
128+
ByteArrayOutputStream bout = new ByteArrayOutputStream();
129+
out.writeTo(bout);
130+
String outString = new String(bout.toByteArray(), defaultCharset());
131+
132+
assertEquals(new String(Files.readAllBytes(Paths.get(fileURI))), outString);
133+
}
134+
117135
@Test
118136
public void testMetadata() throws Exception {
119137

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
GridFS Test File

0 commit comments

Comments
 (0)