Package org.simpleframework.transport
Class TransportReader
- java.lang.Object
-
- org.simpleframework.transport.TransportReader
-
- All Implemented Interfaces:
ByteReader
class TransportReader extends java.lang.Object implements ByteReader
TheTransportReader
object represents a reader that can read and buffer data from an underlying transport. If the number of bytes read from the reader is more than required for the HTTP request then those bytes can be pushed back in to the cursor using thereset
method. This will only allow the last read to be reset within the cursor safely.- See Also:
Transport
-
-
Field Summary
Fields Modifier and Type Field Description private java.nio.ByteBuffer
buffer
This is used to store the bytes read from the transport.private boolean
closed
This is used to determine if the transport has been closed.private int
count
This represents the number of bytes that are ready to read.private Transport
transport
This is the underlying transport to read the bytes from.
-
Constructor Summary
Constructors Constructor Description TransportReader(Transport transport)
Constructor for theTransportReader
object.TransportReader(Transport transport, int size)
Constructor for theTransportReader
object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
This is used to close the underlying transport.boolean
isOpen()
Determines whether the source is still open.boolean
isReady()
Determines whether the source is ready for reading.private int
peek()
Provides the number of bytes that can be read from the stream without blocking.int
read(byte[] data)
Reads a block of bytes from the underlying stream.int
read(byte[] data, int off, int len)
Reads a block of bytes from the underlying stream.int
ready()
Provides the number of bytes that can be read from the stream without blocking.int
reset(int size)
Moves the source backward within the stream.
-
-
-
Field Detail
-
transport
private Transport transport
This is the underlying transport to read the bytes from.
-
buffer
private java.nio.ByteBuffer buffer
This is used to store the bytes read from the transport.
-
closed
private boolean closed
This is used to determine if the transport has been closed.
-
count
private int count
This represents the number of bytes that are ready to read.
-
-
Constructor Detail
-
TransportReader
public TransportReader(Transport transport)
Constructor for theTransportReader
object. This requires a transport to read the bytes from. By default this will create a buffer of two kilobytes to read the input in to which ensures several requests can be read at once.- Parameters:
transport
- this is the underlying transport to use
-
TransportReader
public TransportReader(Transport transport, int size)
Constructor for theTransportReader
object. This requires a transport to read the bytes from. By default this will create a buffer of of the specified size to read the input in to which enabled bytes to be buffered internally.- Parameters:
transport
- this is the underlying transport to usesize
- this is the size of the internal buffer to use
-
-
Method Detail
-
isOpen
public boolean isOpen() throws java.io.IOException
Determines whether the source is still open. The source is considered open if there are still bytes to read. If there is still bytes buffered and the underlying transport is closed then the source is still considered open.- Specified by:
isOpen
in interfaceByteReader
- Returns:
- true if there is nothing more to be read from this
- Throws:
java.io.IOException
-
isReady
public boolean isReady() throws java.io.IOException
Determines whether the source is ready for reading. When the source is ready then it guarantees that some amount of bytes can be read from the underlying stream without blocking.- Specified by:
isReady
in interfaceByteReader
- Returns:
- true if some data can be read without blocking
- Throws:
java.io.IOException
-
read
public int read(byte[] data) throws java.io.IOException
Reads a block of bytes from the underlying stream. This will read up to the requested number of bytes from the underlying stream. If there are no ready bytes on the stream this can return zero, representing the fact that nothing was read.- Specified by:
read
in interfaceByteReader
- Parameters:
data
- this is the array to read the bytes in to- Returns:
- this returns the number of bytes read from the stream
- Throws:
java.io.IOException
-
read
public int read(byte[] data, int off, int len) throws java.io.IOException
Reads a block of bytes from the underlying stream. This will read up to the requested number of bytes from the underlying stream. If there are no ready bytes on the stream this can return zero, representing the fact that nothing was read.- Specified by:
read
in interfaceByteReader
- Parameters:
data
- this is the array to read the bytes in tooff
- this is the offset to begin writing the bytes tolen
- this is the number of bytes that are requested- Returns:
- this returns the number of bytes read from the stream
- Throws:
java.io.IOException
-
ready
public int ready() throws java.io.IOException
Provides the number of bytes that can be read from the stream without blocking. This is typically the number of buffered or available bytes within the stream. When this reaches zero then the source may perform a blocking read.- Specified by:
ready
in interfaceByteReader
- Returns:
- the number of bytes that can be read without blocking
- Throws:
java.io.IOException
-
peek
private int peek() throws java.io.IOException
Provides the number of bytes that can be read from the stream without blocking. This is typically the number of buffered or available bytes within the stream. When this reaches zero then the source may perform a blocking read.- Returns:
- the number of bytes that can be read without blocking
- Throws:
java.io.IOException
-
reset
public int reset(int size) throws java.io.IOException
Moves the source backward within the stream. This ensures that any bytes read from the last read can be pushed back in to the stream so that they can be read again. This will throw an exception if the reset can not be performed.- Specified by:
reset
in interfaceByteReader
- Parameters:
size
- this is the number of bytes to reset back- Returns:
- this is the number of bytes that have been reset
- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException
This is used to close the underlying transport. This is used when the transport returns a negative value, indicating that the client has closed the connection on the other side. If this is invoked the read method returns -1 and the reader is no longer open, further bytes can no longer be read.- Throws:
java.io.IOException
-
-