diff --git a/src/main/java/com/marklogic/client/impl/HandleAccessor.java b/src/main/java/com/marklogic/client/impl/HandleAccessor.java index 9d00daa58..624e794c8 100644 --- a/src/main/java/com/marklogic/client/impl/HandleAccessor.java +++ b/src/main/java/com/marklogic/client/impl/HandleAccessor.java @@ -124,9 +124,7 @@ static public String contentAsString(AbstractWriteHandle handle) { } stringContent = sb.toString(); } - if ( content instanceof File ) { - ((FileInputStream) content).close(); - } + if ( stringContent == null ) { throw new UnsupportedOperationException("contentAsString only supports handles with sendContent() " + "of type String, OutputStreamSender, byte[], File, or InputStream"); diff --git a/src/test/java/com/marklogic/client/test/HandleAccessorTest.java b/src/test/java/com/marklogic/client/test/HandleAccessorTest.java index 219b0e9e3..4c5536713 100644 --- a/src/test/java/com/marklogic/client/test/HandleAccessorTest.java +++ b/src/test/java/com/marklogic/client/test/HandleAccessorTest.java @@ -16,8 +16,10 @@ package com.marklogic.client.test; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.StringReader; import java.net.URISyntaxException; @@ -32,7 +34,10 @@ import com.marklogic.client.io.InputStreamHandle; import com.marklogic.client.io.ReaderHandle; import com.marklogic.client.io.StringHandle; +import java.io.InputStream; public class HandleAccessorTest { + public static boolean fileInputStreamWasClosed; + @Test public void testContentAsString() throws URISyntaxException, IOException { // I'm purposely using a string with a non-ascii character to test @@ -50,5 +55,16 @@ public void testContentAsString() throws URISyntaxException, IOException { HandleAccessor.contentAsString(new FileHandle(new File(filePath.toURI())))); assertEquals("InputStream content mismatch", hola, HandleAccessor.contentAsString(new InputStreamHandle(filePath.openStream()))); + + InputStream fileInputStream = new FileInputStream(new File(filePath.toURI())) { + @Override + public void close() throws IOException { + super.close(); + HandleAccessorTest.fileInputStreamWasClosed = true; + } + }; + assertEquals("InputStream content mismatch", hola, + HandleAccessor.contentAsString(new InputStreamHandle(fileInputStream))); + assertTrue(this.fileInputStreamWasClosed); } }