From 953f2b39e1bb4d2ab256f369e8d66c4fab7e1482 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Tue, 23 Jan 2024 13:14:14 +0100 Subject: [PATCH] Introduce `InputStream{Read,Skip}NBytes` Refaster rules --- .../refasterrules/InputStreamRules.java | 26 +++++++++++++++++-- .../InputStreamRulesTestInput.java | 8 ++++++ .../InputStreamRulesTestOutput.java | 8 ++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/InputStreamRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/InputStreamRules.java index 6eed99b6f1..e0c635d82a 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/InputStreamRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/InputStreamRules.java @@ -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() {} @@ -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); + } + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/InputStreamRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/InputStreamRulesTestInput.java index 633bbda3dc..f31f88797b 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/InputStreamRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/InputStreamRulesTestInput.java @@ -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); + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/InputStreamRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/InputStreamRulesTestOutput.java index fbb99bfdb0..b52074c2e2 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/InputStreamRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/InputStreamRulesTestOutput.java @@ -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); + } }