Loading…

Kryptel/Java

SmartBuffer class

Declaration

package com.kryptel.bslx;

public class SmartBuffer {

    public SmartBuffer()
    public SmartBuffer(byte[] src, int start, int len)

    public synchronized int Size()

    public synchronized void Empty()

    public void Store(byte[] buf)

    public synchronized void Store(byte[] buf, int start, int len)

    public synchronized int Retrieve(byte[] buf, int start, int len)

    public synchronized void Unretrieve(byte[] buf, int start, int len)

    public synchronized int Peek(byte[] buf, int start, int len)

    public synchronized int SkipBytes(int len)

    public synchronized byte[] Merge()
}

Description

This class implements a dynamic buffer to be used when data supplier and data consumer run independently, especially in different threads.

The data buffer is a linked set of smaller buffers, which get created and thrown away dynamically. See also Merge function.

Constructors

public SmartBuffer()
public SmartBuffer(byte[] src, int start, int len)

The default constructor creates empty buffer; the second form stores the specified data.

Empty

public synchronized void Empty()

Empties the buffer by throwing away all the stored data.

Merge

public synchronized byte[] Merge()

Converts a linked list of buffer to one larger buffer containg the same data and return it. The length of the returned byte[] array is exactly the value that Size function returns.

Note that this function does not retrieves the data; it simply re-arranges the buffers.

Peek

public synchronized int Peek(byte[] buf, int start, int len)

Same as Retrieve except it does not remove the retrieved data from the buffer. Like Retrieve, this function returns the actual size of the retrieved data.

Retrieve

public synchronized int Retrieve(byte[] buf, int start, int len)

Retrieves data from the buffer, storing them in the buf array starting at start position. The len argument is the requested data size. The returned value is the actual size of the retrieved data.

Note that the function does not wait for the supplier to provide more data if there are less data than requested. Function retrieves what is contained in the buffer; it is responsibility of the consumer to check the returned value and wait for more data if necessary.

Size

public synchronized int Size()

Returns the size of the data currently stored in the buffer.

SkipBytes

public synchronized int SkipBytes(int len)

Removes the specified number of bytes from the buffer. The number may be larger than the buffer size; in this case the buffer gets emptied.

Store

public void Store(byte[] buf)
public synchronized void Store(byte[] buf, int start, int len)

Stores the specified data in the buffer, extending it if necessary. Note that the first form is also synchronized as it just a shortcut to the latter method, which actually does the job.

Unretrieve

public synchronized void Unretrieve(byte[] buf, int start, int len)

This function is similar to Store function except it inserts the data at the beginning of the buffer.

In most cases using this function is better be avoided; if it is absolutely necessary to check what is contained in the buffer, consider using the Peek function optionally followed by SkipBytes.