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.TestRaidDfs.java

private long createOldFile(FileSystem fileSys, Path name, int repl, int numBlocks, long blocksize)
        throws IOException {
    CRC32 crc = new CRC32();
    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);//from ww w  .j  av a  2  s  .  c  o  m
        stm.write(b);
        crc.update(b);
    }
    stm.close();
    return crc.getValue();
}

From source file:org.cryptomator.crypto.aes256.Aes256Cryptor.java

private long crc32Sum(byte[] source) {
    final CRC32 crc32 = new CRC32();
    crc32.update(source);
    return crc32.getValue();
}

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

/**
 * Adds the attachment to the EmailMessage. Call this method when the email
 * content has most likely already been loaded.
 * /*from  w w  w.  j  a  va2 s . c o  m*/
 * @param containingMessage
 *            the Email Message to add the attachment to
 * @param part
 *            the content of the attachment
 * @throws IOException
 * @throws MessagingException
 */
public void addAttachment(EmailMessage containingMessage, Part part) throws IOException, MessagingException {
    log.trace("addAttachment");

    File attachmentFolder = new File(outputDir.getAbsolutePath() + File.separator + config.getImagesSubDir()
            + File.separator + FILE_DATE_FORMAT.format(containingMessage.getSentDate()));
    File attachmentFile = new File(attachmentFolder, part.getFileName());

    boolean addAttachment = true;
    boolean writeAttachment = false;
    if (!attachmentFolder.exists() || !attachmentFile.exists()) {
        log.warn("Attachment or folder missing, writing attachment {}", attachmentFile.getName());
        writeAttachment = true;
    }

    if (!writeAttachment && part.getContentType().toLowerCase().startsWith("image")) {
        for (Rendition rendition : renditions) {
            File renditionFile = new File(attachmentFolder, rendition.getName() + "-" + part.getFileName());
            if (!renditionFile.exists()) {
                log.warn("Rendition {} missing, writing attachment {}", renditionFile.getName(),
                        attachmentFile.getName());
                writeAttachment = true;
                break;
            }
        }
    }
    if (writeAttachment) {
        addAttachment = writeAttachment(containingMessage, part);
    } else {
        if (this.excludeDuplicates) {
            log.debug("Computing checksum");
            InputStream is = null;
            try {
                CRC32 checksum = new CRC32();
                is = new BufferedInputStream(new FileInputStream(attachmentFile));
                for (int read = is.read(); read != -1; read = is.read()) {
                    checksum.update(read);
                }
                long value = checksum.getValue();
                if (attachmentChecksums.contains(value)) {
                    addAttachment = false;
                } else {
                    attachmentChecksums.add(checksum.getValue());
                }
            } finally {
                IOUtils.closeQuietly(is);
            }
        }
    }
    if (addAttachment) {
        containingMessage.getAttachments().add(attachmentFile);
    } else {
        log.debug("Attachment is a duplicate, not adding as message attachment");
    }
}

From source file:com.sastix.cms.server.services.cache.hazelcast.HazelcastCacheService.java

@Override
public String getUID(String region) {
    CRC32 CRC_32 = new CRC32();
    LOG.info("HazelcastCacheService->GET_UID");
    IdGenerator idGenerator = cm.getIdGenerator(region);
    final String uid = String.valueOf(idGenerator.newId()); //assures uniqueness during the life cycle of the cluster
    final String uuid = UUID.randomUUID().toString();
    String ret = new StringBuilder(uuid).append(region).append(uid).toString();
    CRC_32.reset();//from  www .  j  a  va2  s  . c  om
    CRC_32.update(ret.getBytes());
    return Long.toHexString(CRC_32.getValue());
}

From source file:org.apache.lucene.store.FSDirectory.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.directory.getAbsolutePath()).append("_");
        CRC32 crc32 = new CRC32();
        crc32.update(0);
        long filesize = 0;
        long filemodify = 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);
                }//  w ww  .j a  va  2  s  .  co m

                try {
                    filemodify = Math.max(filemodify, this.fileModified(s));
                } catch (Throwable e) {
                    logger.error("filelength", e);
                }
            }
        }
        long crcvalue = crc32.getValue();
        buff.append(crcvalue).append("_");
        buff.append(filesize).append("_");
        buff.append(filemodify).append("_");
        buff.append(format.format(new Date(filemodify)));
        rtn = buff.toString();
        CACHE_BUFFER.put(kkk, rtn);

    }

    return rtn;
}

From source file:eu.europa.esig.dss.asic.signature.ASiCService.java

private ZipEntry getZipEntryMimeType(final byte[] mimeTypeBytes) {

    final ZipEntry entryMimetype = new ZipEntry(ZIP_ENTRY_MIMETYPE);
    entryMimetype.setMethod(ZipEntry.STORED);
    entryMimetype.setSize(mimeTypeBytes.length);
    entryMimetype.setCompressedSize(mimeTypeBytes.length);
    final CRC32 crc = new CRC32();
    crc.update(mimeTypeBytes);
    entryMimetype.setCrc(crc.getValue());
    return entryMimetype;
}

From source file:brut.androlib.res.AndrolibResources.java

public void installFramework(File frameFile, String tag) throws AndrolibException {
    InputStream in = null;/*from   w  w w.  j a  v  a2s .  c o m*/
    ZipOutputStream out = null;
    try {
        ZipFile zip = new ZipFile(frameFile);
        ZipEntry entry = zip.getEntry("resources.arsc");

        if (entry == null) {
            throw new AndrolibException("Can't find resources.arsc file");
        }

        in = zip.getInputStream(entry);
        byte[] data = IOUtils.toByteArray(in);

        ARSCData arsc = ARSCDecoder.decode(new ByteArrayInputStream(data), true, true);
        publicizeResources(data, arsc.getFlagsOffsets());

        File outFile = new File(getFrameworkDir(),
                String.valueOf(arsc.getOnePackage().getId()) + (tag == null ? "" : '-' + tag) + ".apk");

        out = new ZipOutputStream(new FileOutputStream(outFile));
        out.setMethod(ZipOutputStream.STORED);
        CRC32 crc = new CRC32();
        crc.update(data);
        entry = new ZipEntry("resources.arsc");
        entry.setSize(data.length);
        entry.setCrc(crc.getValue());
        out.putNextEntry(entry);
        out.write(data);
        zip.close();
        LOGGER.info("Framework installed to: " + outFile);
    } catch (IOException ex) {
        throw new AndrolibException(ex);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }
}

From source file:srebrinb.compress.sevenzip.SevenZOutputFile.java

/**
 * Finishes the addition of entries to this archive, without closing it.
 * /*w  ww  .  ja  v a  2  s .  co  m*/
 * @throws IOException if archive is already closed.
 */
public void finish() throws IOException {
    if (finished) {
        throw new IOException("This archive has already been finished");
    }
    finished = true;

    final long headerPosition = channel.position();

    final ByteArrayOutputStream headerBaos = new ByteArrayOutputStream();
    final DataOutputStream header = new DataOutputStream(headerBaos);

    writeHeader(header);
    header.flush();
    final byte[] headerBytes = headerBaos.toByteArray();
    channel.write(ByteBuffer.wrap(headerBytes));

    final CRC32 crc32 = new CRC32();
    crc32.update(headerBytes);

    ByteBuffer bb = ByteBuffer.allocate(SevenZFile.sevenZSignature.length + 2 /* version */
            + 4 /* start header CRC */
            + 8 /* next header position */
            + 8 /* next header length */
            + 4 /* next header CRC */).order(ByteOrder.LITTLE_ENDIAN);
    // signature header
    channel.position(0);
    bb.put(SevenZFile.sevenZSignature);
    // version
    bb.put((byte) 0).put((byte) 2);

    // placeholder for start header CRC
    bb.putInt(0);

    // start header
    bb.putLong(headerPosition - SevenZFile.SIGNATURE_HEADER_SIZE).putLong(0xffffFFFFL & headerBytes.length)
            .putInt((int) crc32.getValue());
    crc32.reset();
    crc32.update(bb.array(), SevenZFile.sevenZSignature.length + 6, 20);
    bb.putInt(SevenZFile.sevenZSignature.length + 2, (int) crc32.getValue());
    bb.flip();
    channel.write(bb);
}

From source file:odml.core.Value.java

/**
 * Base64 encodes the content if it represents either a File, URL, URI, or String that can be converted to a file.
 *
 * @param content - the content that should be encoded.
 * @return encoded content as {@link String}
 *///from   w w w. ja  v  a2 s  . c o  m
private String encodeContent(Object content) {
    if (content == null) {
        return null;
    }
    System.out.println("Encoding content: " + content.toString());
    String encoded = null;
    File file;
    if (content instanceof String) {
        try {
            URI uri = new URI((String) content);
            file = new File(uri);
        } catch (Exception e) {
            return (String) content;
        }
    } else if (content instanceof URL) {
        try {
            file = new File(((URL) content).toURI());
        } catch (Exception e) {
            System.out.println("Could not create a file from the specified URL: " + content.toString());
            file = null;
        }
    } else if (content instanceof URI) {
        try {
            file = new File((URI) content);
        } catch (Exception e) {
            System.out.println("Could not create a file from the specified URI: " + content.toString());
            file = null;
        }
    } else if (content instanceof File) {
        file = (File) content;
    } else {
        System.out.println("Could not create a File from input! Class: " + content.getClass().getSimpleName()
                + " Content: " + content.toString());
        file = null;
    }
    if (file == null) {
        return "";
    }
    Base64 enc = new Base64();
    //the value has to be converted to String; if it is already just take it, if it is not
    //try different things 
    try {
        byte[] bytes = enc.encode(getBytesFromFile(file));
        CRC32 crc = new CRC32();
        crc.update(bytes);
        this.setChecksum("CRC32$" + crc.getValue());
        this.setFilename(file.getName());
        this.setEncoder("Base64");
        encoded = new String(bytes, "UTF-8");
    } catch (Exception e) {
        System.out.println("An error occurred during encoding: " + e.getLocalizedMessage());
    }
    return encoded;
}

From source file:org.smartfrog.services.www.bulkio.client.SunJavaBulkIOClient.java

@Override
public long doUpload(String method, long ioSize) throws IOException, InterruptedException {
    validateURL();//  w w  w .  j a v  a 2s.  c om
    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;
}