List of usage examples for java.util.zip CRC32 update
@Override public void update(ByteBuffer buffer)
From source file:com.haulmont.cuba.core.app.FoldersServiceBean.java
protected ArchiveEntry newStoredEntry(String name, byte[] data) { ZipArchiveEntry zipEntry = new ZipArchiveEntry(name); zipEntry.setSize(data.length);/* w w w . ja v a2 s . co m*/ zipEntry.setCompressedSize(zipEntry.getSize()); CRC32 crc32 = new CRC32(); crc32.update(data); zipEntry.setCrc(crc32.getValue()); return zipEntry; }
From source file:it.jnrpe.net.JNRPEProtocolPacket.java
/** * Validates the packet CRC.//from w w w . j a v a 2 s . c o m * * @throws BadCRCException * If the CRC can't be validated */ public void validate() throws BadCRCException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); DataOutputStream dout = new DataOutputStream(bout); try { dout.writeShort(packetVersion); dout.writeShort(packetTypeCode); dout.writeInt(0); // NO CRC dout.writeShort(resultCode); dout.write(byteBufferAry); dout.write(dummyBytesAry); dout.close(); byte[] vBytes = bout.toByteArray(); CRC32 crcAlg = new CRC32(); crcAlg.update(vBytes); if (!(((int) crcAlg.getValue()) == crcValue)) { throw new BadCRCException("Bad CRC"); } } catch (IOException e) { // Never happens... throw new IllegalStateException(e.getMessage(), e); } }
From source file:org.phpmaven.phar.PharJavaPackager.java
private void packFile(final ByteArrayOutputStream fileEntriesBaos, final ByteArrayOutputStream compressedFilesBaos, final File fileToPack, String filePath) throws IOException { if (DEBUG) {/*ww w . j ava2 s. c o m*/ System.out.println("Packing file " + fileToPack + " with " + fileToPack.length() + " bytes."); } final byte[] fileBytes = filePath.getBytes("UTF-8"); writeIntLE(fileEntriesBaos, fileBytes.length); fileEntriesBaos.write(fileBytes); // TODO Complain with files larger than 4 bytes file length writeIntLE(fileEntriesBaos, (int) fileToPack.length()); writeIntLE(fileEntriesBaos, (int) (fileToPack.lastModified() / 1000)); final byte[] uncompressed = FileUtils.readFileToByteArray(fileToPack); if (DEBUG) { System.out.println("read " + uncompressed.length + " bytes from file."); } final ByteArrayOutputStream compressedStream = new ByteArrayOutputStream(); // final GZIPOutputStream gzipStream = new GZIPOutputStream(compressedStream); // gzipStream.write(uncompressed); // gzipStream.flush(); final CRC32 checksum = new CRC32(); checksum.update(uncompressed); final Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true); deflater.setInput(uncompressed); deflater.finish(); final byte[] buf = new byte[Short.MAX_VALUE]; while (!deflater.needsInput()) { final int bytesRead = deflater.deflate(buf); compressedStream.write(buf, 0, bytesRead); } final byte[] compressed = compressedStream.toByteArray(); if (DEBUG) { System.out.println("compressed to " + compressed.length + " bytes."); } // final Inflater decompresser = new Inflater(); // decompresser.setInput(compressed); // byte[] result = new byte[5000]; // try { // int resultLength = decompresser.inflate(result); // final String str = new String(result, 0, resultLength); // int i = 42; // } catch (DataFormatException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // decompresser.end(); compressedFilesBaos.write(compressed); writeIntLE(fileEntriesBaos, compressed.length); writeIntLE(fileEntriesBaos, checksum.getValue()); // bits: 0x00001000, gzip fileEntriesBaos.write(0); fileEntriesBaos.write(0x10); fileEntriesBaos.write(0); fileEntriesBaos.write(0); // 0 bytes manifest writeIntLE(fileEntriesBaos, 0); }
From source file:eu.semlibproject.annotationserver.managers.UtilsManager.java
/** * Compute the CRC32 checksum of a String. This can be useful to * short an URL.//from w w w. j a v a 2 s . com * * @param text the text from which the checksum will be computed * @return the CRC32 checksum */ public String CRC32(String text) { CRC32 checksumer = new CRC32(); checksumer.update(text.getBytes()); String finalhash = Long.toHexString(checksumer.getValue()); // correctly format the finalHash (e.g. number starting with 00 that was stripped) return StringUtils.leftPad(finalhash, 8, "0"); }
From source file:org.apache.hadoop.hdfs.TestRaidDfs.java
public static long createTestFilePartialLastBlock(FileSystem fileSys, Path name, int repl, int numBlocks, long blocksize) throws IOException { CRC32 crc = new CRC32(); Random rand = new Random(); FSDataOutputStream stm = fileSys.create(name, true, fileSys.getConf().getInt("io.file.buffer.size", 4096), (short) repl, blocksize); // Write whole blocks. byte[] b = new byte[(int) blocksize]; for (int i = 1; i < numBlocks; i++) { rand.nextBytes(b);/*from www .j a v a 2 s . c o m*/ stm.write(b); crc.update(b); } // Write partial block. b = new byte[(int) blocksize / 2 - 1]; rand.nextBytes(b); stm.write(b); crc.update(b); stm.close(); return crc.getValue(); }
From source file:org.apache.hadoop.hdfs.TestRaidDfs.java
public static long createTestFile(FileSystem fileSys, Path name, int repl, int numBlocks, long blocksize) throws IOException { CRC32 crc = new CRC32(); Random rand = new Random(); FSDataOutputStream stm = fileSys.create(name, true, fileSys.getConf().getInt("io.file.buffer.size", 4096), (short) repl, blocksize); // fill random data into file final byte[] b = new byte[(int) blocksize]; for (int i = 0; i < numBlocks; i++) { rand.nextBytes(b);/*w w w .j a v a 2 s .c o m*/ stm.write(b); crc.update(b); } stm.close(); return crc.getValue(); }
From source file:uk.ac.cam.cl.dtg.isaac.dos.eventbookings.PgEventBookings.java
/** * Acquire a globally unique database lock. * This method will block until the lock is released. * Any locks must be released manually./*from w ww. j ava 2 s .c o m*/ * * @param resourceId - the unique id for the object to be locked. */ @Override public void acquireDistributedLock(final String resourceId) throws SegueDatabaseException { // generate 32 bit CRC based on table id and resource id so that is is more likely to be unique globally. CRC32 crc = new CRC32(); crc.update((TABLE_NAME + resourceId).getBytes()); // acquire lock try (Connection conn = ds.getDatabaseConnection()) { PreparedStatement pst; pst = conn.prepareStatement("SELECT pg_advisory_lock(?)"); pst.setLong(1, crc.getValue()); log.debug(String.format("Acquiring advisory lock on %s (%s)", TABLE_NAME + resourceId, crc.getValue())); pst.executeQuery(); } catch (SQLException e) { String msg = String.format("Unable to acquire lock for event (%s).", resourceId); log.error(msg); throw new SegueDatabaseException(msg); } log.debug(String.format("Acquired advisory lock on %s (%s)", TABLE_NAME + resourceId, crc.getValue())); }
From source file:uk.ac.cam.cl.dtg.isaac.dos.eventbookings.PgEventBookings.java
/** * Release a globally unique database lock. * This method will release a previously acquired lock. * * @param resourceId - the unique id for the object to be locked. *///from w w w.j ava2 s . c o m @Override public void releaseDistributedLock(final String resourceId) throws SegueDatabaseException { // generate 32 bit CRC based on table id and resource id so that is is more likely to be unique globally. CRC32 crc = new CRC32(); crc.update((TABLE_NAME + resourceId).getBytes()); // acquire lock try (Connection conn = ds.getDatabaseConnection()) { PreparedStatement pst; pst = conn.prepareStatement("SELECT pg_advisory_unlock(?)"); pst.setLong(1, crc.getValue()); log.debug(String.format("Releasing advisory lock on %s (%s)", TABLE_NAME + resourceId, crc.getValue())); pst.executeQuery(); } catch (SQLException e) { String msg = String.format("Unable to release lock for event (%s).", resourceId); log.error(msg); throw new SegueDatabaseException(msg); } log.debug(String.format("Released advisory lock on %s (%s)", TABLE_NAME + resourceId, crc.getValue())); }
From source file:org.apache.hadoop.raid.TestDirectoryRaidEncoder.java
private void printFileCRC(FileSystem fs, Path file, long bufferSize) throws IOException { byte[] buffer = new byte[(int) bufferSize]; FSDataInputStream stm = fs.open(file); StringBuilder sb = new StringBuilder(); sb.append("CRC for file: " + file + " size " + fs.getFileStatus(file).getLen() + "\n"); while (stm.read(buffer) >= 0) { CRC32 crc = new CRC32(); crc.update(buffer); sb.append(" " + crc.getValue()); }//from www . j a va 2s . c o m sb.append("\n"); System.out.println(sb.toString()); stm.close(); }
From source file:org.apache.lucene.store.Directory.java
public synchronized String getCacheKey(String[] filelist) { CacheKeyBuffer kkk = new CacheKeyBuffer(filelist, this.dir_uuid, System.currentTimeMillis() / 600000l, this.getP()); String rtn = CACHE_BUFFER.get(kkk); if (rtn == null) { StringBuffer buff = new StringBuffer(); buff.append(this.getClass().getName()).append("_"); buff.append(this.dir_uuid).append("_"); CRC32 crc32 = new CRC32(); crc32.update(0); long filesize = 0; if (filelist != null) { buff.append(filelist.length).append("_"); for (String s : filelist) { crc32.update(new String(s).getBytes()); try { filesize += this.fileLength(s); } catch (Throwable e) { logger.error("filelength", e); }/*from w w w.j a va2 s . c o m*/ } } long crcvalue = crc32.getValue(); buff.append(crcvalue).append("_"); buff.append(filesize).append("_"); rtn = buff.toString(); CACHE_BUFFER.put(kkk, rtn); } return rtn; }