Skip to content

Commit

Permalink
Fixed possible mistake of CapInputStream
Browse files Browse the repository at this point in the history
  • Loading branch information
UFA-MOROZOV committed Oct 3, 2024
1 parent 3657add commit c28275d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/main/java/org/takes/rq/CapInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ public int read(final byte[] buf, final int off,

@Override
public long skip(final long num) throws IOException {
return this.origin.skip(num);
final long nskip = Math.min(num, this.more);
final long skipped = this.origin.skip(nskip);
this.more -= skipped;
return skipped;
}

@Override
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/takes/rq/CapInputStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,11 @@ void skipsOnStream() throws Exception {
Mockito.verify(stream, Mockito.times(1)).skip(skip);
}

@Test
void skipRespectsCap() throws IOException {
final InputStream stream = new ByteArrayInputStream(new byte[100]);
final CapInputStream wrapper = new CapInputStream(stream, 50L);
final long skipped = wrapper.skip(75L);
MatcherAssert.assertThat(skipped, Matchers.equalTo(50L));
}
}

0 comments on commit c28275d

Please sign in to comment.