本文整理匯總了Java中com.google.common.primitives.Bytes.lastIndexOf方法的典型用法代碼示例。如果您正苦於以下問題:Java Bytes.lastIndexOf方法的具體用法?Java Bytes.lastIndexOf怎麽用?Java Bytes.lastIndexOf使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.google.common.primitives.Bytes
的用法示例。
在下文中一共展示了Bytes.lastIndexOf方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: validateAndStripChecksum
import com.google.common.primitives.Bytes; //導入方法依賴的package包/類
/**
* extracts the checksum of the source, calculates a new one on the cut off source and compares them.
* returns the cut off data only if the checksums matched
*
* @param source
* @return
*/
protected byte[] validateAndStripChecksum(byte[] source) {
// retrieve checksum
int lastIndexOf = Bytes.lastIndexOf(source, (byte) SEPARATOR);
if (lastIndexOf < 0) {
throw new RuntimeException("no checksum was found on source: " + new String(source));
}
long checkSum = Long.parseLong(new String(Arrays.copyOfRange(source, lastIndexOf + 1, source.length)));
byte[] data = Arrays.copyOfRange(source, 0, lastIndexOf);
CRC32 crc32 = new CRC32();
crc32.update(data);
long calculatedCheckSum = crc32.getValue();
// validate checksum
if (Long.compare(checkSum, calculatedCheckSum) != 0) {
throw new RuntimeException("checksums do not match! calculated: " + calculatedCheckSum + ", provided: " + checkSum);
}
return data;
}