Skip to content

Commit a180daa

Browse files
authored
DataChunkInputStream more then one close does not throw exception (helidon-io#1904)
Signed-off-by: David Kral <david.k.kral@oracle.com>
1 parent a72e926 commit a180daa

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

media/common/src/main/java/io/helidon/media/common/DataChunkInputStream.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.io.InputStream;
2121
import java.nio.ByteBuffer;
22+
import java.util.Optional;
2223
import java.util.concurrent.CompletableFuture;
2324
import java.util.concurrent.ExecutionException;
2425
import java.util.concurrent.Flow;
@@ -97,7 +98,7 @@ private static void releaseChunk(DataChunk chunk, Throwable th) {
9798
@Override
9899
public void close() {
99100
// Assert: if current != next, next cannot ever be resolved with a chunk that needs releasing
100-
current.whenComplete(DataChunkInputStream::releaseChunk);
101+
Optional.ofNullable(current).ifPresent(it -> current.whenComplete(DataChunkInputStream::releaseChunk));
101102
current = null;
102103
}
103104

media/common/src/test/java/io/helidon/media/common/DataChunkInputStreamTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import io.helidon.common.http.DataChunk;
3232
import io.helidon.common.reactive.BufferedEmittingPublisher;
3333
import io.helidon.common.reactive.Multi;
34+
import io.helidon.common.reactive.Single;
3435

3536
import org.junit.jupiter.api.Test;
3637

@@ -69,6 +70,13 @@ public void emptyChunk() throws IOException {
6970
assertThat(sb.toString(), is("foobar"));
7071
}
7172

73+
@Test
74+
public void closeMoreTheOnce() throws IOException {
75+
InputStream is = new DataChunkInputStream(Single.just(DataChunk.create("test".getBytes())));
76+
is.close();
77+
is.close();
78+
}
79+
7280
@Test
7381
public void differentThreads() throws Exception {
7482
List<String> test_data = List.of("test0", "test1", "test2", "test3");

0 commit comments

Comments
 (0)