public class BytesReader extends java.lang.Object implements ProtocolResponseReader
An implementation of ProtocolResponseReader which can read data encoded in pCloud's binary protocol from a stream of bytes in pull-based fashion with minimal overhead.
The implementation can be reused for reading multiple responses from a byte source, as long as they are read completely.
ProtocolReader
,
ProtocolResponseReader
SCOPE_DATA, SCOPE_RESPONSE, UNKNOWN_SIZE
SCOPE_ARRAY, SCOPE_NONE, SCOPE_OBJECT
Constructor and Description |
---|
BytesReader(okio.BufferedSource bufferedSource)
Create a BytesReader instance
|
Modifier and Type | Method and Description |
---|---|
void |
beginArray()
Begin reading an array from the source
|
void |
beginObject()
Begin reading an object from the source
|
long |
beginResponse()
Begin reading a response from the data source
|
void |
close()
Close the BufferedSource
|
int |
currentScope()
Get the current scope of the reader
|
long |
dataContentLength()
Get the length of the data that follows the response
|
void |
endArray()
End reading an array from the source
|
void |
endObject()
End reading an object from the source
|
boolean |
endResponse()
End reading a response from the data source
|
boolean |
hasNext()
Check if the source has more data to read
|
ProtocolResponseReader |
newPeekingReader()
Get a new instance peeking instance of this reader.
|
TypeToken |
peek()
Peek the data and check what is the type of the data that follows
|
boolean |
readBoolean()
Read a boolean value from the source
|
void |
readData(okio.BufferedSink sink)
Read the data after a response.
|
void |
readData(java.io.OutputStream outputStream)
Read the data after a response.
|
long |
readNumber()
Read a number from the source
|
java.lang.String |
readString()
Read a String from the source
|
void |
skipValue()
Consume and discard the next value
|
public BytesReader(okio.BufferedSource bufferedSource)
bufferedSource
- a BufferedSource to read the data fromjava.lang.IllegalArgumentException
- on a null BufferedSource argumentpublic TypeToken peek() throws java.io.IOException
ProtocolReader
Return values can be TypeToken.BEGIN_OBJECT, TypeToken.BEGIN_ARRAY, TypeToken.END_OBJECT, TypeToken.END_ARRAY, TypeToken.STRING, TypeToken.BOOLEAN, TypeToken.NUMBER
peek
in interface ProtocolReader
java.io.IOException
- if the source is exhausted before the next bytes can be readTypeToken
public long beginResponse() throws java.io.IOException
ProtocolResponseReader
beginResponse
in interface ProtocolResponseReader
java.io.IOException
- if the source has been exhausted prior to this operationpublic boolean endResponse() throws java.io.IOException
ProtocolResponseReader
endResponse
in interface ProtocolResponseReader
java.io.IOException
- if the source has been exhausted prior to this operationpublic void beginObject() throws java.io.IOException
ProtocolReader
beginObject
in interface ProtocolReader
java.io.IOException
- if the source has been exhaustedpublic void beginArray() throws java.io.IOException
ProtocolReader
beginArray
in interface ProtocolReader
java.io.IOException
- if the source has been exhaustedpublic void endArray() throws java.io.IOException
ProtocolReader
endArray
in interface ProtocolReader
java.io.IOException
- if the source has been exhaustedpublic void endObject() throws java.io.IOException
ProtocolReader
endObject
in interface ProtocolReader
java.io.IOException
- if the source has been exhaustedpublic boolean readBoolean() throws java.io.IOException
ProtocolReader
readBoolean
in interface ProtocolReader
java.io.IOException
- if the source has been exhaustedpublic java.lang.String readString() throws java.io.IOException
ProtocolReader
readString
in interface ProtocolReader
java.io.IOException
- if the source is exhaustedpublic long readNumber() throws java.io.IOException
ProtocolReader
readNumber
in interface ProtocolReader
java.io.IOException
- if the source is exhaustedpublic boolean hasNext() throws java.io.IOException
ProtocolReader
hasNext
in interface ProtocolReader
java.io.IOException
- if the source has been exhaustedpublic long dataContentLength()
ProtocolResponseReader
dataContentLength
in interface ProtocolResponseReader
public void readData(java.io.OutputStream outputStream) throws java.io.IOException
ProtocolResponseReader
Use this method to red the data after reading a data-enriched response. Calling this method when the scope returned by ProtocolReader.currentScope() is not ProtocolResponseReader.SCOPE_DATA will result in a IllegalStateException error.
readData
in interface ProtocolResponseReader
outputStream
- the non-null recipient of datajava.io.IOException
- or read or write errors.public void readData(okio.BufferedSink sink) throws java.io.IOException
ProtocolResponseReader
Use this method to red the data after reading a data-enriched response. Calling this method when the scope returned by ProtocolReader.currentScope() is not ProtocolResponseReader.SCOPE_DATA will result in a IllegalStateException error.
readData
in interface ProtocolResponseReader
sink
- the non-null recipient of datajava.io.IOException
- or read or write errors.public ProtocolResponseReader newPeekingReader()
ProtocolResponseReader
The peeking reader can read data without consuming it. It inherits all the fields of the current instance such as the data source and the scope. Any operations with the peeking reader will not exhaust the data source.
newPeekingReader
in interface ProtocolReader
newPeekingReader
in interface ProtocolResponseReader
public void skipValue() throws java.io.IOException
ProtocolReader
skipValue
in interface ProtocolReader
java.io.IOException
- if the source has been exhaustedpublic int currentScope()
ProtocolReader
Return values can be ProtocolReader.SCOPE_NONE, ProtocolReader.SCOPE_OBJECT, ProtocolReader.SCOPE_ARRAY, ProtocolResponseReader.SCOPE_RESPONSE
currentScope
in interface ProtocolReader
public void close()
close
in interface ProtocolReader
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable