Class QueueFile
- All Implemented Interfaces:
Closeable,AutoCloseable,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.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(byte[] data) Adds an element to the end of the queue.voidadd(byte[] data, int offset, int count) Adds an element to the end of the queue.voidclear()Clears this queue.voidclose()file()The underlyingFilebacking this queue.booleanReturnstrueif the capacity limit of this queue has been reached, i.e.booleanisEmpty()Returns true if this queue contains no entries.Iterator<byte[]>iterator()Returns an iterator over elements in this QueueFile.@org.jetbrains.annotations.Nullable byte[]peek()Reads the eldest element.voidremove()Removes the eldest element.voidremove(int n) Removes the eldestnelements.intsize()Returns the number of elements in this queue.toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Method Details
-
add
Adds an element to the end of the queue.- Parameters:
data- to copy bytes from- Throws:
IOException
-
add
Adds an element to the end of the queue.- Parameters:
data- to copy bytes fromoffset- to start from in buffercount- number of bytes to copy- Throws:
IndexOutOfBoundsException- ifoffset < 0orcount < 0, or ifoffset + countis bigger than the length ofbuffer.IOException
-
isEmpty
public boolean isEmpty()Returns true if this queue contains no entries. -
peek
Reads the eldest element. Returns null if the queue is empty.- Throws:
IOException
-
iterator
Returns an iterator over elements in this QueueFile.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
IOExceptionduringIterator.next()orIterator.remove(). -
size
public int size()Returns the number of elements in this queue. -
remove
Removes the eldest element.- Throws:
NoSuchElementException- if the queue is emptyIOException
-
remove
Removes the eldestnelements.- Throws:
NoSuchElementException- if the queue is emptyIOException
-
clear
Clears this queue. Truncates the file to the initial size.- Throws:
IOException
-
isAtFullCapacity
public boolean isAtFullCapacity()Returnstrueif the capacity limit of this queue has been reached, i.e. the number of elements stored in the queue equals its maximum size.- Returns:
trueif the capacity limit has been reached,falseotherwise
-
file
The underlyingFilebacking this queue. -
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
toString
-