Example usage for java.util.zip CheckedInputStream CheckedInputStream

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

Introduction

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

Prototype

public CheckedInputStream(InputStream in, Checksum cksum) 

Source Link

Document

Creates an input stream using the specified Checksum.

Usage

From source file:org.openmrs.module.sync.SyncUtil.java

public static String decompress(byte[] data) throws IOException {
    ByteArrayInputStream bais2 = new ByteArrayInputStream(data);
    CheckedInputStream cis = new CheckedInputStream(bais2, new CRC32());
    GZIPInputStream zis = new GZIPInputStream(new BufferedInputStream(cis));
    InputStreamReader reader = new InputStreamReader(zis);
    BufferedReader br = new BufferedReader(reader);
    StringBuffer buffer = new StringBuffer();
    String line = "";
    while ((line = br.readLine()) != null) {
        buffer.append(line);//from ww w  .  j ava2s  .c  om
    }
    return buffer.toString();
}

From source file:com.mozilla.SUTAgentAndroid.service.DoCommand.java

public String Unzip(String zipFileName, String dstDirectory) {
    String sRet = "";
    String fixedZipFileName = fixFileName(zipFileName);
    String fixedDstDirectory = fixFileName(dstDirectory);
    String dstFileName = "";
    int nNumExtracted = 0;
    boolean bRet = false;

    try {/*from ww w .  ja v a 2s  .  co  m*/
        final int BUFFER = 2048;
        BufferedOutputStream dest = null;
        ZipFile zipFile = new ZipFile(fixedZipFileName);
        int nNumEntries = zipFile.size();
        zipFile.close();

        FileInputStream fis = new FileInputStream(fixedZipFileName);
        CheckedInputStream checksum = new CheckedInputStream(fis, new Adler32());
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(checksum));
        ZipEntry entry;

        byte[] data = new byte[BUFFER];

        while ((entry = zis.getNextEntry()) != null) {
            System.out.println("Extracting: " + entry);
            int count;
            if (fixedDstDirectory.length() > 0)
                dstFileName = fixedDstDirectory + entry.getName();
            else
                dstFileName = entry.getName();

            String tmpDir = dstFileName.substring(0, dstFileName.lastIndexOf('/'));
            File tmpFile = new File(tmpDir);
            if (!tmpFile.exists()) {
                bRet = tmpFile.mkdirs();
            } else
                bRet = true;

            if (bRet) {
                // if we aren't just creating a directory
                if (dstFileName.lastIndexOf('/') != (dstFileName.length() - 1)) {
                    // write out the file
                    FileOutputStream fos = new FileOutputStream(dstFileName);
                    dest = new BufferedOutputStream(fos, BUFFER);
                    while ((count = zis.read(data, 0, BUFFER)) != -1) {
                        dest.write(data, 0, count);
                    }
                    dest.flush();
                    dest.close();
                    dest = null;
                    fos.close();
                    fos = null;
                }
                nNumExtracted++;
            } else
                sRet += " - failed" + lineSep;
        }

        data = null;
        zis.close();
        System.out.println("Checksum:          " + checksum.getChecksum().getValue());
        sRet += "Checksum:          " + checksum.getChecksum().getValue();
        sRet += lineSep + nNumExtracted + " of " + nNumEntries + " successfully extracted";
    } catch (Exception e) {
        e.printStackTrace();
    }

    return (sRet);
}

From source file:com.cisco.dvbu.ps.common.util.CommonUtils.java

/**
 * Returns a CRC32 checksum of a file as a whole (as opposed to sum of checksums of all lines/rows).
 * /*from   ww w. j av a 2  s.  co m*/
 * @param filePath      file name with full path
 * @return            checksum value for the input file
 * @throws IOException
 */
public static long fileChecksum(String filePath) throws IOException {

    long checkSumValue = 0L;

    FileInputStream file = new FileInputStream(filePath);

    CheckedInputStream check = new CheckedInputStream(file, new CRC32());

    BufferedInputStream in = new BufferedInputStream(check);

    while (in.read() != -1) {
        // Read file in completely
    }
    checkSumValue = check.getChecksum().getValue();
    //      System.out.println("fileChecksum(): checkSumValue = " + checkSumValue);
    return checkSumValue;
}

From source file:com.neusoft.clw.infomanage.ridingplan.action.RidingPlanAction.java

private String doChecksum(String fileName) {
    long checksum = 0;
    try {//  w  w w  .j  a v a  2s .  c om
        CheckedInputStream cis = null;
        try {
            // Computer CRC32 checksum
            cis = new CheckedInputStream(new FileInputStream(fileName), new CRC32());
        } catch (FileNotFoundException e) {
            LOG.error("File not found.");
        }

        byte[] buf = new byte[128];
        while (cis.read(buf) >= 0) {
        }
        checksum = cis.getChecksum().getValue();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return String.valueOf(checksum);
}

From source file:cn.com.sinosoft.util.io.FileUtils.java

/**
 * Computes the checksum of a file using the specified checksum object.
 * Multiple files may be checked using one <code>Checksum</code> instance
 * if desired simply by reusing the same checksum object.
 * For example://w w w. j  a  va 2 s. co m
 * <pre>
 *   long csum = FileUtils.checksum(file, new CRC32()).getValue();
 * </pre>
 *
 * @param file  the file to checksum, must not be <code>null</code>
 * @param checksum  the checksum object to be used, must not be <code>null</code>
 * @return the checksum specified, updated with the content of the file
 * @throws NullPointerException if the file or checksum is <code>null</code>
 * @throws IllegalArgumentException if the file is a directory
 * @throws IOException if an IO error occurs reading the file
 * @since Commons IO 1.3
 */
public static Checksum checksum(File file, Checksum checksum) throws IOException {
    if (file.isDirectory()) {
        throw new IllegalArgumentException("Checksums can't be computed on directories");
    }
    InputStream in = null;
    try {
        in = new CheckedInputStream(new FileInputStream(file), checksum);
        IOUtils.copy(in, new NullOutputStream());
    } finally {
        IOUtils.closeQuietly(in);
    }
    return checksum;
}

From source file:com.clark.func.Functions.java

/**
 * Computes the checksum of a file using the specified checksum object.
 * Multiple files may be checked using one <code>Checksum</code> instance if
 * desired simply by reusing the same checksum object. For example:
 * //from  w  w w  .  ja  va2 s  .c  o m
 * <pre>
 * long csum = FileUtils.checksum(file, new CRC32()).getValue();
 * </pre>
 * 
 * @param file
 *            the file to checksum, must not be <code>null</code>
 * @param checksum
 *            the checksum object to be used, must not be <code>null</code>
 * @return the checksum specified, updated with the content of the file
 * @throws NullPointerException
 *             if the file or checksum is <code>null</code>
 * @throws IllegalArgumentException
 *             if the file is a directory
 * @throws IOException
 *             if an IO error occurs reading the file
 * @since Commons IO 1.3
 */
public static Checksum checksum(File file, Checksum checksum) throws IOException {
    if (file.isDirectory()) {
        throw new IllegalArgumentException("Checksums can't be computed on directories");
    }
    InputStream in = null;
    try {
        in = new CheckedInputStream(new FileInputStream(file), checksum);
        copy(in, new NullOutputStream());
    } finally {
        closeQuietly(in);
    }
    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
            }//from w  w  w  .  ja  v  a  2s  . 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:net.sourceforge.pmd.cache.AnalysisResult.java

private static long computeFileChecksum(final File sourceFile) {
    try (CheckedInputStream stream = new CheckedInputStream(
            new BufferedInputStream(Files.newInputStream(sourceFile.toPath())), new Adler32());) {
        // Just read it, the CheckedInputStream will update the checksum on it's own
        IOUtils.skipFully(stream, sourceFile.length());

        return stream.getChecksum().getValue();
    } catch (final IOException ignored) {
        // We don't really care, if it's unreadable
        // the analysis will fail and report the error on it's own since the checksum won't match
    }//w w w. j  ava2  s.  c  o  m

    return 0;
}

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

/**
 * Parse a ruleset node to construct a RuleSet.
 *
 * @param ruleSetReferenceId// w  w w  . j  a va2 s  .  com
 *            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 w  ww .  j a v  a  2 s . c  o  m*/
    return cinStream.getChecksum().getValue();
}