Package org.simpleframework.transport
Class TransportCursor
- java.lang.Object
-
- org.simpleframework.transport.TransportCursor
-
- All Implemented Interfaces:
ByteCursor
public class TransportCursor extends java.lang.Object implements ByteCursor
TheTransportCursorobject represents a cursor that can read and buffer data from an underlying transport. If the number of bytes read from the cursor is more than required for the HTTP request then those bytes can be pushed back in to the cursor using theresetmethod. 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 byte[]bufferThis is the buffer used to collect the bytes pushed back.private intcountThis is the number of bytes that have been pushed back.private intlimitThis is the maximum number of bytes that can be pushed back.private intmarkThis is the mark from the last read from this cursor object.private intposThis is the position to read data from the internal buffer.private ByteReaderreaderThis is the stream for the bytes read by this cursor object.
-
Constructor Summary
Constructors Constructor Description TransportCursor(Transport transport)Constructor for theTransportCursorobject.TransportCursor(Transport transport, int size)Constructor for theTransportCursorobject.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidexpand(int capacity)This is used to ensure that there is enough space in the buffer to allow for more bytes to be added.booleanisOpen()Determines whether the cursor is still open.booleanisReady()Determines whether the cursor is ready for reading.voidpush(byte[] data)Pushes the provided data on to the cursor.voidpush(byte[] data, int off, int len)Pushes the provided data on to the cursor.intread(byte[] data)Reads a block of bytes from the underlying stream.intread(byte[] data, int off, int len)Reads a block of bytes from the underlying stream.intready()Provides the number of bytes that can be read from the stream without blocking.intreset(int size)Moves the cursor backward within the stream.
-
-
-
Field Detail
-
reader
private ByteReader reader
This is the stream for the bytes read by this cursor object.
-
buffer
private byte[] buffer
This is the buffer used to collect the bytes pushed back.
-
count
private int count
This is the number of bytes that have been pushed back.
-
mark
private int mark
This is the mark from the last read from this cursor object.
-
pos
private int pos
This is the position to read data from the internal buffer.
-
limit
private int limit
This is the maximum number of bytes that can be pushed back.
-
-
Constructor Detail
-
TransportCursor
public TransportCursor(Transport transport)
Constructor for theTransportCursorobject. 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 use
-
TransportCursor
public TransportCursor(Transport transport, int size)
Constructor for theTransportCursorobject. 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.IOExceptionDetermines whether the cursor is still open. The cursor is considered open if there are still bytes to read. If there is still bytes buffered and the underlying transport is closed then the cursor is still considered open.- Specified by:
isOpenin interfaceByteCursor- Returns:
- true if there is nothing more to be read from this
- Throws:
java.io.IOException
-
isReady
public boolean isReady() throws java.io.IOExceptionDetermines whether the cursor is ready for reading. When the cursor is ready then it guarantees that some amount of bytes can be read from the underlying stream without blocking.- Specified by:
isReadyin interfaceByteCursor- Returns:
- true if some data can be read without blocking
- Throws:
java.io.IOException
-
ready
public int ready() throws java.io.IOExceptionProvides 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 cursor may perform a blocking read.- Specified by:
readyin interfaceByteCursor- Returns:
- the number of bytes that can be read without blocking
- Throws:
java.io.IOException
-
read
public int read(byte[] data) throws java.io.IOExceptionReads 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:
readin interfaceByteCursor- 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.IOExceptionReads 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:
readin interfaceByteCursor- 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
-
push
public void push(byte[] data) throws java.io.IOExceptionPushes the provided data on to the cursor. Data pushed on to the cursor will be the next data read from the cursor. This complements theresetmethod which will reset the cursors position on a stream. Allowing data to be pushed on to the cursor allows more flexibility.- Specified by:
pushin interfaceByteCursor- Parameters:
data- this is the data to be pushed on to the cursor- Throws:
java.io.IOException
-
push
public void push(byte[] data, int off, int len) throws java.io.IOExceptionPushes the provided data on to the cursor. Data pushed on to the cursor will be the next data read from the cursor. This complements theresetmethod which will reset the cursors position on a stream. Allowing data to be pushed on to the cursor allows more flexibility.- Specified by:
pushin interfaceByteCursor- Parameters:
data- this is the data to be pushed on to the cursoroff- this is the offset to begin reading the byteslen- this is the number of bytes that are to be used- Throws:
java.io.IOException
-
expand
private void expand(int capacity) throws java.io.IOExceptionThis is used to ensure that there is enough space in the buffer to allow for more bytes to be added. If the buffer is already larger than the required capacity the this will do nothing.- Parameters:
capacity- the minimum size needed for the buffer- Throws:
java.io.IOException
-
reset
public int reset(int size) throws java.io.IOExceptionMoves the cursor 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:
resetin interfaceByteCursor- 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
-
-