public class BlockingDirectBinaryEncoder extends DirectBinaryEncoder
Encoder for Avro's binary encoding that does not buffer output.
This encoder does not buffer writes in contrast to
BufferedBinaryEncoder. However, it is lighter-weight and useful when:
The buffering in BufferedBinaryEncoder is not desired because you buffer a
different level or the Encoder is very short-lived.
The BlockingDirectBinaryEncoder will encode the number of bytes of the Map
and Array blocks. This will allow to postpone the decoding, or skip over it
at all.
To construct, use
EncoderFactory.blockingDirectBinaryEncoder(OutputStream, BinaryEncoder)
BlockingDirectBinaryEncoder instances returned by this method are not
thread-safeBinaryEncoder,
EncoderFactory,
Encoder,
Decoderout| Constructor and Description |
|---|
BlockingDirectBinaryEncoder(OutputStream out)
Create a writer that sends its output to the underlying stream
out. |
| Modifier and Type | Method and Description |
|---|---|
void |
setItemCount(long itemCount)
Call this method before writing a batch of items in an array or a map.
|
void |
writeArrayEnd()
Call this method to finish writing an array.
|
void |
writeArrayStart()
Call this method to start writing an array.
|
void |
writeMapEnd()
Call this method to terminate the inner-most, currently-opened map.
|
void |
writeMapStart()
Call this to start a new map.
|
bytesBuffered, flush, writeBoolean, writeDouble, writeFixed, writeFloat, writeInt, writeLong, writeZerostartItem, writeBytes, writeBytes, writeEnum, writeIndex, writeNull, writeString, writeStringwriteBytes, writeFixed, writeFixed, writeStringpublic BlockingDirectBinaryEncoder(OutputStream out)
out.out - The Outputstream to write topublic void setItemCount(long itemCount)
throws IOException
EncoderEncoder.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 BinaryEncoderitemCount - The number of Encoder.startItem() calls to follow.IOExceptionpublic void writeArrayStart()
throws IOException
EncoderEncoder.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 BinaryEncoderIOExceptionpublic void writeArrayEnd()
throws IOException
EncoderEncoder.writeArrayStart() for
usage information.writeArrayEnd in class BinaryEncoderIOExceptionpublic void writeMapStart()
throws IOException
EncoderEncoder.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 BinaryEncoderIOExceptionpublic void writeMapEnd()
throws IOException
EncoderEncoder.writeArrayStart() for more details.writeMapEnd in class BinaryEncoderIOExceptionCopyright © 2009–2025 The Apache Software Foundation. All rights reserved.