Skip to content

Commit

Permalink
add first event length arg to constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
carltimmer committed Dec 17, 2024
1 parent 322be2e commit 4a39a74
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
36 changes: 30 additions & 6 deletions java/org/jlab/coda/hipo/Writer.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public class Writer implements AutoCloseable {
private ByteBuffer dictionaryFirstEventBuffer;
/** Evio format "first" event to store in file header's user header. */
private byte[] firstEvent;
/** Length in bytes of firstEvent. */
private int firstEventLength;

/** Byte order of data to write to file/buffer. */
private ByteOrder byteOrder = ByteOrder.LITTLE_ENDIAN;
Expand Down Expand Up @@ -149,7 +151,7 @@ public Writer(ByteOrder order, int maxEventCount, int maxBufferSize) {
* Value of < 8MB results in default of 8MB.
*/
public Writer(HeaderType hType, ByteOrder order, int maxEventCount, int maxBufferSize) {
this(hType, order, maxEventCount, maxBufferSize, null, null,
this(hType, order, maxEventCount, maxBufferSize, null, null, 0,
CompressionType.RECORD_UNCOMPRESSED, false);
}

Expand All @@ -168,11 +170,13 @@ public Writer(HeaderType hType, ByteOrder order, int maxEventCount, int maxBuffe
* @param dictionary string holding an evio format dictionary to be placed in userHeader.
* @param firstEvent byte array containing an evio event to be included in userHeader.
* It must be in the same byte order as the order argument.
* @param firstEventLen number of valid bytes in firstEvent. If 0, then if firstEvent is not null,
* this is set to firstEvent.length.
* @param compType type of data compression to use.
* @param addTrailerIndex if true, we add a record index to the trailer.
*/
public Writer(HeaderType hType, ByteOrder order, int maxEventCount, int maxBufferSize,
String dictionary, byte[] firstEvent,
String dictionary, byte[] firstEvent, int firstEventLen,
CompressionType compType, boolean addTrailerIndex) {

if (order != null) {
Expand All @@ -182,6 +186,15 @@ public Writer(HeaderType hType, ByteOrder order, int maxEventCount, int maxBuffe
this.firstEvent = firstEvent;
this.compressionType = compType;
this.addTrailerIndex = addTrailerIndex;

if (firstEvent == null) {
firstEventLen = 0;
}
else if (firstEventLen < 1) {
firstEventLen = firstEvent.length;
}
firstEventLength = firstEventLen;

headerBuffer.order(byteOrder);

// Create a place to store records currently being written
Expand Down Expand Up @@ -261,7 +274,7 @@ public Writer(String filename, ByteOrder order, int maxEventCount, int maxBuffer
* @throws HipoException if buf arg is null.
*/
public Writer(ByteBuffer buf) throws HipoException {
this(buf, 0, 0, null, null);
this(buf, 0, 0, null, null, 0);
}

/**
Expand All @@ -272,7 +285,7 @@ public Writer(ByteBuffer buf) throws HipoException {
* @throws HipoException if buf arg is null.
*/
public Writer(ByteBuffer buf, byte[] userHeader) throws HipoException {
this(buf, 0, 0, null, null);
this(buf, 0, 0, null, null, 0);
open(buf, userHeader, 0, userHeader.length);
}

Expand All @@ -289,10 +302,13 @@ public Writer(ByteBuffer buf, byte[] userHeader) throws HipoException {
* @param dictionary string holding an evio format dictionary to be placed in userHeader.
* @param firstEvent byte array containing an evio event to be included in userHeader.
* It must be in the same byte order as the order argument.
* @param firstEventLen number of valid bytes in firstEvent. If 0, then if firstEvent is not null,
* this is set to firstEvent.length.
* @throws HipoException if buf arg is null.
*/
public Writer(ByteBuffer buf, int maxEventCount, int maxBufferSize,
String dictionary, byte[] firstEvent) throws HipoException {
String dictionary, byte[] firstEvent, int firstEventLen)
throws HipoException {

if (buf == null) {
throw new HipoException("buf arg is null");
Expand All @@ -304,6 +320,14 @@ public Writer(ByteBuffer buf, int maxEventCount, int maxBufferSize,

this.dictionary = dictionary;
this.firstEvent = firstEvent;
if (firstEvent == null) {
firstEventLen = 0;
}
else if (firstEventLen < 1) {
firstEventLen = firstEvent.length;
}
firstEventLength = firstEventLen;

outputRecord = new RecordOutputStream(byteOrder, maxEventCount, maxBufferSize, CompressionType.RECORD_UNCOMPRESSED);

haveDictionary = dictionary != null;
Expand Down Expand Up @@ -652,7 +676,7 @@ else if (dictionaryFirstEventBuffer != null) {
* Null if both are null.
*/
private ByteBuffer createDictionaryRecord() {
return createRecord(dictionary, firstEvent, firstEvent.length,
return createRecord(dictionary, firstEvent, firstEventLength,
byteOrder, fileHeader, null);
}

Expand Down
2 changes: 1 addition & 1 deletion java/org/jlab/coda/hipo/test/ReadWriteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ static void writeFile(String finalFilename) {
ByteOrder order = ByteOrder.LITTLE_ENDIAN;

Writer writer = new Writer(HeaderType.EVIO_FILE, order, 0, 0,
dictionary, firstEvent, compType,
dictionary, firstEvent, firstEventLen, compType,
addTrailerIndex);

byte[] userHdr = new byte[10];
Expand Down
4 changes: 2 additions & 2 deletions java/org/jlab/coda/jevio/test/FileTestVer6.java
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ public static void main(String args[]) {

if (useFile) {
writer = new Writer(HeaderType.EVIO_FILE, order,
5, targetBlockBytes, dictionary, efArray,
5, targetBlockBytes, dictionary, efArray, 0,
CompressionType.RECORD_UNCOMPRESSED, true);

writer.getFileHeader().setUserIntFirst(111);
Expand All @@ -639,7 +639,7 @@ public static void main(String args[]) {
// Create an event writer to write to buffer
myBuf = ByteBuffer.allocate(10000);
myBuf.order(order);
writer = new Writer(myBuf, 10, targetBlockBytes, null, null);
writer = new Writer(myBuf, 10, targetBlockBytes, null, null, 0);
}

if (useFile) {
Expand Down
2 changes: 1 addition & 1 deletion java/org/jlab/coda/jevio/test/ReadWriteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ static void writeAndReadBuffer() throws EvioException, HipoException, IOExceptio
// We cannot write compressed data into a buffer directly, but we can write it to a file
// and then read the file back into a buffer (minus the file header).
Writer writer = new Writer(HeaderType.EVIO_FILE, order, 0, 0,
null, null, compType, false);
null, null, 0, compType, false);
writer.open("./temp");


Expand Down

0 comments on commit 4a39a74

Please sign in to comment.