From b386b50197a4787b4b813fdf7c3157c11c4a48b5 Mon Sep 17 00:00:00 2001 From: doki Date: Thu, 22 Aug 2024 21:21:21 +0800 Subject: [PATCH 1/2] BytesInput.fromInts --- .../org/apache/parquet/bytes/BytesInput.java | 19 ++++++++++++++++++ .../apache/parquet/bytes/TestBytesInput.java | 20 +++++++++++++------ 2 files changed, 33 insertions(+), 6 deletions(-) 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..66a34f8b7e 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 @@ -18,14 +18,10 @@ */ package org.apache.parquet.bytes; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.Assert.*; import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyObject; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -197,6 +193,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(); From 89fc1862a7ec20b904025d7c9d209bf73ea45271 Mon Sep 17 00:00:00 2001 From: doki Date: Thu, 22 Aug 2024 21:34:43 +0800 Subject: [PATCH 2/2] revert changes to imports --- .../java/org/apache/parquet/bytes/TestBytesInput.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) 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 66a34f8b7e..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 @@ -18,10 +18,14 @@ */ package org.apache.parquet.bytes; -import static org.junit.Assert.*; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyObject; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream;