public class ByteDiskQueue extends Object implements Closeable, Size64
Instances of this class keep track of a queue of bytes using a circular
(and possibly direct)
ByteBuffer that contains the head and the tail of the queue.
The central part of the queue, if necessary, is dumped on disk. Note that clear()
does empty the queue, but it does not remove the dump file on disk: use trim() to trim
the file at the smallest possible length. The dump file can be removed only with a call to
close(), or when the instance is finalised.
Since file handles are a precious resource, this class provides a suspend() method
that releases the file handle without deleting the dump file. All calls to methods accessing the
file handle will reinitialise it lazily (which implies that a certain number of
enqueues/dequeues can be performed on a suspended class without actually reopening
the dump file).
You can create a queue using a new or empty file, or
using an existing file, in which case the
content of the file will become the content of the queue. A freeze() method closes
a queue but leaves behind a dump file containing the current content of the queue, so that it
can be reopened later.
Warning: the close() method will delete the queue file from
disk. This implies that if a file with the same name has been created in the meanwhile, it will
be deleted.
| Modifier | Constructor and Description |
|---|---|
protected |
ByteDiskQueue(File file,
int bufferSize,
boolean direct,
boolean useFileContent)
Creates a new disk-based byte queue.
|
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Clears the queue.
|
void |
close()
Deallocates all disk resources associated with this queue.
|
static ByteDiskQueue |
createFromFile(File file,
int bufferSize,
boolean direct)
Creates a new disk-based byte queue using the content of an existing file.
|
static ByteDiskQueue |
createNew(File file,
int bufferSize,
boolean direct)
Creates a new empty disk-based byte queue.
|
byte |
dequeue()
Dequeues a byte from the queue.
|
void |
dequeue(byte[] a)
Dequeues a sequence of bytes from this queue into an array.
|
void |
dequeue(byte[] a,
int offset,
int length)
Dequeues a sequence of bytes from this queue into an array.
|
int |
dequeueInt()
Dequeues from this queue a vByte-coded natural number.
|
void |
enlargeBuffer(int newBufferSize)
Enlarge the size of the buffer of this queue to a given size.
|
void |
enqueue(byte b)
Enqueues a byte to this queue.
|
void |
enqueue(byte[] a)
Enqueues a byte array to this queue.
|
void |
enqueue(byte[] a,
int offset,
int length)
Enqueues a fragment of byte array to this queue.
|
void |
enqueueInt(int x)
Adds a vByte-coded natural number to this queue.
|
protected void |
finalize() |
void |
freeze() |
boolean |
isEmpty() |
int |
size()
Deprecated.
|
long |
size64() |
void |
suspend()
Suspends this queue, releasing the associated file handles.
|
void |
trim()
Trims the queue dump file at the current write position.
|
protected ByteDiskQueue(File file, int bufferSize, boolean direct, boolean useFileContent) throws IOException
file - the file that will be used to dump the central part of the queue on disk.bufferSize - the capacity of the circular buffer (will be possibly decreased so to be a power of two).direct - whether the circular ByteBuffer used by this queue should be allocated directly.useFileContent - whether the queue is new or should use the content of an existing file; in the first case,
we check that the file does not exists or has length zero.IOExceptionpublic static ByteDiskQueue createNew(File file, int bufferSize, boolean direct) throws IOException
file - the file that will be used to dump the central part of the queue on disk (must not exist
or have length zero).bufferSize - the capacity of the circular buffer (will be possibly decreased so to be a power of two).direct - whether the circular ByteBuffer used by this queue should be allocated directly.IOExceptionpublic static ByteDiskQueue createFromFile(File file, int bufferSize, boolean direct) throws IOException
file - the file that will be used to dump the central part of the queue on disk (must exist).bufferSize - the capacity of the circular buffer (will be possibly decreased so to be a power of two).direct - whether the circular ByteBuffer used by this queue should be allocated directly.IOExceptionpublic void enqueue(byte b)
throws IOException
b - the byte to be enqueued.IOExceptionpublic void enqueueInt(int x)
throws IOException
x - the natural number to be added to the queue.IOExceptionpublic void enqueue(byte[] a,
int offset,
int length)
throws IOException
a - a byte array.offset - the first byte in a to be enqueued.length - the number of bytes to enqueue.IOExceptionpublic void enqueue(byte[] a)
throws IOException
a - the array whose bytes have to be enqueued.IOExceptionpublic byte dequeue()
throws IOException
NoSuchElementException - if there are no bytes in this queue.IOExceptionpublic void dequeue(byte[] a,
int offset,
int length)
throws IOException
a - a byte array.offset - the first position in a where dequeued bytes will be written.length - the number of bytes to dequeue.NoSuchElementException - if there are not enough bytes in this queue.IOExceptionpublic void dequeue(byte[] a)
throws IOException
a - the array that will contain the dequeued bytes.NoSuchElementException - if there are not enough bytes in this queue.IOExceptionpublic int dequeueInt()
throws IOException
IOException@Deprecated public int size()
public boolean isEmpty()
public void close()
throws IOException
IllegalStateException.close in interface Closeableclose in interface AutoCloseableIOExceptionpublic void freeze()
throws IOException
IOExceptionpublic void clear()
trim() to that purpose).public void trim()
throws IOException
IOExceptionpublic void suspend()
throws IOException
IOExceptionprotected void finalize()
throws Throwable
public void enlargeBuffer(int newBufferSize)
newBufferSize - the required buffer size (will be possibly decreased so to be a power of two).
If the new buffer size is smaller than the current size, nothing happens.