Example usage for java.util.zip CheckedInputStream getChecksum

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

Introduction

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

Prototype

public Checksum getChecksum() 

Source Link

Document

Returns the Checksum for this input stream.

Usage

From source file:com.panet.imeta.core.row.ValueDataUtil.java

public static Long ChecksumAdler32(ValueMetaInterface metaA, Object dataA) {
    long checksum = 0;
    FileObject file = null;/* w ww  .  j ava2 s .  c  o  m*/
    try {
        file = KettleVFS.getFileObject(dataA.toString());
        CheckedInputStream cis = null;

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

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

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

From source file:com.adito.core.CoreUtil.java

/**
 * @param f/*from   w w w  .  j av  a2 s . c  o m*/
 * @return long
 * @throws IOException
 */
public static long generateChecksum(File f) throws IOException {
    Adler32 alder = new Adler32();
    FileInputStream fin = new FileInputStream(f);
    CheckedInputStream in = new CheckedInputStream(fin, alder);
    byte[] buf = new byte[32768];
    Util.readFullyIntoBuffer(in, buf);
    alder = (Adler32) in.getChecksum();
    try {
        in.close();
    } catch (IOException ex) {
    }
    try {
        fin.close();
    } catch (IOException ex1) {
    }
    return alder.getValue();
}

From source file:com.sqli.liferay.imex.util.xml.ZipUtils.java

public void _unzipArchive(File archive, File outputDir) throws IOException {
    BufferedOutputStream dest = null;
    FileInputStream fis = new FileInputStream(archive);
    CheckedInputStream checksum = new CheckedInputStream(fis, new Adler32());
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(checksum));
    ZipEntry entry;/* w  w  w.  j a  v  a  2 s  .c o m*/
    while ((entry = zis.getNextEntry()) != null) {
        log.debug("Extracting: " + entry);
        int count;
        byte data[] = new byte[BUFFER];
        // write the files to the disk
        FileOutputStream fos = new FileOutputStream(entry.getName());
        dest = new BufferedOutputStream(fos, BUFFER);
        while ((count = zis.read(data, 0, BUFFER)) != -1) {
            dest.write(data, 0, count);
        }
        dest.flush();
        dest.close();
    }
    zis.close();
    log.debug("Checksum:" + checksum.getChecksum().getValue());

}

From source file:com.alcatel_lucent.nz.wnmsextract.reader.FileUtilities.java

public long checksum(File input) {
    long checksum = -1;
    try {//from   w w w .  j  av a 2  s.c om
        FileInputStream fis = null;
        CheckedInputStream cis = null;
        Adler32 adler = null;
        fis = new FileInputStream(input);
        adler = new Adler32();
        cis = new CheckedInputStream(fis, adler);

        byte[] buffer = new byte[1024];
        while (cis.read(buffer) >= 0) {
            checksum = cis.getChecksum().getValue();
        }

    } catch (IOException e) {
        jlog.fatal("IO Exception on " + input.getAbsolutePath() + e);
    }

    return checksum;

}

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).
 * /*w w  w .ja v  a  2s  .c o  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  va 2  s.c  o m
        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: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  w w  w. j  a  va  2 s .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: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  av a 2  s .c om
    return cinStream.getChecksum().getValue();
}

From source file:org.broadleafcommerce.common.util.StringUtil.java

public static long getChecksum(String test) {
    try {/*from   w w  w .  j  a v  a 2  s.  c  o  m*/
        byte buffer[] = test.getBytes();
        ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
        CheckedInputStream cis = new CheckedInputStream(bais, new Adler32());
        byte readBuffer[] = new byte[buffer.length];
        cis.read(readBuffer);
        return cis.getChecksum().getValue();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.cloudata.core.commitlog.CommitLogServer.java

public long getChecksum(String dirName) throws IOException {
    File logFile = getLogFile(dirName);

    long fileLength = logFile.length();

    CheckedInputStream cksumIn = new CheckedInputStream(new FileInputStream(logFile), new CRC32());
    BufferedInputStream in = new BufferedInputStream(cksumIn, 8192);

    for (long i = 0; i < fileLength; i++) {
        in.read();// w ww .java  2 s. com
    }

    return cksumIn.getChecksum().getValue();
}