List of usage examples for java.util.zip CRC32 CRC32
public CRC32()
From source file:com.splout.db.dnode.HttpFileExchanger.java
public void send(final String tablespace, final int partition, final long version, final File binaryFile, final String url, boolean blockUntilComplete) { Future<?> future = clientExecutors.submit(new Runnable() { @Override/*from w w w. ja v a2s . com*/ public void run() { DataOutputStream writer = null; InputStream input = null; try { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setChunkedStreamingMode(config.getInt(FetcherProperties.DOWNLOAD_BUFFER)); connection.setDoOutput(true); connection.setRequestProperty("filename", binaryFile.getName()); connection.setRequestProperty("tablespace", tablespace); connection.setRequestProperty("partition", partition + ""); connection.setRequestProperty("version", version + ""); Checksum checkSum = new CRC32(); writer = new DataOutputStream(new GZIPOutputStream(connection.getOutputStream())); // 1 - write file size writer.writeLong(binaryFile.length()); writer.flush(); // 2 - write file content input = new FileInputStream(binaryFile); byte[] buffer = new byte[config.getInt(FetcherProperties.DOWNLOAD_BUFFER)]; long wrote = 0; for (int length = 0; (length = input.read(buffer)) > 0;) { writer.write(buffer, 0, length); checkSum.update(buffer, 0, length); wrote += length; } // 3 - add the CRC so that we can verify the download writer.writeLong(checkSum.getValue()); writer.flush(); log.info("Sent file " + binaryFile + " to " + url + " with #bytes: " + wrote + " and checksum: " + checkSum.getValue()); } catch (IOException e) { log.error(e); } finally { try { if (input != null) { input.close(); } if (writer != null) { writer.close(); } } catch (IOException ignore) { } } } }); try { if (blockUntilComplete) { while (future.isDone() || future.isCancelled()) { Thread.sleep(1000); } } } catch (InterruptedException e) { // interrupted! } }
From source file:com.ibm.amc.FileManager.java
public static File compress(File pathToCompress) { try {//from w ww .j a va 2 s .co m File zipFile = new File(pathToCompress.getCanonicalPath() + ZIP_EXTENSION); FileOutputStream fileOutputStream = new FileOutputStream(zipFile); CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32()); ZipOutputStream out = new ZipOutputStream(cos); String basedir = ""; compress(pathToCompress, out, basedir); out.close(); return zipFile; } catch (Exception e) { throw new AmcRuntimeException(e); } }
From source file:org.apache.isis.objectstore.nosql.db.file.server.FileServer.java
private void startSyncing() { final String syncHost = config.getString("fileserver.sync-host", DEFAULT_HOST); final int syncPort = config.getInt("fileserver.sync-port", DEFAULT_SYNC_PORT); final int connectionTimeout = config.getInt("fileserver.connection.timeout", 5000); LOG.info("preparing to sync to secondary server on " + syncHost + " port " + syncPort); final InetAddress address; try {//www.j a v a2s. c om address = InetAddress.getByName(syncHost); } catch (final UnknownHostException e) { LOG.error("Unknown host " + syncHost, e); System.exit(0); return; } while (awaitConnections) { Socket socket = null; try { socket = new Socket(address, syncPort); LOG.info("sync connected to " + socket.getInetAddress().getHostAddress() + " port " + socket.getLocalPort()); final CRC32 crc32 = new CRC32(); final DataOutput output = new DataOutputStream( new CheckedOutputStream(socket.getOutputStream(), crc32)); final DataInput input = new DataInputStream(socket.getInputStream()); output.writeByte(INIT); long logId = input.readLong(); do { final long nextLogId = logId + 1; final File file = Util.logFile(nextLogId); if (file.exists() && server.getLogger().isWritten(nextLogId)) { logId++; output.writeByte(RECOVERY_LOG); crc32.reset(); output.writeLong(logId); LOG.info("sending recovery file: " + file.getName()); final BufferedInputStream fileInput = new BufferedInputStream(new FileInputStream(file)); final byte[] buffer = new byte[8092]; int read; while ((read = fileInput.read(buffer)) > 0) { output.writeInt(read); output.write(buffer, 0, read); } output.writeInt(0); output.writeLong(crc32.getValue()); } try { Thread.sleep(300); } catch (final InterruptedException ignore) { } while (isQuiescent) { try { Thread.sleep(300); } catch (final InterruptedException ignore) { } } } while (awaitConnections); } catch (final ConnectException e) { LOG.warn("not yet connected to secondary server at " + syncHost + " port " + syncPort); try { Thread.sleep(connectionTimeout); } catch (final InterruptedException ignore) { } } catch (final IOException e) { LOG.error("start failure - networking not set up for " + syncHost, e); try { Thread.sleep(300); } catch (final InterruptedException ignore) { } } catch (final RuntimeException e) { LOG.error("start failure", e); try { Thread.sleep(300); } catch (final InterruptedException ignore) { } } } }
From source file:com.jkoolcloud.tnt4j.streams.configure.state.AbstractFileStreamStateHandler.java
/** * Calculates CRC value for bytes read from provided input stream. * * @param in/* w w w . ja va 2s .c om*/ * reader to read bytes for CRC calculation * * @return calculated CRC value * * @throws IOException * if input can't be read. */ protected static Long getInputCrc(Reader in) throws IOException { char[] buff = new char[BYTES_TO_COUNT_CRC]; Checksum crc = new CRC32(); int readLen = in.read(buff); if (readLen > 0) { String str = new String(buff, 0, readLen); final byte[] bytes = str.getBytes(Utils.UTF8); crc.update(bytes, 0, bytes.length); } return crc.getValue(); }
From source file:com.oneops.sensor.Sensor.java
/** * Adds the ci thresholds./* www . ja va 2s . c om*/ * * @param ciId the ci id * @param manifestId the manifest id * @param monitor the monitor * @throws SensorException */ public void addCiThresholdsList(long ciId, long manifestId, List<CmsRfcCISimple> monitors) throws SensorException { if (!isInited || (manifestId % this.poolSize) != this.instanceId) { // this is not my manifestId will post it on mgmt queue for other guy to pick up throw new SensorException("Got Monitor request for the wrong instance - manifestId:" + manifestId + "; pool size:" + this.poolSize + "; my insatnceId:" + this.instanceId); } Set<String> processedMonitors = new HashSet<>(); for (CmsRfcCISimple monitor : monitors) { if (monitor.getCiAttributes().containsKey("enable") && monitor.getCiAttributes().get("enable").equals("false")) { continue; } long checksum = 0; String thresholdsJson = monitor.getCiAttributes().get("thresholds"); String source = monitor.getCiName(); if (thresholdsJson != null) { CRC32 crc = new CRC32(); String crcStr = thresholdsJson + monitor.getCiAttributes().get(HEARTBEAT) + monitor.getCiAttributes().get(DURATION); crc.update(crcStr.getBytes()); checksum = crc.getValue(); } else { // need to clean up thresholds continue; } processedMonitors.add(source); //String key = manifestId + source; ThresholdStatements trStmt = loadedThresholds.containsKey(manifestId) ? loadedThresholds.get(manifestId).get(source) : null; if (trStmt == null) { //load stmts persistAndaddToEngine(ciId, manifestId, source, checksum, thresholdsJson, monitor.getCiAttributes().get(HEARTBEAT).equals("true"), monitor.getCiAttributes().get(DURATION)); } else if (trStmt.getChecksum() != checksum || monitor.getCiAttributes().get(HEARTBEAT).equals("true") != trStmt.isHeartbeat()) { // if checksum is different we assume there was an monitor update // we need to remove old stmts and insert new ones // but before that lets insert fake event to clear out heart beats // if this new mon is not a heartbeat one if (!monitor.getCiAttributes().get(HEARTBEAT).equals("true")) { insertFakeEvent(ciId, manifestId, source); } for (String eplName : trStmt.getStmtNames()) { removeStmtFromEngine(manifestId, source, eplName); } loadedThresholds.get(manifestId).remove(source); persistAndaddToEngine(ciId, manifestId, source, checksum, thresholdsJson, monitor.getCiAttributes().get(HEARTBEAT).equals("true"), monitor.getCiAttributes().get(DURATION)); } } // now we need to clean up the deleted monitors if (loadedThresholds.containsKey(manifestId)) { Set<String> monsToRemove = new HashSet<>(); for (String loadedMon : loadedThresholds.get(manifestId).keySet()) { if (!processedMonitors.contains(loadedMon)) { //this is old monitor that need to be removed //insert fake event to shut down Heartbeat retrigger insertFakeEvent(ciId, manifestId, loadedMon); //and do it for the rest bom guys for (long ciMapedBomId : tsDao.getManifestCiIds(manifestId)) { insertFakeEvent(ciMapedBomId, manifestId, loadedMon); } ThresholdStatements trStmt = loadedThresholds.get(manifestId).get(loadedMon); for (String eplName : trStmt.getStmtNames()) { removeStmtFromEngine(manifestId, loadedMon, eplName); } monsToRemove.add(loadedMon); tsDao.removeManifestThreshold(manifestId, loadedMon); } } for (String monToRemove : monsToRemove) { loadedThresholds.get(manifestId).remove(monToRemove); } } }
From source file:org.kuali.kfs.module.ar.report.service.impl.TransmitContractsAndGrantsInvoicesServiceImpl.java
@Override public boolean printInvoicesAndEnvelopesZip(Collection<ContractsGrantsInvoiceDocument> list, ByteArrayOutputStream baos) throws DocumentException, IOException { if (CollectionUtils.isNotEmpty(list)) { byte[] envelopes = contractsGrantsInvoiceReportService.combineInvoicePdfEnvelopes(list); byte[] report = contractsGrantsInvoiceReportService.combineInvoicePdfs(list); boolean invoiceFileWritten = false; boolean envelopeFileWritten = false; ZipOutputStream zos = new ZipOutputStream(baos); try {/*from ww w . j a v a2 s . c o m*/ int bytesRead; byte[] buffer = new byte[1024]; CRC32 crc = new CRC32(); String invoiceFileName = ArConstants.INVOICES_FILE_PREFIX + getDateTimeService().toDateStringForFilename(getDateTimeService().getCurrentDate()) + KFSConstants.ReportGeneration.PDF_FILE_EXTENSION; invoiceFileWritten = writeFile(report, zos, invoiceFileName); String envelopeFileName = ArConstants.INVOICE_ENVELOPES_FILE_PREFIX + getDateTimeService().toDateStringForFilename(getDateTimeService().getCurrentDate()) + KFSConstants.ReportGeneration.PDF_FILE_EXTENSION; envelopeFileWritten = writeFile(envelopes, zos, envelopeFileName); } finally { zos.close(); } return invoiceFileWritten || envelopeFileWritten; } return false; }
From source file:com.nridge.connector.common.con_com.crawl.CrawlQueue.java
private long nextCrawlId() { UUID uniqueId = UUID.randomUUID(); byte idBytes[] = uniqueId.toString().getBytes(); Checksum checksumValue = new CRC32(); checksumValue.update(idBytes, 0, idBytes.length); return checksumValue.getValue(); }
From source file:com.webpagebytes.cms.controllers.PageController.java
public void update(HttpServletRequest request, HttpServletResponse response, String requestUri) throws WPBException { try {/*from w ww .j ava 2 s. co m*/ Long key = Long.valueOf((String) request.getAttribute("key")); String jsonRequest = httpServletToolbox.getBodyText(request); WPBPage webPage = (WPBPage) jsonObjectConverter.objectFromJSONString(jsonRequest, WPBPage.class); webPage.setPrivkey(key); Map<String, String> errors = pageValidator.validateUpdate(webPage); if (errors.size() > 0) { httpServletToolbox.writeBodyResponseAsJson(response, "{}", errors); return; } CRC32 crc = new CRC32(); crc.update(webPage.getHtmlSource().getBytes()); webPage.setHash(crc.getValue()); webPage.setLastModified(Calendar.getInstance(TimeZone.getTimeZone("GMT")).getTime()); WPBPage newWebPage = adminStorage.update(webPage); WPBResource resource = new WPBResource(newWebPage.getExternalKey(), newWebPage.getName(), WPBResource.PAGE_TYPE); try { adminStorage.update(resource); } catch (Exception e) { // do not propate further } org.json.JSONObject returnJson = new org.json.JSONObject(); returnJson.put(DATA, jsonObjectConverter.JSONFromObject(newWebPage)); httpServletToolbox.writeBodyResponseAsJson(response, returnJson, null); } catch (Exception e) { Map<String, String> errors = new HashMap<String, String>(); errors.put("", WPBErrors.WB_CANT_UPDATE_RECORD); httpServletToolbox.writeBodyResponseAsJson(response, jsonObjectConverter.JSONObjectFromMap(null), errors); } }
From source file:com.rover12421.shaka.apktool.lib.AndrolibAj.java
public static CRC32 calculateCrc(InputStream input) throws IOException { CRC32 crc = new CRC32(); int bytesRead; byte[] buffer = new byte[4096]; while ((bytesRead = input.read(buffer)) != -1) { crc.update(buffer, 0, bytesRead); }// w w w.j ava 2 s . co m return crc; }
From source file:org.exist.xquery.modules.compression.AbstractCompressFunction.java
/** * Adds a element to a archive//from w ww . j a v a 2 s . c om * * @param os * The Output Stream to add the element to * @param element * The element to add to the archive * @param useHierarchy * Whether to use a folder hierarchy in the archive file that * reflects the collection hierarchy */ private void compressElement(OutputStream os, Element element, boolean useHierarchy, String stripOffset) throws XPathException { if (!(element.getNodeName().equals("entry") || element.getNamespaceURI().length() > 0)) throw new XPathException(this, "Item must be type of xs:anyURI or element entry."); if (element.getChildNodes().getLength() > 1) throw new XPathException(this, "Entry content is not valid XML fragment."); String name = element.getAttribute("name"); // if(name == null) // throw new XPathException(this, "Entry must have name attribute."); String type = element.getAttribute("type"); if ("uri".equals(type)) { compressFromUri(os, URI.create(element.getFirstChild().getNodeValue()), useHierarchy, stripOffset, element.getAttribute("method"), name); return; } if (useHierarchy) { name = removeLeadingOffset(name, stripOffset); } else { name = name.substring(name.lastIndexOf("/") + 1); } if ("collection".equals(type)) name += "/"; Object entry = null; try { entry = newEntry(name); if (!"collection".equals(type)) { byte[] value; CRC32 chksum = new CRC32(); Node content = element.getFirstChild(); if (content == null) { value = new byte[0]; } else { if (content.getNodeType() == Node.TEXT_NODE) { String text = content.getNodeValue(); Base64Decoder dec = new Base64Decoder(); if ("binary".equals(type)) { //base64 binary dec.translate(text); value = dec.getByteArray(); } else { //text value = text.getBytes(); } } else { //xml Serializer serializer = context.getBroker().getSerializer(); serializer.setUser(context.getUser()); serializer.setProperty("omit-xml-declaration", "no"); getDynamicSerializerOptions(serializer); value = serializer.serialize((NodeValue) content).getBytes(); } } if (entry instanceof ZipEntry && "store".equals(element.getAttribute("method"))) { ((ZipEntry) entry).setMethod(ZipOutputStream.STORED); chksum.update(value); ((ZipEntry) entry).setCrc(chksum.getValue()); ((ZipEntry) entry).setSize(value.length); } putEntry(os, entry); os.write(value); } } catch (IOException ioe) { throw new XPathException(this, ioe.getMessage(), ioe); } catch (SAXException saxe) { throw new XPathException(this, saxe.getMessage(), saxe); } finally { if (entry != null) try { closeEntry(os); } catch (IOException ioe) { throw new XPathException(this, ioe.getMessage(), ioe); } } }