Skip to content

Commit

Permalink
Properly handles an empty input stream by keeping the internal state …
Browse files Browse the repository at this point in the history
…of a GrowingBufferData consistent. (#8694)

Signed-off-by: Santiago Pericas-Geertsen <[email protected]>
  • Loading branch information
spericas authored Apr 22, 2024
1 parent 50b2fca commit 9d3de35
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
* Copyright (c) 2022, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -74,7 +74,9 @@ public void writeTo(OutputStream out) {
public int readFrom(InputStream in) {
try {
int read = in.read(bytes, writePosition, bytes.length - writePosition);
writePosition += read;
if (read > 0) {
writePosition += read;
}
return read;
} catch (IOException e) {
throw new UncheckedIOException(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
* Copyright (c) 2022, 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@

package io.helidon.common.buffers;

import java.io.InputStream;
import java.util.HexFormat;
import java.util.stream.Stream;

Expand Down Expand Up @@ -285,6 +286,20 @@ void testHexOutput(TestContext context) {
assertThat(bd.debugDataHex(true), is(expected));
}

@ParameterizedTest
@MethodSource("initParams")
void emptyInputStream(TestContext context) {
BufferData b = context.bufferData();
assertThat(b.available(), is(0));
b.readFrom(new InputStream() {
@Override
public int read() {
return -1; // no data
}
});
assertThat(b.available(), is(0));
}

BufferData dataFromHex(String hexEncoded) {
byte[] bytes = HexFormat.of().parseHex(hexEncoded.replace(" ", ""));
return BufferData.create(bytes);
Expand Down

0 comments on commit 9d3de35

Please sign in to comment.