Skip to content

Commit

Permalink
Introduce InputStream{Read,Skip}NBytes Refaster rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 authored and rickie committed Feb 13, 2024
1 parent 1d0d1d6 commit 953f2b3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation;

/** Refaster rules related to expressions dealing with {@link InputStream}s. */
// XXX: Add a rule for `ByteStreams.skipFully(in, n)` -> `in.skipNBytes(n)` once we have a way to
// target JDK 12+ APIs.
@OnlineDocumentation
final class InputStreamRules {
private InputStreamRules() {}
Expand Down Expand Up @@ -38,4 +36,28 @@ byte[] after(InputStream in) throws IOException {
return in.readAllBytes();
}
}

static final class InputStreamReadNBytes {
@BeforeTemplate
byte[] before(InputStream in, int n) throws IOException {
return ByteStreams.limit(in, n).readAllBytes();
}

@AfterTemplate
byte[] after(InputStream in, int n) throws IOException {
return in.readNBytes(n);
}
}

static final class InputStreamSkipNBytes {
@BeforeTemplate
void before(InputStream in, long n) throws IOException {
ByteStreams.skipFully(in, n);
}

@AfterTemplate
void after(InputStream in, long n) throws IOException {
in.skipNBytes(n);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@ long testInputStreamTransferTo() throws IOException {
byte[] testInputStreamReadAllBytes() throws IOException {
return ByteStreams.toByteArray(new ByteArrayInputStream(new byte[0]));
}

byte[] testInputStreamReadNBytes() throws IOException {
return ByteStreams.limit(new ByteArrayInputStream(new byte[0]), 0).readAllBytes();
}

void testInputStreamSkipNBytes() throws IOException {
ByteStreams.skipFully(new ByteArrayInputStream(new byte[0]), 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@ long testInputStreamTransferTo() throws IOException {
byte[] testInputStreamReadAllBytes() throws IOException {
return new ByteArrayInputStream(new byte[0]).readAllBytes();
}

byte[] testInputStreamReadNBytes() throws IOException {
return new ByteArrayInputStream(new byte[0]).readNBytes(0);
}

void testInputStreamSkipNBytes() throws IOException {
new ByteArrayInputStream(new byte[0]).skipNBytes(0);
}
}

0 comments on commit 953f2b3

Please sign in to comment.