Package org.apache.avro.io
Class Encoder
java.lang.Object
org.apache.avro.io.Encoder
- All Implemented Interfaces:
Flushable
- Direct Known Subclasses:
BinaryEncoder
,ParsingEncoder
Low-level support for serializing Avro values.
This class has two types of methods. One type of methods support the writing
of leaf values (for example,
writeLong(long)
and writeString(org.apache.avro.util.Utf8)
).
These methods have analogs in Decoder
.
The other type of methods support the writing of maps and arrays. These
methods are writeArrayStart()
, startItem()
, and
writeArrayEnd()
(and similar methods for maps). Some implementations
of Encoder
handle the buffering required to break large maps and
arrays into blocks, which is necessary for applications that want to do
streaming. (See writeArrayStart()
for details on these methods.)
EncoderFactory
contains Encoder construction and configuration
facilities.- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionabstract void
setItemCount
(long itemCount) Call this method before writing a batch of items in an array or a map.abstract void
Start a new item of an array or map.abstract void
Call this method to finish writing an array.abstract void
Call this method to start writing an array.abstract void
writeBoolean
(boolean b) Write a boolean value.void
writeBytes
(byte[] bytes) Writes a byte string.abstract void
writeBytes
(byte[] bytes, int start, int len) Write a byte string.abstract void
writeBytes
(ByteBuffer bytes) Write a byte string.abstract void
writeDouble
(double d) Write a double.abstract void
writeEnum
(int e) Writes an enumeration.void
writeFixed
(byte[] bytes) A shorthand for writeFixed(bytes, 0, bytes.length)abstract void
writeFixed
(byte[] bytes, int start, int len) Writes a fixed size binary object.void
writeFixed
(ByteBuffer bytes) Writes a fixed from a ByteBuffer.abstract void
writeFloat
(float f) Write a float.abstract void
writeIndex
(int unionIndex) Call this method to write the tag of a union.abstract void
writeInt
(int n) Writes a 32-bit integer.abstract void
writeLong
(long n) Write a 64-bit integer.abstract void
Call this method to terminate the inner-most, currently-opened map.abstract void
Call this to start a new map.abstract void
"Writes" a null value.void
writeString
(CharSequence charSequence) Write a Unicode character string.void
writeString
(String str) Write a Unicode character string.abstract void
writeString
(Utf8 utf8) Write a Unicode character string.
-
Constructor Details
-
Encoder
public Encoder()
-
-
Method Details
-
writeNull
"Writes" a null value. (Doesn't actually write anything, but advances the state of the parser if this class is stateful.) -
writeBoolean
Write a boolean value. -
writeInt
Writes a 32-bit integer. -
writeLong
Write a 64-bit integer. -
writeFloat
Write a float. -
writeDouble
Write a double. -
writeString
Write a Unicode character string. -
writeString
Write a Unicode character string. The default implementation converts the String to aUtf8
. Some Encoder implementations may want to do something different as a performance optimization. -
writeString
Write a Unicode character string. If the CharSequence is anUtf8
it writes this directly, otherwise the CharSequence is converted to a String via toString() and written. -
writeBytes
Write a byte string. -
writeBytes
Write a byte string. -
writeBytes
Writes a byte string. Equivalent to writeBytes(bytes, 0, bytes.length) -
writeFixed
Writes a fixed size binary object.- Parameters:
bytes
- The contents to writestart
- The position within bytes where the contents start.len
- The number of bytes to write.
-
writeFixed
A shorthand for writeFixed(bytes, 0, bytes.length)- Parameters:
bytes
-- Throws:
IOException
-
writeFixed
Writes a fixed from a ByteBuffer.- Throws:
IOException
-
writeEnum
Writes an enumeration.- Parameters:
e
-
-
writeArrayStart
Call this method to start writing an array. When starting to serialize an array, callwriteArrayStart()
. Then, before writing any data for any item callsetItemCount(long)
followed by a sequence ofstartItem()
and the item itself. The number ofstartItem()
should match the number specified insetItemCount(long)
. When actually writing the data of the item, you can call anyEncoder
method (e.g.,writeLong(long)
). When all items of the array have been written, callwriteArrayEnd()
. As an example, let's say you want to write an array of records, the record consisting of an Long field and a Boolean field. Your code would look something like this:out.writeArrayStart(); out.setItemCount(list.size()); for (Record r : list) { out.startItem(); out.writeLong(r.longField); out.writeBoolean(r.boolField); } out.writeArrayEnd();
-
setItemCount
Call this method before writing a batch of items in an array or a map. Then for each item, callstartItem()
followed by any of the other write methods ofEncoder
. The number of calls tostartItem()
must be equal to the count specified insetItemCount(long)
. Once a batch is completed you can start another batch withsetItemCount(long)
.- Parameters:
itemCount
- The number ofstartItem()
calls to follow.- Throws:
IOException
-
startItem
Start a new item of an array or map. SeewriteArrayStart()
for usage information. -
writeArrayEnd
Call this method to finish writing an array. SeewriteArrayStart()
for usage information. -
writeMapStart
Call this to start a new map. SeewriteArrayStart()
for details on usage. As an example of usage, let's say you want to write a map of records, the record consisting of an Long field and a Boolean field. Your code would look something like this:out.writeMapStart(); out.setItemCount(list.size()); for (Map.Entryinvalid input: '<'String, Record> entry : map.entrySet()) { out.startItem(); out.writeString(entry.getKey()); out.writeLong(entry.getValue().longField); out.writeBoolean(entry.getValue().boolField); } out.writeMapEnd();
-
writeMapEnd
Call this method to terminate the inner-most, currently-opened map. SeewriteArrayStart()
for more details. -
writeIndex
Call this method to write the tag of a union. As an example of usage, let's say you want to write a union, whose second branch is a record consisting of an Long field and a Boolean field. Your code would look something like this:out.writeIndex(1); out.writeLong(record.longField); out.writeBoolean(record.boolField);
-