Skip to content

Commit bb97e93

Browse files
authored
Merge pull request #716 from hansenmc/issue716
ensure that FileInputStream is closed to avoid resource leakage
2 parents 1d4b5bc + 3aac663 commit bb97e93

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/main/java/com/marklogic/client/impl/HandleAccessor.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,7 @@ static public String contentAsString(AbstractWriteHandle handle) {
124124
}
125125
stringContent = sb.toString();
126126
}
127-
if ( content instanceof File ) {
128-
((FileInputStream) content).close();
129-
}
127+
130128
if ( stringContent == null ) {
131129
throw new UnsupportedOperationException("contentAsString only supports handles with sendContent() " +
132130
"of type String, OutputStreamSender, byte[], File, or InputStream");

src/test/java/com/marklogic/client/test/HandleAccessorTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
package com.marklogic.client.test;
1717

1818
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertTrue;
1920

2021
import java.io.File;
22+
import java.io.FileInputStream;
2123
import java.io.IOException;
2224
import java.io.StringReader;
2325
import java.net.URISyntaxException;
@@ -32,7 +34,10 @@
3234
import com.marklogic.client.io.InputStreamHandle;
3335
import com.marklogic.client.io.ReaderHandle;
3436
import com.marklogic.client.io.StringHandle;
37+
import java.io.InputStream;
3538
public class HandleAccessorTest {
39+
public static boolean fileInputStreamWasClosed;
40+
3641
@Test
3742
public void testContentAsString() throws URISyntaxException, IOException {
3843
// I'm purposely using a string with a non-ascii character to test
@@ -50,5 +55,16 @@ public void testContentAsString() throws URISyntaxException, IOException {
5055
HandleAccessor.contentAsString(new FileHandle(new File(filePath.toURI()))));
5156
assertEquals("InputStream content mismatch", hola,
5257
HandleAccessor.contentAsString(new InputStreamHandle(filePath.openStream())));
58+
59+
InputStream fileInputStream = new FileInputStream(new File(filePath.toURI())) {
60+
@Override
61+
public void close() throws IOException {
62+
super.close();
63+
HandleAccessorTest.fileInputStreamWasClosed = true;
64+
}
65+
};
66+
assertEquals("InputStream content mismatch", hola,
67+
HandleAccessor.contentAsString(new InputStreamHandle(fileInputStream)));
68+
assertTrue(this.fileInputStreamWasClosed);
5369
}
5470
}

0 commit comments

Comments
 (0)