org.opcfoundation.ua.utils.asyncsocket
Class AsyncInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by org.opcfoundation.ua.utils.asyncsocket.AsyncInputStream
All Implemented Interfaces:
Closeable

public abstract class AsyncInputStream
extends InputStream

Asyncronous input stream of asynchronous socket. There are two positions properties: Bytes received (to buffer) and bytes read.

Synchronous reads can be made with use of alarm, e.g. BufferMonitor a = is.createBufferMonitor(is.getPosition() + 1000, null); a.waitForState(AlarmState.FINAL_STATES); byte[] data = new byte[1000]; is.read(data);

Note there is a limit (40kb) how much data is buffered. To up the limit set an alarm. Creating an alarm to position Long.MAX_VALUE removes any limits of the buffer.

Author:
Toni Kalajainen (toni.kalajainen@vtt.fi)
See Also:
AsyncSocketImpl, BufferMonitor

Constructor Summary
AsyncInputStream()
           
 
Method Summary
abstract  int available()
          Return the number of bytes available in the buffer.
abstract  BufferMonitor createMonitor(long position, MonitorListener listener)
          Create an object that monitors the position input stream.
abstract  int getBufferSize()
          Get the number of bytes the stream buffers from TCP stack.
abstract  long getPosition()
          Get stream position.
abstract  long getReceivedBytes()
          Get the number of bytes received to this stream
abstract  void peek(byte[] buf)
          Peek ahead into buffered content.
abstract  void peek(byte[] buf, int off, int len)
          Peek bytes to buffer
abstract  ByteBuffer peek(int len)
          Peek bytes to returned buffer
abstract  ByteBuffer[] peekChunks(int len)
          Peek bytes to byte chunks.
abstract  int read(byte[] b)
          Reads some number of bytes from the input stream and stores them into the buffer array b.
abstract  int read(byte[] b, int off, int len)
           Reads up to len bytes of data from the input stream into an array of bytes.
abstract  void read(ByteBuffer dst)
          Read to byte buffer
abstract  void read(ByteBuffer dst, int length)
          Read the given number of bytes to byte buffer.
abstract  ByteBuffer read(int len)
          Read from the stream.
abstract  ByteBuffer[] readChunks(int len)
          Read from the input stream.
abstract  void setBufferSize(int size)
          Set a hint number for buffering.
 
Methods inherited from class java.io.InputStream
close, mark, markSupported, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AsyncInputStream

public AsyncInputStream()
Method Detail

read

public abstract int read(byte[] b)
Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown.

Overrides:
read in class InputStream
Parameters:
b - the buffer into which the data is read.
Returns:
the total number of bytes read into the buffer, or -1 is there is no more data because the end of the stream has been reached.
See Also:
InputStream.read(byte[], int, int)

read

public abstract int read(byte[] b,
                         int off,
                         int len)
Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer.

Overrides:
read in class InputStream
See Also:
InputStream.read()

getReceivedBytes

public abstract long getReceivedBytes()
Get the number of bytes received to this stream

Returns:
bytes received

getPosition

public abstract long getPosition()
Get stream position.

Returns:
number of bytes read

createMonitor

public abstract BufferMonitor createMonitor(long position,
                                            MonitorListener listener)
Create an object that monitors the position input stream. Alarm is triggered when buffer reaches the requested position.

The buffer size is enlarged when a monitor with a position outside the buffer range is created. For instance, there is an input stream. Its position is 5, buffer size is 5. It received data to 10 and stops. If monitor at position 20 is created, the stream received 10 more additional bytes.

Parameters:
position - position at which to trigger an alarm
listener - alarm listener
Returns:
alarm

getBufferSize

public abstract int getBufferSize()
Get the number of bytes the stream buffers from TCP stack.

Returns:
buffer size

setBufferSize

public abstract void setBufferSize(int size)
Set a hint number for buffering. Buffer reads ahead values from TCP Stack. The given number is minimum for buffering.

Parameters:
size - new buffer size value

read

public abstract void read(ByteBuffer dst)
Read to byte buffer

Parameters:
dst - a ByteBuffer object.

read

public abstract void read(ByteBuffer dst,
                          int length)
Read the given number of bytes to byte buffer.

Parameters:
dst - a ByteBuffer object.
length - a int.

read

public abstract ByteBuffer read(int len)
Read from the stream. Returns a readable byte buffer.

Parameters:
len - a int.
Returns:
byte buffer

readChunks

public abstract ByteBuffer[] readChunks(int len)
Read from the input stream. Returns readable byte buffers. This is the most efficient read method as it does not copy byte data.

Parameters:
len - a int.
Returns:
byte buffer

peek

public abstract void peek(byte[] buf)
Peek ahead into buffered content. Use available() to see how many bytes there is buffered. Use setBufferSize() to control how much to buffer.

Parameters:
buf - an array of byte.

peek

public abstract void peek(byte[] buf,
                          int off,
                          int len)
Peek bytes to buffer

Parameters:
buf - an array of byte.
off - a int.
len - a int.

peek

public abstract ByteBuffer peek(int len)
Peek bytes to returned buffer

Parameters:
len - a int.
Returns:
byte buffer

peekChunks

public abstract ByteBuffer[] peekChunks(int len)
Peek bytes to byte chunks. (Efficient for big chunks as there is no byte copying)

Parameters:
len - a int.
Returns:
byte buffer array

available

public abstract int available()
Return the number of bytes available in the buffer.

Overrides:
available in class InputStream
Returns:
a int.


Copyright © 2018. All rights reserved.