Interface ObjectReader
-
public interface ObjectReader
Interface for object read.Common specification for object reader and object writer is specified in
ObjectWriter
. It is recommended to read object writer documentation first before you read this documentation.An object is readable if the object is a builtin object or its class has the following method.
public static UserClass readObject(ObjectReader r)
A simple object is read by corresponding read method. The following is an example of reading simple objects.
str = objectReader.readString(); i = objectReader.readInt();
A list is read by a
beginList()
call followed by calls for reading zero or more its elements followed by aend()
call.A map is read by a
beginMap()
call followed by calls for reading zero or more its elements followed by aend()
call.You can read a custom object indirectly if its class has the following method.
When you read a custom object of this class, thepublic static UserClass readObject(ObjectReader r)
readObject
method is called. In the method, you must read one equivalent builtin object. It is error to read no object or two or more objects. The method may read on list with multiple elements.If an exception is thrown during any read call, the reader becomes invalidated. An invalidated reader fails any method.
- See Also:
ObjectWriter
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
beginList()
Reads a list header and begins a list.void
beginMap()
Reads a map header and begins a map.boolean
beginNullableList()
Reads a nullable list header.boolean
beginNullableMap()
Reads a nullable map header.void
end()
Ends the current container.boolean
hasNext()
Returns true if this reader has next object to read.<T> T
read(java.lang.Class<T> c)
Reads an object of the classc
.Address
readAddress()
Reads an address.java.math.BigInteger
readBigInteger()
Reads a big integer.boolean
readBoolean()
Reads a boolean.byte
readByte()
Reads a byte.byte[]
readByteArray()
Reads a byte array.char
readChar()
Reads a character.double
readDouble()
Reads a double.float
readFloat()
Reads a float.int
readInt()
Reads an integer.long
readLong()
Reads a long.<T> T
readNullable(java.lang.Class<T> c)
Reads a nullable object.<T> T
readNullableOrDefault(java.lang.Class<T> c, T def)
Reads a nullable object or returns default object if there is no next object.<T> T
readOrDefault(java.lang.Class<T> c, T def)
Reads an object or returns default object if there is no next object.short
readShort()
Reads a short.java.lang.String
readString()
Reads a string.void
skip()
Skips an element of the current container.void
skip(int count)
Skips elements of the current container.
-
-
-
Method Detail
-
readBoolean
boolean readBoolean()
Reads a boolean.- Returns:
- the boolean read.
- Throws:
java.lang.IllegalStateException
- If this reader cannot read the given type of object because of end of stream, end of list, end of map, type mismatch, corrupted stream or invalidated reader.java.lang.UnsupportedOperationException
- If this reader cannot read an object because the object is too long (for example, 2^32 bytes or longer byte array).- See Also:
read(Class)
-
readByte
byte readByte()
Reads a byte.- Returns:
- the byte read.
- Throws:
java.lang.IllegalStateException
- If this reader cannot read the given type of object because of end of stream, end of list, end of map, type mismatch, corrupted stream or invalidated reader.java.lang.UnsupportedOperationException
- If this reader cannot read an object because the object is too long (for example, 2^32 bytes or longer byte array).- See Also:
read(Class)
-
readShort
short readShort()
Reads a short.- Returns:
- the short read.
- Throws:
java.lang.IllegalStateException
- If this reader cannot read the given type of object because of end of stream, end of list, end of map, type mismatch, corrupted stream or invalidated reader.java.lang.UnsupportedOperationException
- If this reader cannot read an object because the object is too long (for example, 2^32 bytes or longer byte array).- See Also:
read(Class)
-
readChar
char readChar()
Reads a character.- Returns:
- the char read.
- Throws:
java.lang.IllegalStateException
- If this reader cannot read the given type of object because of end of stream, end of list, end of map, type mismatch, corrupted stream or invalidated reader.java.lang.UnsupportedOperationException
- If this reader cannot read an object because the object is too long (for example, 2^32 bytes or longer byte array).- See Also:
read(Class)
-
readInt
int readInt()
Reads an integer.- Returns:
- the int read.
- Throws:
java.lang.IllegalStateException
- If this reader cannot read the given type of object because of end of stream, end of list, end of map, type mismatch, corrupted stream or invalidated reader.java.lang.UnsupportedOperationException
- If this reader cannot read an object because the object is too long (for example, 2^32 bytes or longer byte array).- See Also:
read(Class)
-
readFloat
float readFloat()
Reads a float.- Returns:
- the float read.
- Throws:
java.lang.IllegalStateException
- If this reader cannot read the given type of object because of end of stream, end of list, end of map, type mismatch, corrupted stream or invalidated reader.java.lang.UnsupportedOperationException
- If this reader cannot read an object because the object is too long (for example, 2^32 bytes or longer byte array).- See Also:
read(Class)
-
readLong
long readLong()
Reads a long.- Returns:
- the long read.
- Throws:
java.lang.IllegalStateException
- If this reader cannot read the given type of object because of end of stream, end of list, end of map, type mismatch, corrupted stream or invalidated reader.java.lang.UnsupportedOperationException
- If this reader cannot read an object because the object is too long (for example, 2^32 bytes or longer byte array).- See Also:
read(Class)
-
readDouble
double readDouble()
Reads a double.- Returns:
- the double read.
- Throws:
java.lang.IllegalStateException
- If this reader cannot read the given type of object because of end of stream, end of list, end of map, type mismatch, corrupted stream or invalidated reader.java.lang.UnsupportedOperationException
- If this reader cannot read an object because the object is too long (for example, 2^32 bytes or longer byte array).- See Also:
read(Class)
-
readBigInteger
java.math.BigInteger readBigInteger()
Reads a big integer.- Returns:
- the big integer read.
- Throws:
java.lang.IllegalStateException
- If this reader cannot read the given type of object because of end of stream, end of list, end of map, type mismatch, corrupted stream or invalidated reader.java.lang.UnsupportedOperationException
- If this reader cannot read an object because the object is too long (for example, 2^32 bytes or longer byte array).- See Also:
read(Class)
-
readString
java.lang.String readString()
Reads a string.- Returns:
- the string read.
- Throws:
java.lang.IllegalStateException
- If this reader cannot read the given type of object because of end of stream, end of list, end of map, type mismatch, corrupted stream or invalidated reader.java.lang.UnsupportedOperationException
- If this reader cannot read an object because the object is too long (for example, 2^32 bytes or longer byte array).- See Also:
read(Class)
-
readByteArray
byte[] readByteArray()
Reads a byte array.- Returns:
- the byte array read.
- Throws:
java.lang.IllegalStateException
- If this reader cannot read the given type of object because of end of stream, end of list, end of map, type mismatch, corrupted stream or invalidated reader.java.lang.UnsupportedOperationException
- If this reader cannot read an object because the object is too long (for example, 2^32 bytes or longer byte array).- See Also:
read(Class)
-
readAddress
Address readAddress()
Reads an address.- Returns:
- the address read.
- Throws:
java.lang.IllegalStateException
- If this reader cannot read the given type of object because of end of stream, end of list, end of map, type mismatch, corrupted stream or invalidated reader.java.lang.UnsupportedOperationException
- If this reader cannot read an object because the object is too long (for example, 2^32 bytes or longer byte array).- See Also:
read(Class)
-
read
<T> T read(java.lang.Class<T> c)
Reads an object of the classc
.- Type Parameters:
T
- type of object to be read- Parameters:
c
- Class of object to be read. It shall be one ofBoolean
,Byte
,Short
,Character
,Integer
,Float
,Long
,Double
,BigInteger
,String
, byte array,Address
, or a custom class with the following method:public static UserClass readObject(ObjectReader r)
- Returns:
- the object read.
- Throws:
java.lang.IllegalStateException
- If this reader cannot read the given type of object because of end of stream, end of list, end of map, type mismatch, corrupted stream or invalidated reader.java.lang.IllegalArgumentException
- If the object is not a simple object and correctreadObject
method is not available or the method threwThrowable
which is not anRuntimeException
.java.lang.UnsupportedOperationException
- If this reader cannot read an object because the object is too long (for example, 2^32 bytes or longer byte array).
-
readOrDefault
<T> T readOrDefault(java.lang.Class<T> c, T def)
Reads an object or returns default object if there is no next object.- Type Parameters:
T
- type of object to be read- Parameters:
c
- class of object to be read.def
- the default object.- Returns:
- the object read or default object.
- Throws:
java.lang.IllegalStateException
- If this reader cannot read the given type of object because of type mismatch, corrupted stream or invalidated reader.java.lang.IllegalArgumentException
- If the object is not a simple object and correctreadObject
method is not available or the method threwThrowable
which is not anRuntimeException
.java.lang.UnsupportedOperationException
- If this reader cannot read an object because the object is too long (for example, 2^32 bytes or longer byte array).- See Also:
read(Class)
,hasNext()
-
readNullable
<T> T readNullable(java.lang.Class<T> c)
Reads a nullable object.- Type Parameters:
T
- type of object to be read- Parameters:
c
- class of object to be read.- Returns:
- read object or null.
- Throws:
java.lang.IllegalStateException
- If this reader cannot read the given type of object because of end of stream, end of list, end of map, type mismatch, corrupted stream or invalidated reader.java.lang.IllegalArgumentException
- If the object is not a simple object and correctreadObject
method is not available or the method threwThrowable
which is not anRuntimeException
.java.lang.UnsupportedOperationException
- If this reader cannot read an object because the object is too long (for example, 2^32 bytes or longer byte array).- See Also:
read(Class)
-
readNullableOrDefault
<T> T readNullableOrDefault(java.lang.Class<T> c, T def)
Reads a nullable object or returns default object if there is no next object.- Type Parameters:
T
- type of object to be read- Parameters:
c
- class of object to be read.def
- the default object.- Returns:
- read object or null if an object or null is read. Default object if there is no more item in current list or map.
- Throws:
java.lang.IllegalStateException
- If this reader cannot read the given type of object because of type mismatch, corrupted stream or invalidated reader.java.lang.IllegalArgumentException
- If the object is not a simple object and correctreadObject
method is not available or the method threwThrowable
which is not anRuntimeException
.java.lang.UnsupportedOperationException
- If this reader cannot read an object because the object is too long (for example, 2^32 bytes or longer byte array).- See Also:
read(Class)
,hasNext()
-
beginList
void beginList()
Reads a list header and begins a list.If a list was successfully begun, a read operation reads an element of the list in writing order. After reading zero or more elements, the caller must call
end()
to end list. It is not required to read all elements before anend()
call.- Throws:
java.lang.IllegalStateException
- If this reader cannot read the given type of object because of end of stream, end of list, end of map, type mismatch, corrupted stream or invalidated reader.java.lang.UnsupportedOperationException
- If this reader cannot read an object because the object is too long (for example, 2^32 bytes or longer byte array).
-
beginNullableList
boolean beginNullableList()
Reads a nullable list header. If the reader reads a list header, a list is begun andtrue
is returned. If the reader reads null, no list is begun andfalse
is returned.If a list was successfully begun, a read operation reads an element of the list in writing order. After reading zero or more elements, the caller must call
end()
to end list. It is not required to read all elements before anend()
call.- Returns:
- true if a list header is read or false if null is read
- Throws:
java.lang.IllegalStateException
- If this reader cannot read the given type of object because of end of stream, end of list, end of map, type mismatch, corrupted stream or invalidated reader.java.lang.UnsupportedOperationException
- If this reader cannot read an object because the object is too long (for example, 2^32 bytes or longer byte array).- See Also:
beginList()
-
beginMap
void beginMap()
Reads a map header and begins a map.If a map was successfully begun, elements of map can be read. A map element consist of a key and its value and can be read by two separate reads and elements are read in writing order. For example, The first read operation after beginning of a map reads the key of the first map element and the next read operation reads it value and the next read operation reads the key of the second element, and so on. After reading keys and values, the caller must call
end()
to end map. It is not required to read all elements before anend()
call.- Throws:
java.lang.IllegalStateException
- If this reader cannot read the given type of object because of end of stream, end of list, end of map, type mismatch, corrupted stream or invalidated reader.java.lang.UnsupportedOperationException
- If this reader cannot read an object because the object is too long (for example, 2^32 bytes or longer byte array).
-
beginNullableMap
boolean beginNullableMap()
Reads a nullable map header. If the reader reads a map header, a map is begun andtrue
is returned. If the reader reads null, no map is begun andfalse
is returned.If a map was successfully begun, elements of map can be read. A map element consist of a key and its value and can be read by two separate reads and elements are read in writing order. For example, The first read operation after beginning of a map reads the key of the first map element and the next read operation reads it value and the next read operation reads the key of the second element, and so on. After reading keys and values, the caller must call
end()
to end map. It is not required to read all elements before anend()
call.- Returns:
- true if a map header is read or false if null is read
- Throws:
java.lang.IllegalStateException
- If this reader cannot read the given type of object because of end of stream, end of list, end of map, type mismatch, corrupted stream or invalidated reader.java.lang.UnsupportedOperationException
- If this reader cannot read an object because the object is too long (for example, 2^32 bytes or longer byte array).- See Also:
beginMap()
-
hasNext
boolean hasNext()
Returns true if this reader has next object to read. If the reader is reading a container, this method returns true if the top most container has more object to read, returns false otherwise.- Returns:
- true if this reader has next object to read.
- Throws:
java.lang.IllegalStateException
- if this reader was already invalidated.
-
end
void end()
Ends the current container. Unread elements of the current container are all skipped.- Throws:
java.lang.IllegalStateException
- if this end is imbalanced.
-
skip
void skip()
Skips an element of the current container.- Throws:
java.lang.IllegalStateException
- If this reader cannot read the given type of object because of end of stream, end of list, end of map, type mismatch, corrupted stream or invalidated reader.java.lang.UnsupportedOperationException
- If this reader cannot read an object because the object is too long (for example, 2^32 bytes or longer byte array).
-
skip
void skip(int count)
Skips elements of the current container.- Parameters:
count
- the count.- Throws:
java.lang.IllegalStateException
- If this reader cannot read the given type of object because of end of stream, end of list, end of map, type mismatch, corrupted stream or invalidated reader.java.lang.UnsupportedOperationException
- If this reader cannot read an object because the object is too long (for example, 2^32 bytes or longer byte array).
-
-