List of usage examples for java.util.zip CRC32 CRC32
public CRC32()
From source file:com.esri.geoportal.harvester.api.base.SimpleScrambler.java
/** * Decodes string./* w w w.j a v a 2 s . c o m*/ * @param encoded encoded string to decode * @return decoded string or <code>null</code> if error decoding string */ public static String decode(String encoded) { try { encoded = StringUtils.defaultIfEmpty(encoded, ""); Base64.Decoder decoder = Base64.getDecoder(); String crctxt = new String(decoder.decode(encoded), "UTF-8"); if (crctxt.length() < 10) { return null; } long crc = Long.parseLong(StringUtils.trimToEmpty(crctxt.substring(0, 10))); String txt = crctxt.substring(10); CRC32 crC32 = new CRC32(); crC32.update(txt.getBytes("UTF-8")); if (crc != crC32.getValue()) { return null; } return txt; } catch (NumberFormatException | UnsupportedEncodingException ex) { return null; } }
From source file:org.apache.mnemonic.ChunkBufferNGTest.java
@Test public void testGenChunkBuffers() { Checksum cs = new CRC32(); cs.reset();/* ww w.ja v a 2s . c om*/ NonVolatileMemAllocator act = new NonVolatileMemAllocator( Utils.getNonVolatileMemoryAllocatorService("pmalloc"), 1024 * 1024 * 1024L, "./pmchunkbuffertest.dat", true); act.setChunkReclaimer(new Reclaim<Long>() { @Override public boolean reclaim(Long mres, Long sz) { System.out.println(String.format("Reclaim Memory Chunk: %X Size: %s", System.identityHashCode(mres), null == sz ? "NULL" : sz.toString())); return false; } }); DurableChunk<NonVolatileMemAllocator> mch; mch = act.createChunk(1000 * 1024 * 1024L); Assert.assertNotNull(mch); act.setHandler(m_keyid, mch.getHandler()); long bufcnt = mch.getSize() / m_bufsize; ChunkBuffer ckbuf; byte[] rdbytes; for (long idx = 0; idx < bufcnt; ++idx) { // System.err.println(String.format("---- bufcnt: %d, bufsize: %d, idx: %d", bufcnt, m_bufsize, idx)); ckbuf = mch.getChunkBuffer(idx * m_bufsize, m_bufsize); Assert.assertNotNull(ckbuf); rdbytes = RandomUtils.nextBytes(m_bufsize); Assert.assertNotNull(rdbytes); ckbuf.get().clear(); ckbuf.get().put(rdbytes); cs.update(rdbytes, 0, rdbytes.length); } m_checksum = cs.getValue(); m_count = bufcnt; act.close(); }
From source file:org.trellisldp.rosid.file.FileUtils.java
/** * Partition an identifier into a directory structure * @param identifier the identifier/*from w w w . j ava2 s.com*/ * @return a string usable as a directory path */ public static String partition(final String identifier) { requireNonNull(identifier, "identifier must not be null!"); final StringJoiner joiner = new StringJoiner(separator); final CRC32 hasher = new CRC32(); hasher.update(identifier.getBytes(UTF_8)); final String intermediate = Long.toHexString(hasher.getValue()); range(0, intermediate.length() / LENGTH).limit(MAX) .forEach(i -> joiner.add(intermediate.substring(i * LENGTH, (i + 1) * LENGTH))); joiner.add(md5Hex(identifier)); return joiner.toString(); }
From source file:com.littcore.io.util.ZipUtils.java
public static void zip(File srcFileOrPath, File targetFileNamePath) throws IOException { //?//from w ww.jav a 2s .co m CheckedOutputStream cs = new CheckedOutputStream(new FileOutputStream(targetFileNamePath), new CRC32()); //zip? ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(cs)); out.setEncoding("GBK"); zip(out, srcFileOrPath, ""); out.close(); }
From source file:org.codice.ddf.checksum.impl.CRC32ChecksumProvider.java
@Override public String calculateChecksum(InputStream inputStream) throws IOException { if (inputStream == null) { throw new IllegalArgumentException("InputStream cannot be null"); }//w w w.j a va2 s .c o m byte[] bytes = IOUtils.toByteArray(inputStream); Checksum checksum = new CRC32(); checksum.update(bytes, 0, bytes.length); long checkSumValue = checksum.getValue(); return Long.toHexString(checkSumValue); }
From source file:com.arcanix.php.phar.PharEntry.java
public PharEntry(final File file, final String localPath, final PharCompression pharCompression) throws IOException { this.file = file; this.localPath = localPath; this.pharCompression = pharCompression; byte[] uncompressedBytes = Files.readAllBytes(this.file.toPath()); this.checksum = new CRC32(); this.checksum.update(uncompressedBytes); ByteArrayOutputStream compressed = new ByteArrayOutputStream(); OutputStream compressor = null; try {/*from ww w .j av a 2s. c o m*/ compressor = getCompressorOutputStream(compressed); Files.copy(this.file.toPath(), compressor); compressor.flush(); } finally { compressor.close(); } this.compressedBytes = compressed.toByteArray(); }
From source file:JarUtil.java
/** * Writes all given files to the specified jar-file. * /*from w ww . j a va 2 s .c om*/ * @param files * all files that should be added to the JAR file * @param sourceDir * The parent directory containing the given files. * @param target * The jar file which should be created * @param compress * True when the jar file should be compressed * @throws FileNotFoundException * when a file could not be found * @throws IOException * when a file could not be read or the jar file could not be * written to. */ public static void jar(File sourceDir, OutputStream target, boolean compress) throws IOException { File[] files = sourceDir.listFiles(); // creates target-jar-file: JarOutputStream out = new JarOutputStream(target); if (compress) { out.setLevel(ZipOutputStream.DEFLATED); } else { out.setLevel(ZipOutputStream.STORED); } // create a CRC32 object: CRC32 crc = new CRC32(); byte[] buffer = new byte[1024 * 1024]; // add all files: int sourceDirLength = sourceDir.getAbsolutePath().length() + 1; for (File file : files) { addFile(file, out, crc, sourceDirLength, buffer); } out.close(); }
From source file:org.openmrs.module.sync.server.ConnectionResponse.java
/** * // w ww . ja va 2s. co m * @param inputStream * @throws SyncException */ public ConnectionResponse(InputStream is, boolean useCompression) throws SyncException { try { this.useCompression = useCompression; this.cis = new CheckedInputStream(is, new CRC32()); if (this.useCompression) { GZIPInputStream zis = new GZIPInputStream(new BufferedInputStream(cis)); this.responsePayload = IOUtils.toString(zis, "UTF-8"); IOUtils.closeQuietly(zis); IOUtils.closeQuietly(cis); log.info("********************** CHECKSUM: " + cis.getChecksum().getValue()); this.checksum = cis.getChecksum().getValue(); } else { this.responsePayload = IOUtils.toString(cis, "UTF-8"); } log.info("Response compressed: " + useCompression); //log.info("Response input: " + is.toString()); //log.info("Response data: " + this.responsePayload); log.info("Response checksum: " + this.checksum); this.setState(ServerConnectionState.OK); } catch (IOException e) { //throw new SyncException(e); log.error("An error occurred while unzipping response", e); } }
From source file:name.npetrovski.jphar.DataEntry.java
/** * Create entry from file/*from w w w. j av a2s . c o m*/ * */ public static DataEntry createFromFile(File file, Compression.Type compression) throws IOException { EntryManifest em = new EntryManifest(); em.setCompression(new Compression(compression)); em.setTimestamp((int) file.lastModified() / 1000); em.getPath().setName(file.toPath().toString().replace("\\", "/")); DataEntry entry = new DataEntry(em); entry.setSource(file.getCanonicalFile()); if (file.isDirectory()) { em.getCompression().setType(Compression.Type.NONE); } else { byte[] data = Files.readAllBytes(file.toPath()); CRC32 crc = new CRC32(); crc.update(data); em.setCRC32((int) crc.getValue()); em.setUncompressedSize(data.length); } return entry; }
From source file:org.smartfrog.services.www.bulkio.client.SunJavaBulkIOClient.java
@Override public long doUpload(String method, long ioSize) throws IOException, InterruptedException { validateURL();//from ww w . j av a 2 s. c o m CRC32 checksum = new CRC32(); URL targetUrl = getUrl(); getLog().info("Uploading " + ioSize + " bytes to " + targetUrl); HttpURLConnection connection = openConnection(); connection.setRequestMethod(method); connection.setRequestProperty(HttpHeaders.CONTENT_LENGTH, Long.toString(ioSize)); connection.setDoOutput(true); maybeSetChunking(connection); connection.connect(); OutputStream stream = connection.getOutputStream(); long bytes = 0; try { for (bytes = 0; bytes < ioSize; bytes++) { int octet = (AbstractBulkioServlet.getByteFromCounter(bytes)); stream.write(octet); checksum.update(octet); if (interrupted) { throw new InterruptedException( "Interrupted after sending " + bytes + " bytes" + " to " + targetUrl); } } } finally { closeQuietly(stream); } getLog().info("Upload complete, checking results"); checkStatusCode(targetUrl, connection, HttpURLConnection.HTTP_OK); long expectedChecksum = checksum.getValue(); getLog().info("Uploaded " + bytes + " bytes to " + targetUrl + " checksum=" + expectedChecksum); if (bytes != ioSize) { throw new IOException( "Wrong content length uploaded to " + targetUrl + " : put " + ioSize + " but got " + bytes); } if (parseResults) { InputStream inStream = null; Properties props = new Properties(); try { inStream = connection.getInputStream(); props.load(inStream); } finally { closeQuietly(inStream); } long actualChecksum = getLongPropValue(props, IoAttributes.CHECKSUM); if (actualChecksum != expectedChecksum) { throw new IOException("Expected the checksum from upload of " + ioSize + " bytes " + " to " + targetUrl + "to be " + expectedChecksum + " but got " + actualChecksum + "\n Properties: " + props.toString()); } } return ioSize; }