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,
ProtocolResponseReaderSCOPE_DATA, SCOPE_RESPONSE, UNKNOWN_SIZESCOPE_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
ProtocolReaderReturn values can be TypeToken.BEGIN_OBJECT, TypeToken.BEGIN_ARRAY, TypeToken.END_OBJECT, TypeToken.END_ARRAY, TypeToken.STRING, TypeToken.BOOLEAN, TypeToken.NUMBER
peek in interface ProtocolReaderjava.io.IOException - if the source is exhausted before the next bytes can be readTypeTokenpublic long beginResponse()
throws java.io.IOException
ProtocolResponseReaderbeginResponse in interface ProtocolResponseReaderjava.io.IOException - if the source has been exhausted prior to this operationpublic boolean endResponse()
throws java.io.IOException
ProtocolResponseReaderendResponse in interface ProtocolResponseReaderjava.io.IOException - if the source has been exhausted prior to this operationpublic void beginObject()
throws java.io.IOException
ProtocolReaderbeginObject in interface ProtocolReaderjava.io.IOException - if the source has been exhaustedpublic void beginArray()
throws java.io.IOException
ProtocolReaderbeginArray in interface ProtocolReaderjava.io.IOException - if the source has been exhaustedpublic void endArray()
throws java.io.IOException
ProtocolReaderendArray in interface ProtocolReaderjava.io.IOException - if the source has been exhaustedpublic void endObject()
throws java.io.IOException
ProtocolReaderendObject in interface ProtocolReaderjava.io.IOException - if the source has been exhaustedpublic boolean readBoolean()
throws java.io.IOException
ProtocolReaderreadBoolean in interface ProtocolReaderjava.io.IOException - if the source has been exhaustedpublic java.lang.String readString()
throws java.io.IOException
ProtocolReaderreadString in interface ProtocolReaderjava.io.IOException - if the source is exhaustedpublic long readNumber()
throws java.io.IOException
ProtocolReaderreadNumber in interface ProtocolReaderjava.io.IOException - if the source is exhaustedpublic boolean hasNext()
throws java.io.IOException
ProtocolReaderhasNext in interface ProtocolReaderjava.io.IOException - if the source has been exhaustedpublic long dataContentLength()
ProtocolResponseReaderdataContentLength in interface ProtocolResponseReaderpublic void readData(java.io.OutputStream outputStream)
throws java.io.IOException
ProtocolResponseReaderUse 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 ProtocolResponseReaderoutputStream - the non-null recipient of datajava.io.IOException - or read or write errors.public void readData(okio.BufferedSink sink)
throws java.io.IOException
ProtocolResponseReaderUse 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 ProtocolResponseReadersink - the non-null recipient of datajava.io.IOException - or read or write errors.public ProtocolResponseReader newPeekingReader()
ProtocolResponseReaderThe 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 ProtocolReadernewPeekingReader in interface ProtocolResponseReaderpublic void skipValue()
throws java.io.IOException
ProtocolReaderskipValue in interface ProtocolReaderjava.io.IOException - if the source has been exhaustedpublic int currentScope()
ProtocolReaderReturn values can be ProtocolReader.SCOPE_NONE, ProtocolReader.SCOPE_OBJECT, ProtocolReader.SCOPE_ARRAY, ProtocolResponseReader.SCOPE_RESPONSE
currentScope in interface ProtocolReaderpublic void close()
close in interface ProtocolReaderclose in interface java.io.Closeableclose in interface java.lang.AutoCloseable