Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Oct 2, 2024
2 parents 864a803 + 98a2389 commit 3657add
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/main/java/org/takes/rq/ChunkedInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@

/**
* Input stream from chunked coded http request body.
*
* @since 0.31.2
* @link <a href="https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6.1">Chunked Transfer Coding</a>
* @since 0.31.2
*/
final class ChunkedInputStream extends InputStream {

Expand Down Expand Up @@ -70,6 +69,7 @@ final class ChunkedInputStream extends InputStream {

/**
* Ctor.
*
* @param stream The raw input stream
*/
ChunkedInputStream(final InputStream stream) {
Expand Down Expand Up @@ -109,7 +109,13 @@ public int read(final byte[] buf, final int off, final int len)
if (shift == len) {
result = len;
} else {
result = shift + this.read(buf, off + shift, len - shift);
result = shift + Math.max(
this.read(
buf,
off + shift,
len - shift
), 0
);
}
}
return result;
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/org/takes/rq/ChunkedInputStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,33 @@ void ignoresParameterAfterSemiColon() throws IOException {
MatcherAssert.assertThat(stream.available(), Matchers.equalTo(0));
stream.close();
}

@Test
void readsWithLenGreaterThanTotalSize() throws IOException {
final String data = "Hello, World!";
final String length = Integer.toHexString(data.length());
final InputStream stream = new ChunkedInputStream(
IOUtils.toInputStream(
new Joined(
ChunkedInputStreamTest.CRLF,
length,
data,
ChunkedInputStreamTest.END_OF_CHUNK,
""
).toString(),
StandardCharsets.UTF_8
)
);
final byte[] buf = new byte[data.length() + 10];
MatcherAssert.assertThat(
stream.read(buf),
Matchers.equalTo(data.length())
);
MatcherAssert.assertThat(
buf,
Matchers.equalTo((data + new String(new byte[10])).getBytes())
);
MatcherAssert.assertThat(stream.available(), Matchers.equalTo(0));
stream.close();
}
}

0 comments on commit 3657add

Please sign in to comment.