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
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)
Invokes the given reader once for each element in the queue, from eldest to most recently added.
|
boolean |
hasSpaceFor(int dataSizeBytes,
int maxSizeBytes)
Returns whether the current used data size, plus the data size provided, plus element header size will
stay within the max size provided
|
boolean |
isEmpty()
Returns true if this queue contains no entries.
|
byte[] |
peek()
Reads the eldest element.
|
void |
peek(QueueFile.ElementReader reader)
Invokes reader with the eldest element, if an element is available.
|
void |
remove()
Removes the eldest element.
|
int |
size()
Returns the number of elements in this queue.
|
String |
toString() |
int |
usedBytes()
Returns the number of used bytes.
|
public QueueFile(File file)
throws IOException
QueueFile instance should access a
given file at a time.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 int usedBytes()
public boolean isEmpty()
public byte[] peek()
throws IOException
IOExceptionpublic void peek(QueueFile.ElementReader reader) throws IOException
IOExceptionpublic void forEach(QueueFile.ElementReader reader) throws IOException
IOExceptionpublic int size()
public void remove()
throws IOException
NoSuchElementException - if the queue is emptyIOExceptionpublic void clear()
throws IOException
IOExceptionpublic void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableIOExceptionpublic boolean hasSpaceFor(int dataSizeBytes,
int maxSizeBytes)
public String toString()
toString in class Object