Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/com/marklogic/client/test/HandleAccessorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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);
}
}