public class QueueFile extends Object implements Closeable
QueueFile instance after an exception.
All operations are 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.
| Modifier and Type | Class and Description |
|---|---|
static interface |
QueueFile.ElementReader
Deprecated.
use
QueueFile.ElementVisitor instead. |
static interface |
QueueFile.ElementVisitor
Reads queue elements.
|
| Constructor and Description |
|---|
QueueFile(File file)
Constructs a new queue backed by the given file.
|
| 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()
Closes the underlying file.
|
void |
forEach(QueueFile.ElementReader reader)
Deprecated.
|
int |
forEach(QueueFile.ElementVisitor reader)
Invokes the given reader once for each element in the queue, from eldest to most recently
added.
|
boolean |
isEmpty()
Returns true if this queue contains no entries.
|
byte[] |
peek()
Reads the eldest element.
|
void |
peek(QueueFile.ElementReader reader)
Deprecated.
|
void |
peek(QueueFile.ElementVisitor visitor)
Invokes
visitor with the eldest element, if an element is available. |
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.
|
String |
toString() |
public QueueFile(File file) throws IOException
IOExceptionpublic void add(byte[] data)
throws IOException
data - to copy bytes fromIOExceptionpublic void add(byte[] data,
int offset,
int count)
throws IOException
data - to copy bytes fromoffset - to start from in buffercount - number of bytes to copyIndexOutOfBoundsException - if offset < 0 or count < 0, or if offset + count is bigger than the length of buffer.IOExceptionpublic boolean isEmpty()
public byte[] peek()
throws IOException
IOException@Deprecated public void peek(QueueFile.ElementReader reader) throws IOException
peek(ElementVisitor)IOExceptionpublic void peek(QueueFile.ElementVisitor visitor) throws IOException
visitor with the eldest element, if an element is available.IOException@Deprecated public void forEach(QueueFile.ElementReader reader) throws IOException
forEach(ElementVisitor)IOExceptionpublic int forEach(QueueFile.ElementVisitor reader) throws IOException
reader.read()
returns false.IOExceptionpublic int size()
public void remove()
throws IOException
NoSuchElementException - if the queue is emptyIOExceptionpublic void remove(int n)
throws IOException
n elements.NoSuchElementException - if the queue is emptyIOExceptionpublic void clear()
throws IOException
IOExceptionpublic void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableIOExceptionCopyright © 2015 Square, Inc.. All Rights Reserved.