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 new file mode 100644 index 0000000000..6eed99b6f1 --- /dev/null +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/InputStreamRules.java @@ -0,0 +1,41 @@ +package tech.picnic.errorprone.refasterrules; + +import com.google.common.io.ByteStreams; +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +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() {} + + static final class InputStreamTransferTo { + @BeforeTemplate + long before(InputStream in, OutputStream out) throws IOException { + return ByteStreams.copy(in, out); + } + + @AfterTemplate + long after(InputStream in, OutputStream out) throws IOException { + return in.transferTo(out); + } + } + + static final class InputStreamReadAllBytes { + @BeforeTemplate + byte[] before(InputStream in) throws IOException { + return ByteStreams.toByteArray(in); + } + + @AfterTemplate + byte[] after(InputStream in) throws IOException { + return in.readAllBytes(); + } + } +} diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java index b7a7a017a6..a7c35621a8 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java @@ -42,6 +42,7 @@ final class RefasterRulesTest { DoubleStreamRules.class, EqualityRules.class, FileRules.class, + InputStreamRules.class, ImmutableListRules.class, ImmutableListMultimapRules.class, ImmutableMapRules.class, 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 new file mode 100644 index 0000000000..633bbda3dc --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/InputStreamRulesTestInput.java @@ -0,0 +1,23 @@ +package tech.picnic.errorprone.refasterrules; + +import com.google.common.collect.ImmutableSet; +import com.google.common.io.ByteStreams; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; + +final class InputStreamRulesTest implements RefasterRuleCollectionTestCase { + @Override + public ImmutableSet elidedTypesAndStaticImports() { + return ImmutableSet.of(ByteStreams.class); + } + + long testInputStreamTransferTo() throws IOException { + return ByteStreams.copy(new ByteArrayInputStream(new byte[0]), new ByteArrayOutputStream()); + } + + byte[] testInputStreamReadAllBytes() throws IOException { + return ByteStreams.toByteArray(new ByteArrayInputStream(new byte[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 new file mode 100644 index 0000000000..fbb99bfdb0 --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/InputStreamRulesTestOutput.java @@ -0,0 +1,23 @@ +package tech.picnic.errorprone.refasterrules; + +import com.google.common.collect.ImmutableSet; +import com.google.common.io.ByteStreams; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; + +final class InputStreamRulesTest implements RefasterRuleCollectionTestCase { + @Override + public ImmutableSet elidedTypesAndStaticImports() { + return ImmutableSet.of(ByteStreams.class); + } + + long testInputStreamTransferTo() throws IOException { + return new ByteArrayInputStream(new byte[0]).transferTo(new ByteArrayOutputStream()); + } + + byte[] testInputStreamReadAllBytes() throws IOException { + return new ByteArrayInputStream(new byte[0]).readAllBytes(); + } +}