Skip to content

Add methods for variable-length encoding integral arrays #14087

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,15 @@ public int[] readIntArray() throws IOException {
return values;
}

public int[] readVIntArray() throws IOException {
int length = readVInt();
int[] values = new int[length];
for (int i = 0; i < length; i++) {
values[i] = readVInt();
}
return values;
}

public long[] readLongArray() throws IOException {
int length = readVInt();
long[] values = new long[length];
Expand All @@ -469,6 +478,15 @@ public long[] readLongArray() throws IOException {
return values;
}

public long[] readVLongArray() throws IOException {
int length = readVInt();
long[] values = new long[length];
for (int i = 0; i < length; i++) {
values[i] = readVLong();
}
return values;
}

public float[] readFloatArray() throws IOException {
int length = readVInt();
float[] values = new float[length];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,13 +438,27 @@ public void writeIntArray(int[] values) throws IOException {
}
}

public void writeVIntArray(int[] values) throws IOException {
writeVInt(values.length);
for (int value : values) {
writeVInt(value);
}
}

public void writeLongArray(long[] values) throws IOException {
writeVInt(values.length);
for (long value : values) {
writeLong(value);
}
}

public void writeVLongArray(long[] values) throws IOException {
writeVInt(values.length);
for (long value : values) {
writeVLong(value);
}
}

public void writeFloatArray(float[] values) throws IOException {
writeVInt(values.length);
for (float value : values) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,12 @@ public void testSimpleStreams() throws Exception {
out.writeDouble(2.2);
int[] intArray = {1, 2, 3};
out.writeGenericValue(intArray);
int[] vIntArray = {4, 5, 6};
out.writeVIntArray(vIntArray);
long[] longArray = {1, 2, 3};
out.writeGenericValue(longArray);
long[] vLongArray = {4, 5, 6};
out.writeVLongArray(vLongArray);
float[] floatArray = {1.1f, 2.2f, 3.3f};
out.writeGenericValue(floatArray);
double[] doubleArray = {1.1, 2.2, 3.3};
Expand All @@ -296,7 +300,9 @@ public void testSimpleStreams() throws Exception {
assertThat((double)in.readFloat(), closeTo(1.1, 0.0001));
assertThat(in.readDouble(), closeTo(2.2, 0.0001));
assertThat(in.readGenericValue(), equalTo((Object) intArray));
assertThat(in.readVIntArray(), equalTo(vIntArray));
assertThat(in.readGenericValue(), equalTo((Object)longArray));
assertThat(in.readVLongArray(), equalTo(vLongArray));
assertThat(in.readGenericValue(), equalTo((Object)floatArray));
assertThat(in.readGenericValue(), equalTo((Object)doubleArray));
assertThat(in.readString(), equalTo("hello"));
Expand Down