Skip to content

Commit a27e4e3

Browse files
sarankumarbaskarSarankumar Baskar
andauthored
[IO-855] Clarify PeekableInputStream.peek() behavior (#856)
Add tests for exact-match and non-consuming behavior. Co-authored-by: Sarankumar Baskar <sbaskar@redhat.com>
1 parent 77c4e1f commit a27e4e3

2 files changed

Lines changed: 36 additions & 7 deletions

File tree

src/main/java/org/apache/commons/io/input/buffer/PeekableInputStream.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ public PeekableInputStream(final InputStream inputStream, final int bufferSize)
5050
}
5151

5252
/**
53-
* Returns whether the next bytes in the buffer are as given by {@code sourceBuffer}. This is equivalent to
54-
* {@link #peek(byte[], int, int)} with {@code offset} == 0, and {@code length} == {@code sourceBuffer.length}
53+
* Returns whether the next bytes in the buffer exactly match {@code sourceBuffer}. This is equivalent to
54+
* {@link #peek(byte[], int, int)} with {@code offset} == 0, and {@code length} == {@code sourceBuffer.length}.
55+
* This method does not perform a prefix or starts-with check; for example, peeking {@code "Some"} against a stream
56+
* containing {@code "Some text buffer"} returns {@code false}. This method does not consume bytes from the stream.
5557
*
5658
* @param sourceBuffer the buffer to compare against.
57-
* @return true if the next bytes are as given.
59+
* @return true if the next bytes exactly match {@code sourceBuffer}.
5860
* @throws IOException Refilling the buffer failed.
5961
*/
6062
public boolean peek(final byte[] sourceBuffer) throws IOException {
@@ -63,13 +65,13 @@ public boolean peek(final byte[] sourceBuffer) throws IOException {
6365
}
6466

6567
/**
66-
* Returns whether the next bytes in the buffer are as given by {@code sourceBuffer}, {code offset}, and
67-
* {@code length}.
68+
* Returns whether the next bytes in the buffer exactly match {@code length} bytes from {@code sourceBuffer}, starting at
69+
* {@code offset}. This method does not consume bytes from the stream.
6870
*
6971
* @param sourceBuffer the buffer to compare against.
70-
* @param offset the start offset.
72+
* @param offset the start offset in {@code sourceBuffer}.
7173
* @param length the length to compare.
72-
* @return true if the next bytes in the buffer are as given.
74+
* @return true if the next bytes exactly match the requested range from {@code sourceBuffer}.
7375
* @throws IOException if there is a problem calling fillBuffer().
7476
*/
7577
public boolean peek(final byte[] sourceBuffer, final int offset, final int length) throws IOException {

src/test/java/org/apache/commons/io/input/buffer/PeekableInputStreamTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717

1818
package org.apache.commons.io.input.buffer;
1919

20+
import static org.junit.jupiter.api.Assertions.assertFalse;
2021
import static org.junit.jupiter.api.Assertions.assertEquals;
2122
import static org.junit.jupiter.api.Assertions.assertNotEquals;
2223
import static org.junit.jupiter.api.Assertions.assertTrue;
2324
import static org.junit.jupiter.api.Assertions.fail;
2425

2526
import java.io.ByteArrayInputStream;
2627
import java.io.IOException;
28+
import java.nio.charset.StandardCharsets;
2729
import java.util.Random;
2830

2931
import org.junit.jupiter.api.Test;
@@ -100,4 +102,29 @@ void testRandomRead() throws Exception {
100102
}
101103
assertTrue(true, "Test finished OK");
102104
}
105+
106+
@Test
107+
void testPeekExactMatch() throws IOException {
108+
final byte[] input = "Some text buffer".getBytes(StandardCharsets.UTF_8);
109+
try (PeekableInputStream inputStream = new PeekableInputStream(new ByteArrayInputStream(input))) {
110+
assertTrue(inputStream.peek(input));
111+
}
112+
}
113+
114+
@Test
115+
void testPeekPrefixDoesNotMatchLongerInput() throws IOException {
116+
try (PeekableInputStream inputStream = new PeekableInputStream(
117+
new ByteArrayInputStream("Some text buffer".getBytes(StandardCharsets.UTF_8)))) {
118+
assertFalse(inputStream.peek("Some".getBytes(StandardCharsets.UTF_8)));
119+
}
120+
}
121+
122+
@Test
123+
void testPeekDoesNotConsumeBytes() throws IOException {
124+
final byte[] input = "Some".getBytes(StandardCharsets.UTF_8);
125+
try (PeekableInputStream inputStream = new PeekableInputStream(new ByteArrayInputStream(input))) {
126+
assertTrue(inputStream.peek(input));
127+
assertEquals('S', inputStream.read());
128+
}
129+
}
103130
}

0 commit comments

Comments
 (0)