public abstract class BinaryEncoder extends Encoder
Encoder
for Avro's binary encoding.
To construct and configure instances, use EncoderFactory
Constructor and Description |
---|
BinaryEncoder() |
Modifier and Type | Method and Description |
---|---|
abstract int |
bytesBuffered()
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 |
startItem()
Start a new item of an array or map.
|
void |
writeArrayEnd()
Call this method to finish writing an array.
|
void |
writeArrayStart()
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 |
writeMapEnd()
Call this method to terminate the inner-most, currently-opened
map.
|
void |
writeMapStart()
Call this to start a new map.
|
void |
writeNull()
"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 |
writeZero()
Write a zero byte to the underlying output.
|
writeBoolean, writeBytes, writeDouble, writeFixed, writeFixed, writeFixed, writeFloat, writeInt, writeLong, writeString
public void writeNull() throws IOException
Encoder
writeNull
in class Encoder
IOException
public void writeString(Utf8 utf8) throws IOException
Encoder
writeString
in class Encoder
IOException
public void writeString(String string) throws IOException
Encoder
Utf8
. Some Encoder
implementations may want to do something different as a performance optimization.writeString
in class Encoder
IOException
public void writeBytes(ByteBuffer bytes) throws IOException
Encoder
writeBytes
in class Encoder
IOException
public void writeBytes(byte[] bytes, int start, int len) throws IOException
Encoder
writeBytes
in class Encoder
IOException
public void writeEnum(int e) throws IOException
Encoder
writeEnum
in class Encoder
IOException
public void writeArrayStart() throws IOException
Encoder
Encoder.writeArrayStart()
. Then, before writing any data for any item
call Encoder.setItemCount(long)
followed by a sequence of
Encoder.startItem()
and the item itself. The number of
Encoder.startItem()
should match the number specified in
Encoder.setItemCount(long)
.
When actually writing the data of the item, you can call any Encoder
method (e.g., Encoder.writeLong(long)
). When all items
of the array have been written, call Encoder.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();
writeArrayStart
in class Encoder
IOException
public void setItemCount(long itemCount) throws IOException
Encoder
Encoder.startItem()
followed by any of the
other write methods of Encoder
. The number of calls
to Encoder.startItem()
must be equal to the count specified
in Encoder.setItemCount(long)
. Once a batch is completed you
can start another batch with Encoder.setItemCount(long)
.setItemCount
in class Encoder
itemCount
- The number of Encoder.startItem()
calls to follow.IOException
public void startItem() throws IOException
Encoder
Encoder.writeArrayStart()
for usage information.startItem
in class Encoder
IOException
public void writeArrayEnd() throws IOException
Encoder
Encoder.writeArrayStart()
for usage information.writeArrayEnd
in class Encoder
IOException
public void writeMapStart() throws IOException
Encoder
Encoder.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.Entryentry : map.entrySet()) { out.startItem(); out.writeString(entry.getKey()); out.writeLong(entry.getValue().longField); out.writeBoolean(entry.getValue().boolField); } out.writeMapEnd();
writeMapStart
in class Encoder
IOException
public void writeMapEnd() throws IOException
Encoder
Encoder.writeArrayStart()
for more details.writeMapEnd
in class Encoder
IOException
public void writeIndex(int unionIndex) throws IOException
Encoder
out.writeIndex(1); out.writeLong(record.longField); out.writeBoolean(record.boolField);
writeIndex
in class Encoder
IOException
protected abstract void writeZero() throws IOException
IOException
public abstract int bytesBuffered()
Flushable.flush()
to empty the buffer to the underlying output.Copyright © 2009–2017 The Apache Software Foundation. All rights reserved.