org.opcfoundation.ua.utils.bytebuffer
Class ByteQueue

java.lang.Object
  extended by org.opcfoundation.ua.utils.bytebuffer.ByteQueue

public class ByteQueue
extends Object

Byte Queue is a LIFO queue of bytes. It uses ByteBuffers as back-end for data. There are two incremental memory pointers, read and write position. Write position returns the number of bytes written to the queue, and read bytes read. ByteQueue allocates new memory as required in chunks.

Author:
Toni Kalajainen (toni.kalajainen@vtt.fi)

Constructor Summary
ByteQueue()
          Construct new byte queue usign default allocation size.
ByteQueue(int chunkSize)
          Construct new byte queue.
 
Method Summary
 void clear()
          Clears bytes and resets all pointers.
 void get(byte[] buf)
          Read from buf
 void get(byte[] buf, int off, int len)
          Read from buf
 void get(ByteBuffer dst)
          Reads to dst remaining bytes
 void get(ByteBuffer dst, int length)
          Reads to dst
 ByteBuffer get(int len)
          Read bytes.
 void getAvailable(ByteBuffer dst)
          Reads as much as there is available and dst has remaining allocation
 ByteBufferFactory getByteBufferFactory()
          Get byte buffer factory
 long getBytesRead()
          Get the total number of bytes read from the queue.
 long getBytesWritten()
          Get the total number of bytes written to the queue.
 ByteBuffer[] getChunks(int len)
          Reads bytes.
 int getChunkSize()
          Get the block size of back-end chunks
 ByteBuffer getReadChunk()
          Expose the a byte buffer of backend.
 int getWriteableBytesRemaining()
          Get the number of bytes remaining for write
 ByteBuffer getWriteChunk()
          Exposes internal bytebuffer of this object.
 long getWriteLimit()
          Get writable bytes limit
 boolean hasRemaining()
          hasRemaining.
 boolean isEmpty()
          Is buffer empty
 void offer(ByteBuffer buf)
          Offers a byte buffer object for the queue.
 void omitAll()
          Clears queue and moves read pointer to written pointer.
 ByteOrder order()
          Get byte order
 void order(ByteOrder order)
          Set byte order
 void peek(byte[] buf)
          Peek data, read without moving read pointer.
 void peek(byte[] buf, int off, int len)
          Peek data, read without moving read pointer
 void peek(ByteBuffer dst)
          Peeks to dst.
 ByteBuffer peek(int len)
          Peek, read without moving read pointer.
 void peekAvailable(ByteBuffer dst)
          Peek available data.
 ByteBuffer[] peekChunks(int len)
          Peeks given number of bytes.
 void put(byte value)
          put.
 void put(byte[] buf)
          Write bytes
 void put(byte[] buf, int off, int len)
          Write bytes
 void put(ByteBuffer src)
          Write remaining bytes of src
 void put(ByteBuffer src, int length)
          Write remaining bytes of src
 long remaining()
          Get the number of readable bytes.
 void setByteBufferFactory(ByteBufferFactory factory)
          Set byte buffer constructor.
 void setChunkSize(int chunkSize)
          Set the block size of back-end chunks
 void setWriteLimit(long writeLimit)
          Set writable bytes limit.
 void skip(int bytes)
          Omit data by moving read pointer forward.
 String toString()
          
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ByteQueue

public ByteQueue()
Construct new byte queue usign default allocation size.


ByteQueue

public ByteQueue(int chunkSize)
Construct new byte queue.

Parameters:
chunkSize - allocation size for new memory chunks
Method Detail

put

public void put(byte[] buf)
         throws BufferOverflowException
Write bytes

Parameters:
buf - bytes to write
Throws:
BufferOverflowException - write limit exeeded

put

public void put(byte[] buf,
                int off,
                int len)
         throws BufferOverflowException
Write bytes

Parameters:
buf - bytes
off - offset
len - length
Throws:
BufferOverflowException - if any.

put

public void put(byte value)

put.

Parameters:
value - a byte.

put

public void put(ByteBuffer src)
Write remaining bytes of src

Parameters:
src - a ByteBuffer object.
Throws:
BufferOverflowException - write limit exeeded

put

public void put(ByteBuffer src,
                int length)
Write remaining bytes of src

Parameters:
src - a ByteBuffer object.
length - a int.
Throws:
BufferOverflowException - write limit exeeded

offer

public void offer(ByteBuffer buf)
           throws BufferOverflowException
Offers a byte buffer object for the queue. The remaining bytes of buf are added to the queue. ByteQueue takes the ownership of buf.

Parameters:
buf - buffer to write
Throws:
BufferOverflowException - write limit exeeded

get

public void get(byte[] buf)
         throws BufferUnderflowException
Read from buf

Parameters:
buf - buf to be filled
Throws:
BufferUnderflowException - if any.

get

public void get(byte[] buf,
                int off,
                int len)
Read from buf

Parameters:
buf - an array of byte.
off - a int.
len - a int.
Throws:
BufferUnderflowException - if any.

get

public void get(ByteBuffer dst)
         throws BufferUnderflowException
Reads to dst remaining bytes

Parameters:
dst - a ByteBuffer object.
Throws:
BufferUnderflowException - if any.

get

public void get(ByteBuffer dst,
                int length)
         throws BufferUnderflowException
Reads to dst

Parameters:
dst - a ByteBuffer object.
length - a int.
Throws:
BufferUnderflowException - if any.

get

public ByteBuffer get(int len)
               throws BufferUnderflowException
Read bytes. (Efficient) This implementation copies memory only if the requested data is span over several ByteBuffers in the back-end.

Parameters:
len - a int.
Returns:
ByteBuffer containing read data
Throws:
BufferUnderflowException - if any.

getAvailable

public void getAvailable(ByteBuffer dst)
Reads as much as there is available and dst has remaining allocation

Parameters:
dst - a ByteBuffer object.

getChunks

public ByteBuffer[] getChunks(int len)
                       throws BufferUnderflowException
Reads bytes. This implementation does not copy memory.

Parameters:
len - number of bytes to read
Returns:
byte buffer array
Throws:
BufferUnderflowException - if any.

peek

public void peek(byte[] buf)
          throws BufferUnderflowException
Peek data, read without moving read pointer.

Parameters:
buf - an array of byte.
Throws:
BufferUnderflowException - if any.

peek

public void peek(byte[] buf,
                 int off,
                 int len)
          throws BufferUnderflowException
Peek data, read without moving read pointer

Parameters:
buf - an array of byte.
off - a int.
len - a int.
Throws:
BufferUnderflowException - if any.

peek

public void peek(ByteBuffer dst)
          throws BufferUnderflowException
Peeks to dst.

Parameters:
dst - a ByteBuffer object.
Throws:
BufferUnderflowException - if any.

peek

public ByteBuffer peek(int len)
                throws BufferUnderflowException
Peek, read without moving read pointer.

Parameters:
len - a int.
Returns:
a byte buffer containing the peeked bytes
Throws:
BufferUnderflowException - if any.

peekAvailable

public void peekAvailable(ByteBuffer dst)
Peek available data.

Parameters:
dst - a ByteBuffer object.

peekChunks

public ByteBuffer[] peekChunks(int len)
                        throws BufferUnderflowException
Peeks given number of bytes. This method does not copy memory.

Parameters:
len - a int.
Returns:
array of buffers.
Throws:
BufferUnderflowException - if any.

remaining

public long remaining()
Get the number of readable bytes.

Returns:
number of readable bytes.

isEmpty

public boolean isEmpty()
Is buffer empty

Returns:
true if buffer is empty

hasRemaining

public boolean hasRemaining()

hasRemaining.

Returns:
a boolean.

getBytesRead

public long getBytesRead()
Get the total number of bytes read from the queue.

Returns:
the total number of bytes read from the queue

getBytesWritten

public long getBytesWritten()
Get the total number of bytes written to the queue.

Returns:
the total number of bytes written to the queue

omitAll

public void omitAll()
Clears queue and moves read pointer to written pointer.


clear

public void clear()
Clears bytes and resets all pointers.


skip

public void skip(int bytes)
          throws BufferOverflowException
Omit data by moving read pointer forward.

Parameters:
bytes - to omit
Throws:
BufferOverflowException - if any.

getReadChunk

public ByteBuffer getReadChunk()
Expose the a byte buffer of backend. Moving the position of this byte buffer, moves the byte queue aswell.

Returns:
active read byte buffer or null

getWriteChunk

public ByteBuffer getWriteChunk()
Exposes internal bytebuffer of this object. Writing to this bytebuffer moves the pointer in this bytequeue aswell.

Returns:
returns a chunk with atleast 1 writeable byte

getWriteableBytesRemaining

public int getWriteableBytesRemaining()
Get the number of bytes remaining for write

Returns:
bytes remaining

order

public ByteOrder order()
Get byte order

Returns:
the byte order

order

public void order(ByteOrder order)
Set byte order

Parameters:
order - a ByteOrder object.

setByteBufferFactory

public void setByteBufferFactory(ByteBufferFactory factory)
Set byte buffer constructor. The constructed buffers may be larger than requested but remaining bytes must be the requested amount.

Parameters:
factory - a ByteBufferFactory object.

getByteBufferFactory

public ByteBufferFactory getByteBufferFactory()
Get byte buffer factory

Returns:
byte buffer factory.

getChunkSize

public int getChunkSize()
Get the block size of back-end chunks

Returns:
chunk size

setChunkSize

public void setChunkSize(int chunkSize)
Set the block size of back-end chunks

Parameters:
chunkSize - a int.

getWriteLimit

public long getWriteLimit()
Get writable bytes limit

Returns:
writeable bytes constraint or Long.MAX_VALUE

setWriteLimit

public void setWriteLimit(long writeLimit)
Set writable bytes limit.

Parameters:
writeLimit - new writeable bytes constraint

toString

public String toString()

Overrides:
toString in class Object


Copyright © 2018. All rights reserved.