Example usage for java.util.zip Adler32 Adler32

List of usage examples for java.util.zip Adler32 Adler32

Introduction

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

Prototype

public Adler32() 

Source Link

Document

Creates a new Adler32 object.

Usage

From source file:fr.itldev.koya.alfservice.KoyaContentService.java

/**
 *
 * @param nodeRefs//from ww  w .ja  va 2s  .c  o m
 * @return
 * @throws KoyaServiceException
 */
public File zip(List<String> nodeRefs) throws KoyaServiceException {
    File tmpZipFile = null;
    try {
        tmpZipFile = TempFileProvider.createTempFile("tmpDL", ".zip");
        FileOutputStream fos = new FileOutputStream(tmpZipFile);
        CheckedOutputStream checksum = new CheckedOutputStream(fos, new Adler32());
        BufferedOutputStream buff = new BufferedOutputStream(checksum);
        ZipArchiveOutputStream zipStream = new ZipArchiveOutputStream(buff);
        // NOTE: This encoding allows us to workaround bug...
        // http://bugs.sun.com/bugdatabase/view_bug.do;:WuuT?bug_id=4820807
        zipStream.setEncoding("UTF-8");

        zipStream.setMethod(ZipArchiveOutputStream.DEFLATED);
        zipStream.setLevel(Deflater.BEST_COMPRESSION);

        zipStream.setCreateUnicodeExtraFields(ZipArchiveOutputStream.UnicodeExtraFieldPolicy.ALWAYS);
        zipStream.setUseLanguageEncodingFlag(true);
        zipStream.setFallbackToUTF8(true);

        try {
            for (String nodeRef : nodeRefs) {
                addToZip(koyaNodeService.getNodeRef(nodeRef), zipStream, "");
            }
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
        } finally {
            zipStream.close();
            buff.close();
            checksum.close();
            fos.close();

        }
    } catch (IOException | WebScriptException e) {
        logger.error(e.getMessage(), e);
        throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
    }

    return tmpZipFile;
}

From source file:org.theospi.portfolio.help.HelpManagerImpl.java

public void packageGlossaryForExport(Id worksiteId, OutputStream os) throws IOException {
    getAuthzManager().checkPermission(HelpFunctionConstants.EXPORT_TERMS, getToolId());

    CheckedOutputStream checksum = new CheckedOutputStream(os, new Adler32());
    ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(checksum));

    putWorksiteTermsIntoZip(worksiteId, zos);

    zos.finish();/*from w ww.j  a  v a2 s  . c om*/
    zos.flush();
}

From source file:org.pentaho.di.core.row.ValueDataUtil.java

public static Long ChecksumAdler32(ValueMetaInterface metaA, Object dataA) {
    long checksum = 0;
    FileObject file = null;/*from   ww w .  j  av  a 2s. com*/
    try {
        file = KettleVFS.getFileObject(dataA.toString());
        CheckedInputStream cis = null;

        // Computer Adler-32 checksum
        cis = new CheckedInputStream(((LocalFile) file).getInputStream(), new Adler32());

        byte[] buf = new byte[128];
        int readSize = 0;
        do {
            readSize = cis.read(buf);
        } while (readSize >= 0);
        checksum = cis.getChecksum().getValue();

    } catch (Exception e) {
        // throw new Exception(e);
    } finally {
        if (file != null) {
            try {
                file.close();
                file = null;
            } catch (Exception e) {
                // Ignore
            }
        }
    }
    return checksum;
}

From source file:es.mityc.firmaJava.libreria.utilidades.CopiaFicherosManager.java

/**
 * Comprueba si el fichero indicado es ntegro.
 * /*w  w  w.  j a  v  a2s.  co m*/
 * @param file
 * @param crcValue
 * @param size
 * @return
 */
private boolean checkIntegrityFile(File file, long crcValue, long size)
        throws FileNotFoundException, IOException {
    if (log.isTraceEnabled())
        log.trace(I18n.getResource(ConstantesXADES.LIBRERIAXADES_FIRMAMOZILLA_INFO_6) + ConstantesXADES.ESPACIO
                + file.getAbsolutePath());
    if (!file.exists())
        return false;
    // Primero comprueba el tamao
    if (file.length() != size)
        return false;
    // despus comprueba el crc
    Adler32 crc = new Adler32();
    InputStream entrada = new BufferedInputStream(new FileInputStream(file), BUFFER_IN_SIZE);
    byte[] buffer = new byte[BUFFER_OUT_SIZE];
    int readed = entrada.read(buffer);
    while (readed > 0) {
        crc.update(buffer, 0, readed);
        readed = entrada.read(buffer);
    }
    if (log.isTraceEnabled())
        log.trace(I18n.getResource(ConstantesXADES.LIBRERIAXADES_FIRMAMOZILLA_INFO_7) + ConstantesXADES.ESPACIO
                + crc.getValue() + ConstantesXADES.ESPACIO
                + I18n.getResource(ConstantesXADES.LIBRERIAXADES_FIRMAMOZILLA_INFO_8) + ConstantesXADES.ESPACIO
                + crcValue);
    if (crc.getValue() != crcValue)
        return false;
    return true;
}

From source file:org.talend.utils.io.FilesUtils.java

public static long getChecksumAlder32(File file) throws IOException {
    BufferedInputStream bufferedInputStream = null;
    CheckedInputStream cis = null;
    try {/* w w w. j  a v a2 s  . c o  m*/
        bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
        // Compute Adler-32 checksum
        cis = new CheckedInputStream(bufferedInputStream, new Adler32());
        byte[] tempBuf = new byte[128];
        while (cis.read(tempBuf) >= 0) {
            // do nothing
        }
        return cis.getChecksum().getValue();
    } catch (IOException e) {
        throw e;
    } finally {
        if (bufferedInputStream != null) {
            bufferedInputStream.close();
        }
        if (cis != null) {
            cis.close();
        }
    }
}

From source file:it.cnr.icar.eric.server.query.CompressContentQueryFilterPlugin.java

private ZipOutputStream getZipOutputStream(File file) throws FileNotFoundException {
    //ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file));
    //zos.setMethod(ZipOutputStream.STORED);
    FileOutputStream dest = new FileOutputStream(file);
    CheckedOutputStream checksum = new CheckedOutputStream(dest, new Adler32());
    ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(checksum));
    return zos;/* w  w  w  . j a  va2s  . c  o m*/
}

From source file:watne.seis720.project.AES_Utilities.java

/**
 * Compute the Adler-32 checksum of the specified file. Code based on
 * &quot;e457. Calculating the Checksum of a File&quot;, &quot;The Java
 * Developers Almanac 1.4&quot;, <a
 * href="http://www.exampledepot.com/egs/java.util.zip/ChecksumFile.html">http://www.exampledepot.com/egs/java.util.zip/ChecksumFile.html</a>
 * @param checkFile the file to be checked.
 * @return the Adler-32 checkusm of the specified file.
 * @throws IOException if an error occurs reading checkFile.
 *//* www. j  av a 2 s.  c  o  m*/
public static long getFileChecksum(File checkFile) throws IOException {
    long checksum = -1;

    CheckedInputStream cis = new CheckedInputStream(new FileInputStream(checkFile), new Adler32());
    byte[] tempBuf = new byte[128];

    while (cis.read(tempBuf) >= 0) {

    }
    checksum = cis.getChecksum().getValue();
    return checksum;
}

From source file:net.sourceforge.pmd.RuleSetFactory.java

/**
 * Parse a ruleset node to construct a RuleSet.
 *
 * @param ruleSetReferenceId//from  w  w w  .  j  ava  2  s  . c  om
 *            The RuleSetReferenceId of the RuleSet being parsed.
 * @param withDeprecatedRuleReferences
 *            whether rule references that are deprecated should be ignored
 *            or not
 * @return The new RuleSet.
 */
private RuleSet parseRuleSetNode(RuleSetReferenceId ruleSetReferenceId, boolean withDeprecatedRuleReferences)
        throws RuleSetNotFoundException {
    try (CheckedInputStream inputStream = new CheckedInputStream(
            ruleSetReferenceId.getInputStream(resourceLoader), new Adler32());) {
        if (!ruleSetReferenceId.isExternal()) {
            throw new IllegalArgumentException(
                    "Cannot parse a RuleSet from a non-external reference: <" + ruleSetReferenceId + ">.");
        }
        DocumentBuilder builder = createDocumentBuilder();
        InputSource inputSource;
        if (compatibilityFilter != null) {
            inputSource = new InputSource(compatibilityFilter.filterRuleSetFile(inputStream));
        } else {
            inputSource = new InputSource(inputStream);
        }
        Document document = builder.parse(inputSource);
        Element ruleSetElement = document.getDocumentElement();

        RuleSetBuilder ruleSetBuilder = new RuleSetBuilder(inputStream.getChecksum().getValue())
                .withFileName(ruleSetReferenceId.getRuleSetFileName());

        if (ruleSetElement.hasAttribute("name")) {
            ruleSetBuilder.withName(ruleSetElement.getAttribute("name"));
        } else {
            LOG.warning("RuleSet name is missing. Future versions of PMD will require it.");
            ruleSetBuilder.withName("Missing RuleSet Name");
        }

        NodeList nodeList = ruleSetElement.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                String nodeName = node.getNodeName();
                if (DESCRIPTION.equals(nodeName)) {
                    ruleSetBuilder.withDescription(parseTextNode(node));
                } else if ("include-pattern".equals(nodeName)) {
                    ruleSetBuilder.addIncludePattern(parseTextNode(node));
                } else if ("exclude-pattern".equals(nodeName)) {
                    ruleSetBuilder.addExcludePattern(parseTextNode(node));
                } else if ("rule".equals(nodeName)) {
                    parseRuleNode(ruleSetReferenceId, ruleSetBuilder, node, withDeprecatedRuleReferences);
                } else {
                    throw new IllegalArgumentException(UNEXPECTED_ELEMENT + node.getNodeName()
                            + "> encountered as child of <ruleset> element.");
                }
            }
        }

        if (!ruleSetBuilder.hasDescription()) {
            LOG.warning("RuleSet description is missing. Future versions of PMD will require it.");
            ruleSetBuilder.withDescription("Missing description");
        }

        ruleSetBuilder.filterRulesByPriority(minimumPriority);

        return ruleSetBuilder.build();
    } catch (ReflectiveOperationException ex) {
        ex.printStackTrace();
        throw new RuntimeException("Couldn't find the class " + ex.getMessage(), ex);
    } catch (ParserConfigurationException | IOException | SAXException ex) {
        ex.printStackTrace();
        throw new RuntimeException("Couldn't read the ruleset " + ruleSetReferenceId + ": " + ex.getMessage(),
                ex);
    }
}

From source file:org.apache.cassandra.db.VerifyTest.java

protected long simpleFullChecksum(String filename) throws IOException {
    FileInputStream inputStream = new FileInputStream(filename);
    Adler32 adlerChecksum = new Adler32();
    CheckedInputStream cinStream = new CheckedInputStream(inputStream, adlerChecksum);
    byte[] b = new byte[128];
    while (cinStream.read(b) >= 0) {
    }//from www .ja v a2  s . c o m
    return cinStream.getChecksum().getValue();
}

From source file:org.efaps.db.databases.OracleDatabase.java

/**
 * @param _name name/*from   w w w .  j  a  v  a  2s  . c om*/
 * @param _maxLength maximum length
 * @return new name
 * @throws EFapsException on error
 */
protected String getName4DB(final String _name, final int _maxLength) throws EFapsException {
    String ret = _name;
    if (_name.length() > 30) {
        try {
            final byte[] buffer = _name.getBytes("UTF8");
            final ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
            final CheckedInputStream cis = new CheckedInputStream(bais, new Adler32());
            final byte[] readBuffer = new byte[5];
            long value = 0;
            while (cis.read(readBuffer) >= 0) {
                value = cis.getChecksum().getValue();
            }
            final String valueSt = String.valueOf(value);
            ret = ret.substring(0, 30);
            final int sizeSuf = ret.length() - valueSt.length();
            ret = ret.substring(0, sizeSuf) + value;
        } catch (final UnsupportedEncodingException e) {
            throw new EFapsException("UnsupportedEncodingException", e);
        } catch (final IOException e) {
            throw new EFapsException("IOException", e);
        }
    }
    return ret;
}