You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by dk...@apache.org on 2018/11/29 20:34:37 UTC
[avro] branch master updated: AVRO-1743: override writeFixed in
BlockingBinaryEncoder Closed #78
This is an automated email from the ASF dual-hosted git repository.
dkulp pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/master by this push:
new 6583fcc AVRO-1743: override writeFixed in BlockingBinaryEncoder Closed #78
6583fcc is described below
commit 6583fcce6d32627c55faf9e1df35cc353b8c8841
Author: Taras Bobrovytsky <tb...@cloudera.com>
AuthorDate: Wed Mar 16 14:21:02 2016 -0700
AVRO-1743: override writeFixed in BlockingBinaryEncoder
Closed #78
---
.../org/apache/avro/io/BlockingBinaryEncoder.java | 14 +++++
.../java/org/apache/avro/ByteBufferRecord.java | 62 ++++++++++++++++++++++
.../java/org/apache/avro/TestDataFileReflect.java | 34 ++++++++++++
3 files changed, 110 insertions(+)
diff --git a/lang/java/avro/src/main/java/org/apache/avro/io/BlockingBinaryEncoder.java b/lang/java/avro/src/main/java/org/apache/avro/io/BlockingBinaryEncoder.java
index 3c7095c..986233c 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/io/BlockingBinaryEncoder.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/io/BlockingBinaryEncoder.java
@@ -19,6 +19,7 @@ package org.apache.avro.io;
import java.io.IOException;
import java.io.OutputStream;
+import java.nio.ByteBuffer;
import java.util.Arrays;
import org.apache.avro.AvroTypeException;
@@ -289,6 +290,19 @@ public class BlockingBinaryEncoder extends BufferedBinaryEncoder {
}
@Override
+ public void writeFixed(ByteBuffer bytes) throws IOException {
+ int pos = bytes.position();
+ int len = bytes.remaining();
+ if (bytes.hasArray()) {
+ doWriteBytes(bytes.array(), bytes.arrayOffset() + pos, len);
+ } else {
+ byte[] b = new byte[len];
+ bytes.duplicate().get(b, 0, len);
+ doWriteBytes(b, 0, len);
+ }
+ }
+
+ @Override
protected void writeZero() throws IOException {
ensureBounds(1);
buf[pos++] = (byte) 0;
diff --git a/lang/java/avro/src/test/java/org/apache/avro/ByteBufferRecord.java b/lang/java/avro/src/test/java/org/apache/avro/ByteBufferRecord.java
new file mode 100644
index 0000000..2ef7b10
--- /dev/null
+++ b/lang/java/avro/src/test/java/org/apache/avro/ByteBufferRecord.java
@@ -0,0 +1,62 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avro;
+
+import java.nio.ByteBuffer;
+
+public class ByteBufferRecord {
+
+ private ByteBuffer payload;
+ private TypeEnum tp;
+
+ public ByteBufferRecord() { }
+
+ public ByteBuffer getPayload() {
+ return payload;
+ }
+
+ public void setPayload(ByteBuffer payload) {
+ this.payload = payload;
+ }
+
+ public TypeEnum getTp() {
+ return tp;
+ }
+
+ public void setTp(TypeEnum tp) {
+ this.tp = tp;
+ }
+
+ @Override
+ public boolean equals(Object ob) {
+ if (this == ob) return true;
+ if (!(ob instanceof ByteBufferRecord))
+ return false;
+ ByteBufferRecord that = (ByteBufferRecord)ob;
+ if (this.getPayload() == null) return that.getPayload() == null;
+ if (!this.getPayload().equals(that.getPayload())) return false;
+ if (this.getTp() == null) return that.getTp() == null;
+ if (!this.getTp().equals(that.getTp())) return false;
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ return this.payload.hashCode();
+ }
+}
diff --git a/lang/java/avro/src/test/java/org/apache/avro/TestDataFileReflect.java b/lang/java/avro/src/test/java/org/apache/avro/TestDataFileReflect.java
index a21f76f..91236a4 100644
--- a/lang/java/avro/src/test/java/org/apache/avro/TestDataFileReflect.java
+++ b/lang/java/avro/src/test/java/org/apache/avro/TestDataFileReflect.java
@@ -17,9 +17,12 @@
*/
package org.apache.avro;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -27,6 +30,10 @@ import java.util.List;
import org.apache.avro.file.DataFileReader;
import org.apache.avro.file.DataFileWriter;
import org.apache.avro.file.SeekableFileInput;
+import org.apache.avro.io.BinaryDecoder;
+import org.apache.avro.io.BinaryEncoder;
+import org.apache.avro.io.DecoderFactory;
+import org.apache.avro.io.EncoderFactory;
import org.apache.avro.reflect.ReflectData;
import org.apache.avro.reflect.ReflectDatumReader;
import org.apache.avro.reflect.ReflectDatumWriter;
@@ -112,6 +119,33 @@ public class TestDataFileReflect {
}
}
+ @Test
+ public void testNew() throws IOException {
+ ByteBuffer payload = ByteBuffer.allocateDirect(8 * 1024);
+ for (int i = 0; i < 500; i++) {
+ payload.putInt(1);
+ }
+ payload.flip();
+ ByteBufferRecord bbr = new ByteBufferRecord();
+ bbr.setPayload(payload);
+ bbr.setTp(TypeEnum.b);
+
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+ ReflectDatumWriter<ByteBufferRecord> writer = new ReflectDatumWriter<ByteBufferRecord>(ByteBufferRecord.class);
+ BinaryEncoder avroEncoder = EncoderFactory.get().blockingBinaryEncoder(outputStream, null);
+ writer.write(bbr, avroEncoder);
+ avroEncoder.flush();
+
+ byte[] bytes = outputStream.toByteArray();
+
+ ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
+ ReflectDatumReader<ByteBufferRecord> datumReader = new ReflectDatumReader<ByteBufferRecord>(ByteBufferRecord.class);
+ BinaryDecoder avroDecoder = DecoderFactory.get().binaryDecoder(inputStream, null);
+ ByteBufferRecord deserialized = datumReader.read(null, avroDecoder);
+
+ Assert.assertEquals(bbr, deserialized);
+ }
+
/*
* Test that writing out and reading in a nested class works
*/