Example usage for java.util.zip CRC32 update

List of usage examples for java.util.zip CRC32 update

Introduction

In this page you can find the example usage for java.util.zip CRC32 update.

Prototype

@Override
public void update(ByteBuffer buffer) 

Source Link

Document

Updates the CRC-32 checksum with the bytes from the specified buffer.

Usage

From source file:org.apache.hadoop.raid.TestRaidShell.java

private long getCRC(FileSystem fs, Path p) throws IOException {
    CRC32 crc = new CRC32();
    FSDataInputStream stm = fs.open(p);//ww  w.j  a va 2 s .c om
    for (int b = 0; b > 0; b = stm.read()) {
        crc.update(b);
    }
    stm.close();
    return crc.getValue();
}

From source file:de.catma.ui.repository.wizard.FileTypePanel.java

private boolean loadSourceDocumentAndContent(SourceDocumentResult sdr) {
    try {/*from w  w w  .  j ava 2 s . c  o m*/
        SourceDocumentHandler sourceDocumentHandler = new SourceDocumentHandler();
        SourceDocument sourceDocument = sourceDocumentHandler.loadSourceDocument(sdr.getSourceDocumentID(),
                sdr.getSourceDocumentInfo());
        sdr.setSourceDocument(sourceDocument);

        TechInfoSet techInfoSet = sdr.getSourceDocumentInfo().getTechInfoSet();
        String documentId = sdr.getSourceDocumentID();

        ProtocolHandler protocolHandler = getProtocolHandlerForUri(techInfoSet.getURI(), documentId,
                techInfoSet.getMimeType());

        byte[] currentByteContent = protocolHandler.getByteContent();

        sourceDocument.getSourceContentHandler().load(new ByteArrayInputStream(currentByteContent));

        FileOSType fileOSType = FileOSType.getFileOSType(sourceDocument.getContent());

        sdr.getSourceDocumentInfo().getTechInfoSet().setFileOSType(fileOSType);
        CRC32 checksum = new CRC32();
        checksum.update(currentByteContent);
        sdr.getSourceDocumentInfo().getTechInfoSet().setChecksum(checksum.getValue());
        return true;
    } catch (Exception e) {
        TechInfoSet techInfoSet = sdr.getSourceDocumentInfo().getTechInfoSet();
        Notification.show(
                "Information",
                "Sorry, CATMA wasn't able to process the file as "
                        + techInfoSet.getFileType()
                        + (techInfoSet.getFileType().isCharsetSupported()
                                ? " with " + ((techInfoSet.getCharset() == null) ? "unknown charset"
                                        : " charset " + techInfoSet.getCharset())
                                : "")
                        + "\n\nThe original error message is: " + e.getLocalizedMessage(),
                Notification.Type.WARNING_MESSAGE);
        return false;
    }
}

From source file:com.netflix.spinnaker.halyard.config.model.v1.node.Node.java

public void stageLocalFiles(Path outputPath) {
    if (!GlobalApplicationOptions.getInstance().isUseRemoteDaemon()) {
        return;//from   w  w w .  ja  v a2s.c  om
    }
    localFiles().forEach(f -> {
        try {
            f.setAccessible(true);
            String fContent = (String) f.get(this);
            if (fContent != null) {
                CRC32 crc = new CRC32();
                crc.update(fContent.getBytes());
                String fPath = Paths
                        .get(outputPath.toAbsolutePath().toString(), Long.toHexString(crc.getValue()))
                        .toString();
                FileUtils.writeStringToFile(new File(fPath), fContent);
                f.set(this, fPath);
            }
        } catch (IllegalAccessException | IOException e) {
            throw new RuntimeException("Failed to get local files for node " + this.getNodeName(), e);
        } finally {
            f.setAccessible(false);
        }
    });
}

From source file:com.sastix.cms.server.services.content.impl.HashedDirectoryServiceImpl.java

/**
 * Returns the crc32 hash for the input String.
 *
 * @param text a String with the text// w  w  w. ja  v a  2  s  . c  o m
 * @return a BigInteger with the hash
 */
@Override
public String hashText(final String text) {
    final CRC32 crc32 = new CRC32();
    crc32.reset();
    crc32.update(text.getBytes());
    return Long.toHexString(crc32.getValue());
}

From source file:org.docx4j.openpackaging.io3.stores.ZipPartStore.java

public void saveBinaryPart(Part part) throws Docx4JException {

    // Drop the leading '/'
    String resolvedPartUri = part.getPartName().getName().substring(1);

    try {//w  w w .  ja  v a  2  s  . c  o  m

        byte[] bytes = null;

        if (((BinaryPart) part).isLoaded()) {

            bytes = ((BinaryPart) part).getBytes();

        } else {

            if (this.sourcePartStore == null) {

                throw new Docx4JException("part store has changed, and sourcePartStore not set");

            } else if (this.sourcePartStore == this) {

                // Just use the ByteArray
                log.debug(part.getPartName() + " is clean");
                ByteArray byteArray = partByteArrays.get(part.getPartName().getName().substring(1));
                if (byteArray == null)
                    throw new IOException("part '" + part.getPartName() + "' not found");
                bytes = byteArray.getBytes();

            } else {

                InputStream is = sourcePartStore.loadPart(part.getPartName().getName().substring(1));
                bytes = IOUtils.toByteArray(is);
            }
        }

        // Add ZIP entry to output stream.
        if (part instanceof OleObjectBinaryPart) {
            // Workaround: Powerpoint 2010 (32-bit) can't play eg WMV if it is compressed!
            // (though 64-bit version is fine)

            ZipEntry ze = new ZipEntry(resolvedPartUri);
            ze.setMethod(ZipOutputStream.STORED);

            // must set size, compressed size, and crc-32
            ze.setSize(bytes.length);
            ze.setCompressedSize(bytes.length);

            CRC32 crc = new CRC32();
            crc.update(bytes);
            ze.setCrc(crc.getValue());

            zos.putNextEntry(ze);
        } else {
            zos.putNextEntry(new ZipEntry(resolvedPartUri));
        }

        zos.write(bytes);

        // Complete the entry
        zos.closeEntry();

    } catch (Exception e) {
        throw new Docx4JException("Failed to put binary part", e);
    }

    log.info("success writing part: " + resolvedPartUri);

}

From source file:org.apache.hadoop.hdfs.TestRaidDfs.java

public static long createTestFile(FileSystem fileSys, Path name, int repl, long fileSize, long blockSize,
        int seed) throws IOException {
    CRC32 crc = new CRC32();
    Random rand = new Random(seed);
    FSDataOutputStream stm = fileSys.create(name, true, fileSys.getConf().getInt("io.file.buffer.size", 4096),
            (short) repl, blockSize);
    LOG.info("create file " + name + " size: " + fileSize + " blockSize: " + blockSize + " repl: " + repl);
    // fill random data into file
    byte[] b = new byte[(int) blockSize];
    long numBlocks = fileSize / blockSize;
    for (int i = 0; i < numBlocks; i++) {
        rand.nextBytes(b);/*from  www.  ja v a2s  .  c om*/
        stm.write(b);
        crc.update(b);
    }
    long lastBlock = fileSize - numBlocks * blockSize;
    if (lastBlock > 0) {
        b = new byte[(int) lastBlock];
        rand.nextBytes(b);
        stm.write(b);
        crc.update(b);
    }
    stm.close();
    return crc.getValue();
}

From source file:org.klco.email2html.OutputWriter.java

/**
 * Writes the attachment contained in the body part to a file.
 * //from w w  w .ja v  a  2 s.com
 * @param containingMessage
 *            the message this body part is contained within
 * @param part
 *            the part containing the attachment
 * @return the file that was created/written to
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws MessagingException
 *             the messaging exception
 */
public boolean writeAttachment(EmailMessage containingMessage, Part part)
        throws IOException, MessagingException {
    log.trace("writeAttachment");

    File attachmentFolder;
    File attachmentFile;
    InputStream in = null;
    OutputStream out = null;
    try {

        attachmentFolder = new File(outputDir.getAbsolutePath() + File.separator + config.getImagesSubDir()
                + File.separator + FILE_DATE_FORMAT.format(containingMessage.getSentDate()));
        if (!attachmentFolder.exists()) {
            log.debug("Creating attachment folder");
            attachmentFolder.mkdirs();
        }

        attachmentFile = new File(attachmentFolder, part.getFileName());
        log.debug("Writing attachment file: {}", attachmentFile.getAbsolutePath());
        if (!attachmentFile.exists()) {
            attachmentFile.createNewFile();
        }

        in = new BufferedInputStream(part.getInputStream());
        out = new BufferedOutputStream(new FileOutputStream(attachmentFile));

        log.debug("Downloading attachment");
        CRC32 checksum = new CRC32();
        for (int b = in.read(); b != -1; b = in.read()) {
            checksum.update(b);
            out.write(b);
        }

        if (this.excludeDuplicates) {
            log.debug("Computing checksum");
            long value = checksum.getValue();
            if (this.attachmentChecksums.contains(value)) {
                log.info("Skipping duplicate attachment: {}", part.getFileName());
                attachmentFile.delete();
                return false;
            } else {
                attachmentChecksums.add(value);
            }
        }

        log.debug("Attachement saved");
    } finally {
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(in);
    }

    if (part.getContentType().toLowerCase().startsWith("image")) {
        log.debug("Creating renditions");
        String contentType = part.getContentType().substring(0, part.getContentType().indexOf(";"));
        log.debug("Creating renditions of type: " + contentType);

        for (Rendition rendition : renditions) {
            File renditionFile = new File(attachmentFolder, rendition.getName() + "-" + part.getFileName());
            try {
                if (!renditionFile.exists()) {
                    renditionFile.createNewFile();
                }
                log.debug("Creating rendition file: {}", renditionFile.getAbsolutePath());
                createRendition(attachmentFile, renditionFile, rendition);
                log.debug("Rendition created");
            } catch (OutOfMemoryError oome) {
                Runtime rt = Runtime.getRuntime();
                rt.gc();
                log.warn("Ran out of memory creating rendition: " + rendition, oome);

                log.warn("Free Memory: {}", rt.freeMemory());
                log.warn("Max Memory: {}", rt.maxMemory());
                log.warn("Total Memory: {}", rt.totalMemory());

                String[] command = null;
                if (rendition.getFill()) {
                    command = new String[] { "convert", attachmentFile.getAbsolutePath(), "-resize",
                            (rendition.getHeight() * 2) + "x", "-resize",
                            "'x" + (rendition.getHeight() * 2) + "<'", "-resize", "50%", "-gravity", "center",
                            "-crop", rendition.getHeight() + "x" + rendition.getWidth() + "+0+0", "+repage",
                            renditionFile.getAbsolutePath() };
                } else {
                    command = new String[] { "convert", attachmentFile.getAbsolutePath(), "-resize",
                            rendition.getHeight() + "x" + rendition.getWidth(),
                            renditionFile.getAbsolutePath() };

                }
                log.debug("Trying to resize with ImageMagick: " + StringUtils.join(command, " "));

                rt.exec(command);
            } catch (Exception t) {
                log.warn("Exception creating rendition: " + rendition, t);
            }
        }
    }
    return true;
}

From source file:com.jaeksoft.searchlib.crawler.web.spider.DownloadItem.java

public void writeToZip(ZipArchiveOutputStream zipOutput) throws IOException {
    if (contentInputStream == null)
        return;//from w  w w.  ja va 2 s.c  o m
    String[] domainParts = StringUtils.split(uri.getHost(), '.');
    StringBuilder path = new StringBuilder();
    for (int i = domainParts.length - 1; i >= 0; i--) {
        path.append(domainParts[i]);
        path.append('/');
    }
    String[] pathParts = StringUtils.split(uri.getPath(), '/');
    for (int i = 0; i < pathParts.length - 1; i++) {
        if (StringUtils.isEmpty(pathParts[i]))
            continue;
        path.append(pathParts[i]);
        path.append('/');
    }
    if (contentDispositionFilename != null)
        path.append(contentDispositionFilename);
    else {
        String lastPart = pathParts == null || pathParts.length == 0 ? null : pathParts[pathParts.length - 1];
        if (StringUtils.isEmpty(lastPart))
            path.append("index");
        else
            path.append(lastPart);
    }
    if (uri.getPath().endsWith("/"))
        path.append("/_index");
    String query = uri.getQuery();
    String fragment = uri.getFragment();
    if (!StringUtils.isEmpty(query) || !StringUtils.isEmpty(fragment)) {
        CRC32 crc32 = new CRC32();
        if (!StringUtils.isEmpty(query))
            crc32.update(query.getBytes());
        if (!StringUtils.isEmpty(fragment))
            crc32.update(fragment.getBytes());
        path.append('.');
        path.append(crc32.getValue());
    }
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(path.toString());
    zipOutput.putArchiveEntry(zipEntry);
    BufferedInputStream bis = null;
    byte[] buffer = new byte[65536];
    try {
        bis = new BufferedInputStream(contentInputStream);
        int l;
        while ((l = bis.read(buffer)) != -1)
            zipOutput.write(buffer, 0, l);
        zipOutput.closeArchiveEntry();
    } finally {
        IOUtils.close(bis);
    }
}

From source file:org.ambiance.codec.YEncEncoder.java

public void encode(File input) throws EncoderException {
    try {//w  ww  . ja  v a  2s  .c o  m
        // Initialize
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(input));

        YEncHeader header = new YEncHeader();
        header.setName(input.getName());
        header.setLine(lineSize);

        YEncTrailer trailer = new YEncTrailer();

        CRC32 crc32 = new CRC32();

        StringBuffer outputName = new StringBuffer(outputDirName);
        outputName.append(File.separator);
        outputName.append(header.getName());
        outputName.append(extension);

        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outputName.toString()));

        // Read and encode
        int c;
        long size = 0;
        while ((c = bis.read()) != -1) {
            crc32.update(c);
            if (size % header.getLine() == 0 && size != 0) {
                bos.write((int) '\r');
                bos.write((int) '\n');
            }
            size++;
        }
    } catch (FileNotFoundException e) {
        throw new EncoderException("Unable to find file to encode");
    } catch (IOException e) {
        throw new EncoderException("Unable to read file to encode");
    }

}

From source file:com.occamlab.te.parsers.ImageParser.java

private static void processBufferedImage(BufferedImage buffimage, String formatName, NodeList nodes)
        throws Exception {
    HashMap<Object, Object> bandMap = new HashMap<Object, Object>();

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getLocalName().equals("subimage")) {
                Element e = (Element) node;
                int x = Integer.parseInt(e.getAttribute("x"));
                int y = Integer.parseInt(e.getAttribute("y"));
                int w = Integer.parseInt(e.getAttribute("width"));
                int h = Integer.parseInt(e.getAttribute("height"));
                processBufferedImage(buffimage.getSubimage(x, y, w, h), formatName, e.getChildNodes());
            } else if (node.getLocalName().equals("checksum")) {
                CRC32 checksum = new CRC32();
                Raster raster = buffimage.getRaster();
                DataBufferByte buffer;
                if (node.getParentNode().getLocalName().equals("subimage")) {
                    WritableRaster outRaster = raster.createCompatibleWritableRaster();
                    buffimage.copyData(outRaster);
                    buffer = (DataBufferByte) outRaster.getDataBuffer();
                } else {
                    buffer = (DataBufferByte) raster.getDataBuffer();
                }//from   ww w .  j  ava2 s . c o m
                int numbanks = buffer.getNumBanks();
                for (int j = 0; j < numbanks; j++) {
                    checksum.update(buffer.getData(j));
                }
                Document doc = node.getOwnerDocument();
                node.appendChild(doc.createTextNode(Long.toString(checksum.getValue())));
            } else if (node.getLocalName().equals("count")) {
                String band = ((Element) node).getAttribute("bands");
                String sample = ((Element) node).getAttribute("sample");
                if (sample.equals("all")) {
                    bandMap.put(band, null);
                } else {
                    HashMap<Object, Object> sampleMap = (HashMap<Object, Object>) bandMap.get(band);
                    if (sampleMap == null) {
                        if (!bandMap.containsKey(band)) {
                            sampleMap = new HashMap<Object, Object>();
                            bandMap.put(band, sampleMap);
                        }
                    }
                    sampleMap.put(Integer.decode(sample), new Integer(0));
                }
            } else if (node.getLocalName().equals("transparentNodata")) { // 2011-08-24
                                                                          // PwD
                String transparentNodata = checkTransparentNodata(buffimage, node);
                node.setTextContent(transparentNodata);
            }
        }
    }

    Iterator bandIt = bandMap.keySet().iterator();
    while (bandIt.hasNext()) {
        String band_str = (String) bandIt.next();
        int band_indexes[];
        if (buffimage.getType() == BufferedImage.TYPE_BYTE_BINARY
                || buffimage.getType() == BufferedImage.TYPE_BYTE_GRAY) {
            band_indexes = new int[1];
            band_indexes[0] = 0;
        } else {
            band_indexes = new int[band_str.length()];
            for (int i = 0; i < band_str.length(); i++) {
                if (band_str.charAt(i) == 'A')
                    band_indexes[i] = 3;
                if (band_str.charAt(i) == 'B')
                    band_indexes[i] = 2;
                if (band_str.charAt(i) == 'G')
                    band_indexes[i] = 1;
                if (band_str.charAt(i) == 'R')
                    band_indexes[i] = 0;
            }
        }

        Raster raster = buffimage.getRaster();
        java.util.HashMap sampleMap = (java.util.HashMap) bandMap.get(band_str);
        boolean addall = (sampleMap == null);
        if (sampleMap == null) {
            sampleMap = new java.util.HashMap();
            bandMap.put(band_str, sampleMap);
        }

        int minx = raster.getMinX();
        int maxx = minx + raster.getWidth();
        int miny = raster.getMinY();
        int maxy = miny + raster.getHeight();
        int bands[][] = new int[band_indexes.length][raster.getWidth()];

        for (int y = miny; y < maxy; y++) {
            for (int i = 0; i < band_indexes.length; i++) {
                raster.getSamples(minx, y, maxx, 1, band_indexes[i], bands[i]);
            }
            for (int x = minx; x < maxx; x++) {
                int sample = 0;
                for (int i = 0; i < band_indexes.length; i++) {
                    sample |= bands[i][x] << ((band_indexes.length - i - 1) * 8);
                }

                Integer sampleObj = new Integer(sample);

                boolean add = addall;
                if (!addall) {
                    add = sampleMap.containsKey(sampleObj);
                }
                if (add) {
                    Integer count = (Integer) sampleMap.get(sampleObj);
                    if (count == null) {
                        count = new Integer(0);
                    }
                    count = new Integer(count.intValue() + 1);
                    sampleMap.put(sampleObj, count);
                }
            }
        }
    }

    Node node = nodes.item(0);
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getLocalName().equals("count")) {
                String band = ((Element) node).getAttribute("bands");
                String sample = ((Element) node).getAttribute("sample");
                HashMap sampleMap = (HashMap) bandMap.get(band);
                Document doc = node.getOwnerDocument();
                if (sample.equals("all")) {
                    Node parent = node.getParentNode();
                    Node prevSibling = node.getPreviousSibling();
                    Iterator sampleIt = sampleMap.keySet().iterator();
                    Element countnode = null;
                    int digits;
                    String prefix;
                    switch (buffimage.getType()) {
                    case BufferedImage.TYPE_BYTE_BINARY:
                        digits = 1;
                        prefix = "";
                        break;
                    case BufferedImage.TYPE_BYTE_GRAY:
                        digits = 2;
                        prefix = "0x";
                        break;
                    default:
                        prefix = "0x";
                        digits = band.length() * 2;
                    }
                    while (sampleIt.hasNext()) {
                        countnode = doc.createElementNS(node.getNamespaceURI(), "count");
                        Integer sampleInt = (Integer) sampleIt.next();
                        Integer count = (Integer) sampleMap.get(sampleInt);
                        if (band.length() > 0) {
                            countnode.setAttribute("bands", band);
                        }
                        countnode.setAttribute("sample", prefix + HexString(sampleInt.intValue(), digits));
                        Node textnode = doc.createTextNode(count.toString());
                        countnode.appendChild(textnode);
                        parent.insertBefore(countnode, node);
                        if (sampleIt.hasNext()) {
                            if (prevSibling != null && prevSibling.getNodeType() == Node.TEXT_NODE) {
                                parent.insertBefore(prevSibling.cloneNode(false), node);
                            }
                        }
                    }
                    parent.removeChild(node);
                    node = countnode;
                } else {
                    Integer count = (Integer) sampleMap.get(Integer.decode(sample));
                    if (count == null)
                        count = new Integer(0);
                    Node textnode = doc.createTextNode(count.toString());
                    node.appendChild(textnode);
                }
            }
        }
        node = node.getNextSibling();
    }
}