AVRO-1704: Java: Add toByteArray and fromByteArray to specific.
diff --git a/lang/java/avro/src/test/java/org/apache/avro/specific/TestRecordWithLogicalTypes.java b/lang/java/avro/src/test/java/org/apache/avro/specific/TestRecordWithLogicalTypes.java
index c1b98c7..da3f878 100644
--- a/lang/java/avro/src/test/java/org/apache/avro/specific/TestRecordWithLogicalTypes.java
+++ b/lang/java/avro/src/test/java/org/apache/avro/specific/TestRecordWithLogicalTypes.java
@@ -5,7 +5,8 @@
*/
package org.apache.avro.specific;
-import org.apache.avro.Conversions;
+import org.apache.avro.message.BinaryMessageDecoder;
+import org.apache.avro.message.BinaryMessageEncoder;
import java.math.BigDecimal;
@@ -15,6 +16,26 @@
private static final long serialVersionUID = -4211233492739285532L;
public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"TestRecordWithLogicalTypes\",\"namespace\":\"org.apache.avro.specific\",\"fields\":[{\"name\":\"b\",\"type\":\"boolean\"},{\"name\":\"i32\",\"type\":\"int\"},{\"name\":\"i64\",\"type\":\"long\"},{\"name\":\"f32\",\"type\":\"float\"},{\"name\":\"f64\",\"type\":\"double\"},{\"name\":\"s\",\"type\":[\"null\",\"string\"],\"default\":null},{\"name\":\"d\",\"type\":{\"type\":\"int\",\"logicalType\":\"date\"}},{\"name\":\"t\",\"type\":{\"type\":\"int\",\"logicalType\":\"time-millis\"}},{\"name\":\"ts\",\"type\":{\"type\":\"long\",\"logicalType\":\"timestamp-millis\"}},{\"name\":\"dec\",\"type\":{\"type\":\"bytes\",\"logicalType\":\"decimal\",\"precision\":9,\"scale\":2}}]}");
public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
+
+ private static SpecificData MODEL$ = new SpecificData();
+
+ private static final BinaryMessageEncoder<TestRecordWithLogicalTypes> ENCODER =
+ new BinaryMessageEncoder<TestRecordWithLogicalTypes>(MODEL$, SCHEMA$);
+
+ private static final BinaryMessageDecoder<TestRecordWithLogicalTypes> DECODER =
+ new BinaryMessageDecoder<TestRecordWithLogicalTypes>(MODEL$, SCHEMA$);
+
+ /** Serializes this ${schema.getName()} to a ByteBuffer. */
+ public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException {
+ return ENCODER.encode(this);
+ }
+
+ /** Deserializes a ${schema.getName()} from a ByteBuffer. */
+ public static TestRecordWithLogicalTypes fromByteBuffer(
+ java.nio.ByteBuffer b) throws java.io.IOException {
+ return DECODER.decode(b);
+ }
+
@Deprecated public boolean b;
@Deprecated public int i32;
@Deprecated public long i64;
@@ -757,7 +778,7 @@
}
private static final org.apache.avro.io.DatumWriter
- WRITER$ = new org.apache.avro.specific.SpecificDatumWriter(SCHEMA$);
+ WRITER$ = new org.apache.avro.specific.SpecificDatumWriter(SCHEMA$);
@Override public void writeExternal(java.io.ObjectOutput out)
throws java.io.IOException {
@@ -765,7 +786,7 @@
}
private static final org.apache.avro.io.DatumReader
- READER$ = new org.apache.avro.specific.SpecificDatumReader(SCHEMA$);
+ READER$ = new org.apache.avro.specific.SpecificDatumReader(SCHEMA$);
@Override public void readExternal(java.io.ObjectInput in)
throws java.io.IOException {
diff --git a/lang/java/avro/src/test/java/org/apache/avro/specific/TestRecordWithoutLogicalTypes.java b/lang/java/avro/src/test/java/org/apache/avro/specific/TestRecordWithoutLogicalTypes.java
index 86fb8f0..92a5e60 100644
--- a/lang/java/avro/src/test/java/org/apache/avro/specific/TestRecordWithoutLogicalTypes.java
+++ b/lang/java/avro/src/test/java/org/apache/avro/specific/TestRecordWithoutLogicalTypes.java
@@ -5,6 +5,8 @@
*/
package org.apache.avro.specific;
+import org.apache.avro.message.BinaryMessageDecoder;
+import org.apache.avro.message.BinaryMessageEncoder;
import java.nio.ByteBuffer;
@SuppressWarnings("all")
@@ -12,6 +14,26 @@
public class TestRecordWithoutLogicalTypes extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord {
public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"TestRecordWithoutLogicalTypes\",\"namespace\":\"org.apache.avro.specific\",\"fields\":[{\"name\":\"b\",\"type\":\"boolean\"},{\"name\":\"i32\",\"type\":\"int\"},{\"name\":\"i64\",\"type\":\"long\"},{\"name\":\"f32\",\"type\":\"float\"},{\"name\":\"f64\",\"type\":\"double\"},{\"name\":\"s\",\"type\":[\"null\",\"string\"],\"default\":null},{\"name\":\"d\",\"type\":{\"type\":\"int\",\"logicalType\":\"date\"}},{\"name\":\"t\",\"type\":{\"type\":\"int\",\"logicalType\":\"time-millis\"}},{\"name\":\"ts\",\"type\":{\"type\":\"long\",\"logicalType\":\"timestamp-millis\"}},{\"name\":\"dec\",\"type\":{\"type\":\"bytes\",\"logicalType\":\"decimal\",\"precision\":9,\"scale\":2}}]}");
public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
+
+ private static SpecificData MODEL$ = new SpecificData();
+
+ private static final BinaryMessageEncoder<TestRecordWithoutLogicalTypes> ENCODER =
+ new BinaryMessageEncoder<TestRecordWithoutLogicalTypes>(MODEL$, SCHEMA$);
+
+ private static final BinaryMessageDecoder<TestRecordWithoutLogicalTypes> DECODER =
+ new BinaryMessageDecoder<TestRecordWithoutLogicalTypes>(MODEL$, SCHEMA$);
+
+ /** Serializes this ${schema.getName()} to a ByteBuffer. */
+ public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException {
+ return ENCODER.encode(this);
+ }
+
+ /** Deserializes a ${schema.getName()} from a ByteBuffer. */
+ public static TestRecordWithoutLogicalTypes fromByteBuffer(
+ java.nio.ByteBuffer b) throws java.io.IOException {
+ return DECODER.decode(b);
+ }
+
private boolean b;
private int i32;
private long i64;
diff --git a/lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificToFromByteArray.java b/lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificToFromByteArray.java
new file mode 100644
index 0000000..b17cad2
--- /dev/null
+++ b/lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificToFromByteArray.java
@@ -0,0 +1,102 @@
+package org.apache.avro.specific;
+
+import org.apache.avro.Conversions;
+import org.apache.avro.LogicalTypes;
+import org.apache.avro.data.TimeConversions;
+import org.apache.avro.message.MissingSchemaException;
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+import org.joda.time.LocalDate;
+import org.joda.time.LocalTime;
+import org.junit.Test;
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.nio.ByteBuffer;
+
+import static org.junit.Assert.assertEquals;
+
+public class TestSpecificToFromByteArray {
+ @Test
+ public void testSpecificToFromByteBufferWithLogicalTypes() throws IOException {
+ TestRecordWithLogicalTypes record = new TestRecordWithLogicalTypes(
+ true,
+ 34,
+ 35L,
+ 3.14F,
+ 3019.34,
+ null,
+ LocalDate.now(),
+ LocalTime.now(),
+ DateTime.now().withZone(DateTimeZone.UTC),
+ new BigDecimal("123.45")
+ );
+
+ ByteBuffer b = record.toByteBuffer();
+ TestRecordWithLogicalTypes copy = TestRecordWithLogicalTypes.fromByteBuffer(b);
+
+ assertEquals(record, copy);
+ }
+
+ @Test
+ public void testSpecificToFromByteBufferWithoutLogicalTypes() throws IOException {
+ TestRecordWithoutLogicalTypes record = new TestRecordWithoutLogicalTypes(
+ true,
+ 34,
+ 35L,
+ 3.14F,
+ 3019.34,
+ null,
+ new TimeConversions.DateConversion().toInt(LocalDate.now(), null, null),
+ new TimeConversions.TimeConversion().toInt(LocalTime.now(), null, null),
+ new TimeConversions.TimestampConversion().toLong(
+ DateTime.now().withZone(DateTimeZone.UTC), null, null),
+ new Conversions.DecimalConversion().toBytes(
+ new BigDecimal("123.45"), null, LogicalTypes.decimal(9, 2))
+ );
+
+ ByteBuffer b = record.toByteBuffer();
+ TestRecordWithoutLogicalTypes copy = TestRecordWithoutLogicalTypes.fromByteBuffer(b);
+
+ assertEquals(record, copy);
+ }
+
+ @Test(expected = MissingSchemaException.class)
+ public void testSpecificByteArrayIncompatibleWithLogicalTypes() throws IOException {
+ TestRecordWithoutLogicalTypes withoutLogicalTypes = new TestRecordWithoutLogicalTypes(
+ true,
+ 34,
+ 35L,
+ 3.14F,
+ 3019.34,
+ null,
+ new TimeConversions.DateConversion().toInt(LocalDate.now(), null, null),
+ new TimeConversions.TimeConversion().toInt(LocalTime.now(), null, null),
+ new TimeConversions.TimestampConversion().toLong(
+ DateTime.now().withZone(DateTimeZone.UTC), null, null),
+ new Conversions.DecimalConversion().toBytes(
+ new BigDecimal("123.45"), null, LogicalTypes.decimal(9, 2))
+ );
+
+ ByteBuffer b = withoutLogicalTypes.toByteBuffer();
+ TestRecordWithLogicalTypes.fromByteBuffer(b);
+ }
+
+ @Test(expected = MissingSchemaException.class)
+ public void testSpecificByteArrayIncompatibleWithoutLogicalTypes() throws IOException {
+ TestRecordWithLogicalTypes withLogicalTypes = new TestRecordWithLogicalTypes(
+ true,
+ 34,
+ 35L,
+ 3.14F,
+ 3019.34,
+ null,
+ LocalDate.now(),
+ LocalTime.now(),
+ DateTime.now().withZone(DateTimeZone.UTC),
+ new BigDecimal("123.45")
+ );
+
+ ByteBuffer b = withLogicalTypes.toByteBuffer();
+ TestRecordWithoutLogicalTypes.fromByteBuffer(b);
+ }
+}
diff --git a/lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm b/lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm
index dc81aba..1f99f38 100644
--- a/lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm
+++ b/lang/java/compiler/src/main/velocity/org/apache/avro/compiler/specific/templates/java/classic/record.vm
@@ -20,6 +20,8 @@
#end
import org.apache.avro.specific.SpecificData;
+import org.apache.avro.message.BinaryMessageEncoder;
+import org.apache.avro.message.BinaryMessageDecoder;
@SuppressWarnings("all")
#if ($schema.getDoc())
@@ -33,6 +35,28 @@
private static final long serialVersionUID = ${this.fingerprint64($schema)}L;
public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse(${this.javaSplit($schema.toString())});
public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
+
+ private static SpecificData MODEL$ = new SpecificData();
+
+#if (!$schema.isError())
+ private static final BinaryMessageEncoder<${this.mangle($schema.getName())}> ENCODER =
+ new BinaryMessageEncoder<${this.mangle($schema.getName())}>(MODEL$, SCHEMA$);
+
+ private static final BinaryMessageDecoder<${this.mangle($schema.getName())}> DECODER =
+ new BinaryMessageDecoder<${this.mangle($schema.getName())}>(MODEL$, SCHEMA$);
+
+ /** Serializes this ${schema.getName()} to a ByteBuffer. */
+ public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException {
+ return ENCODER.encode(this);
+ }
+
+ /** Deserializes a ${schema.getName()} from a ByteBuffer. */
+ public static ${this.mangle($schema.getName())} fromByteBuffer(
+ java.nio.ByteBuffer b) throws java.io.IOException {
+ return DECODER.decode(b);
+ }
+#end
+
#foreach ($field in $schema.getFields())
#if ($field.doc())
/** $field.doc() */
@@ -405,7 +429,7 @@
}
private static final org.apache.avro.io.DatumWriter
- WRITER$ = new org.apache.avro.specific.SpecificDatumWriter(SCHEMA$);
+ WRITER$ = MODEL$.createDatumWriter(SCHEMA$);
@Override public void writeExternal(java.io.ObjectOutput out)
throws java.io.IOException {
@@ -413,7 +437,7 @@
}
private static final org.apache.avro.io.DatumReader
- READER$ = new org.apache.avro.specific.SpecificDatumReader(SCHEMA$);
+ READER$ = MODEL$.createDatumReader(SCHEMA$);
@Override public void readExternal(java.io.ObjectInput in)
throws java.io.IOException {
diff --git a/lang/java/compiler/src/test/java/org/apache/avro/compiler/specific/TestSpecificCompiler.java b/lang/java/compiler/src/test/java/org/apache/avro/compiler/specific/TestSpecificCompiler.java
index 25ad97d..49174a8 100644
--- a/lang/java/compiler/src/test/java/org/apache/avro/compiler/specific/TestSpecificCompiler.java
+++ b/lang/java/compiler/src/test/java/org/apache/avro/compiler/specific/TestSpecificCompiler.java
@@ -516,4 +516,8 @@
Assert.assertEquals("Should use null for decimal if the flag is off",
"null", compiler.conversionInstance(uuidSchema));
}
+
+ public void testToFromByteBuffer() {
+
+ }
}
diff --git a/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Player.java b/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Player.java
index 07a3483..89b4af8 100644
--- a/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Player.java
+++ b/lang/java/tools/src/test/compiler/output-string/avro/examples/baseball/Player.java
@@ -6,6 +6,8 @@
package avro.examples.baseball;
import org.apache.avro.specific.SpecificData;
+import org.apache.avro.message.BinaryMessageEncoder;
+import org.apache.avro.message.BinaryMessageDecoder;
@SuppressWarnings("all")
/** 選手 is Japanese for player. */
@@ -14,6 +16,26 @@
private static final long serialVersionUID = 3865593031278745715L;
public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"Player\",\"namespace\":\"avro.examples.baseball\",\"doc\":\"選手 is Japanese for player.\",\"fields\":[{\"name\":\"number\",\"type\":\"int\",\"doc\":\"The number of the player\"},{\"name\":\"first_name\",\"type\":{\"type\":\"string\",\"avro.java.string\":\"String\"}},{\"name\":\"last_name\",\"type\":{\"type\":\"string\",\"avro.java.string\":\"String\"}},{\"name\":\"position\",\"type\":{\"type\":\"array\",\"items\":{\"type\":\"enum\",\"name\":\"Position\",\"symbols\":[\"P\",\"C\",\"B1\",\"B2\",\"B3\",\"SS\",\"LF\",\"CF\",\"RF\",\"DH\"]}}}]}");
public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
+
+ private static SpecificData MODEL$ = new SpecificData();
+
+ private static final BinaryMessageEncoder<Player> ENCODER =
+ new BinaryMessageEncoder<Player>(MODEL$, SCHEMA$);
+
+ private static final BinaryMessageDecoder<Player> DECODER =
+ new BinaryMessageDecoder<Player>(MODEL$, SCHEMA$);
+
+ /** Serializes this Player to a ByteBuffer. */
+ public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException {
+ return ENCODER.encode(this);
+ }
+
+ /** Deserializes a Player from a ByteBuffer. */
+ public static Player fromByteBuffer(
+ java.nio.ByteBuffer b) throws java.io.IOException {
+ return DECODER.decode(b);
+ }
+
/** The number of the player */
@Deprecated public int number;
@Deprecated public java.lang.String first_name;
@@ -52,6 +74,7 @@
default: throw new org.apache.avro.AvroRuntimeException("Bad index");
}
}
+
// Used by DatumReader. Applications should not call.
@SuppressWarnings(value="unchecked")
public void put(int field$, java.lang.Object value$) {
@@ -395,7 +418,7 @@
}
private static final org.apache.avro.io.DatumWriter
- WRITER$ = new org.apache.avro.specific.SpecificDatumWriter(SCHEMA$);
+ WRITER$ = MODEL$.createDatumWriter(SCHEMA$);
@Override public void writeExternal(java.io.ObjectOutput out)
throws java.io.IOException {
@@ -403,7 +426,7 @@
}
private static final org.apache.avro.io.DatumReader
- READER$ = new org.apache.avro.specific.SpecificDatumReader(SCHEMA$);
+ READER$ = MODEL$.createDatumReader(SCHEMA$);
@Override public void readExternal(java.io.ObjectInput in)
throws java.io.IOException {
diff --git a/lang/java/tools/src/test/compiler/output/Player.java b/lang/java/tools/src/test/compiler/output/Player.java
index 252eaab..bfd3d07 100644
--- a/lang/java/tools/src/test/compiler/output/Player.java
+++ b/lang/java/tools/src/test/compiler/output/Player.java
@@ -6,6 +6,8 @@
package avro.examples.baseball;
import org.apache.avro.specific.SpecificData;
+import org.apache.avro.message.BinaryMessageEncoder;
+import org.apache.avro.message.BinaryMessageDecoder;
@SuppressWarnings("all")
/** 選手 is Japanese for player. */
@@ -14,6 +16,26 @@
private static final long serialVersionUID = 3865593031278745715L;
public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"Player\",\"namespace\":\"avro.examples.baseball\",\"doc\":\"選手 is Japanese for player.\",\"fields\":[{\"name\":\"number\",\"type\":\"int\",\"doc\":\"The number of the player\"},{\"name\":\"first_name\",\"type\":\"string\"},{\"name\":\"last_name\",\"type\":\"string\"},{\"name\":\"position\",\"type\":{\"type\":\"array\",\"items\":{\"type\":\"enum\",\"name\":\"Position\",\"symbols\":[\"P\",\"C\",\"B1\",\"B2\",\"B3\",\"SS\",\"LF\",\"CF\",\"RF\",\"DH\"]}}}]}");
public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
+
+ private static SpecificData MODEL$ = new SpecificData();
+
+ private static final BinaryMessageEncoder<Player> ENCODER =
+ new BinaryMessageEncoder<Player>(MODEL$, SCHEMA$);
+
+ private static final BinaryMessageDecoder<Player> DECODER =
+ new BinaryMessageDecoder<Player>(MODEL$, SCHEMA$);
+
+ /** Serializes this Player to a ByteBuffer. */
+ public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException {
+ return ENCODER.encode(this);
+ }
+
+ /** Deserializes a Player from a ByteBuffer. */
+ public static Player fromByteBuffer(
+ java.nio.ByteBuffer b) throws java.io.IOException {
+ return DECODER.decode(b);
+ }
+
/** The number of the player */
@Deprecated public int number;
@Deprecated public java.lang.CharSequence first_name;
@@ -52,6 +74,7 @@
default: throw new org.apache.avro.AvroRuntimeException("Bad index");
}
}
+
// Used by DatumReader. Applications should not call.
@SuppressWarnings(value="unchecked")
public void put(int field$, java.lang.Object value$) {
@@ -395,7 +418,7 @@
}
private static final org.apache.avro.io.DatumWriter
- WRITER$ = new org.apache.avro.specific.SpecificDatumWriter(SCHEMA$);
+ WRITER$ = MODEL$.createDatumWriter(SCHEMA$);
@Override public void writeExternal(java.io.ObjectOutput out)
throws java.io.IOException {
@@ -403,7 +426,7 @@
}
private static final org.apache.avro.io.DatumReader
- READER$ = new org.apache.avro.specific.SpecificDatumReader(SCHEMA$);
+ READER$ = MODEL$.createDatumReader(SCHEMA$);
@Override public void readExternal(java.io.ObjectInput in)
throws java.io.IOException {