public final class QueueFile
extends java.lang.Object
implements java.io.Closeable, java.lang.Iterable<byte[]>
QueueFile instance after an exception.
Note that this implementation is not synchronized.
In a traditional queue, the remove operation returns an element. In this queue,
peek() and remove() are used in conjunction. Use
peek to retrieve the first element, and then remove to remove it after
successful processing. If the system crashes after peek and during processing, the
element will remain in the queue, to be processed when the system restarts.
NOTE: The current implementation is built for file systems that support
atomic segment writes (like YAFFS). Most conventional file systems don't support this; if the
power goes out while writing a segment, the segment will contain garbage and the file will be
corrupt. We'll add journaling support so this class can be used with more file systems later.
Construct instances with QueueFile.Builder.
| Modifier and Type | Class and Description |
|---|---|
static class |
QueueFile.Builder
Fluent API for creating
QueueFile instances. |
| Modifier and Type | Method and Description |
|---|---|
void |
add(byte[] data)
Adds an element to the end of the queue.
|
void |
add(byte[] data,
int offset,
int count)
Adds an element to the end of the queue.
|
void |
clear()
Clears this queue.
|
void |
close() |
java.io.File |
file()
The underlying
File backing this queue. |
boolean |
isEmpty()
Returns true if this queue contains no entries.
|
java.util.Iterator<byte[]> |
iterator()
Returns an iterator over elements in this QueueFile.
|
byte[] |
peek()
Reads the eldest element.
|
void |
remove()
Removes the eldest element.
|
void |
remove(int n)
Removes the eldest
n elements. |
int |
size()
Returns the number of elements in this queue.
|
java.lang.String |
toString() |
public void add(byte[] data)
throws java.io.IOException
data - to copy bytes fromjava.io.IOExceptionpublic void add(byte[] data,
int offset,
int count)
throws java.io.IOException
data - to copy bytes fromoffset - to start from in buffercount - number of bytes to copyjava.lang.IndexOutOfBoundsException - if offset < 0 or count < 0, or if offset + count is bigger than the length of buffer.java.io.IOExceptionpublic boolean isEmpty()
@Nullable
public byte[] peek()
throws java.io.IOException
java.io.IOExceptionpublic java.util.Iterator<byte[]> iterator()
The iterator disallows modifications to be made to the QueueFile during iteration. Removing
elements from the head of the QueueFile is permitted during iteration using
Iterator.remove().
The iterator may throw an unchecked IOException during Iterator.next()
or Iterator.remove().
iterator in interface java.lang.Iterable<byte[]>public int size()
public void remove()
throws java.io.IOException
java.util.NoSuchElementException - if the queue is emptyjava.io.IOExceptionpublic void remove(int n)
throws java.io.IOException
n elements.java.util.NoSuchElementException - if the queue is emptyjava.io.IOExceptionpublic void clear()
throws java.io.IOException
java.io.IOExceptionpublic java.io.File file()
File backing this queue.public void close()
throws java.io.IOException
close in interface java.io.Closeableclose in interface java.lang.AutoCloseablejava.io.IOExceptionpublic java.lang.String toString()
toString in class java.lang.Object