Java Code Examples for java.util.zip.CRC32#getValue()

The following examples show how to use java.util.zip.CRC32#getValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BlobTest.java    From gemfirexd-oss with Apache License 2.0 7 votes vote down vote up
/**
 * 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();
}
 
Example 2
Source File: ZipUtil.java    From atlas with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 3
Source File: ZipUtil.java    From GPT with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example 4
Source File: ZipUtil.java    From letv with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 5
Source File: ProtocolUtils.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
    }
}
 
Example 6
Source File: BigJar.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example 7
Source File: BigJar.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example 8
Source File: T6836682.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example 9
Source File: IndexFileEntryTest.java    From HaloDB with Apache License 2.0 5 votes vote down vote up
@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]);
}
 
Example 10
Source File: SimpleScrambler.java    From geoportal-server-harvester with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
  }
}
 
Example 11
Source File: M2Model.java    From alfresco-data-model with GNU Lesser General Public License v3.0 5 votes vote down vote up
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();
}
 
Example 12
Source File: CacheAggregator.java    From OpenRS with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 13
Source File: JoH.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
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();
}
 
Example 14
Source File: ProtocolUtils.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
    }
}
 
Example 15
Source File: SheetMusicActivity.java    From MidiSheetMusic-Android with GNU General Public License v3.0 4 votes vote down vote up
/** 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();
}
 
Example 16
Source File: V5PacketEncoder.java    From feeyo-hlsserver with Apache License 2.0 4 votes vote down vote up
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;
	
}
 
Example 17
Source File: Utils.java    From dbys with GNU General Public License v3.0 4 votes vote down vote up
public static int crc32(byte[] bytes) {
    CRC32 checksum = new CRC32();
    checksum.update(bytes);
    return (int)checksum.getValue();
}
 
Example 18
Source File: SpillableMapUtils.java    From hudi with Apache License 2.0 4 votes vote down vote up
/**
 * 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();
}
 
Example 19
Source File: Utils.java    From waltz with Apache License 2.0 4 votes vote down vote up
public static int checksum(byte[] data, int offset, int length) {
    CRC32 crc32 = new CRC32();
    crc32.update(data, offset, length);
    return (int) crc32.getValue();
}
 
Example 20
Source File: DataStore.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * 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();
}