File tree Expand file tree Collapse file tree 4 files changed +26
-6
lines changed
functional/com/mongodb/gridfs Expand file tree Collapse file tree 4 files changed +26
-6
lines changed Original file line number Diff line number Diff line change 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" />
Original file line number Diff line number Diff 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 /**
Original file line number Diff line number Diff line change 2828
2929import java .io .ByteArrayInputStream ;
3030import java .io .ByteArrayOutputStream ;
31+ import java .io .File ;
3132import java .io .IOException ;
3233import java .io .InputStream ;
3334import java .io .OutputStream ;
35+ import java .net .URI ;
36+ import java .nio .file .Files ;
37+ import java .nio .file .Paths ;
3438
3539import static java .nio .charset .Charset .defaultCharset ;
3640import 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
Original file line number Diff line number Diff line change 1+ GridFS Test File
You can’t perform that action at this time.
0 commit comments