Skip to content
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

GH-2997: BytesInput.fromInts #2998

Closed
wants to merge 2 commits into from
Closed
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 @@ -125,6 +125,25 @@ public static BytesInput from(byte[] in, int offset, int length) {
return new ByteArrayBytesInput(in, offset, length);
}

/**
* @param intValues the ints to write
* @return a BytesInput that will write 4 * number of intValues bytes in little endian
*/
public static BytesInput fromInts(int... intValues) {
int bytesLen = 4 * intValues.length;
CapacityByteArrayOutputStream out = CapacityByteArrayOutputStream.withTargetNumSlabs(bytesLen, bytesLen, 0);
try {
for (int i : intValues) {
BytesUtils.writeIntLittleEndian(out, i);
}
} catch (IOException e) {
// this can't happen, because CapacityByteArrayOutputStream won't throw exception
out.close();
throw new RuntimeException(e);
}
return from(out);
}

/**
* @param intValue the int to write
* @return a BytesInput that will write 4 bytes in little endian
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,18 @@ public void testFromCapacityByteArrayOutputStreamMultipleSlabs() throws IOExcept
}
}

@Test
public void testFromInts() throws IOException {
int[] values = new int[] {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, Integer.MIN_VALUE, Integer.MAX_VALUE};
ByteArrayOutputStream baos = new ByteArrayOutputStream(4 * values.length);
for (int value : values) {
BytesUtils.writeIntLittleEndian(baos, value);
}
byte[] data = baos.toByteArray();
Supplier<BytesInput> factory = () -> BytesInput.fromInts(values);
validate(data, factory);
}

@Test
public void testFromInt() throws IOException {
int value = RANDOM.nextInt();
Expand Down