Package org.apache.avro.io
Class BinaryDecoder
java.lang.Object
org.apache.avro.io.Decoder
org.apache.avro.io.BinaryDecoder
An
Decoder
for binary-format data.
Instances are created using DecoderFactory
.
This class may read-ahead and buffer bytes from the source beyond what is
required to serve its read methods. The number of unused bytes in the buffer
can be accessed by inputStream().remaining(), if the BinaryDecoder is not
'direct'.
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionlong
Processes the next block of an array and returns the number of items in the block and let's the caller read those items.protected void
doReadBytes
(byte[] bytes, int start, int length) Reads length bytes into bytes starting at start.protected long
Returns the number of items to follow in the current array or map.protected void
doSkipBytes
(long length) Returns anInputStream
that is aware of any buffering that may occur in this BinaryDecoder.boolean
isEnd()
Returns true if the current BinaryDecoder is at the end of its source data and cannot read any further without throwing an EOFException or other IOException.long
mapNext()
Processes the next block of map entries and returns the count of them.long
Reads and returns the size of the first block of an array.boolean
Reads a boolean value written byEncoder.writeBoolean(boolean)
.readBytes
(ByteBuffer old) Reads a byte-string written byEncoder.writeBytes(java.nio.ByteBuffer)
. if old is not null and has sufficient capacity to take in the bytes being read, the bytes are returned in old.double
Reads a double written byEncoder.writeDouble(double)
.int
readEnum()
Reads an enumeration.void
readFixed
(byte[] bytes, int start, int length) Reads fixed sized binary object.float
Reads a float written byEncoder.writeFloat(float)
.int
Reads the tag of a union written byEncoder.writeIndex(int)
.int
readInt()
Reads an integer written byEncoder.writeInt(int)
.long
readLong()
Reads a long written byEncoder.writeLong(long)
.long
Reads and returns the size of the next block of map-entries.void
readNull()
"Reads" a null value.Reads a char-string written byEncoder.writeString(org.apache.avro.util.Utf8)
.readString
(Utf8 old) Reads a char-string written byEncoder.writeString(org.apache.avro.util.Utf8)
.long
Used for quickly skipping through an array.void
Discards a byte-string written byEncoder.writeBytes(java.nio.ByteBuffer)
.void
skipFixed
(int length) Discards fixed sized binary object.long
skipMap()
Support for quickly skipping through a map similar toDecoder.skipArray()
.void
Discards a char-string written byEncoder.writeString(org.apache.avro.util.Utf8)
.
-
Constructor Details
-
BinaryDecoder
protected BinaryDecoder()protected constructor for child classes
-
-
Method Details
-
readNull
Description copied from class:Decoder
"Reads" a null value. (Doesn't actually read anything, but advances the state of the parser if the implementation is stateful.)- Specified by:
readNull
in classDecoder
- Throws:
IOException
-
readBoolean
Description copied from class:Decoder
Reads a boolean value written byEncoder.writeBoolean(boolean)
.- Specified by:
readBoolean
in classDecoder
- Throws:
IOException
-
readInt
Description copied from class:Decoder
Reads an integer written byEncoder.writeInt(int)
.- Specified by:
readInt
in classDecoder
- Throws:
IOException
-
readLong
Description copied from class:Decoder
Reads a long written byEncoder.writeLong(long)
.- Specified by:
readLong
in classDecoder
- Throws:
IOException
-
readFloat
Description copied from class:Decoder
Reads a float written byEncoder.writeFloat(float)
.- Specified by:
readFloat
in classDecoder
- Throws:
IOException
-
readDouble
Description copied from class:Decoder
Reads a double written byEncoder.writeDouble(double)
.- Specified by:
readDouble
in classDecoder
- Throws:
IOException
-
readString
Description copied from class:Decoder
Reads a char-string written byEncoder.writeString(org.apache.avro.util.Utf8)
.- Specified by:
readString
in classDecoder
- Throws:
IOException
-
readString
Description copied from class:Decoder
Reads a char-string written byEncoder.writeString(org.apache.avro.util.Utf8)
.- Specified by:
readString
in classDecoder
- Throws:
IOException
-
skipString
Description copied from class:Decoder
Discards a char-string written byEncoder.writeString(org.apache.avro.util.Utf8)
.- Specified by:
skipString
in classDecoder
- Throws:
IOException
-
readBytes
Description copied from class:Decoder
Reads a byte-string written byEncoder.writeBytes(java.nio.ByteBuffer)
. if old is not null and has sufficient capacity to take in the bytes being read, the bytes are returned in old.- Specified by:
readBytes
in classDecoder
- Throws:
IOException
-
skipBytes
Description copied from class:Decoder
Discards a byte-string written byEncoder.writeBytes(java.nio.ByteBuffer)
.- Specified by:
skipBytes
in classDecoder
- Throws:
IOException
-
readFixed
Description copied from class:Decoder
Reads fixed sized binary object.- Specified by:
readFixed
in classDecoder
- Parameters:
bytes
- The buffer to store the contents being read.start
- The position where the data needs to be written.length
- The size of the binary object.- Throws:
IOException
-
skipFixed
Description copied from class:Decoder
Discards fixed sized binary object.- Specified by:
skipFixed
in classDecoder
- Parameters:
length
- The size of the binary object to be skipped.- Throws:
IOException
-
readEnum
Description copied from class:Decoder
Reads an enumeration.- Specified by:
readEnum
in classDecoder
- Returns:
- The enumeration's value.
- Throws:
IOException
-
doSkipBytes
- Throws:
IOException
-
doReadBytes
Reads length bytes into bytes starting at start.- Throws:
EOFException
- If there are not enough number of bytes in the source.IOException
-
doReadItemCount
Returns the number of items to follow in the current array or map. Returns 0 if there are no more items in the current array and the array/map has ended. Arrays are encoded as a series of blocks. Each block consists of a long count value, followed by that many array items. A block with count zero indicates the end of the array. If a block's count is negative, its absolute value is used, and the count is followed immediately by a long block size indicating the number of bytes in the block.- Throws:
IOException
- If the first byte cannot be read for any reason other than the end of the file, if the input stream has been closed, or if some other I/O error occurs.
-
readArrayStart
Description copied from class:Decoder
Reads and returns the size of the first block of an array. If this method returns non-zero, then the caller should read the indicated number of items, and then callDecoder.arrayNext()
to find out the number of items in the next block. The typical pattern for consuming an array looks like:for(long i = in.readArrayStart(); i != 0; i = in.arrayNext()) { for (long j = 0; j invalid input: '<' i; j++) { read next element of the array; } }
- Specified by:
readArrayStart
in classDecoder
- Throws:
IOException
-
arrayNext
Description copied from class:Decoder
Processes the next block of an array and returns the number of items in the block and let's the caller read those items.- Specified by:
arrayNext
in classDecoder
- Throws:
IOException
-
skipArray
Description copied from class:Decoder
Used for quickly skipping through an array. Note you can either skip the entire array, or read the entire array (withDecoder.readArrayStart()
), but you can't mix the two on the same array. This method will skip through as many items as it can, all of them if possible. It will return zero if there are no more items to skip through, or an item count if it needs the client's help in skipping. The typical usage pattern is:for(long i = in.skipArray(); i != 0; i = i.skipArray()) { for (long j = 0; j invalid input: '<' i; j++) { read and discard the next element of the array; } }
Note that this method can automatically skip through items if a byte-count is found in the underlying data, or if a schema has been provided to the implementation, but otherwise the client will have to skip through items itself.- Specified by:
skipArray
in classDecoder
- Throws:
IOException
-
readMapStart
Description copied from class:Decoder
Reads and returns the size of the next block of map-entries. Similar toDecoder.readArrayStart()
. As an example, let's say you want to read a map of records, the record consisting of an Long field and a Boolean field. Your code would look something like this:Mapinvalid input: '<'String, Record> m = new HashMapinvalid input: '<'String, Record>(); Record reuse = new Record(); for (long i = in.readMapStart(); i != 0; i = in.readMapNext()) { for (long j = 0; j invalid input: '<' i; j++) { String key = in.readString(); reuse.intField = in.readInt(); reuse.boolField = in.readBoolean(); m.put(key, reuse); } }
- Specified by:
readMapStart
in classDecoder
- Throws:
IOException
-
mapNext
Description copied from class:Decoder
Processes the next block of map entries and returns the count of them. Similar toDecoder.arrayNext()
. SeeDecoder.readMapStart()
for details.- Specified by:
mapNext
in classDecoder
- Throws:
IOException
-
skipMap
Description copied from class:Decoder
Support for quickly skipping through a map similar toDecoder.skipArray()
. As an example, let's say you want to skip a map of records, the record consisting of an Long field and a Boolean field. Your code would look something like this:for (long i = in.skipMap(); i != 0; i = in.skipMap()) { for (long j = 0; j invalid input: '<' i; j++) { in.skipString(); // Discard key in.readInt(); // Discard int-field of value in.readBoolean(); // Discard boolean-field of value } }
- Specified by:
skipMap
in classDecoder
- Throws:
IOException
-
readIndex
Description copied from class:Decoder
Reads the tag of a union written byEncoder.writeIndex(int)
.- Specified by:
readIndex
in classDecoder
- Throws:
IOException
-
isEnd
Returns true if the current BinaryDecoder is at the end of its source data and cannot read any further without throwing an EOFException or other IOException. Not all implementations of BinaryDecoder support isEnd(). Implementations that do not support isEnd() will throw aUnsupportedOperationException
.- Throws:
IOException
- If the first byte cannot be read for any reason other than the end of the file, if the input stream has been closed, or if some other I/O error occurs.
-
inputStream
Returns anInputStream
that is aware of any buffering that may occur in this BinaryDecoder. Readers that need to interleave decoding Avro data with other reads must access this InputStream to do so unless the implementation is 'direct' and does not read beyond the minimum bytes necessary from the source.
-