Package org.apache.avro.io
Class BlockingBinaryEncoder
java.lang.Object
org.apache.avro.io.Encoder
org.apache.avro.io.BinaryEncoder
org.apache.avro.io.BufferedBinaryEncoder
org.apache.avro.io.BlockingBinaryEncoder
- All Implemented Interfaces:
Flushable
A
BinaryEncoder implementation that writes large arrays and maps as a
sequence of blocks. So long as individual primitive values fit in memory,
arbitrarily long arrays and maps may be written and subsequently read without
exhausting memory. Values are buffered until the specified block size would
be exceeded, minimizing block overhead.
Use EncoderFactory.blockingBinaryEncoder(OutputStream, BinaryEncoder)
to construct and configure.
BlockingBinaryEncoder buffers writes, data may not appear on the output until
flush() is called.
BlockingBinaryEncoder is not thread-safe- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionintReturns the number of bytes currently buffered by this encoder.voidflush()voidsetItemCount(long itemCount) Call this method before writing a batch of items in an array or a map.voidStart a new item of an array or map.voidCall this method to finish writing an array.voidCall this method to start writing an array.voidwriteBoolean(boolean b) Write a boolean value.voidwriteDouble(double d) Write a double.voidwriteFixed(byte[] bytes, int start, int len) Writes a fixed size binary object.voidwriteFixed(ByteBuffer bytes) Writes a fixed from a ByteBuffer.voidwriteFloat(float f) Write a float.voidwriteIndex(int unionIndex) Call this method to write the tag of a union.voidwriteInt(int n) Writes a 32-bit integer.voidwriteLong(long n) Write a 64-bit integer.voidCall this method to terminate the inner-most, currently-opened map.voidCall this to start a new map.protected voidWrite a zero byte to the underlying output.Methods inherited from class org.apache.avro.io.BinaryEncoder
writeBytes, writeBytes, writeEnum, writeNull, writeString, writeStringMethods inherited from class org.apache.avro.io.Encoder
writeBytes, writeFixed, writeString
-
Method Details
-
flush
- Specified by:
flushin interfaceFlushable- Overrides:
flushin classBufferedBinaryEncoder- Throws:
IOException
-
writeBoolean
Description copied from class:EncoderWrite a boolean value.- Overrides:
writeBooleanin classBufferedBinaryEncoder- Throws:
IOException
-
writeInt
Description copied from class:EncoderWrites a 32-bit integer.- Overrides:
writeIntin classBufferedBinaryEncoder- Throws:
IOException
-
writeLong
Description copied from class:EncoderWrite a 64-bit integer.- Overrides:
writeLongin classBufferedBinaryEncoder- Throws:
IOException
-
writeFloat
Description copied from class:EncoderWrite a float.- Overrides:
writeFloatin classBufferedBinaryEncoder- Throws:
IOException
-
writeDouble
Description copied from class:EncoderWrite a double.- Overrides:
writeDoublein classBufferedBinaryEncoder- Throws:
IOException
-
writeFixed
Description copied from class:EncoderWrites a fixed size binary object.- Overrides:
writeFixedin classBufferedBinaryEncoder- Parameters:
bytes- The contents to writestart- The position within bytes where the contents start.len- The number of bytes to write.- Throws:
IOException
-
writeFixed
Description copied from class:EncoderWrites a fixed from a ByteBuffer.- Overrides:
writeFixedin classBufferedBinaryEncoder- Throws:
IOException
-
writeZero
Description copied from class:BinaryEncoderWrite a zero byte to the underlying output.- Overrides:
writeZeroin classBufferedBinaryEncoder- 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
-
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
-
startItem
Description copied from class:EncoderStart a new item of an array or map. SeeEncoder.writeArrayStart()for usage information.- Overrides:
startItemin 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
-
writeIndex
Description copied from class:EncoderCall 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);
- Overrides:
writeIndexin classBinaryEncoder- Throws:
IOException
-
bytesBuffered
public int bytesBuffered()Description copied from class:BinaryEncoderReturns 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.- Overrides:
bytesBufferedin classBufferedBinaryEncoder
-