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 19
19
<Bug pattern =" RV_RETURN_VALUE_IGNORED" /> <!-- Deliberately ignoring return value of CountDownLatch.await -->
20
20
</Class >
21
21
</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 >
22
28
<Match >
23
29
<Class name =" com.mongodb.DocumentCodec" >
24
30
<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) {
330
330
* @throws IOException if there are problems reading {@code file}
331
331
*/
332
332
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 );
339
334
}
340
335
341
336
/**
Original file line number Diff line number Diff line change 28
28
29
29
import java .io .ByteArrayInputStream ;
30
30
import java .io .ByteArrayOutputStream ;
31
+ import java .io .File ;
31
32
import java .io .IOException ;
32
33
import java .io .InputStream ;
33
34
import java .io .OutputStream ;
35
+ import java .net .URI ;
36
+ import java .nio .file .Files ;
37
+ import java .nio .file .Paths ;
34
38
35
39
import static java .nio .charset .Charset .defaultCharset ;
36
40
import static org .junit .Assert .assertArrayEquals ;
@@ -114,6 +118,20 @@ public void testOutStreamBigAligned() throws Exception {
114
118
testOutStream (s );
115
119
}
116
120
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
+
117
135
@ Test
118
136
public void testMetadata () throws Exception {
119
137
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