下面列出了java.util.zip.CRC32#getValue ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
static long computeCrcOfCentralDir(RandomAccessFile raf, CentralDirectory dir)
throws IOException {
CRC32 crc = new CRC32();
long stillToRead = dir.size;
raf.seek(dir.offset);
int length = (int) Math.min(BUFFER_SIZE, stillToRead);
byte[] buffer = new byte[BUFFER_SIZE];
length = raf.read(buffer, 0, length);
while (length != -1) {
crc.update(buffer, 0, length);
stillToRead -= length;
if (stillToRead == 0) {
break;
}
length = (int) Math.min(BUFFER_SIZE, stillToRead);
length = raf.read(buffer, 0, length);
}
return crc.getValue();
}
static long computeCrcOfCentralDir(RandomAccessFile raf, CentralDirectory dir) throws IOException {
CRC32 crc = new CRC32();
long stillToRead = dir.size;
raf.seek(dir.offset);
byte[] buffer = new byte[16384];
int length = raf.read(buffer, 0, (int) Math.min(16384, stillToRead));
while (length != -1) {
crc.update(buffer, 0, length);
stillToRead -= (long) length;
if (stillToRead == 0) {
break;
}
length = raf.read(buffer, 0, (int) Math.min(16384, stillToRead));
}
return crc.getValue();
}
/**
* computeCrcOfCentralDir
*
* @param raf RandomAccessFile
* @param dir CentralDirectory
* @return crc
* @throws IOException
*/
static long computeCrcOfCentralDir(RandomAccessFile raf, CentralDirectory dir)
throws IOException {
CRC32 crc = new CRC32();
long stillToRead = dir.size;
raf.seek(dir.offset);
int length = (int) Math.min(BUFFER_SIZE, stillToRead);
byte[] buffer = new byte[BUFFER_SIZE];
length = raf.read(buffer, 0, length);
while (length != -1) {
crc.update(buffer, 0, length);
stillToRead -= length;
if (stillToRead == 0) {
break;
}
length = (int) Math.min(BUFFER_SIZE, stillToRead);
length = raf.read(buffer, 0, length);
}
return crc.getValue();
}
/**
* Get the checksum of a stream, reading its contents entirely and closing it.
*/
private long getStreamCheckSum(InputStream in) throws Exception {
CRC32 sum = new CRC32();
byte[] buf = new byte[32 * 1024];
for (;;) {
int read = in.read(buf);
if (read == -1) {
break;
}
sum.update(buf, 0, read);
}
in.close();
return sum.getValue();
}
/**
* Generate crc32 for WriteOp.
*/
public static Long writeOpCRC32(String stream, byte[] payload) {
CRC32 crc = requestCRC.get();
try {
crc.update(stream.getBytes(UTF_8));
crc.update(payload);
return crc.getValue();
} finally {
crc.reset();
}
}
public static boolean checkChecksum(byte[] bytes) {
if ((bytes == null) || (bytes.length < 4)) return false;
final CRC32 crc = new CRC32();
crc.update(bytes, 0, bytes.length - 4);
final long buffer_crc = UnsignedInts.toLong(ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getInt(bytes.length - 4));
return buffer_crc == crc.getValue();
}
private static boolean isRepackingRequired(Cache store, Entry entry, int type, int file) {
ByteBuffer buffer;
try {
buffer = store.getStore().read(type, file);
} catch (IOException ex) {
return true;
}
if (buffer.capacity() <= 2) {
return true;
}
/* last two bytes are the version and shouldn't be included */
byte[] bytes = new byte[buffer.limit() - 2];
buffer.position(0);
buffer.get(bytes, 0, bytes.length);
CRC32 crc = new CRC32();
crc.update(bytes, 0, bytes.length);
if ((int) crc.getValue() != entry.getCrc()) {
return true;
}
buffer.position(buffer.limit() - 2);
if ((buffer.getShort() & 0xFFFF) != entry.getVersion()) {
return true;
}
return false;
}
public long getChecksum(ModelDefinition.XMLBindingType bindingType)
{
final CRC32 crc = new CRC32();
// construct the crc directly from the model's xml stream
toXML(bindingType, new OutputStream() {
public void write(int b) throws IOException
{
crc.update(b);
}
});
return crc.getValue();
}
/**
* Encodes string.
* @param txt string to encode
* @return encoded string or <code>null</code> if error encoding string
*/
public static String encode(String txt) {
txt = StringUtils.defaultIfEmpty(txt, "");
try {
CRC32 crC32 = new CRC32();
crC32.update(txt.getBytes("UTF-8"));
long crc = crC32.getValue();
String crctxt = String.format("%10d%s", crc, txt);
Base64.Encoder encoder = Base64.getEncoder();
return encoder.encodeToString(crctxt.getBytes("UTF-8"));
} catch (UnsupportedEncodingException ex) {
return null;
}
}
/**
* Generate crc32 for TruncateOp.
*/
public static Long truncateOpCRC32(String stream, DLSN dlsn) {
CRC32 crc = requestCRC.get();
try {
crc.update(stream.getBytes(UTF_8));
crc.update(dlsn.serializeBytes());
return crc.getValue();
} finally {
crc.reset();
}
}
@Test
public void serializeIndexFileEntry() {
byte[] key = TestUtils.generateRandomByteArray(8);
int recordSize = 1024;
int recordOffset = 10240;
byte keySize = (byte) key.length;
long sequenceNumber = 100;
int version = 200;
ByteBuffer header = ByteBuffer.allocate(INDEX_FILE_HEADER_SIZE);
header.put(VERSION_OFFSET, (byte)version);
header.put(KEY_SIZE_OFFSET, keySize);
header.putInt(RECORD_SIZE_OFFSET, recordSize);
header.putInt(RECORD_OFFSET, recordOffset);
header.putLong(SEQUENCE_NUMBER_OFFSET, sequenceNumber);
CRC32 crc32 = new CRC32();
crc32.update(header.array(), VERSION_OFFSET, INDEX_FILE_HEADER_SIZE-CHECKSUM_SIZE);
crc32.update(key);
long checkSum = crc32.getValue();
header.putInt(CHECKSUM_OFFSET, Utils.toSignedIntFromLong(checkSum));
IndexFileEntry entry = new IndexFileEntry(key, recordSize, recordOffset, sequenceNumber, version, -1);
ByteBuffer[] buffers = entry.serialize();
Assert.assertEquals(header, buffers[0]);
Assert.assertEquals(ByteBuffer.wrap(key), buffers[1]);
}
static long computeCRC(long minlength) {
CRC32 crc = new CRC32();
byte[] buffer = new byte[BUFFER_LEN];
long count = getCount(minlength);
for (long i = 0; i < count; i++) {
crc.update(buffer);
}
return crc.getValue();
}
long computeCRC(File inFile) throws IOException {
byte[] buffer = new byte[8192];
CRC32 crc = new CRC32();
try (FileInputStream fis = new FileInputStream(inFile);
BufferedInputStream bis = new BufferedInputStream(fis)) {
int n = bis.read(buffer);
while (n > 0) {
crc.update(buffer, 0, n);
n = bis.read(buffer);
}
}
return crc.getValue();
}
long computeCRC(long minlength) {
CRC32 crc = new CRC32();
byte[] buffer = new byte[BUFFER_LEN];
long count = getCount(minlength);
for (long i = 0; i < count; i++) {
crc.update(buffer);
}
return crc.getValue();
}
/**
* Generate a checksum for a given set of bytes.
*/
public static long generateChecksum(byte[] data) {
CRC32 crc = new CRC32();
crc.update(data);
return crc.getValue();
}
/** Create this SheetMusicActivity.
* The Intent should have two parameters:
* - data: The uri of the midi file to open.
* - MidiTitleID: The title of the song (String)
*/
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
// Hide the navigation bar before the views are laid out
hideSystemUI();
setContentView(R.layout.sheet_music_layout);
// Keep screen on
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
ClefSymbol.LoadImages(this);
TimeSigSymbol.LoadImages(this);
// Parse the MidiFile from the raw bytes
Uri uri = this.getIntent().getData();
if (uri == null) {
this.finish();
return;
}
String title = this.getIntent().getStringExtra(MidiTitleID);
if (title == null) {
title = uri.getLastPathSegment();
}
FileUri file = new FileUri(uri, title);
this.setTitle("MidiSheetMusic: " + title);
byte[] data;
try {
data = file.getData(this);
midifile = new MidiFile(data, title);
}
catch (MidiFileException e) {
this.finish();
return;
}
// Initialize the settings (MidiOptions).
// If previous settings have been saved, use those
options = new MidiOptions(midifile);
CRC32 crc = new CRC32();
crc.update(data);
midiCRC = crc.getValue();
SharedPreferences settings = getPreferences(0);
options.scrollVert = settings.getBoolean("scrollVert", false);
options.shade1Color = settings.getInt("shade1Color", options.shade1Color);
options.shade2Color = settings.getInt("shade2Color", options.shade2Color);
options.showPiano = settings.getBoolean("showPiano", true);
String json = settings.getString("" + midiCRC, null);
MidiOptions savedOptions = MidiOptions.fromJson(json);
if (savedOptions != null) {
options.merge(savedOptions);
}
createViews();
}
public List<V5Packet> encode(int mtu, int packetSender, byte packetType, byte[] packetReserved, int packetId, byte[] data) {
//
if ( mtu <= V5Packet.HEAD_LENGTH + V5Packet.TAIL_LENGTH ) {
throw new java.lang.IllegalArgumentException(" mtu must be greater than packet header size , "
+ " headSize=" + V5Packet.HEAD_LENGTH + ", tailSize=" + V5Packet.TAIL_LENGTH );
}
//1、根据原始的包长和MTU值 来分成几个碎片包
int maxLen = mtu - V5Packet.HEAD_LENGTH - V5Packet.TAIL_LENGTH;
int n = data.length / maxLen;
if ( (data.length % maxLen) != 0 )
n++;
//2、构造碎片包
List<V5Packet> packs = new ArrayList<V5Packet>(n);
for (int i = 0; i < n; i++) {
int dataOffset = i * maxLen;
int dataLength = (i < (n - 1)) ? maxLen : data.length - i * maxLen;
//
byte[] packetData = new byte[ dataLength ];
//
int idx = 0;
int start = dataOffset;
int end = dataOffset + dataLength;
for (int j = start; j < end; j++) {
packetData[idx++] = data[j];
}
// 包尾,crc
CRC32 crc32 = new CRC32();
crc32.update( packetData, 0, packetData.length);
long crc = crc32.getValue();
int packetLength = data.length;
int packetOffset = i * maxLen;
packs.add( new V5Packet(packetSender, packetType, packetReserved, packetId, packetLength, packetOffset, packetData, crc) );
}
return packs;
}
public static int crc32(byte[] bytes) {
CRC32 checksum = new CRC32();
checksum.update(bytes);
return (int)checksum.getValue();
}
public static int checksum(byte[] data, int offset, int length) {
CRC32 crc32 = new CRC32();
crc32.update(data, offset, length);
return (int) crc32.getValue();
}
/**
* Gets a hash code for the supplied data.
*
* @param data The data to calculate the hash code for.
* @return A hash code for the supplied data.
*/
private int getDataHash(byte[] data) {
CRC32 crc32 = new CRC32();
crc32.update(data);
return (int) crc32.getValue();
}