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 ca139d9243..89c1bca4d8 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 @@ -690,18 +690,9 @@ public ByteBuffer toByteBuffer() throws IOException { return java.nio.ByteBuffer.wrap(in, offset, length); } - /** - * Zero-copy override: returns the backing array directly when fully used, - * skipping the base-class BAOS allocation + copy on every decompressor call. - * Returning the mutable array is safe — the base class already exposes a - * mutable {@code BAOS.getBuf()}. - */ @SuppressWarnings("deprecation") @Override public byte[] toByteArray() { - if (offset == 0 && length == in.length) { - return in; - } return Arrays.copyOfRange(in, offset, offset + length); } 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 a80c874fa2..18e9b02efb 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 @@ -123,16 +123,14 @@ public void testFromByteArray(ByteBufferAllocator innerAllocator) throws IOExcep @ParameterizedTest(name = "{0}") @MethodSource("parameters") - public void testFromByteArrayToByteArrayZeroCopy(ByteBufferAllocator innerAllocator) throws IOException { + public void testCopyDoesNotAliasSourceBytes(ByteBufferAllocator innerAllocator) throws IOException { initAllocator(innerAllocator); - // Full array (offset=0, length=array.length): toByteArray() returns the backing array directly - byte[] data = new byte[1000]; - RANDOM.nextBytes(data); - BytesInput bi = BytesInput.from(data, 0, data.length); - byte[] result = bi.toByteArray(); - assertThat(result) - .as("toByteArray() should return the backing array when offset=0 and length=full") - .isSameAs(data); + byte[] source = {'a'}; + BytesInput copied = BytesInput.copy(BytesInput.from(source)); + + source[0] = 'b'; + + assertThat(copied.toByteArray()).isEqualTo(new byte[] {'a'}); } @ParameterizedTest(name = "{0}")