Package org.apache.avro.io
Class BlockingDirectBinaryEncoder
java.lang.Object
org.apache.avro.io.Encoder
org.apache.avro.io.BinaryEncoder
org.apache.avro.io.DirectBinaryEncoder
org.apache.avro.io.BlockingDirectBinaryEncoder
- All Implemented Interfaces:
Flushable
An
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-safe- See Also:
-
Field Summary
Fields inherited from class org.apache.avro.io.DirectBinaryEncoder
out -
Constructor Summary
ConstructorsConstructorDescriptionCreate a writer that sends its output to the underlying streamout. -
Method Summary
Modifier and TypeMethodDescriptionvoidsetItemCount(long itemCount) Call this method before writing a batch of items in an array or a map.voidCall this method to finish writing an array.voidCall this method to start writing an array.voidCall this method to terminate the inner-most, currently-opened map.voidCall this to start a new map.Methods inherited from class org.apache.avro.io.DirectBinaryEncoder
bytesBuffered, flush, writeBoolean, writeDouble, writeFixed, writeFloat, writeInt, writeLong, writeZeroMethods inherited from class org.apache.avro.io.BinaryEncoder
startItem, writeBytes, writeBytes, writeEnum, writeIndex, writeNull, writeString, writeStringMethods inherited from class org.apache.avro.io.Encoder
writeBytes, writeFixed, writeFixed, writeString
-
Constructor Details
-
BlockingDirectBinaryEncoder
Create a writer that sends its output to the underlying streamout.- Parameters:
out- The Outputstream to write to
-
-
Method Details
-
setItemCount
Description copied from class:EncoderCall 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).- Overrides:
setItemCountin classBinaryEncoder- Parameters:
itemCount- The number ofEncoder.startItem()calls to follow.- Throws:
IOException
-
writeArrayStart
Description copied from class:EncoderCall 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 anyEncodermethod (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();- Overrides:
writeArrayStartin classBinaryEncoder- Throws:
IOException
-
writeArrayEnd
Description copied from class:EncoderCall this method to finish writing an array. SeeEncoder.writeArrayStart()for usage information.- Overrides:
writeArrayEndin classBinaryEncoder- Throws:
IOException
-
writeMapStart
Description copied from class:EncoderCall 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();- Overrides:
writeMapStartin classBinaryEncoder- Throws:
IOException
-
writeMapEnd
Description copied from class:EncoderCall this method to terminate the inner-most, currently-opened map. SeeEncoder.writeArrayStart()for more details.- Overrides:
writeMapEndin classBinaryEncoder- Throws:
IOException
-