Skip to content

Commit

Permalink
#123: bug
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jun 13, 2017
1 parent e40eeb8 commit b1f65f2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/org/cactoos/io/TeeInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@
* @since 0.1
*/
public final class TeeInputStream extends InputStream {

/**
* Input.
*/
private final InputStream input;

/**
* Output.
*/
private final OutputStream output;

/**
* Ctor.
* @param src Source of data
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/org/cactoos/io/InputAsBytesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@
*/
package org.cactoos.io;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.atomic.AtomicBoolean;
import org.cactoos.func.FuncAsMatcher;
import org.cactoos.text.BytesAsText;
import org.cactoos.text.StringAsText;
import org.cactoos.text.TextAsBytes;
import org.hamcrest.MatcherAssert;
Expand Down Expand Up @@ -84,4 +89,36 @@ public void readsInputIntoBytesWithSmallBuffer() throws IOException {
);
}

@Test
public void closesInputStream() throws IOException {
final AtomicBoolean closed = new AtomicBoolean();
final InputStream input = new ByteArrayInputStream(
"how are you?".getBytes()
);
MatcherAssert.assertThat(
"Can't close InputStream correctly",
new BytesAsText(
new InputAsBytes(
new InputStreamAsInput(
new InputStream() {
@Override
public int read() throws IOException {
return input.read();
}
@Override
public void close() throws IOException {
input.close();
closed.set(true);
}
}
)
).asBytes(),
StandardCharsets.UTF_8
).asString(),
new FuncAsMatcher<>(
text -> closed.get()
)
);
}

}

0 comments on commit b1f65f2

Please sign in to comment.