Skip to content

Commit 0a711eb

Browse files
committed
PARQUET-415: Fix ByteBuffer Binary serialization.
This also adds a test to validate that serialization works for all Binary objects that are already test cases. Author: Ryan Blue <blue@apache.org> Closes #305 from rdblue/PARQUET-415-fix-bytebuffer-binary-serialization and squashes the following commits: 4e75d54 [Ryan Blue] PARQUET-415: Fix ByteBuffer Binary serialization.
1 parent 06a4689 commit 0a711eb

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

parquet-column/src/main/java/org/apache/parquet/io/api/Binary.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,9 @@ public static Binary fromByteArray(final byte[] value) {
355355

356356
private static class ByteBufferBackedBinary extends Binary {
357357
private ByteBuffer value;
358-
private byte[] cachedBytes;
359-
private final int offset;
360-
private final int length;
358+
private transient byte[] cachedBytes;
359+
private int offset;
360+
private int length;
361361

362362
public ByteBufferBackedBinary(ByteBuffer value, int offset, int length, boolean isBackingBytesReused) {
363363
this.value = value;
@@ -502,6 +502,8 @@ private void readObject(java.io.ObjectInputStream in) throws IOException, ClassN
502502
byte[] bytes = new byte[length];
503503
in.readFully(bytes, 0, length);
504504
this.value = ByteBuffer.wrap(bytes);
505+
this.offset = 0;
506+
this.length = length;
505507
}
506508

507509
private void readObjectNoData() throws ObjectStreamException {

parquet-column/src/test/java/org/apache/parquet/io/api/TestBinary.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
*/
1919
package org.apache.parquet.io.api;
2020

21+
import java.io.ByteArrayInputStream;
2122
import java.io.ByteArrayOutputStream;
2223
import java.io.IOException;
24+
import java.io.ObjectInputStream;
25+
import java.io.ObjectOutputStream;
2326
import java.nio.ByteBuffer;
2427
import java.util.Arrays;
2528

@@ -29,6 +32,7 @@
2932
import static org.junit.Assert.assertArrayEquals;
3033
import static org.junit.Assert.assertEquals;
3134
import static org.junit.Assert.assertSame;
35+
import static org.junit.Assert.assertTrue;
3236

3337
public class TestBinary {
3438

@@ -227,6 +231,22 @@ private void testReusedCopy(BinaryFactory bf) throws Exception {
227231
assertArrayEquals(testString.getBytes(UTF8), copy.copy().getBytes());
228232
}
229233

234+
private void testSerializable(BinaryFactory bf, boolean reused) throws Exception {
235+
BinaryAndOriginal bao = bf.get("polygon".getBytes(UTF8), reused);
236+
237+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
238+
ObjectOutputStream out = new ObjectOutputStream(baos);
239+
out.writeObject(bao.binary);
240+
out.close();
241+
baos.close();
242+
243+
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(
244+
baos.toByteArray()));
245+
Object object = in.readObject();
246+
assertTrue(object instanceof Binary);
247+
assertEquals(bao.binary, object);
248+
}
249+
230250
private void testBinary(BinaryFactory bf, boolean reused) throws Exception {
231251
testSlice(bf, reused);
232252

@@ -236,5 +256,6 @@ private void testBinary(BinaryFactory bf, boolean reused) throws Exception {
236256
testConstantCopy(bf);
237257
}
238258

259+
testSerializable(bf, reused);
239260
}
240261
}

0 commit comments

Comments
 (0)