diff --git a/parquet-column/src/main/java/org/apache/parquet/column/values/plain/PlainValuesWriter.java b/parquet-column/src/main/java/org/apache/parquet/column/values/plain/PlainValuesWriter.java index 1c4b1e46cb..ff1520ef16 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/values/plain/PlainValuesWriter.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/values/plain/PlainValuesWriter.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import org.apache.parquet.bytes.ByteBufferAllocator; import org.apache.parquet.bytes.BytesInput; import org.apache.parquet.bytes.CapacityByteArrayOutputStream; @@ -36,8 +37,11 @@ public class PlainValuesWriter extends ValuesWriter { private static final Logger LOG = LoggerFactory.getLogger(PlainValuesWriter.class); + /** + * @deprecated Use {@link StandardCharsets#UTF_8} instead + */ @Deprecated - public static final Charset CHARSET = Charset.forName("UTF-8"); + public static final Charset CHARSET = StandardCharsets.UTF_8; private CapacityByteArrayOutputStream arrayOut; diff --git a/parquet-column/src/test/java/org/apache/parquet/io/api/TestBinary.java b/parquet-column/src/test/java/org/apache/parquet/io/api/TestBinary.java index e8a3e2397a..80af70f1ed 100644 --- a/parquet-column/src/test/java/org/apache/parquet/io/api/TestBinary.java +++ b/parquet-column/src/test/java/org/apache/parquet/io/api/TestBinary.java @@ -37,7 +37,6 @@ public class TestBinary { private static final String testString = "test-123"; - private static final String UTF8 = "UTF-8"; static interface BinaryFactory { static class BinaryAndOriginal { @@ -130,7 +129,7 @@ public BinaryAndOriginal get(byte[] bytes, boolean reused) throws Exception { private static final BinaryFactory STRING_BF = new BinaryFactory() { @Override public BinaryAndOriginal get(byte[] bytes, boolean reused) throws Exception { - Binary b = Binary.fromString(new String(bytes, UTF8)); + Binary b = Binary.fromString(new String(bytes, StandardCharsets.UTF_8)); return new BinaryAndOriginal(b, b.getBytesUnsafe()); // only way to get underlying bytes for testing } }; @@ -185,7 +184,7 @@ public void testDirectByteBufferBackedBinary() throws Exception { public void testDirectByteBufferCopyAlwaysMaterializesToHeap() throws Exception { // For constant (non-reused) direct ByteBuffers, copy() must return a new Binary // rather than 'this', because the direct memory can be freed independently. - byte[] data = testString.getBytes(UTF8); + byte[] data = testString.getBytes(StandardCharsets.UTF_8); ByteBuffer direct = ByteBuffer.allocateDirect(data.length); direct.put(data); direct.flip(); @@ -205,7 +204,7 @@ public void testDirectByteBufferCopyAlwaysMaterializesToHeap() throws Exception public void testDirectByteBufferCopyIsIndependentOfOriginalBuffer() throws Exception { // Verify the copied Binary is independent of the original direct ByteBuffer. // Simulates the scenario where direct memory is overwritten after copy. - byte[] data = testString.getBytes(UTF8); + byte[] data = testString.getBytes(StandardCharsets.UTF_8); ByteBuffer direct = ByteBuffer.allocateDirect(data.length); direct.put(data); direct.flip(); @@ -227,7 +226,7 @@ public void testDirectByteBufferCopyIsIndependentOfOriginalBuffer() throws Excep @Test public void testHeapByteBufferConstantCopyReturnsSame() throws Exception { // For heap-backed constant ByteBuffers, copy() should return 'this' (existing behavior) - byte[] data = testString.getBytes(UTF8); + byte[] data = testString.getBytes(StandardCharsets.UTF_8); ByteBuffer heap = ByteBuffer.wrap(data); Binary binary = Binary.fromConstantByteBuffer(heap); @@ -290,22 +289,23 @@ public void testFromStringBinary() throws Exception { } private void testSlice(BinaryFactory bf, boolean reused) throws Exception { - BinaryAndOriginal bao = bf.get(testString.getBytes(UTF8), reused); + BinaryAndOriginal bao = bf.get(testString.getBytes(StandardCharsets.UTF_8), reused); - assertThat(bao.binary.slice(0, testString.length()).getBytesUnsafe()).isEqualTo(testString.getBytes(UTF8)); - assertThat(bao.binary.slice(5, 3).getBytesUnsafe()).isEqualTo("123".getBytes(UTF8)); + assertThat(bao.binary.slice(0, testString.length()).getBytesUnsafe()) + .isEqualTo(testString.getBytes(StandardCharsets.UTF_8)); + assertThat(bao.binary.slice(5, 3).getBytesUnsafe()).isEqualTo("123".getBytes(StandardCharsets.UTF_8)); } private void testConstantCopy(BinaryFactory bf) throws Exception { - BinaryAndOriginal bao = bf.get(testString.getBytes(UTF8), false); + BinaryAndOriginal bao = bf.get(testString.getBytes(StandardCharsets.UTF_8), false); assertThat(bao.binary.isBackingBytesReused()).isFalse(); - assertThat(bao.binary.getBytes()).isEqualTo(testString.getBytes(UTF8)); - assertThat(bao.binary.getBytesUnsafe()).isEqualTo(testString.getBytes(UTF8)); - assertThat(bao.binary.copy().getBytesUnsafe()).isEqualTo(testString.getBytes(UTF8)); - assertThat(bao.binary.copy().getBytes()).isEqualTo(testString.getBytes(UTF8)); + assertThat(bao.binary.getBytes()).isEqualTo(testString.getBytes(StandardCharsets.UTF_8)); + assertThat(bao.binary.getBytesUnsafe()).isEqualTo(testString.getBytes(StandardCharsets.UTF_8)); + assertThat(bao.binary.copy().getBytesUnsafe()).isEqualTo(testString.getBytes(StandardCharsets.UTF_8)); + assertThat(bao.binary.copy().getBytes()).isEqualTo(testString.getBytes(StandardCharsets.UTF_8)); - bao = bf.get(testString.getBytes(UTF8), false); + bao = bf.get(testString.getBytes(StandardCharsets.UTF_8), false); assertThat(bao.binary.isBackingBytesReused()).isFalse(); Binary copy = bao.binary.copy(); @@ -314,24 +314,24 @@ private void testConstantCopy(BinaryFactory bf) throws Exception { } private void testReusedCopy(BinaryFactory bf) throws Exception { - BinaryAndOriginal bao = bf.get(testString.getBytes(UTF8), true); + BinaryAndOriginal bao = bf.get(testString.getBytes(StandardCharsets.UTF_8), true); assertThat(bao.binary.isBackingBytesReused()).isTrue(); - assertThat(bao.binary.getBytes()).isEqualTo(testString.getBytes(UTF8)); - assertThat(bao.binary.getBytesUnsafe()).isEqualTo(testString.getBytes(UTF8)); - assertThat(bao.binary.copy().getBytesUnsafe()).isEqualTo(testString.getBytes(UTF8)); - assertThat(bao.binary.copy().getBytes()).isEqualTo(testString.getBytes(UTF8)); + assertThat(bao.binary.getBytes()).isEqualTo(testString.getBytes(StandardCharsets.UTF_8)); + assertThat(bao.binary.getBytesUnsafe()).isEqualTo(testString.getBytes(StandardCharsets.UTF_8)); + assertThat(bao.binary.copy().getBytesUnsafe()).isEqualTo(testString.getBytes(StandardCharsets.UTF_8)); + assertThat(bao.binary.copy().getBytes()).isEqualTo(testString.getBytes(StandardCharsets.UTF_8)); - bao = bf.get(testString.getBytes(UTF8), true); + bao = bf.get(testString.getBytes(StandardCharsets.UTF_8), true); assertThat(bao.binary.isBackingBytesReused()).isTrue(); Binary copy = bao.binary.copy(); mutate(bao.original); - assertThat(copy.getBytes()).isEqualTo(testString.getBytes(UTF8)); - assertThat(copy.getBytesUnsafe()).isEqualTo(testString.getBytes(UTF8)); - assertThat(copy.copy().getBytesUnsafe()).isEqualTo(testString.getBytes(UTF8)); - assertThat(copy.copy().getBytes()).isEqualTo(testString.getBytes(UTF8)); + assertThat(copy.getBytes()).isEqualTo(testString.getBytes(StandardCharsets.UTF_8)); + assertThat(copy.getBytesUnsafe()).isEqualTo(testString.getBytes(StandardCharsets.UTF_8)); + assertThat(copy.copy().getBytesUnsafe()).isEqualTo(testString.getBytes(StandardCharsets.UTF_8)); + assertThat(copy.copy().getBytes()).isEqualTo(testString.getBytes(StandardCharsets.UTF_8)); } /** @@ -340,15 +340,15 @@ private void testReusedCopy(BinaryFactory bf) throws Exception { * memory can be freed independently. */ private void testDirectConstantCopy(BinaryFactory bf) throws Exception { - BinaryAndOriginal bao = bf.get(testString.getBytes(UTF8), false); + BinaryAndOriginal bao = bf.get(testString.getBytes(StandardCharsets.UTF_8), false); assertThat(bao.binary.isBackingBytesReused()).isFalse(); - assertThat(bao.binary.getBytes()).isEqualTo(testString.getBytes(UTF8)); - assertThat(bao.binary.getBytesUnsafe()).isEqualTo(testString.getBytes(UTF8)); - assertThat(bao.binary.copy().getBytesUnsafe()).isEqualTo(testString.getBytes(UTF8)); - assertThat(bao.binary.copy().getBytes()).isEqualTo(testString.getBytes(UTF8)); + assertThat(bao.binary.getBytes()).isEqualTo(testString.getBytes(StandardCharsets.UTF_8)); + assertThat(bao.binary.getBytesUnsafe()).isEqualTo(testString.getBytes(StandardCharsets.UTF_8)); + assertThat(bao.binary.copy().getBytesUnsafe()).isEqualTo(testString.getBytes(StandardCharsets.UTF_8)); + assertThat(bao.binary.copy().getBytes()).isEqualTo(testString.getBytes(StandardCharsets.UTF_8)); - bao = bf.get(testString.getBytes(UTF8), false); + bao = bf.get(testString.getBytes(StandardCharsets.UTF_8), false); assertThat(bao.binary.isBackingBytesReused()).isFalse(); Binary copy = bao.binary.copy(); @@ -364,28 +364,28 @@ private void testDirectConstantCopy(BinaryFactory bf) throws Exception { * The copy must be fully independent and survive mutation of the original buffer. */ private void testDirectReusedCopy(BinaryFactory bf) throws Exception { - BinaryAndOriginal bao = bf.get(testString.getBytes(UTF8), true); + BinaryAndOriginal bao = bf.get(testString.getBytes(StandardCharsets.UTF_8), true); assertThat(bao.binary.isBackingBytesReused()).isTrue(); - assertThat(bao.binary.getBytes()).isEqualTo(testString.getBytes(UTF8)); - assertThat(bao.binary.getBytesUnsafe()).isEqualTo(testString.getBytes(UTF8)); - assertThat(bao.binary.copy().getBytesUnsafe()).isEqualTo(testString.getBytes(UTF8)); - assertThat(bao.binary.copy().getBytes()).isEqualTo(testString.getBytes(UTF8)); + assertThat(bao.binary.getBytes()).isEqualTo(testString.getBytes(StandardCharsets.UTF_8)); + assertThat(bao.binary.getBytesUnsafe()).isEqualTo(testString.getBytes(StandardCharsets.UTF_8)); + assertThat(bao.binary.copy().getBytesUnsafe()).isEqualTo(testString.getBytes(StandardCharsets.UTF_8)); + assertThat(bao.binary.copy().getBytes()).isEqualTo(testString.getBytes(StandardCharsets.UTF_8)); - bao = bf.get(testString.getBytes(UTF8), true); + bao = bf.get(testString.getBytes(StandardCharsets.UTF_8), true); assertThat(bao.binary.isBackingBytesReused()).isTrue(); Binary copy = bao.binary.copy(); assertThat(bao.binary).isNotSameAs(copy); - assertThat(copy.getBytes()).isEqualTo(testString.getBytes(UTF8)); - assertThat(copy.getBytesUnsafe()).isEqualTo(testString.getBytes(UTF8)); - assertThat(copy.copy().getBytesUnsafe()).isEqualTo(testString.getBytes(UTF8)); - assertThat(copy.copy().getBytes()).isEqualTo(testString.getBytes(UTF8)); + assertThat(copy.getBytes()).isEqualTo(testString.getBytes(StandardCharsets.UTF_8)); + assertThat(copy.getBytesUnsafe()).isEqualTo(testString.getBytes(StandardCharsets.UTF_8)); + assertThat(copy.copy().getBytesUnsafe()).isEqualTo(testString.getBytes(StandardCharsets.UTF_8)); + assertThat(copy.copy().getBytes()).isEqualTo(testString.getBytes(StandardCharsets.UTF_8)); } private void testSerializable(BinaryFactory bf, boolean reused) throws Exception { - BinaryAndOriginal bao = bf.get("polygon".getBytes(UTF8), reused); + BinaryAndOriginal bao = bf.get("polygon".getBytes(StandardCharsets.UTF_8), reused); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(baos); diff --git a/parquet-common/src/main/java/org/apache/parquet/bytes/BytesUtils.java b/parquet-common/src/main/java/org/apache/parquet/bytes/BytesUtils.java index 07bcc28903..9d4772717b 100644 --- a/parquet-common/src/main/java/org/apache/parquet/bytes/BytesUtils.java +++ b/parquet-common/src/main/java/org/apache/parquet/bytes/BytesUtils.java @@ -38,7 +38,7 @@ public class BytesUtils { * @deprecated Use {@link StandardCharsets#UTF_8} instead */ @Deprecated - public static final Charset UTF8 = Charset.forName("UTF-8"); + public static final Charset UTF8 = StandardCharsets.UTF_8; /** * give the number of bits needed to encode an int given the max value diff --git a/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/codec/SnappyUtil.java b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/codec/SnappyUtil.java index e46a029b1d..b3eb3ce795 100644 --- a/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/codec/SnappyUtil.java +++ b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/codec/SnappyUtil.java @@ -18,6 +18,7 @@ */ package org.apache.parquet.hadoop.codec; +import java.util.Objects; import org.apache.parquet.Preconditions; /** @@ -25,7 +26,7 @@ */ public class SnappyUtil { public static void validateBuffer(byte[] buffer, int off, int len) { - Preconditions.checkNotNull(buffer, "buffer"); + Objects.requireNonNull(buffer, "buffer"); Preconditions.checkArgument( off >= 0 && len >= 0 && off <= buffer.length - len, "Invalid buffer offset or length: buffer.length=%s off=%s len=%s", diff --git a/parquet-hadoop/src/test/java/org/apache/parquet/format/converter/TestParquetMetadataConverter.java b/parquet-hadoop/src/test/java/org/apache/parquet/format/converter/TestParquetMetadataConverter.java index a47166ae93..080dc7f6bd 100644 --- a/parquet-hadoop/src/test/java/org/apache/parquet/format/converter/TestParquetMetadataConverter.java +++ b/parquet-hadoop/src/test/java/org/apache/parquet/format/converter/TestParquetMetadataConverter.java @@ -56,7 +56,7 @@ import java.io.IOException; import java.math.BigInteger; import java.nio.ByteBuffer; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.security.SecureRandom; import java.util.ArrayList; import java.util.Arrays; @@ -985,11 +985,11 @@ private void testBinaryStatsWithTruncation(int truncateLen, int minLen, int maxL assertThat(formatStats.getMin_value()).isNull(); assertThat(formatStats.getMax_value()).isNull(); } else { - String minString = new String(min, Charset.forName("UTF-8")); - String minStatString = new String(formatStats.getMin_value(), Charset.forName("UTF-8")); + String minString = new String(min, StandardCharsets.UTF_8); + String minStatString = new String(formatStats.getMin_value(), StandardCharsets.UTF_8); assertThat(minStatString.compareTo(minString)).isLessThanOrEqualTo(0); - String maxString = new String(max, Charset.forName("UTF-8")); - String maxStatString = new String(formatStats.getMax_value(), Charset.forName("UTF-8")); + String maxString = new String(max, StandardCharsets.UTF_8); + String maxStatString = new String(formatStats.getMax_value(), StandardCharsets.UTF_8); assertThat(maxStatString.compareTo(maxString)).isGreaterThanOrEqualTo(0); } }