mirror of
https://github.com/jlengrand/error-prone-support.git
synced 2026-03-10 08:11:25 +00:00
Introduce InputStreamRules Refaster rule collection (#963)
This commit is contained in:
committed by
GitHub
parent
09317abb18
commit
641bb5c566
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,6 +42,7 @@ final class RefasterRulesTest {
|
||||
DoubleStreamRules.class,
|
||||
EqualityRules.class,
|
||||
FileRules.class,
|
||||
InputStreamRules.class,
|
||||
ImmutableListRules.class,
|
||||
ImmutableListMultimapRules.class,
|
||||
ImmutableMapRules.class,
|
||||
|
||||
@@ -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<Object> 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]));
|
||||
}
|
||||
}
|
||||
@@ -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<Object> 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user