public class MappedBusReader
extends java.lang.Object
Messages can either be message based or byte array based.
The typical usage is as follows:
// Construct a reader
MappedBusReader reader = new MappedBusReader("/tmp/test", 100000L, 32);
reader.open();
// A: read messages as objects
while (true) {
if (reader.next()) {
int type = reader.readType();
if (type == 0) {
reader.readMessage(priceUpdate)
}
}
}
// B: read messages as byte arrays
while (true) {
if (reader.next()) {
int length = reader.read(buffer, 0);
}
}
// Close the reader
reader.close();
Constructor and Description |
---|
MappedBusReader(java.lang.String fileName,
long fileSize,
int recordSize)
Constructs a new reader.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the reader.
|
boolean |
hasRecovered()
Indicates whether all records available when the reader was created have been read.
|
boolean |
next()
Steps forward to the next record if there's one available.
|
void |
open()
Opens the reader.
|
int |
readBuffer(byte[] dst,
int offset)
Reads the next buffer of data.
|
MappedBusMessage |
readMessage(MappedBusMessage message)
Reads the next message.
|
int |
readType()
Reads the message type.
|
void |
setTimeout(int timeout)
Sets the time for a reader to wait for a record to be committed.
|
public MappedBusReader(java.lang.String fileName, long fileSize, int recordSize)
fileName
- the name of the memory mapped filefileSize
- the maximum size of the filerecordSize
- the maximum size of a record (excluding status flags and meta data)public void open() throws java.io.IOException
java.io.IOException
- if there was a problem opening the filepublic void setTimeout(int timeout)
timeout
- the timeout in millisecondspublic boolean next() throws java.io.EOFException
java.io.EOFException
- in case the end of the file was reachedpublic int readType()
public MappedBusMessage readMessage(MappedBusMessage message)
message
- the message object to populatepublic int readBuffer(byte[] dst, int offset)
dst
- the input bufferoffset
- the offset in the buffer of the first byte to read data intopublic boolean hasRecovered()
public void close() throws java.io.IOException
java.io.IOException
- if there was an error closing the file