public class Reader
extends java.lang.Object
File has this structure:
+----------------------------------+
| General File Header |
+----------------------------------+
+----------------------------------+
| Index (optional) |
+----------------------------------+
+----------------------------------+
| User Header (optional) |
+----------------------------------+
+----------------------------------+
| |
| Record 1 |
| |
| |
| |
+----------------------------------+
...
+----------------------------------+
| |
| Record N |
| |
| |
| |
+----------------------------------+
+----------------------------------+
| Trailer (optional) |
+----------------------------------+
+----------------------------------+
| Trailer's Index (optional) |
+----------------------------------+
Buffer or streamed data has this structure:
+----------------------------------+
| |
| Record 1 |
| |
| |
| |
+----------------------------------+
...
+----------------------------------+
| |
| Record N |
| |
| | RecordPosition
| |
+----------------------------------+
+----------------------------------+
| Trailer (optional) |
+----------------------------------+
The important thing with a buffer or streaming is for the last header or
trailer to set the "last record" bit.
Something to keep in mind is one can intersperse sequential calls
(getNextEvent, getPrevEvent, or getNextEventNode) with random access
calls (getEvent or getEventNode), and the sequence remains unchanged
after the random access.FileHeader
,
RecordInputStream
Modifier and Type | Class and Description |
---|---|
static class |
Reader.RecordPosition
Internal class to keep track of the records in the file/buffer.
|
Constructor and Description |
---|
Reader()
Default constructor.
|
Reader(java.nio.ByteBuffer buffer)
Constructor for reading buffer with evio data.
|
Reader(java.nio.ByteBuffer buffer,
EvioNodeSource pool)
Constructor for reading buffer with evio data.
|
Reader(java.nio.ByteBuffer buffer,
EvioNodeSource pool,
boolean checkRecordNumSeq)
Constructor for reading buffer with evio data.
|
Reader(java.io.RandomAccessFile file)
Constructor with file already opened.
|
Reader(java.lang.String filename)
Constructor with filename.
|
Reader(java.lang.String filename,
boolean forceScan)
Constructor with filename.
|
Modifier and Type | Method and Description |
---|---|
java.nio.ByteBuffer |
addStructure(int eventNumber,
java.nio.ByteBuffer addBuffer)
This method adds an evio container (bank, segment, or tag segment) as the last
structure contained in an event.
|
void |
close()
This closes the file.
|
static void |
findRecordInfo(java.nio.ByteBuffer buf,
int offset,
int[] info)
Reads data from a record header in order to determine things
like the bitInfo word, various lengths, etc.
|
void |
forceScanFile()
Scan file to find all records and store their position, length, and event count.
|
java.nio.ByteBuffer |
getBuffer()
Get the buffer being read, if any.
|
int |
getBufferOffset()
Get the beginning position of the buffer being read.
|
java.nio.ByteOrder |
getByteOrder()
Get the byte order of the file/buffer being read.
|
boolean |
getCheckRecordNumberSequence()
Get whether or not record numbers are enforced to be sequential.
|
int |
getCurrentRecord()
Get the index of the current record.
|
RecordInputStream |
getCurrentRecordStream()
Get the current record stream.
|
java.lang.String |
getDictionary()
Get the XML format dictionary if there is one.
|
java.nio.ByteBuffer |
getEvent(java.nio.ByteBuffer buf,
int index)
Get a byte array representing the specified event from the file/buffer
and place it in the given buf.
|
byte[] |
getEvent(int index)
Get a byte array representing the specified event from the file/buffer.
|
int |
getEventCount()
Get the number of events in file/buffer.
|
EvioNode |
getEventNode(int index)
Get an EvioNode representing the specified event from the buffer.
|
java.util.ArrayList<EvioNode> |
getEventNodes()
Get the list of EvioNode objects contained in the buffer being read.
|
FileHeader |
getFileHeader()
Get the file header from reading a file.
|
java.lang.String |
getFileName()
Get the name of the file being read.
|
long |
getFileSize()
Get the size of the file being read, in bytes.
|
byte[] |
getFirstEvent()
Get a byte array representing the first event.
|
RecordHeader |
getFirstRecordHeader()
Get the first record header from reading a file/buffer.
|
byte[] |
getNextEvent()
Get a byte array representing the next event from the file/buffer while sequentially reading.
|
EvioNode |
getNextEventNode()
Get an EvioNode representing the next event from the buffer while sequentially reading.
|
int |
getNumEventsRemaining()
Get the number of events remaining in the file/buffer.
|
byte[] |
getPrevEvent()
Get a byte array representing the previous event from the sequential queue.
|
int |
getRecordCount()
Get the number of records read from the file/buffer.
|
int |
getRecordEventCount()
Get the number of events in current record.
|
java.util.List<Reader.RecordPosition> |
getRecordPositions()
Returns the list of record positions in the file.
|
int |
getVersion()
Get the Evio format version number of the file/buffer being read.
|
boolean |
hasDictionary()
Does this evio file/buffer have an associated XML dictionary?
|
boolean |
hasFirstEvent()
Does this evio file/buffer have an associated first event?
|
boolean |
hasNext()
Checks if the file has an event to read next.
|
boolean |
hasPrev()
Checks if the stream has previous event to be accessed through, getPrevEvent()
|
boolean |
isClosed()
Has
close() been called (without reopening by calling
setBuffer(ByteBuffer) )? |
boolean |
isCompressed()
Is the data in the file/buffer compressed?
|
boolean |
isEvioFormat()
Does this file/buffer contain non-evio format events?
|
boolean |
isFile()
Is a file being read?
|
static void |
main(java.lang.String[] args) |
void |
open(java.lang.String filename)
Opens an input stream in binary mode.
|
void |
open(java.lang.String filename,
boolean scan)
Opens an input stream in binary mode.
|
boolean |
readRecord(int index)
Reads record from the file/buffer at the given record index.
|
java.nio.ByteBuffer |
readUserHeader()
Reads user header of the file header/first record header of buffer.
|
java.nio.ByteBuffer |
removeStructure(EvioNode removeNode)
This method removes the data, represented by the given node, from the buffer.
|
java.nio.ByteBuffer |
scanBuffer()
This method scans a buffer to find all records and store their position, length,
and event count.
|
void |
scanFile(boolean force)
Scans the file to index all the record positions.
|
void |
scanUncompressedBuffer()
Scan buffer to find all records and store their position, length, and event count.
|
void |
setBuffer(java.nio.ByteBuffer buf)
This method can be used to avoid creating additional Reader
objects by reusing this one with another buffer.
|
void |
setBuffer(java.nio.ByteBuffer buf,
EvioNodeSource pool)
This method can be used to avoid creating additional Reader
objects by reusing this one with another buffer.
|
void |
show() |
public Reader()
open(String)
method has to be called to open the input stream.
Also forceScanFile()
needs to be called to find records.public Reader(java.io.RandomAccessFile file)
forceScanFile()
needs to be called to find records.file
- open filepublic Reader(java.lang.String filename) throws java.io.IOException, HipoException
filename
- input file name.java.io.IOException
- if error reading fileHipoException
- if file is not in the proper format or earlier than version 6public Reader(java.lang.String filename, boolean forceScan) throws java.io.IOException, HipoException
filename
- input file name.forceScan
- if true, force a scan of file, else use existing indexes first.java.io.IOException
- if error reading fileHipoException
- if file is not in the proper format or earlier than version 6public Reader(java.nio.ByteBuffer buffer) throws HipoException
getBuffer()
.buffer
- buffer with evio data.HipoException
- if buffer too small, not in the proper format, or earlier than version 6public Reader(java.nio.ByteBuffer buffer, EvioNodeSource pool) throws HipoException
getBuffer()
.buffer
- buffer with evio data.pool
- pool of EvioNode objects for garbage-free operation.HipoException
- if buffer too small, not in the proper format, or earlier than version 6public Reader(java.nio.ByteBuffer buffer, EvioNodeSource pool, boolean checkRecordNumSeq) throws HipoException
getBuffer()
.buffer
- buffer with evio data.pool
- pool of EvioNode objects for garbage-free operation.checkRecordNumSeq
- if true, check to see if all record numbers are in order,
if not throw exception.HipoException
- if buffer too small, not in the proper format, or earlier than version 6;
if checkRecordNumSeq is true and records are out of sequence.public final void open(java.lang.String filename) throws java.io.IOException, HipoException
filename
- input file namejava.io.IOException
- if file not found or error opening fileHipoException
- if file is not in the proper format or earlier than version 6public final void open(java.lang.String filename, boolean scan) throws java.io.IOException, HipoException
filename
- input file namescan
- if true, call scanFile(false).java.io.IOException
- if file not found or error opening fileHipoException
- if file is not in the proper format or earlier than version 6public void close() throws java.io.IOException
java.io.IOException
- if error accessing filepublic boolean isClosed()
close()
been called (without reopening by calling
setBuffer(ByteBuffer)
)?true
if this object closed, else false
.public boolean isFile()
true
if a file is being read, false
if it's a buffer.public void setBuffer(java.nio.ByteBuffer buf) throws HipoException
getBuffer()
.buf
- ByteBuffer to be readHipoException
- if buf arg is null, buffer too small,
not in the proper format, or earlier than version 6public void setBuffer(java.nio.ByteBuffer buf, EvioNodeSource pool) throws HipoException
getBuffer()
.buf
- ByteBuffer to be readpool
- pool of EvioNode objects to use when parsing buf.HipoException
- if buf arg is null, buffer too small,
not in the proper format, or earlier than version 6public java.lang.String getFileName()
public long getFileSize()
public java.nio.ByteBuffer getBuffer()
public int getBufferOffset()
public FileHeader getFileHeader()
public RecordHeader getFirstRecordHeader()
public java.nio.ByteOrder getByteOrder()
public int getVersion()
public boolean isCompressed()
public boolean isEvioFormat()
public java.lang.String getDictionary()
public boolean hasDictionary()
true
if this evio file/buffer has an associated XML dictionary,
else false
.public byte[] getFirstEvent()
public boolean hasFirstEvent()
true
if this evio file/buffer has an associated first event,
else false
.public int getEventCount()
public int getRecordCount()
public java.util.List<Reader.RecordPosition> getRecordPositions()
public java.util.ArrayList<EvioNode> getEventNodes()
public boolean getCheckRecordNumberSequence()
true
if record numbers are enforced to be sequential.public int getNumEventsRemaining()
public byte[] getNextEvent() throws HipoException
getPrevEvent()
, this will get the event
past what that returned. Once the last event is returned, this will return null.HipoException
- if file/buffer not in hipo formatpublic byte[] getPrevEvent() throws HipoException
getNextEvent()
, this will get the event
previous to what that returned. If this is called before getNextEvent,
it will always return null. Once the first event is returned, this will
return null.HipoException
- if the file/buffer is not in HIPO formatpublic EvioNode getNextEventNode()
getNextEvent()
have the same effect in terms of
advancing the same internal counter.
If the previous call was to getPrevEvent()
, this will get the event
past what that returned. Once the last event is returned, this will return null.public java.nio.ByteBuffer readUserHeader() throws java.io.IOException
java.io.IOException
- if error reading filepublic byte[] getEvent(int index) throws HipoException
index
- index of specified event within the entire file/buffer,
contiguous starting at 0.HipoException
- if file/buffer not in hipo formatpublic java.nio.ByteBuffer getEvent(java.nio.ByteBuffer buf, int index) throws HipoException
buf
- buffer in which to place event data.index
- index of specified event within the entire file/buffer,
contiguous starting at 0.HipoException
- if file/buffer not in hipo format, or
if buf has insufficient space to contain event
(buf.capacity() < event size).public EvioNode getEventNode(int index)
index
- index of specified event within the entire buffer,
starting at 0.public boolean hasNext()
public boolean hasPrev()
public int getRecordEventCount()
public int getCurrentRecord()
public RecordInputStream getCurrentRecordStream()
public boolean readRecord(int index) throws HipoException
index
- record index (starting at 0).HipoException
- if file/buffer not in hipo formatpublic static void findRecordInfo(java.nio.ByteBuffer buf, int offset, int[] info) throws HipoException, java.nio.BufferUnderflowException
buf
- buffer containing evio header.offset
- byte offset into buffer.info
- array in which to store header info. Elements are:
java.nio.BufferUnderflowException
- if not enough data in buffer.HipoException
- null arg(s), negative offset, or info.length < 7.public java.nio.ByteBuffer scanBuffer() throws HipoException, java.nio.BufferUnderflowException
The difficulty with doing this is that the buffer may contain compressed data. It must then be uncompressed into a different buffer. There are 2 possibilities. First, if the buffer being parsed is too small to hold its uncompressed form, then a new, larger buffer is created, filled with the uncompressed data and then given as the return value of this method. Second, if the buffer being parsed is large enough to hold its uncompressed form, the data is uncompressed into a temporary holding buffer. When all decompresssion and parsing is finished, the contents of the temporary buffer are copied back into the original buffer which then becomes the return value.
HipoException
- if buffer not in the proper format or earlier than version 6;
if checkRecordNumberSequence is true and records are out of sequence.java.nio.BufferUnderflowException
- if not enough data in buffer.public void scanUncompressedBuffer() throws HipoException
HipoException
- if buffer too small, not in the proper format, or earlier than version 6;
if checkRecordNumberSequence is true and records are out of sequence.public void forceScanFile() throws java.io.IOException, HipoException
java.io.IOException
- if error reading fileHipoException
- if file is not in the proper format or earlier than version 6;
if checkRecordNumberSequence is true and records are out of sequence.public void scanFile(boolean force) throws java.io.IOException, HipoException
force
- if true, force a file scan even if header
or trailer have index info.java.io.IOException
- if error reading fileHipoException
- if file is not in the proper format or earlier than version 6public java.nio.ByteBuffer removeStructure(EvioNode removeNode) throws HipoException
removeNode
- evio structure to remove from bufferHipoException
- if object closed;
if node was not found in any event;
if internal programming error;
if buffer has compressed data;public java.nio.ByteBuffer addStructure(int eventNumber, java.nio.ByteBuffer addBuffer) throws HipoException
To produce such evio data use BaseStructure.write(ByteBuffer)
,
BaseStructure.write(ByteBuffer)
or
BaseStructure.write(ByteBuffer)
depending on whether
a bank, seg, or tagseg is being added.
The given buffer argument must be ready to read with its position and limit defining the limits of the data to copy.
eventNumber
- number of event to which addBuffer is to be addedaddBuffer
- buffer containing evio data to add (not evio file format,
i.e. no record headers)HipoException
- if eventNumber out of bounds;
if addBuffer is null;
if addBuffer arg is empty or has non-evio format;
if addBuffer is opposite endian to current event buffer;
if added data is not the proper length (i.e. multiple of 4 bytes);
if the event number does not correspond to an existing event;
if there is an internal programming error;
if object closedpublic void show()
public static void main(java.lang.String[] args)