Package org.apache.avro.io
Class BinaryEncoder
java.lang.Object
org.apache.avro.io.Encoder
org.apache.avro.io.BinaryEncoder
- All Implemented Interfaces:
Flushable
- Direct Known Subclasses:
BufferedBinaryEncoder
,DirectBinaryEncoder
An abstract
Encoder
for Avro's binary encoding.
To construct and configure instances, use EncoderFactory
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionabstract int
Returns the number of bytes currently buffered by this encoder.void
setItemCount
(long itemCount) Call this method before writing a batch of items in an array or a map.void
Start a new item of an array or map.void
Call this method to finish writing an array.void
Call this method to start writing an array.void
writeBytes
(byte[] bytes, int start, int len) Write a byte string.void
writeBytes
(ByteBuffer bytes) Write a byte string.void
writeEnum
(int e) Writes an enumeration.void
writeIndex
(int unionIndex) Call this method to write the tag of a union.void
Call this method to terminate the inner-most, currently-opened map.void
Call this to start a new map.void
"Writes" a null value.void
writeString
(String string) Write a Unicode character string.void
writeString
(Utf8 utf8) Write a Unicode character string.protected abstract void
Write a zero byte to the underlying output.Methods inherited from class org.apache.avro.io.Encoder
writeBoolean, writeBytes, writeDouble, writeFixed, writeFixed, writeFixed, writeFloat, writeInt, writeLong, writeString
-
Constructor Details
-
BinaryEncoder
public BinaryEncoder()
-
-
Method Details
-
writeNull
Description copied from class:Encoder
"Writes" a null value. (Doesn't actually write anything, but advances the state of the parser if this class is stateful.)- Specified by:
writeNull
in classEncoder
- Throws:
IOException
-
writeString
Description copied from class:Encoder
Write a Unicode character string.- Specified by:
writeString
in classEncoder
- Throws:
IOException
-
writeString
Description copied from class:Encoder
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.- Overrides:
writeString
in classEncoder
- Throws:
IOException
-
writeBytes
Description copied from class:Encoder
Write a byte string.- Specified by:
writeBytes
in classEncoder
- Throws:
IOException
-
writeBytes
Description copied from class:Encoder
Write a byte string.- Specified by:
writeBytes
in classEncoder
- Throws:
IOException
-
writeEnum
Description copied from class:Encoder
Writes an enumeration.- Specified by:
writeEnum
in classEncoder
- Parameters:
e
-- Throws:
IOException
-
writeArrayStart
Description copied from class:Encoder
Call this method to start writing an array. When starting to serialize an array, callEncoder.writeArrayStart()
. Then, before writing any data for any item callEncoder.setItemCount(long)
followed by a sequence ofEncoder.startItem()
and the item itself. The number ofEncoder.startItem()
should match the number specified inEncoder.setItemCount(long)
. When actually writing the data of the item, you can call anyEncoder
method (e.g.,Encoder.writeLong(long)
). When all items of the array have been written, callEncoder.writeArrayEnd()
. 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();
- Specified by:
writeArrayStart
in classEncoder
- Throws:
IOException
-
setItemCount
Description copied from class:Encoder
Call this method before writing a batch of items in an array or a map. Then for each item, callEncoder.startItem()
followed by any of the other write methods ofEncoder
. The number of calls toEncoder.startItem()
must be equal to the count specified inEncoder.setItemCount(long)
. Once a batch is completed you can start another batch withEncoder.setItemCount(long)
.- Specified by:
setItemCount
in classEncoder
- Parameters:
itemCount
- The number ofEncoder.startItem()
calls to follow.- Throws:
IOException
-
startItem
Description copied from class:Encoder
Start a new item of an array or map. SeeEncoder.writeArrayStart()
for usage information.- Specified by:
startItem
in classEncoder
- Throws:
IOException
-
writeArrayEnd
Description copied from class:Encoder
Call this method to finish writing an array. SeeEncoder.writeArrayStart()
for usage information.- Specified by:
writeArrayEnd
in classEncoder
- Throws:
IOException
-
writeMapStart
Description copied from class:Encoder
Call this to start a new map. SeeEncoder.writeArrayStart()
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();
- Specified by:
writeMapStart
in classEncoder
- Throws:
IOException
-
writeMapEnd
Description copied from class:Encoder
Call this method to terminate the inner-most, currently-opened map. SeeEncoder.writeArrayStart()
for more details.- Specified by:
writeMapEnd
in classEncoder
- Throws:
IOException
-
writeIndex
Description copied from class:Encoder
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);
- Specified by:
writeIndex
in classEncoder
- Throws:
IOException
-
writeZero
Write a zero byte to the underlying output.- Throws:
IOException
-
bytesBuffered
public abstract int bytesBuffered()Returns the number of bytes currently buffered by this encoder. If this Encoder does not buffer, this will always return zero. CallFlushable.flush()
to empty the buffer to the underlying output.
-