Skip to content

Commit

Permalink
Introduce InputStreamRules Refaster rule collection (#963)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 authored Jan 14, 2024
1 parent 09317ab commit 641bb5c
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ final class RefasterRulesTest {
DoubleStreamRules.class,
EqualityRules.class,
FileRules.class,
InputStreamRules.class,
ImmutableListRules.class,
ImmutableListMultimapRules.class,
ImmutableMapRules.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -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]));
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 641bb5c

Please sign in to comment.