public final class Utilities
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static int[] |
padValue
Array to help find number of bytes to pad data.
|
Constructor and Description |
---|
Utilities() |
Modifier and Type | Method and Description |
---|---|
static byte[] |
bankToBytes(EvioBank bank,
java.nio.ByteOrder order)
Method to create a byte array representing a bank.
|
static void |
bufferToFile(java.lang.String fileName,
java.nio.ByteBuffer buf,
boolean overWriteOK,
boolean addBlockHeader)
This method takes a byte buffer and saves it to the specified file,
overwriting whatever is there.
|
static int[] |
generateBaseFileName(java.lang.String baseName,
java.lang.String runType,
java.lang.StringBuilder newNameBuilder)
This method generates part of a file name given a base file name as an argument.
|
static java.lang.String |
generateFileName(java.lang.String fileName,
int specifierCount,
int runNumber,
long split,
int splitNumber,
int streamId,
int streamCount)
This method generates a complete file name from the previously determined baseFileName
obtained from calling
generateBaseFileName(String, String, StringBuilder) . |
static DataType |
getDataType(java.lang.String type)
This method returns an XML element name given an evio data type.
|
static int |
getEvioVersion(java.nio.ByteBuffer headerData)
Get the evio version of the given evio header in ByteBuffer form.
|
static int |
getEvioVersion(java.io.File file)
Get the evio version of the given file.
|
static int |
getPadding(int length)
Returns number of bytes needed to pad to 4-byte boundary for the given length.
|
static int |
getWords(int length)
Returns length padded to 4-byte boundary for given length in bytes.
|
static EvioEvent |
nodeToEvent(EvioNode node)
This method takes an EvioNode object and converts it to an EvioEvent object.
|
static int |
powerOfTwo(int x,
boolean roundUp)
Return the power of 2 closest to the given argument.
|
static void |
printBuffer(java.nio.ByteBuffer buf,
int position,
int words,
java.lang.String label)
This method takes a byte buffer and prints out the desired number of words
from the given position.
|
static void |
printBytes(byte[] array,
int offset,
int bytes,
java.lang.String label)
This method takes a byte array and prints out the desired number of bytes
from the given index.
|
static void |
printBytes(java.nio.ByteBuffer buf,
int position,
int bytes,
java.lang.String label)
This method takes a byte buffer and prints out the desired number of bytes
from the given position.
|
static void |
printBytes(java.lang.String fileName,
long offset,
int bytes,
java.lang.String label)
This method takes a file and prints out the desired number of bytes
from the given offset.
|
static void |
printStackTrace()
This method prints out the current stack trace.
|
static void |
readBytes(java.lang.String fileName,
java.nio.ByteBuffer buf)
This method reads part of a file into a ByteBuffer.
|
static byte[] |
stringToBank(java.lang.String text,
short tag,
byte num,
java.nio.ByteOrder order)
Method to create a byte array representing a bank with one string as data.
|
static java.util.List<EvioEvent> |
toEvents(java.lang.String xmlString)
This method takes a string representation of evio events
(each starting with lt; event gt;)
and converts them to a list of EvioEvent objects.
|
static java.util.List<EvioEvent> |
toEvents(java.lang.String xmlString,
int maxEvents,
int skip,
EvioXMLDictionary dictionary,
boolean debug)
This method takes a string representation of evio events
(each starting with lt; event gt;)
and converts them to a list of EvioEvent objects.
|
static java.lang.String |
toXML(EvioNode node,
boolean hex)
This method takes an EvioNode object and converts it to a readable, XML String.
|
public static int getWords(int length)
length
- length in bytes.public static int getPadding(int length)
length
- length in bytes.public static int getEvioVersion(java.io.File file) throws EvioException
file
- file name.EvioException
- if version cannot be found.public static int getEvioVersion(java.nio.ByteBuffer headerData) throws EvioException
headerData
- ByteBuffer object with at least 8 words (32 bytes) of data.EvioException
- if version cannot be found.public static int[] generateBaseFileName(java.lang.String baseName, java.lang.String runType, java.lang.StringBuilder newNameBuilder) throws EvioException
The base file name may contain up to 3, C-style integer format specifiers
(such as %03d, or %x). If more than 3 are found, an exception
will be thrown.
If no "0" precedes any integer between the "%" and the "d" or "x" of the format specifier,
it will be added automatically in order to avoid spaces in the returned string.
In the generateFileName(String, int, int, long, int, int, int)
method,
the first occurrence will be substituted with the given runNumber value.
If the file is being split, the second will be substituted with the split number.
If there are multiple streams, the third will be substituted with the stream id.
The base file name may contain characters of the form $(ENV_VAR) which will be substituted with the value of the associated environmental variable or a blank string if none is found.
Finally, the base file name may contain occurrences of the string "%s" which will be substituted with the value of the runType arg or nothing if the runType is null.
baseName
- file name to start withrunType
- run type/configuration namenewNameBuilder
- object which contains generated base file nameEvioException
- if baseName arg is improperly formatted;
if baseName or newNameBuilder arg is nullpublic static java.lang.String generateFileName(java.lang.String fileName, int specifierCount, int runNumber, long split, int splitNumber, int streamId, int streamCount) throws java.util.IllegalFormatException
generateBaseFileName(String, String, StringBuilder)
.
If evio data is to be split up into multiple files (split > 0), numbers are used to
distinguish between the split files with splitNumber.
If baseFileName contains C-style int format specifiers (specifierCount > 0), then
the first occurrence will be substituted with the given runNumber value.
If the file is being split, the second will be substituted with the splitNumber.
If there are multiple streams, the third will be substituted with the stream id.If no specifier for the splitNumber exists, it is tacked onto the end of the file name. If no specifier for the stream id exists, it is tacked onto the end of the file name, after the splitNumber. No run numbers are ever tacked on without a specifier.
For splitting: if there is only 1 stream, no stream ids are used and any third specifier is removed. For non-splitting: if there is only 1 stream, no stream ids are used and any second and third specifiers are removed. For multiple streams, the second specifier is removed and the 3rd substituted with the stream id. For all cases: if there are more than 3 specifiers, NO SUBSTITUTIONS ARE DONE. This is because an error would be thrown in the String.format() method.
fileName
- file name to use as a basis for the generated file namespecifierCount
- number of C-style int format specifiers in baseFileName argrunNumber
- CODA run numbersplit
- number of bytes at which to split off evio filesplitNumber
- number of the split filestreamId
- number of the stream idstreamCount
- total number of streamsjava.util.IllegalFormatException
- if the baseFileName arg contains printing format
specifiers which are not compatible with integers
and interfere with formattingpublic static void bufferToFile(java.lang.String fileName, java.nio.ByteBuffer buf, boolean overWriteOK, boolean addBlockHeader) throws java.io.IOException, EvioException
fileName
- name of file to writebuf
- buffer to write to fileoverWriteOK
- if true
, OK to overwrite previously existing fileaddBlockHeader
- if true
, add evio block header for proper evio file formatjava.io.IOException
- if trouble writing to fileEvioException
- if file exists but overwriting is not permitted;
if null arg(s)public static void printStackTrace()
public static void printBuffer(java.nio.ByteBuffer buf, int position, int words, java.lang.String label)
buf
- buffer to print outposition
- position of data (bytes) in buffer to start printingwords
- number of 32 bit words to print in hexlabel
- a label to print as headerpublic static void printBytes(java.nio.ByteBuffer buf, int position, int bytes, java.lang.String label)
buf
- buffer to print outposition
- position of data (bytes) in buffer to start printingbytes
- number of bytes to print in hexlabel
- a label to print as headerpublic static void printBytes(byte[] array, int offset, int bytes, java.lang.String label)
array
- byte array to print outoffset
- offset into array to start printingbytes
- number of bytes to print in hexlabel
- a label to print as headerpublic static void printBytes(java.lang.String fileName, long offset, int bytes, java.lang.String label)
fileName
- file to print outoffset
- offset into file to start printingbytes
- number of bytes to print in hexlabel
- a label to print as headerpublic static void readBytes(java.lang.String fileName, java.nio.ByteBuffer buf) throws EvioException
fileName
- file to read from.buf
- buffer to write into.EvioException
- if file not found, is a directory, or could not be read;
if bad file name or unable to do I/O.public static byte[] stringToBank(java.lang.String text, short tag, byte num, java.nio.ByteOrder order)
text
- string to use as data.tag
- bank's tag.num
- bank's num.order
- byte order of returned bank. If null use big endian.public static byte[] bankToBytes(EvioBank bank, java.nio.ByteOrder order)
bank
- bank to encode.order
- byte order of returned bank. If null use big endian.public static EvioEvent nodeToEvent(EvioNode node) throws EvioException
node
- EvioNode object to EvioEventEvioException
- if node is not a bank or cannot parse node's bufferpublic static int powerOfTwo(int x, boolean roundUp)
x
- value to get the power of 2 closest to.roundUp
- if true, round up, else downpublic static java.util.List<EvioEvent> toEvents(java.lang.String xmlString) throws EvioException
xmlString
- xml format string to parseEvioException
- if xml is not in proper formatpublic static java.util.List<EvioEvent> toEvents(java.lang.String xmlString, int maxEvents, int skip, EvioXMLDictionary dictionary, boolean debug) throws EvioException
xmlString
- xml format string to parsemaxEvents
- max number of events to returnskip
- number of events to initially skip overdictionary
- dictionary objectdebug
- true for debug outputEvioException
- if xml is not in proper formatpublic static DataType getDataType(java.lang.String type)
type
- evio data typepublic static java.lang.String toXML(EvioNode node, boolean hex) throws EvioException
node
- EvioNode object to print outhex
- if true, ints get displayed in hexadecimalEvioException
- if node is not a bank or cannot parse node's buffer