/** * Length of all the fixed size fields part of the record length header except status and record length. * @return fixedRecordLength */ private int getFixedRecordLength() { // record header length + current time + sequence number + checksum + GTRID size + GTRID + unique names count + end record marker return 4 + 8 + 4 + 4 + 1 + gtrid.length() + 4 + 4; }
/** * Length of all the fixed size fields part of the record length header except status and record length. * @return fixedRecordLength */ private int getFixedRecordLength() { // record header length + current time + sequence number + checksum + GTRID size + GTRID + unique names count + end record marker return 4 + 8 + 4 + 4 + 1 + gtrid.length() + 4 + 4; }
/** * Calculate the CRC32 value of this record. * @return the CRC32 value of this record. */ public int calculateCrc32() { int total = 0; for (String uniqueName : uniqueNames) { total += 2 + uniqueName.length(); // 2 bytes for storing the unique name length + unique name length } recordLength = total + getFixedRecordLength(); ByteBuffer buf = ByteBuffer.allocate(24 + gtrid.length() + 4 /*uniqueNames.size*/ + total + 4 /*endRecord*/); buf.putInt(status); // offset: 0 buf.putInt(recordLength); // offset: 4 buf.putInt(headerLength); // offset: 8 buf.putLong(time); // offset: 12 buf.putInt(sequenceNumber); // offset: 20 buf.put(gtrid.getArray()); // offset: 24 buf.putInt(uniqueNames.size()); // offset: 24 + gtridArray.length for (String name : uniqueNames) { buf.putShort((short) name.length()); buf.put(name.getBytes(US_ASCII)); } buf.putInt(endRecord); CRC32 crc32 = new CRC32(); crc32.update(buf.array()); return (int) crc32.getValue(); }
/** * Calculate the CRC32 value of this record. * @return the CRC32 value of this record. */ public int calculateCrc32() { int total = 0; for (String uniqueName : uniqueNames) { total += 2 + uniqueName.length(); // 2 bytes for storing the unique name length + unique name length } recordLength = total + getFixedRecordLength(); ByteBuffer buf = ByteBuffer.allocate(24 + gtrid.length() + 4 /*uniqueNames.size*/ + total + 4 /*endRecord*/); buf.putInt(status); // offset: 0 buf.putInt(recordLength); // offset: 4 buf.putInt(headerLength); // offset: 8 buf.putLong(time); // offset: 12 buf.putInt(sequenceNumber); // offset: 20 buf.put(gtrid.getArray()); // offset: 24 buf.putInt(uniqueNames.size()); // offset: 24 + gtridArray.length for (String name : uniqueNames) { buf.putShort((short) name.length()); buf.put(name.getBytes(US_ASCII)); } buf.putInt(endRecord); CRC32 crc32 = new CRC32(); crc32.update(buf.array()); return (int) crc32.getValue(); }