Skip to content
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 @@ -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;
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
};
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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));
}

/**
Expand All @@ -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();
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
*/
package org.apache.parquet.hadoop.codec;

import java.util.Objects;
import org.apache.parquet.Preconditions;

/**
* Utilities for SnappyCompressor and SnappyDecompressor.
*/
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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down
Loading