Skip to content

Commit 2845a0c

Browse files
tjgqcopybara-github
authored andcommitted
Use DataInput and DataOutput instead of their stream versions.
This communicates the intent better: MapCodec implementations aren't supposed to interact with the stream other than reading from/writing to it. PiperOrigin-RevId: 742661148 Change-Id: I11fa47f37aab3d82e45c494a532f8a2008659d80
1 parent df988c5 commit 2845a0c

File tree

5 files changed

+34
-32
lines changed

5 files changed

+34
-32
lines changed

src/main/java/com/google/devtools/build/lib/actions/cache/CompactPersistentActionCache.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
import com.google.devtools.build.lib.vfs.Path;
4242
import com.google.devtools.build.lib.vfs.PathFragment;
4343
import java.io.ByteArrayOutputStream;
44-
import java.io.DataInputStream;
45-
import java.io.DataOutputStream;
44+
import java.io.DataInput;
45+
import java.io.DataOutput;
4646
import java.io.IOException;
4747
import java.io.PrintStream;
4848
import java.nio.BufferUnderflowException;
@@ -82,12 +82,12 @@ public class CompactPersistentActionCache implements ActionCache {
8282
private static final MapCodec<Integer, byte[]> CODEC =
8383
new MapCodec<Integer, byte[]>() {
8484
@Override
85-
protected Integer readKey(DataInputStream in) throws IOException {
85+
protected Integer readKey(DataInput in) throws IOException {
8686
return in.readInt();
8787
}
8888

8989
@Override
90-
protected byte[] readValue(DataInputStream in) throws IOException {
90+
protected byte[] readValue(DataInput in) throws IOException {
9191
int size = in.readInt();
9292
if (size < 0) {
9393
throw new IOException("found negative array size: " + size);
@@ -98,12 +98,12 @@ protected byte[] readValue(DataInputStream in) throws IOException {
9898
}
9999

100100
@Override
101-
protected void writeKey(Integer key, DataOutputStream out) throws IOException {
101+
protected void writeKey(Integer key, DataOutput out) throws IOException {
102102
out.writeInt(key);
103103
}
104104

105105
@Override
106-
protected void writeValue(byte[] value, DataOutputStream out) throws IOException {
106+
protected void writeValue(byte[] value, DataOutput out) throws IOException {
107107
out.writeInt(value.length);
108108
out.write(value);
109109
}

src/main/java/com/google/devtools/build/lib/actions/cache/PersistentStringIndexer.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
import com.google.devtools.build.lib.util.PersistentMap;
2323
import com.google.devtools.build.lib.util.StringIndexer;
2424
import com.google.devtools.build.lib.vfs.Path;
25-
import java.io.DataInputStream;
26-
import java.io.DataOutputStream;
25+
import java.io.DataInput;
26+
import java.io.DataOutput;
2727
import java.io.IOException;
2828
import java.util.Map;
2929
import java.util.concurrent.ConcurrentHashMap;
@@ -187,7 +187,7 @@ public String toString() {
187187
private static final MapCodec<String, Integer> CODEC =
188188
new MapCodec<String, Integer>() {
189189
@Override
190-
protected String readKey(DataInputStream in) throws IOException {
190+
protected String readKey(DataInput in) throws IOException {
191191
int length = in.readInt();
192192
if (length < 0) {
193193
throw new IOException("corrupt key length: " + length);
@@ -198,19 +198,19 @@ protected String readKey(DataInputStream in) throws IOException {
198198
}
199199

200200
@Override
201-
protected Integer readValue(DataInputStream in) throws IOException {
201+
protected Integer readValue(DataInput in) throws IOException {
202202
return in.readInt();
203203
}
204204

205205
@Override
206-
protected void writeKey(String key, DataOutputStream out) throws IOException {
206+
protected void writeKey(String key, DataOutput out) throws IOException {
207207
byte[] content = key.getBytes(UTF_8);
208208
out.writeInt(content.length);
209209
out.write(content);
210210
}
211211

212212
@Override
213-
protected void writeValue(Integer value, DataOutputStream out) throws IOException {
213+
protected void writeValue(Integer value, DataOutput out) throws IOException {
214214
out.writeInt(value);
215215
}
216216
};

src/main/java/com/google/devtools/build/lib/util/MapCodec.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
import com.google.devtools.build.lib.vfs.Path;
1717
import java.io.BufferedOutputStream;
1818
import java.io.ByteArrayInputStream;
19+
import java.io.DataInput;
1920
import java.io.DataInputStream;
21+
import java.io.DataOutput;
2022
import java.io.DataOutputStream;
2123
import java.io.IOException;
2224
import java.io.InputStream;
@@ -36,17 +38,17 @@ public IncompatibleFormatException(String message) {
3638
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
3739
private static final int ENTRY_MAGIC = 0xfe;
3840

39-
/** Reads a key from a {@link DataInputStream}. */
40-
protected abstract K readKey(DataInputStream in) throws IOException;
41+
/** Reads a key from a {@link DataInput}. */
42+
protected abstract K readKey(DataInput in) throws IOException;
4143

42-
/** Reads a value from a {@link DataInputStream}. */
43-
protected abstract V readValue(DataInputStream in) throws IOException;
44+
/** Reads a value from a {@link DataInput}. */
45+
protected abstract V readValue(DataInput in) throws IOException;
4446

45-
/** Writes a key into a {@link DataOutputStream}. */
46-
protected abstract void writeKey(K key, DataOutputStream out) throws IOException;
47+
/** Writes a key into a {@link DataOutput}. */
48+
protected abstract void writeKey(K key, DataOutput out) throws IOException;
4749

48-
/** Writes a value into a {@link DataOutputStream}. */
49-
protected abstract void writeValue(V value, DataOutputStream out) throws IOException;
50+
/** Writes a value into a {@link DataOutput}. */
51+
protected abstract void writeValue(V value, DataOutput out) throws IOException;
5052

5153
/**
5254
* A key/value pair representing the presence or absence of a map entry.

src/test/java/com/google/devtools/build/lib/util/MapCodecTest.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
import com.google.devtools.build.lib.vfs.FileSystemUtils;
2323
import com.google.devtools.build.lib.vfs.Path;
2424
import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
25-
import java.io.DataInputStream;
26-
import java.io.DataOutputStream;
25+
import java.io.DataInput;
26+
import java.io.DataOutput;
2727
import java.io.IOException;
2828
import org.junit.Test;
2929
import org.junit.runner.RunWith;
@@ -39,22 +39,22 @@ public final class MapCodecTest {
3939
private static final MapCodec<Integer, Integer> TEST_CODEC =
4040
new MapCodec<Integer, Integer>() {
4141
@Override
42-
protected Integer readKey(DataInputStream in) throws IOException {
42+
protected Integer readKey(DataInput in) throws IOException {
4343
return in.readInt();
4444
}
4545

4646
@Override
47-
protected Integer readValue(DataInputStream in) throws IOException {
47+
protected Integer readValue(DataInput in) throws IOException {
4848
return in.readInt();
4949
}
5050

5151
@Override
52-
protected void writeKey(Integer key, DataOutputStream out) throws IOException {
52+
protected void writeKey(Integer key, DataOutput out) throws IOException {
5353
out.writeInt(key);
5454
}
5555

5656
@Override
57-
protected void writeValue(Integer value, DataOutputStream out) throws IOException {
57+
protected void writeValue(Integer value, DataOutput out) throws IOException {
5858
out.writeInt(value);
5959
}
6060
};

src/test/java/com/google/devtools/build/lib/util/PersistentMapTest.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import com.google.devtools.build.lib.testutil.TestThread;
2020
import com.google.devtools.build.lib.testutil.TestUtils;
2121
import com.google.devtools.build.lib.vfs.Path;
22-
import java.io.DataInputStream;
23-
import java.io.DataOutputStream;
22+
import java.io.DataInput;
23+
import java.io.DataOutput;
2424
import java.io.IOException;
2525
import java.util.concurrent.ConcurrentHashMap;
2626
import java.util.concurrent.ConcurrentMap;
@@ -39,22 +39,22 @@ private static class PersistentStringMap extends PersistentMap<String, String> {
3939
private static final MapCodec<String, String> CODEC =
4040
new MapCodec<String, String>() {
4141
@Override
42-
protected String readKey(DataInputStream in) throws IOException {
42+
protected String readKey(DataInput in) throws IOException {
4343
return in.readUTF();
4444
}
4545

4646
@Override
47-
protected String readValue(DataInputStream in) throws IOException {
47+
protected String readValue(DataInput in) throws IOException {
4848
return in.readUTF();
4949
}
5050

5151
@Override
52-
protected void writeKey(String key, DataOutputStream out) throws IOException {
52+
protected void writeKey(String key, DataOutput out) throws IOException {
5353
out.writeUTF(key);
5454
}
5555

5656
@Override
57-
protected void writeValue(String value, DataOutputStream out) throws IOException {
57+
protected void writeValue(String value, DataOutput out) throws IOException {
5858
out.writeUTF(value);
5959
}
6060
};

0 commit comments

Comments
 (0)