diff --git a/parquet-common/src/main/java/org/apache/parquet/bytes/BytesInput.java b/parquet-common/src/main/java/org/apache/parquet/bytes/BytesInput.java index 88bb1da7cf..ce859c4058 100644 --- a/parquet-common/src/main/java/org/apache/parquet/bytes/BytesInput.java +++ b/parquet-common/src/main/java/org/apache/parquet/bytes/BytesInput.java @@ -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 diff --git a/parquet-common/src/test/java/org/apache/parquet/bytes/TestBytesInput.java b/parquet-common/src/test/java/org/apache/parquet/bytes/TestBytesInput.java index d2c9e82353..894a6a22e7 100644 --- a/parquet-common/src/test/java/org/apache/parquet/bytes/TestBytesInput.java +++ b/parquet-common/src/test/java/org/apache/parquet/bytes/TestBytesInput.java @@ -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 factory = () -> BytesInput.fromInts(values); + validate(data, factory); + } + @Test public void testFromInt() throws IOException { int value = RANDOM.nextInt();