Skip to content

Commit

Permalink
fix endianness
Browse files Browse the repository at this point in the history
  • Loading branch information
haileyajohnson committed Sep 30, 2022
1 parent 87f7285 commit ca18de7
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions cdm/core/src/main/java/ucar/nc2/filter/Checksum32.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public byte[] encode(byte[] dataIn) {
int dataStart = this.type == CType.FLETCHER ? 0 : nbytes;
System.arraycopy(dataIn, 0, dataOut, dataStart, dataIn.length);
int checksumStart = this.type == CType.FLETCHER ? dataOut.length - nbytes : 0;
System.arraycopy(Ints.toByteArray(checksum), 0, dataOut, checksumStart, nbytes);;
// encode as little endian by default
System.arraycopy(Ints.toByteArray(Integer.reverseBytes(checksum)), 0, dataOut, checksumStart, nbytes);;
return dataOut;
}

Expand All @@ -76,7 +77,7 @@ public byte[] decode(byte[] dataIn) {
byte[] bytes = new byte[nbytes];
int checksumStart = this.type == CType.FLETCHER ? dataIn.length - nbytes : 0;
System.arraycopy(dataIn, checksumStart, bytes, 0, nbytes);
int i = Ints.fromByteArray(bytes);
int i = Integer.reverseBytes(Ints.fromByteArray(bytes)); // convert from little endian
if (i != checksum) {
throw new RuntimeException("Checksum invalid");
}
Expand Down Expand Up @@ -145,7 +146,7 @@ public void update(byte[] b, int off, int len) {

@Override
public long getValue() {
return Integer.reverseBytes((int) ((sum2 << 16) | sum1));
return (sum2 << 16) | sum1;
}
}

Expand Down

0 comments on commit ca18de7

Please sign in to comment.