Skip to content

Commit fef357e

Browse files
Fix ByteCountingInputStream when reading past EOF
1 parent 88aa5b2 commit fef357e

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

dd-java-agent/agent-profiling/profiling-uploader/src/main/java/com/datadog/profiling/uploader/ByteCountingInputStream.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ final class ByteCountingInputStream extends InputStream {
1515
@Override
1616
public int read() throws IOException {
1717
int data = source.read();
18-
readBytes++;
18+
if (data >= 0) {
19+
readBytes++;
20+
}
1921
return data;
2022
}
2123

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.datadog.profiling.uploader;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.io.ByteArrayInputStream;
6+
import java.io.IOException;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class ByteCountingInputStreamTest {
10+
11+
@Test
12+
public void testDoesNotCountAfterEof() throws IOException {
13+
byte[] data = {1, 2, 3};
14+
15+
ByteCountingInputStream in = new ByteCountingInputStream(new ByteArrayInputStream(data));
16+
for (byte datum : data) {
17+
int b = in.read();
18+
assertEquals(datum, b);
19+
}
20+
assertEquals(data.length, (int) in.getReadBytes());
21+
22+
// read past EOF
23+
assertEquals(-1, in.read());
24+
assertEquals(data.length, (int) in.getReadBytes());
25+
}
26+
}

0 commit comments

Comments
 (0)