Class BinaryQueue

java.lang.Object
org.nustaq.offheap.BinaryQueue

public class BinaryQueue extends Object
an automatically growing byte queue organized as a ringbuffer. poll* methods don't throw excpetions but return incomplete or no result instead if the queue's content is not sufficient. read* methods throw excpetions in case the q contains not enough bytes (need to use available() to ensure data is present). add methods add to the queue. If the adding outperforms reading/polling, the queue is grown automatically.
  • Constructor Details

    • BinaryQueue

      public BinaryQueue()
    • BinaryQueue

      public BinaryQueue(int qsize)
  • Method Details

    • add

      public void add(ByteSource source)
      add bytes to the queue. Again by using (reusable) Wrapper classes any kind of memory (offheap, byte arrays, nio bytebuffer, memory mapped) can be added.
      Parameters:
      source -
    • add

      public void add(ByteSource source, long sourceoff, long sourcelen)
      add bytes to the queue. Again by using (reusable) Wrapper classes any kind of memory (offheap, byte arrays, nio bytebuffer, memory mapped) can be added.
      Parameters:
      source -
      sourceoff -
      sourcelen -
    • addInt

      public void addInt(int written)
    • add

      public void add(byte b)
    • grow

      protected void grow(long sourcelen)
    • remaining

      public long remaining()
      Returns:
      number of bytes free for an add operation
    • poll

      public long poll(ByteSink destination, long destoff, long destlen)
      read up to destlen bytes (if available). Note you can use HeapBytez (implements ByteSink) in order to read to a regular byte array. HeapBytez wrapper can be reused to avoid unnecessary allocation. Also possible is ByteBufferBasicBytes to read into a ByteBuffer and MallocBytes or MMFBytes to read into Unsafe alloc'ed off heap memory or persistent mem mapped memory regions.
      Parameters:
      destination -
      destoff -
      destlen -
      Returns:
    • readByteArray

      public byte[] readByteArray(int len)
      convenience method to read len byte array. Throws an excpetion if not enough data is present
      Parameters:
      len -
      Returns:
    • readInt

      public int readInt()
      read an int. throws an exception if not enough data is present
      Returns:
    • poll

      public int poll()
      Returns:
      -1 or the next byte unsigned value
    • back

      public void back(int len)
      'unread' len bytes
      Parameters:
      len -
    • available

      public long available()
      Returns:
      number of readable bytes
    • capacity

      public long capacity()
      Returns:
      size of underlying ringbuffer
    • toString

      public String toString()
      Overrides:
      toString in class Object