Skip to content

Commit

Permalink
fix problems with composite data endianness
Browse files Browse the repository at this point in the history
  • Loading branch information
carltimmer committed Aug 19, 2024
1 parent 56996bd commit 2358032
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 64 deletions.
84 changes: 48 additions & 36 deletions java/org/jlab/coda/jevio/CompositeData.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,14 @@ private CompositeData() {}
*
* @param format format String defining data
* @param data data in given format
* @param byteOrder byte order of resulting data.
*
* @throws EvioException data or format arg = null;
* if improper format string
*/
public CompositeData(String format, CompositeData.Data data)
public CompositeData(String format, CompositeData.Data data, ByteOrder byteOrder)
throws EvioException {
this(format, data.formatTag, data, data.dataTag, data.dataNum);
this(format, data.formatTag, data, data.dataTag, data.dataNum, byteOrder);
}


Expand All @@ -142,18 +143,20 @@ public CompositeData(String format, CompositeData.Data data)
* @param data data in given format
* @param dataTag tag used in bank containing data
* @param dataNum num used in bank containing data
* @param byteOrder byte order of resulting data.
*
* @throws EvioException data or format arg = null;
* if improper format string
*/
public CompositeData(String format, int formatTag,
CompositeData.Data data, int dataTag, int dataNum)
CompositeData.Data data, int dataTag, int dataNum,
ByteOrder byteOrder)
throws EvioException {

boolean debug = false;

this.format = format;
byteOrder = ByteOrder.BIG_ENDIAN;
this.byteOrder = byteOrder;

if (debug) System.out.println("Analyzing composite data:");

Expand Down Expand Up @@ -568,9 +571,11 @@ static public byte[] generateRawBytes(CompositeData[] data, ByteOrder order) thr
len = cd.getRawBytes().length;
if (cd.byteOrder != order) {
// This CompositeData object has a rawBytes array of the wrong byte order, so swap it
swapAll(cd.getRawBytes(), 0, rawBytes, offset, len/4, order);
//System.out.println("CompositeData::generateRawBytes call swapAll(), data in " + cd.byteOrder);
swapAll(cd.getRawBytes(), 0, rawBytes, offset, len/4, cd.byteOrder);
}
else {
//System.out.println("CompositeData::generateRawBytes call arraycopy()");
System.arraycopy(cd.getRawBytes(), 0, rawBytes, offset, len);
}
offset += len;
Expand Down Expand Up @@ -1641,7 +1646,7 @@ public void swap() throws EvioException {
* @param length length of data array in 32 bit words
* @param srcOrder the byte order of data in src
*
* @throws EvioException if offsets or length < 0; if src = null;
* @throws EvioException if offsets < 0; if length < 4; if src = null;
* if src or dest is too small
*/
public static void swapAll (byte[] src, int srcOff, byte[] dest, int destOff,
Expand All @@ -1658,21 +1663,24 @@ public static void swapAll (byte[] src, int srcOff, byte[] dest, int destOff,
inPlace = true;
}

if (srcOff < 0 || destOff < 0 || length < 0) {
throw new EvioException("offsets or length must be >= 0");
if (srcOff < 0 || destOff < 0) {
throw new EvioException("offsets must be >= 0");
}

if (length < 4) {
throw new EvioException("length must be >= 4");
}

// Byte order of swapped data
ByteOrder destOrder = (srcOrder == ByteOrder.BIG_ENDIAN) ?
ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN;


// How many unused bytes are left in the src array?
int totalBytes = 4*length;
int srcBytesLeft = totalBytes;

// How many bytes taken for this CompositeData object?
int dataOffset = 0;
int dataOff = 0;

// Wrap input & output arrays in ByteBuffers for convenience
ByteBuffer srcBuffer = ByteBuffer.wrap( src, srcOff, 4*length);
Expand All @@ -1682,11 +1690,13 @@ public static void swapAll (byte[] src, int srcOff, byte[] dest, int destOff,
srcBuffer.order(srcOrder);
destBuffer.order(destOrder);

Utilities.printBytes(src, srcOff, 32, "CD src bytes");

while (srcBytesLeft > 0) {
//System.out.println("start src offset = " + (srcOff + dataOffset));
//System.out.println("start src offset = " + (srcOff + dataOff));

// First read the tag segment header
TagSegmentHeader tsHeader = EventParser.createTagSegmentHeader(src, srcOff + dataOffset, srcOrder);
TagSegmentHeader tsHeader = EventParser.createTagSegmentHeader(src, srcOff + dataOff, srcOrder);
int headerLen = tsHeader.getHeaderLength();
int dataLength = tsHeader.getLength() - (headerLen - 1);

Expand All @@ -1701,10 +1711,10 @@ public static void swapAll (byte[] src, int srcOff, byte[] dest, int destOff,
tsHeader.write(destBuffer);

// Move to beginning of string data
dataOffset += 4*headerLen;
dataOff += 4*headerLen;

// Read the format string it contains
String[] strs = BaseStructure.unpackRawBytesToStrings(src, srcOff + dataOffset,
String[] strs = BaseStructure.unpackRawBytesToStrings(src, srcOff + dataOff,
4*(tsHeader.getLength()));

if (strs.length < 1) {
Expand All @@ -1721,21 +1731,23 @@ public static void swapAll (byte[] src, int srcOff, byte[] dest, int destOff,
// Char data does not get swapped but needs
// to be copied if not swapping in place.
if (!inPlace) {
System.arraycopy(src, srcOff + dataOffset,
dest, destOff + dataOffset, 4*dataLength);
System.arraycopy(src, srcOff + dataOff,
dest, destOff + dataOff, 4*dataLength);
}

// Move to beginning of bank header
dataOffset += 4*dataLength;
dataOff += 4*dataLength;

// Read the data bank header
BankHeader bHeader = EventParser.createBankHeader(src, srcOff + dataOffset, srcOrder);
BankHeader bHeader = EventParser.createBankHeader(src, srcOff + dataOff, srcOrder);
headerLen = bHeader.getHeaderLength();
dataLength = bHeader.getLength() - (headerLen - 1);

//System.out.println("swapAll: bank len = " + bHeader.getLength() + ", dataLen = " + dataLength +
//", tag = " + bHeader.getTag() + ", num = " + bHeader.getNumber() + ", type = " + bHeader.getDataTypeName() +
//", pad = " + bHeader.getPadding());
// ", tag = " + bHeader.getTag() +
// ", num = " + bHeader.getNumber() +
// ", type = " + bHeader.getDataTypeName() +
// ", pad = " + bHeader.getPadding());

// Oops, no data
if (dataLength < 1) {
Expand All @@ -1748,25 +1760,25 @@ public static void swapAll (byte[] src, int srcOff, byte[] dest, int destOff,
dataLength = 4*dataLength - padding;

// Got all we needed from the bank header, now swap as it's written out.
destBuffer.position(destOff + dataOffset);
destBuffer.position(destOff + dataOff);
bHeader.write(destBuffer);

// Move to beginning of data
dataOffset += 4*headerLen;
srcBuffer.position( srcOff + dataOffset);
destBuffer.position(destOff + dataOffset);
dataOff += 4*headerLen;
srcBuffer.position( srcOff + dataOff);
destBuffer.position(destOff + dataOff);

// Swap data
swapData(srcBuffer, destBuffer, dataLength, formatInts);

// Set buffer positions and offset
dataOffset += dataLength;
srcBuffer.position( srcOff + dataOffset);
destBuffer.position(srcOff + dataOffset);
dataOff += dataLength;
srcBuffer.position( srcOff + dataOff);
destBuffer.position(srcOff + dataOff);

srcBytesLeft = totalBytes - (dataOffset + padding);
srcBytesLeft = totalBytes - (dataOff + padding);

//System.out.println("bytes left = " + srcBytesLeft + ",offset = " + dataOffset + ", padding = " + padding);
//System.out.println("bytes left = " + srcBytesLeft + ",offset = " + dataOff + ", padding = " + padding);
//System.out.println("src pos = " + srcBuffer.position() + ", dest pos = " + destBuffer.position());
}

Expand Down Expand Up @@ -1811,10 +1823,10 @@ static void swapAll(ByteBuffer srcBuffer, ByteBuffer destBuffer,
// Bytes to swap
int totalBytes = 4*len;
int srcBytesLeft = totalBytes;
int dataOffset, byteLen;
int dataOff, byteLen;

// Initialize
dataOffset = 0;
dataOff = 0;

while (srcBytesLeft > 0) {

Expand All @@ -1825,7 +1837,7 @@ static void swapAll(ByteBuffer srcBuffer, ByteBuffer destBuffer,
// Move to beginning of string data
srcPos += 4;
destPos += 4;
dataOffset += 4;
dataOff += 4;

// Read the format string it contains
String[] strs = BaseStructure.unpackRawBytesToStrings(srcBuffer, srcPos, 4*node.dataLen);
Expand Down Expand Up @@ -1855,7 +1867,7 @@ static void swapAll(ByteBuffer srcBuffer, ByteBuffer destBuffer,
// Move to beginning of bank header
srcPos += byteLen;
destPos += byteLen;
dataOffset += byteLen;
dataOff += byteLen;

// Read & swap data bank header
ByteDataTransformer.swapBankHeader(node, srcBuffer, destBuffer, srcPos, destPos);
Expand All @@ -1868,7 +1880,7 @@ static void swapAll(ByteBuffer srcBuffer, ByteBuffer destBuffer,
// Move to beginning of bank data
srcPos += 8;
destPos += 8;
dataOffset += 8;
dataOff += 8;

// Bank data length in bytes
byteLen = 4*node.dataLen;
Expand All @@ -1879,8 +1891,8 @@ static void swapAll(ByteBuffer srcBuffer, ByteBuffer destBuffer,
// Move past bank data
srcPos += byteLen;
destPos += byteLen;
dataOffset += byteLen;
srcBytesLeft = totalBytes - dataOffset;
dataOff += byteLen;
srcBytesLeft = totalBytes - dataOff;

//System.out.println("bytes left = " + srcBytesLeft);
//System.out.println("src pos = " + srcBuffer.position() + ", dest pos = " + destBuffer.position());
Expand Down
3 changes: 2 additions & 1 deletion java/org/jlab/coda/jevio/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -1522,7 +1522,8 @@ else if (name.equalsIgnoreCase("paren")) {
else if (name.equalsIgnoreCase("comp") && (cDataCount+1) >= level.nData) {
// System.out.println("Got last CompositeData item");
for (int i=0; i < level.nData; i++) {
cdArray[i] = new CompositeData(formats[i], cData[i]);
cdArray[i] = new CompositeData(formats[i], cData[i],
level.bs.byteOrder);
}
level.bs.appendCompositeData(cdArray);
return;
Expand Down
17 changes: 11 additions & 6 deletions java/org/jlab/coda/jevio/test/CompositeTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public static void main6(String args[]) {

// // Create composite object
//System.out.println("Call CompositeData()");
// CompositeData cData2 = new CompositeData(byteArray, ByteOrder.LITTLE_ENDIAN);
// CompositeData cData2 = new CompositeData(byteArray, ByteOrder.BIG_ENDIAN);

// // print swapped data
// System.out.println("After making CompositeData object DATA:");
Expand Down Expand Up @@ -190,7 +190,8 @@ public static void main1(String args[]) {
// Create CompositeData object
CompositeData cData = null;
try {
cData = new CompositeData(format, 1, myData, 0 ,0);
cData = new CompositeData(format, 1, myData, 0 ,0,
ByteOrder.BIG_ENDIAN);
}
catch (EvioException e) {
e.printStackTrace();
Expand Down Expand Up @@ -244,7 +245,8 @@ public static void main2(String args[]) {
// Create CompositeData object
CompositeData cData = null;
try {
cData = new CompositeData(format, 1, myData, 0 ,0);
cData = new CompositeData(format, 1, myData, 0 ,0,
ByteOrder.BIG_ENDIAN);
}
catch (EvioException e) {
e.printStackTrace();
Expand Down Expand Up @@ -311,9 +313,12 @@ public static void main(String args[]) {
// Create CompositeData object
CompositeData[] cData = new CompositeData[3];
try {
cData[0] = new CompositeData(format, 1, myData1, 1 ,1);
cData[1] = new CompositeData(format, 2, myData2, 2 ,2);
cData[2] = new CompositeData(format, 3, myData3, 3 ,3);
cData[0] = new CompositeData(format, 1, myData1, 1 ,1,
ByteOrder.BIG_ENDIAN);
cData[1] = new CompositeData(format, 2, myData2, 2 ,2,
ByteOrder.BIG_ENDIAN);
cData[2] = new CompositeData(format, 3, myData3, 3 ,3,
ByteOrder.BIG_ENDIAN);
}
catch (EvioException e) {
e.printStackTrace();
Expand Down
3 changes: 2 additions & 1 deletion java/org/jlab/coda/jevio/test/ReadWriteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,8 @@ static ByteBuffer generateEvioBuffer(ByteOrder order, int tag, int num) throws E

// Create CompositeData object
CompositeData[] cData = new CompositeData[1];
cData[0] = new CompositeData(format, 1, myData, 1, 1);
cData[0] = new CompositeData(format, 1, myData, 1, 1,
ByteOrder.BIG_ENDIAN);

// Add to bank
builder.addCompositeData(cData);
Expand Down
3 changes: 2 additions & 1 deletion java/org/jlab/coda/jevio/test/ReadWriteV4Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ static ByteBuffer generateEvioBuffer(ByteOrder order, int tag, int num) throws E

// Create CompositeData object
CompositeData[] cData = new CompositeData[1];
cData[0] = new CompositeData(format, 1, myData, 1, 1);
cData[0] = new CompositeData(format, 1, myData,
1, 1, order);

// Add to bank
builder.addCompositeData(cData);
Expand Down
14 changes: 8 additions & 6 deletions java/org/jlab/coda/jevio/test/SwapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.File;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;

Expand Down Expand Up @@ -333,12 +334,13 @@ static CompositeData[] createCompositeData() {
// Create CompositeData array
CompositeData[] cData = new CompositeData[5];
try {
cData[0] = new CompositeData(format1, 1, myData1, 1, 1);
cData[1] = new CompositeData(format2, 2, myData2, 2, 2);
cData[2] = new CompositeData(format3, 3, myData3, 3, 3);
// cData[3] = new CompositeData(format4, 4, myData4, 4, 4);
cData[3] = new CompositeData(format5, 5, myData5, 5, 5);
cData[4] = new CompositeData(format6, 6, myData6, 6, 6);
ByteOrder order = ByteOrder.BIG_ENDIAN;
cData[0] = new CompositeData(format1, 1, myData1, 1, 1, order);
cData[1] = new CompositeData(format2, 2, myData2, 2, 2, order);
cData[2] = new CompositeData(format3, 3, myData3, 3, 3, order);
// cData[3] = new CompositeData(format4, 4, myData4, 4, 4, order);
cData[3] = new CompositeData(format5, 5, myData5, 5, 5, order);
cData[4] = new CompositeData(format6, 6, myData6, 6, 6, order);
}
catch (EvioException e) {
e.printStackTrace();
Expand Down
Loading

0 comments on commit 2358032

Please sign in to comment.