public class BinaryDecoder extends Decoder
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'.
To prevent this class from making large allocations when handling potentially
pathological input data, set Java properties
org.apache.avro.limits.string.maxLength and
org.apache.avro.limits.bytes.maxLength before instantiating this
class to limit the maximum sizes of string and bytes types
handled. The default is to permit sizes up to Java's maximum array length.Encoder
Modifier | Constructor and Description |
---|---|
protected |
BinaryDecoder()
protected constructor for child classes
|
Modifier and Type | Method and Description |
---|---|
long |
arrayNext()
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 |
doReadItemCount()
Returns the number of items to follow in the current array or map.
|
protected void |
doSkipBytes(long length) |
InputStream |
inputStream()
Returns an
InputStream 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 |
readArrayStart()
Reads and returns the size of the first block of an array.
|
boolean |
readBoolean()
Reads a boolean value written by
Encoder.writeBoolean(boolean) . |
ByteBuffer |
readBytes(ByteBuffer old)
Reads a byte-string written by
Encoder.writeBytes(java.nio.ByteBuffer) . |
double |
readDouble()
Reads a double written by
Encoder.writeDouble(double) . |
int |
readEnum()
Reads an enumeration.
|
void |
readFixed(byte[] bytes,
int start,
int length)
Reads fixed sized binary object.
|
float |
readFloat()
Reads a float written by
Encoder.writeFloat(float) . |
int |
readIndex()
Reads the tag of a union written by
Encoder.writeIndex(int) . |
int |
readInt()
Reads an integer written by
Encoder.writeInt(int) . |
long |
readLong()
Reads a long written by
Encoder.writeLong(long) . |
long |
readMapStart()
Reads and returns the size of the next block of map-entries.
|
void |
readNull()
"Reads" a null value.
|
String |
readString()
Reads a char-string written by
Encoder.writeString(org.apache.avro.util.Utf8) . |
Utf8 |
readString(Utf8 old)
Reads a char-string written by
Encoder.writeString(org.apache.avro.util.Utf8) . |
long |
skipArray()
Used for quickly skipping through an array.
|
void |
skipBytes()
Discards a byte-string written by
Encoder.writeBytes(java.nio.ByteBuffer) . |
void |
skipFixed(int length)
Discards fixed sized binary object.
|
long |
skipMap()
Support for quickly skipping through a map similar to
Decoder.skipArray() . |
void |
skipString()
Discards a char-string written by
Encoder.writeString(org.apache.avro.util.Utf8) . |
public void readNull() throws IOException
Decoder
readNull
in class Decoder
IOException
public boolean readBoolean() throws IOException
Decoder
Encoder.writeBoolean(boolean)
.readBoolean
in class Decoder
IOException
public int readInt() throws IOException
Decoder
Encoder.writeInt(int)
.readInt
in class Decoder
IOException
public long readLong() throws IOException
Decoder
Encoder.writeLong(long)
.readLong
in class Decoder
IOException
public float readFloat() throws IOException
Decoder
Encoder.writeFloat(float)
.readFloat
in class Decoder
IOException
public double readDouble() throws IOException
Decoder
Encoder.writeDouble(double)
.readDouble
in class Decoder
IOException
public Utf8 readString(Utf8 old) throws IOException
Decoder
Encoder.writeString(org.apache.avro.util.Utf8)
.readString
in class Decoder
IOException
public String readString() throws IOException
Decoder
Encoder.writeString(org.apache.avro.util.Utf8)
.readString
in class Decoder
IOException
public void skipString() throws IOException
Decoder
Encoder.writeString(org.apache.avro.util.Utf8)
.skipString
in class Decoder
IOException
public ByteBuffer readBytes(ByteBuffer old) throws IOException
Decoder
Encoder.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.readBytes
in class Decoder
IOException
public void skipBytes() throws IOException
Decoder
Encoder.writeBytes(java.nio.ByteBuffer)
.skipBytes
in class Decoder
IOException
public void readFixed(byte[] bytes, int start, int length) throws IOException
Decoder
readFixed
in class Decoder
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.IOException
public void skipFixed(int length) throws IOException
Decoder
skipFixed
in class Decoder
length
- The size of the binary object to be skipped.IOException
public int readEnum() throws IOException
Decoder
readEnum
in class Decoder
IOException
protected void doSkipBytes(long length) throws IOException
IOException
protected void doReadBytes(byte[] bytes, int start, int length) throws IOException
EOFException
- If there are not enough number of bytes in the source.IOException
protected long doReadItemCount() throws IOException
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.public long readArrayStart() throws IOException
Decoder
Decoder.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 < i; j++) { read next element of the array; } }
readArrayStart
in class Decoder
IOException
public long arrayNext() throws IOException
Decoder
arrayNext
in class Decoder
IOException
public long skipArray() throws IOException
Decoder
Decoder.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 < 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.
skipArray
in class Decoder
IOException
public long readMapStart() throws IOException
Decoder
Decoder.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:
Mapm = new HashMap (); Record reuse = new Record(); for (long i = in.readMapStart(); i != 0; i = in.readMapNext()) { for (long j = 0; j < i; j++) { String key = in.readString(); reuse.intField = in.readInt(); reuse.boolField = in.readBoolean(); m.put(key, reuse); } }
readMapStart
in class Decoder
IOException
public long mapNext() throws IOException
Decoder
Decoder.arrayNext()
. See Decoder.readMapStart()
for details.mapNext
in class Decoder
IOException
public long skipMap() throws IOException
Decoder
Decoder.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 < i; j++) { in.skipString(); // Discard key in.readInt(); // Discard int-field of value in.readBoolean(); // Discard boolean-field of value } }
skipMap
in class Decoder
IOException
public int readIndex() throws IOException
Decoder
Encoder.writeIndex(int)
.readIndex
in class Decoder
IOException
public boolean isEnd() throws IOException
UnsupportedOperationException
.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.public InputStream inputStream()
InputStream
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.Copyright © 2009–2022 The Apache Software Foundation. All rights reserved.