Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,17 @@ public class CrcComposer {

private int curCompositeCrc = 0;
private long curPositionInStripe = 0;
private ByteArrayOutputStream digestOut = new ByteArrayOutputStream();
private final ByteArrayOutputStream digestOut = new ByteArrayOutputStream();

/**
* Returns a CrcComposer which will collapse all ingested CRCs into a single
* value.
*
* @param type type.
* @param bytesPerCrcHint bytesPerCrcHint.
* @throws IOException raised on errors performing I/O.
* @return a CrcComposer which will collapse all ingested CRCs into a single value.
*/
public static CrcComposer newCrcComposer(
DataChecksum.Type type, long bytesPerCrcHint)
throws IOException {
public static CrcComposer newCrcComposer(DataChecksum.Type type, long bytesPerCrcHint) {
return newStripedCrcComposer(type, bytesPerCrcHint, Long.MAX_VALUE);
}

Expand All @@ -77,15 +74,12 @@ public static CrcComposer newCrcComposer(
* @param stripeLength stripeLength.
* @return a CrcComposer which will collapse CRCs for every combined.
* underlying data size which aligns with the specified stripe boundary.
* @throws IOException raised on errors performing I/O.
*/
public static CrcComposer newStripedCrcComposer(
DataChecksum.Type type, long bytesPerCrcHint, long stripeLength)
throws IOException {
int polynomial = org.apache.hadoop.ozone.client.checksum.CrcUtil.getCrcPolynomialForType(type);
public static CrcComposer newStripedCrcComposer(DataChecksum.Type type, long bytesPerCrcHint, long stripeLength) {
final int polynomial = CrcUtil.getCrcPolynomialForType(type);
return new CrcComposer(
polynomial,
org.apache.hadoop.ozone.client.checksum.CrcUtil.getMonomial(bytesPerCrcHint, polynomial),
CrcUtil.getMonomial(bytesPerCrcHint, polynomial),
bytesPerCrcHint,
stripeLength);
}
Expand Down Expand Up @@ -117,20 +111,17 @@ public static CrcComposer newStripedCrcComposer(
* @param offset offset.
* @param length must be a multiple of the expected byte-size of a CRC.
* @param bytesPerCrc bytesPerCrc.
* @throws IOException raised on errors performing I/O.
*/
public void update(
byte[] crcBuffer, int offset, int length, long bytesPerCrc)
throws IOException {
public void update(byte[] crcBuffer, int offset, int length, long bytesPerCrc) {
if (length % CRC_SIZE_BYTES != 0) {
throw new IOException(String.format(
throw new IllegalArgumentException(String.format(
"Trying to update CRC from byte array with length '%d' at offset "
+ "'%d' which is not a multiple of %d!",
length, offset, CRC_SIZE_BYTES));
}
int limit = offset + length;
while (offset < limit) {
int crcB = org.apache.hadoop.ozone.client.checksum.CrcUtil.readInt(crcBuffer, offset);
final int crcB = CrcUtil.readInt(crcBuffer, offset);
update(crcB, bytesPerCrc);
offset += CRC_SIZE_BYTES;
}
Expand Down Expand Up @@ -161,30 +152,27 @@ public void update(
*
* @param crcB crcB.
* @param bytesPerCrc bytesPerCrc.
* @throws IOException raised on errors performing I/O.
*/
public void update(int crcB, long bytesPerCrc) throws IOException {
public void update(int crcB, long bytesPerCrc) {
if (curCompositeCrc == 0) {
curCompositeCrc = crcB;
} else if (bytesPerCrc == bytesPerCrcHint) {
curCompositeCrc = org.apache.hadoop.ozone.client.checksum.CrcUtil.composeWithMonomial(
curCompositeCrc, crcB, precomputedMonomialForHint, crcPolynomial);
curCompositeCrc = CrcUtil.composeWithMonomial(curCompositeCrc, crcB, precomputedMonomialForHint, crcPolynomial);
} else {
curCompositeCrc = org.apache.hadoop.ozone.client.checksum.CrcUtil.compose(
curCompositeCrc, crcB, bytesPerCrc, crcPolynomial);
curCompositeCrc = CrcUtil.compose(curCompositeCrc, crcB, bytesPerCrc, crcPolynomial);
}

curPositionInStripe += bytesPerCrc;

if (curPositionInStripe > stripeLength) {
throw new IOException(String.format(
throw new IllegalStateException(String.format(
"Current position in stripe '%d' after advancing by bytesPerCrc '%d' "
+ "exceeds stripeLength '%d' without stripe alignment.",
curPositionInStripe, bytesPerCrc, stripeLength));
} else if (curPositionInStripe == stripeLength) {
// Hit a stripe boundary; flush the curCompositeCrc and reset for next
// stripe.
digestOut.write(org.apache.hadoop.ozone.client.checksum.CrcUtil.intToBytes(curCompositeCrc), 0, CRC_SIZE_BYTES);
digestOut.write(CrcUtil.intToBytes(curCompositeCrc), 0, CRC_SIZE_BYTES);
curCompositeCrc = 0;
curPositionInStripe = 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package org.apache.hadoop.ozone.client.checksum;

import java.io.IOException;
import java.util.Arrays;
import org.apache.hadoop.hdds.annotation.InterfaceAudience;
import org.apache.hadoop.hdds.annotation.InterfaceStability;
Expand Down Expand Up @@ -45,17 +44,15 @@ private CrcUtil() {
* @param type type.
* @return the int representation of the polynomial associated with the
* CRC {@code type}, suitable for use with further CRC arithmetic.
* @throws IOException if there is no CRC polynomial applicable
* to the given {@code type}.
*/
public static int getCrcPolynomialForType(DataChecksum.Type type) throws IOException {
public static int getCrcPolynomialForType(DataChecksum.Type type) {
switch (type) {
case CRC32:
return GZIP_POLYNOMIAL;
case CRC32C:
return CASTAGNOLI_POLYNOMIAL;
default:
throw new IOException(
throw new IllegalArgumentException(
"No CRC polynomial could be associated with type: " + type);
}
}
Expand Down Expand Up @@ -132,15 +129,7 @@ public static int compose(int crcA, int crcB, long lengthB, int mod) {
*/
public static byte[] intToBytes(int value) {
byte[] buf = new byte[4];
try {
writeInt(buf, 0, value);
} catch (IOException ioe) {
// Since this should only be able to occur from code bugs within this
// class rather than user input, we throw as a RuntimeException
// rather than requiring this method to declare throwing IOException
// for something the caller can't control.
throw new RuntimeException(ioe);
}
writeInt(buf, 0, value);
return buf;
}

Expand All @@ -152,16 +141,14 @@ public static byte[] intToBytes(int value) {
* @param buf buf size.
* @param offset offset.
* @param value value.
* @throws IOException raised on errors performing I/O.
*/
public static void writeInt(byte[] buf, int offset, int value)
throws IOException {
public static void writeInt(byte[] buf, int offset, int value) {
if (offset + 4 > buf.length) {
throw new IOException(String.format(
throw new ArrayIndexOutOfBoundsException(String.format(
"writeInt out of bounds: buf.length=%d, offset=%d",
buf.length, offset));
}
buf[offset + 0] = (byte)((value >>> 24) & 0xff);
buf[offset ] = (byte)((value >>> 24) & 0xff);
buf[offset + 1] = (byte)((value >>> 16) & 0xff);
buf[offset + 2] = (byte)((value >>> 8) & 0xff);
buf[offset + 3] = (byte)(value & 0xff);
Expand All @@ -174,20 +161,17 @@ public static void writeInt(byte[] buf, int offset, int value)
* @param offset offset.
* @param buf buf.
* @return int.
* @throws IOException raised on errors performing I/O.
*/
public static int readInt(byte[] buf, int offset)
throws IOException {
public static int readInt(byte[] buf, int offset) {
if (offset + 4 > buf.length) {
throw new IOException(String.format(
throw new ArrayIndexOutOfBoundsException(String.format(
"readInt out of bounds: buf.length=%d, offset=%d",
buf.length, offset));
}
int value = ((buf[offset + 0] & 0xff) << 24) |
return ((buf[offset ] & 0xff) << 24) |
((buf[offset + 1] & 0xff) << 16) |
((buf[offset + 2] & 0xff) << 8) |
((buf[offset + 3] & 0xff));
return value;
}

/**
Expand All @@ -196,13 +180,11 @@ public static int readInt(byte[] buf, int offset)
* formatted value.
*
* @param bytes bytes.
* @throws IOException raised on errors performing I/O.
* @return a list of hex formatted values.
*/
public static String toSingleCrcString(final byte[] bytes)
throws IOException {
public static String toSingleCrcString(final byte[] bytes) {
if (bytes.length != 4) {
throw new IOException((String.format(
throw new IllegalArgumentException((String.format(
"Unexpected byte[] length '%d' for single CRC. Contents: %s",
bytes.length, Arrays.toString(bytes))));
}
Expand All @@ -215,13 +197,11 @@ public static String toSingleCrcString(final byte[] bytes)
* hex formatted values.
*
* @param bytes bytes.
* @throws IOException raised on errors performing I/O.
* @return a list of hex formatted values.
*/
public static String toMultiCrcString(final byte[] bytes)
throws IOException {
public static String toMultiCrcString(final byte[] bytes) {
if (bytes.length % 4 != 0) {
throw new IOException((String.format(
throw new IllegalArgumentException((String.format(
"Unexpected byte[] length '%d' not divisible by 4. Contents: %s",
bytes.length, Arrays.toString(bytes))));
}
Expand Down
Loading