[
https://issues.apache.org/jira/browse/AVRO-1743?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16703773#comment-16703773
]
ASF GitHub Bot commented on AVRO-1743:
--------------------------------------
dkulp closed pull request #78: AVRO-1743: override writeFixed in
BlockingBinaryEncoder
URL: https://github.com/apache/avro/pull/78
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
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 e8b6c3388..351a3aab4 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 @@
import java.io.IOException;
import java.io.OutputStream;
+import java.nio.ByteBuffer;
import java.util.Arrays;
import org.apache.avro.AvroTypeException;
@@ -287,7 +288,20 @@ public void writeDouble(double d) throws IOException {
public void writeFixed(byte[] bytes, int start, int len) throws IOException {
doWriteBytes(bytes, start, len);
}
-
+
+ @Override
+ public void writeFixed(ByteBuffer bytes) throws IOException {
+ int pos = bytes.position();
+ int len = bytes.limit() - pos;
+ 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);
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 000000000..2ef7b10df
--- /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 78ff01456..15ad25701 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,8 @@
*/
package org.apache.avro;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
+import java.io.*;
+import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -27,6 +26,10 @@
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;
@@ -105,6 +108,33 @@ public void testNull() throws IOException {
reader.close();
}
+ @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
*/
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> BlockingBinaryEncoder should override writeFixed(ByteBuffer bytes)
> ------------------------------------------------------------------
>
> Key: AVRO-1743
> URL: https://issues.apache.org/jira/browse/AVRO-1743
> Project: Apache Avro
> Issue Type: Bug
> Components: java
> Affects Versions: 1.7.6, 1.7.7
> Environment: All environments
> Reporter: Dmitry Spikhalskiy
> Assignee: Daniel Kulp
> Priority: Major
> Labels: starter
> Fix For: 1.9.0
>
>
> BlockingBinaryEncoder which extends BufferedBinaryEncoder should override
> "public void writeFixed(ByteBuffer bytes) throws IOException" method.
> Now if we use BlockingBinaryEncoder - all writeFixed(ByteBuffer bytes) are
> addressed by BufferedBinaryEncoder. As a result, if "!bytes.hasArray() &&
> bytes.remaining() > bulkLimit", then in flushBuffer() we flush empty buffer
> from BufferedBinaryEncoder and don't flush actual buffer from
> BlockingBinaryEncoder.
> I prepared localized unit tests to replicate bugs here:
> https://github.com/Spikhalskiy/avro-blockingbinaryencoder-error
> Bug could appears in silently incorrect serialization (We will read another
> object) or in deserialization errors. Both replicated in provided tests.
> Looks like BlockingBinaryEncoder which extends BufferedBinaryEncoder is
> error-prone approach and mistake in class hierarchy. We mostly override
> everything from BufferedBinaryEncoder, creating unused buffers and fields
> (like double pos, buf, etc), and it's already not first bug relating to
> "somebody forget to override method in BlockingBinaryEncoder from
> BufferedBinaryEncoder" (ex. https://issues.apache.org/jira/browse/AVRO-88).
> So, this classes should be separated at all or have common interface, or at
> least work with same buffer and pos instances. But BlockingBinaryEncoder
> shouldn't inherit method implementations, which work with another buffer
> object.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)