List of usage examples for java.util.zip Adler32 Adler32
public Adler32()
From source file:org.anarres.lzo.LzopOutputStream.java
/** * Writes an lzop-compatible header to the OutputStream provided. *//* w w w . ja v a 2 s . c o m*/ protected void writeLzopHeader() throws IOException { DataOutputBuffer dob = new DataOutputBuffer(); try { dob.writeShort(LzopConstants.LZOP_VERSION); dob.writeShort(LzoVersion.LZO_LIBRARY_VERSION); dob.writeShort(LzopConstants.LZOP_COMPAT_VERSION); switch (getAlgorithm()) { case LZO1X: // case LZO1X_1: dob.writeByte(LzopConstants.M_LZO1X_1); dob.writeByte(5); break; /* case LZO1X_15: dob.writeByte(LzopConstants.M_LZO1X_1_15); dob.writeByte(1); break; case LZO1X_999: dob.writeByte(LzopConstants.M_LZO1X_999); dob.writeByte(9); break; */ default: throw new IOException("Incompatible lzop algorithm " + getAlgorithm()); } long mask = LzopConstants.F_ADLER32_C | LzopConstants.F_ADLER32_D; mask = mask | LzopConstants.F_CRC32_C | LzopConstants.F_CRC32_D; dob.writeInt((int) (flags & mask & 0xFFFFFFFF)); // all flags 0 dob.writeInt(33188); // mode dob.writeInt((int) (System.currentTimeMillis() / 1000)); // mtime dob.writeInt(0); // gmtdiff ignored dob.writeByte(0); // no filename Adler32 headerChecksum = new Adler32(); headerChecksum.update(dob.getData(), 0, dob.getLength()); int hc = (int) headerChecksum.getValue(); dob.writeInt(hc); out.write(LzopConstants.LZOP_MAGIC); out.write(dob.getData(), 0, dob.getLength()); } finally { dob.close(); } }
From source file:org.mule.transports.vfs.VFSReceiver.java
protected boolean hasChanged(FileObject fileObject) { boolean changed = false; String key = fileObject.getName().getPath(); long checksum = 0; if (checksumMap.containsKey(key)) { checksum = ((Long) checksumMap.get(key)).longValue(); }/*w w w . jav a2 s . com*/ long newChecksum = 0; CheckedInputStream checkedInputStream = null; try { InputStream inputStream = fileObject.getContent().getInputStream(); checkedInputStream = new CheckedInputStream(inputStream, new Adler32()); int bufferSize = 1; if (inputStream.available() > 0) { bufferSize = inputStream.available(); } byte[] buffer = new byte[bufferSize]; while (checkedInputStream.read(buffer) > -1) { ; } newChecksum = checkedInputStream.getChecksum().getValue(); if (newChecksum != checksum) { if (logger.isDebugEnabled()) { logger.debug("calculated a new checksum of " + newChecksum); } checksumMap.put(key, new Long(newChecksum)); changed = true; } } catch (IOException e) { connector.handleException(e); } return changed; }
From source file:com.atolcd.web.scripts.ZipContents.java
public void createZipFile(List<String> nodeIds, OutputStream os, boolean noaccent) throws IOException { File zip = null;//from w w w . jav a2s.c om try { if (nodeIds != null && !nodeIds.isEmpty()) { zip = TempFileProvider.createTempFile(TEMP_FILE_PREFIX, ZIP_EXTENSION); FileOutputStream stream = new FileOutputStream(zip); CheckedOutputStream checksum = new CheckedOutputStream(stream, new Adler32()); BufferedOutputStream buff = new BufferedOutputStream(checksum); ZipArchiveOutputStream out = new ZipArchiveOutputStream(buff); out.setEncoding(encoding); out.setMethod(ZipArchiveOutputStream.DEFLATED); out.setLevel(Deflater.BEST_COMPRESSION); if (logger.isDebugEnabled()) { logger.debug("Using encoding '" + encoding + "' for zip file."); } try { for (String nodeId : nodeIds) { NodeRef node = new NodeRef(storeRef, nodeId); addToZip(node, out, noaccent, ""); } } catch (Exception e) { logger.error(e.getMessage(), e); throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, e.getMessage()); } finally { out.close(); buff.close(); checksum.close(); stream.close(); if (nodeIds.size() > 0) { InputStream in = new FileInputStream(zip); try { byte[] buffer = new byte[BUFFER_SIZE]; int len; while ((len = in.read(buffer)) > 0) { os.write(buffer, 0, len); } } finally { IOUtils.closeQuietly(in); } } } } } catch (Exception e) { logger.error(e.getMessage(), e); throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, e.getMessage()); } finally { // try and delete the temporary file if (zip != null) { zip.delete(); } } }
From source file:org.apache.zookeeper.server.persistence.TxnLogToolkit.java
public void dump(Scanner scanner) throws Exception { crcFixed = 0;//w w w. j av a 2s .co m FileHeader fhdr = new FileHeader(); fhdr.deserialize(logStream, "fileheader"); if (fhdr.getMagic() != TXNLOG_MAGIC) { throw new TxnLogToolkitException(ExitCode.INVALID_INVOCATION.getValue(), "Invalid magic number for %s", txnLogFile.getName()); } System.out.println("ZooKeeper Transactional Log File with dbid " + fhdr.getDbid() + " txnlog format version " + fhdr.getVersion()); if (recoveryMode) { fhdr.serialize(recoveryOa, "fileheader"); recoveryFos.flush(); filePadding.setCurrentSize(recoveryFos.getChannel().position()); } int count = 0; while (true) { long crcValue; byte[] bytes; try { crcValue = logStream.readLong("crcvalue"); bytes = logStream.readBuffer("txnEntry"); } catch (EOFException e) { System.out.println("EOF reached after " + count + " txns."); return; } if (bytes.length == 0) { // Since we preallocate, we define EOF to be an // empty transaction System.out.println("EOF reached after " + count + " txns."); return; } Checksum crc = new Adler32(); crc.update(bytes, 0, bytes.length); if (crcValue != crc.getValue()) { if (recoveryMode) { if (!force) { printTxn(bytes, "CRC ERROR"); if (askForFix(scanner)) { crcValue = crc.getValue(); ++crcFixed; } } else { crcValue = crc.getValue(); printTxn(bytes, "CRC FIXED"); ++crcFixed; } } else { printTxn(bytes, "CRC ERROR"); } } if (!recoveryMode || verbose) { printTxn(bytes); } if (logStream.readByte("EOR") != 'B') { throw new TxnLogToolkitException(ExitCode.UNEXPECTED_ERROR.getValue(), "Last transaction was partial."); } if (recoveryMode) { filePadding.padFile(recoveryFos.getChannel()); recoveryOa.writeLong(crcValue, "crcvalue"); recoveryOa.writeBuffer(bytes, "txnEntry"); recoveryOa.writeByte((byte) 'B', "EOR"); } count++; } }
From source file:net.sourceforge.jaulp.file.checksum.ChecksumUtils.java
/** * Gets the checksum from the given file. If the flag crc is true than the CheckedInputStream is * constructed with an instance of <code>java.util.zip.CRC32</code> otherwise with an instance * of <code>java.util.zip.Adler32</code>. * * @param file//from w w w.ja v a 2 s . com * The file The file from what to get the checksum. * @param crc * The crc If the flag crc is true than the CheckedInputStream is constructed with an * instance of {@link java.util.zip.CRC32} object otherwise it is constructed with an * instance of * @return The checksum from the given file as long. * @throws FileNotFoundException * Is thrown if the file is not found. * @throws IOException * Signals that an I/O exception has occurred. {@link java.util.zip.CRC32} object * otherwise it is constructed with an instance of {@link java.util.zip.Adler32} * object. {@link java.util.zip.Adler32} object. */ public static long getChecksum(File file, boolean crc) throws FileNotFoundException, IOException { CheckedInputStream cis = null; if (crc) { cis = new CheckedInputStream(new FileInputStream(file), new CRC32()); } else { cis = new CheckedInputStream(new FileInputStream(file), new Adler32()); } int length = (int) file.length(); byte[] buffer = new byte[length]; long checksum = 0; while (cis.read(buffer) >= 0) { checksum = cis.getChecksum().getValue(); } checksum = cis.getChecksum().getValue(); StreamUtils.closeInputStream(cis); return checksum; }
From source file:com.panet.imeta.core.row.ValueDataUtil.java
public static Long ChecksumAdler32(ValueMetaInterface metaA, Object dataA) { long checksum = 0; FileObject file = null;//w ww .j av a 2 s .c o m try { file = KettleVFS.getFileObject(dataA.toString()); CheckedInputStream cis = null; // Computer Adler-32 checksum cis = new CheckedInputStream((FileInputStream) ((LocalFile) file).getInputStream(), new Adler32()); byte[] buf = new byte[128]; while (cis.read(buf) >= 0) { } checksum = cis.getChecksum().getValue(); } catch (Exception e) { //throw new Exception(e); } finally { if (file != null) try { file.close(); } catch (Exception e) { } ; } return checksum; }
From source file:org.openossad.util.core.row.ValueDataUtil.java
public static Long ChecksumAdler32(ValueMetaInterface metaA, Object dataA) { long checksum = 0; FileObject file = null;// w w w . jav a 2 s. com try { file = OpenDESIGNERVFS.getFileObject(dataA.toString()); CheckedInputStream cis = null; // Computer Adler-32 checksum cis = new CheckedInputStream((FileInputStream) ((LocalFile) file).getInputStream(), new Adler32()); byte[] buf = new byte[128]; while (cis.read(buf) >= 0) { } checksum = cis.getChecksum().getValue(); } catch (Exception e) { //throw new Exception(e); } finally { if (file != null) try { file.close(); } catch (Exception e) { } ; } return checksum; }
From source file:net.sourceforge.pmd.cache.AbstractAnalysisCache.java
private long computeClassPathHash(final URL... classpathEntry) { final Adler32 adler32 = new Adler32(); for (final URL url : classpathEntry) { try (CheckedInputStream inputStream = new CheckedInputStream(url.openStream(), adler32)) { // Just read it, the CheckedInputStream will update the checksum on it's own while (IOUtils.skip(inputStream, Long.MAX_VALUE) == Long.MAX_VALUE) { // just loop }/* w ww . ja va 2 s. c om*/ } catch (final FileNotFoundException ignored) { LOG.warning("Auxclasspath entry " + url.toString() + " doesn't exist, ignoring it"); } catch (final IOException e) { // Can this even happen? LOG.log(Level.SEVERE, "Incremental analysis can't check auxclasspath contents", e); throw new RuntimeException(e); } } return adler32.getValue(); }
From source file:de.alpharogroup.file.checksum.ChecksumUtils.java
/** * Gets the checksum from the given file. If the flag crc is true than the CheckedInputStream is * constructed with an instance of <code>java.util.zip.CRC32</code> otherwise with an instance * of <code>java.util.zip.Adler32</code>. * * @param file//from www . j a va2 s . c o m * The file The file from what to get the checksum. * @param crc * The crc If the flag crc is true than the CheckedInputStream is constructed with an * instance of {@link java.util.zip.CRC32} object otherwise it is constructed with an * instance of * @return The checksum from the given file as long. * @throws FileNotFoundException * Is thrown if the file is not found. * @throws IOException * Signals that an I/O exception has occurred. {@link java.util.zip.CRC32} object * otherwise it is constructed with an instance of {@link java.util.zip.Adler32} * object. {@link java.util.zip.Adler32} object. */ public static long getChecksum(final File file, final boolean crc) throws FileNotFoundException, IOException { CheckedInputStream cis = null; if (crc) { cis = new CheckedInputStream(new FileInputStream(file), new CRC32()); } else { cis = new CheckedInputStream(new FileInputStream(file), new Adler32()); } final int length = (int) file.length(); final byte[] buffer = new byte[length]; long checksum = 0; while (cis.read(buffer) >= 0) { checksum = cis.getChecksum().getValue(); } checksum = cis.getChecksum().getValue(); StreamUtils.closeInputStream(cis); return checksum; }
From source file:org.akvo.flow.service.DataSyncService.java
private ZipFileData formZip(long surveyInstanceId) { ZipFileData zipFileData = new ZipFileData(); StringBuilder surveyBuf = new StringBuilder(); // Hold the responses in the StringBuilder String uuid = processSurveyData(surveyInstanceId, surveyBuf, zipFileData.imagePaths); // THe filename will match the Survey Instance UUID File zipFile = new File(FileUtil.getFilesDir(FileType.DATA), uuid + ConstantUtil.ARCHIVE_SUFFIX); // Write the data into the zip file try {//from ww w . j av a2 s.c o m String fileName = zipFile.getAbsolutePath();// Will normalize filename. zipFileData.filename = fileName; Log.i(TAG, "Creating zip file: " + fileName); FileOutputStream fout = new FileOutputStream(zipFile); CheckedOutputStream checkedOutStream = new CheckedOutputStream(fout, new Adler32()); ZipOutputStream zos = new ZipOutputStream(checkedOutStream); writeTextToZip(zos, surveyBuf.toString(), SURVEY_DATA_FILE); String signingKeyString = mProps.getProperty(SIGNING_KEY_PROP); if (!StringUtil.isNullOrEmpty(signingKeyString)) { MessageDigest sha1Digest = MessageDigest.getInstance("SHA1"); byte[] digest = sha1Digest.digest(surveyBuf.toString().getBytes("UTF-8")); SecretKeySpec signingKey = new SecretKeySpec(signingKeyString.getBytes("UTF-8"), SIGNING_ALGORITHM); Mac mac = Mac.getInstance(SIGNING_ALGORITHM); mac.init(signingKey); byte[] hmac = mac.doFinal(digest); String encodedHmac = Base64.encodeBytes(hmac); writeTextToZip(zos, encodedHmac, SIG_FILE_NAME); } final String checksum = "" + checkedOutStream.getChecksum().getValue(); zos.close(); Log.i(TAG, "Closed zip output stream for file: " + fileName + ". Checksum: " + checksum); } catch (IOException e) { PersistentUncaughtExceptionHandler.recordException(e); Log.e(TAG, e.getMessage()); zipFileData = null; } catch (NoSuchAlgorithmException e) { PersistentUncaughtExceptionHandler.recordException(e); Log.e(TAG, e.getMessage()); zipFileData = null; } catch (InvalidKeyException e) { PersistentUncaughtExceptionHandler.recordException(e); Log.e(TAG, e.getMessage()); zipFileData = null; } return zipFileData; }