public class QueueFile
extends java.lang.Object
implements java.io.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.
Constructor and Description |
---|
QueueFile(java.io.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.
|
int |
forEach(com.segment.analytics.PayloadQueue.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 |
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 QueueFile(java.io.File file) throws java.io.IOException
java.io.IOException
public void add(byte[] data) throws java.io.IOException
data
- to copy bytes fromjava.io.IOException
public 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.IOException
public boolean isEmpty()
public byte[] peek() throws java.io.IOException
java.io.IOException
public int forEach(com.segment.analytics.PayloadQueue.ElementVisitor reader) throws java.io.IOException
reader.read()
returns false
.java.io.IOException
public int size()
public void remove() throws java.io.IOException
java.util.NoSuchElementException
- if the queue is emptyjava.io.IOException
public void remove(int n) throws java.io.IOException
n
elements.java.util.NoSuchElementException
- if the queue is emptyjava.io.IOException
public void clear() throws java.io.IOException
java.io.IOException
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
java.io.IOException
public java.lang.String toString()
toString
in class java.lang.Object