Search in sources :

Example 11 with CheckedInputStream

use of java.util.zip.CheckedInputStream in project zookeeper by apache.

the class FileSnap method deserialize.

/**
     * deserialize a data tree from the most recent snapshot
     * @return the zxid of the snapshot
     */
public long deserialize(DataTree dt, Map<Long, Integer> sessions) throws IOException {
    // we run through 100 snapshots (not all of them)
    // if we cannot get it running within 100 snapshots
    // we should  give up
    List<File> snapList = findNValidSnapshots(100);
    if (snapList.size() == 0) {
        return -1L;
    }
    File snap = null;
    boolean foundValid = false;
    for (int i = 0; i < snapList.size(); i++) {
        snap = snapList.get(i);
        InputStream snapIS = null;
        CheckedInputStream crcIn = null;
        try {
            LOG.info("Reading snapshot " + snap);
            snapIS = new BufferedInputStream(new FileInputStream(snap));
            crcIn = new CheckedInputStream(snapIS, new Adler32());
            InputArchive ia = BinaryInputArchive.getArchive(crcIn);
            deserialize(dt, sessions, ia);
            long checkSum = crcIn.getChecksum().getValue();
            long val = ia.readLong("val");
            if (val != checkSum) {
                throw new IOException("CRC corruption in snapshot :  " + snap);
            }
            foundValid = true;
            break;
        } catch (IOException e) {
            LOG.warn("problem reading snap file " + snap, e);
        } finally {
            if (snapIS != null)
                snapIS.close();
            if (crcIn != null)
                crcIn.close();
        }
    }
    if (!foundValid) {
        throw new IOException("Not able to find valid snapshots in " + snapDir);
    }
    dt.lastProcessedZxid = Util.getZxidFromName(snap.getName(), "snapshot");
    return dt.lastProcessedZxid;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) CheckedInputStream(java.util.zip.CheckedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) InputArchive(org.apache.jute.InputArchive) BinaryInputArchive(org.apache.jute.BinaryInputArchive) IOException(java.io.IOException) File(java.io.File) CheckedInputStream(java.util.zip.CheckedInputStream) FileInputStream(java.io.FileInputStream) Adler32(java.util.zip.Adler32)

Example 12 with CheckedInputStream

use of java.util.zip.CheckedInputStream in project pinot by linkedin.

the class CrcUtils method computeCrc.

public long computeCrc() {
    CheckedInputStream cis = null;
    InputStream is = null;
    final Checksum checksum = new Adler32();
    final byte[] tempBuf = new byte[BUFFER_SIZE];
    for (final File file : filesToProcess) {
        try {
            is = new BufferedInputStream(new FileInputStream(file));
            cis = new CheckedInputStream(is, checksum);
            while (cis.read(tempBuf) >= 0) {
            }
            cis.close();
            is.close();
        } catch (final Exception e) {
            LOGGER.error("Caught exception while computing CRC", e);
            Utils.rethrowException(e);
            throw new AssertionError("Should not reach this");
        }
    }
    return checksum.getValue();
}
Also used : BufferedInputStream(java.io.BufferedInputStream) CheckedInputStream(java.util.zip.CheckedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Checksum(java.util.zip.Checksum) File(java.io.File) CheckedInputStream(java.util.zip.CheckedInputStream) Adler32(java.util.zip.Adler32) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 13 with CheckedInputStream

use of java.util.zip.CheckedInputStream in project platform_frameworks_base by android.

the class FileUtils method checksumCrc32.

/**
     * Computes the checksum of a file using the CRC32 checksum routine.
     * The value of the checksum is returned.
     *
     * @param file  the file to checksum, must not be null
     * @return the checksum value or an exception is thrown.
     */
public static long checksumCrc32(File file) throws FileNotFoundException, IOException {
    CRC32 checkSummer = new CRC32();
    CheckedInputStream cis = null;
    try {
        cis = new CheckedInputStream(new FileInputStream(file), checkSummer);
        byte[] buf = new byte[128];
        while (cis.read(buf) >= 0) {
        // Just read for checksum to get calculated.
        }
        return checkSummer.getValue();
    } finally {
        if (cis != null) {
            try {
                cis.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : CRC32(java.util.zip.CRC32) IOException(java.io.IOException) CheckedInputStream(java.util.zip.CheckedInputStream) FileInputStream(java.io.FileInputStream)

Example 14 with CheckedInputStream

use of java.util.zip.CheckedInputStream in project android_frameworks_base by AOSPA.

the class FileUtils method checksumCrc32.

/**
     * Computes the checksum of a file using the CRC32 checksum routine.
     * The value of the checksum is returned.
     *
     * @param file  the file to checksum, must not be null
     * @return the checksum value or an exception is thrown.
     */
public static long checksumCrc32(File file) throws FileNotFoundException, IOException {
    CRC32 checkSummer = new CRC32();
    CheckedInputStream cis = null;
    try {
        cis = new CheckedInputStream(new FileInputStream(file), checkSummer);
        byte[] buf = new byte[128];
        while (cis.read(buf) >= 0) {
        // Just read for checksum to get calculated.
        }
        return checkSummer.getValue();
    } finally {
        if (cis != null) {
            try {
                cis.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : CRC32(java.util.zip.CRC32) IOException(java.io.IOException) CheckedInputStream(java.util.zip.CheckedInputStream) FileInputStream(java.io.FileInputStream)

Example 15 with CheckedInputStream

use of java.util.zip.CheckedInputStream in project screenbird by adamhub.

the class FileUtil method getChecksum.

public static String getChecksum(String filePath) throws FileNotFoundException, IOException {
    FileInputStream file = new FileInputStream(filePath);
    CheckedInputStream check = new CheckedInputStream(file, new CRC32());
    BufferedInputStream in = new BufferedInputStream(check);
    String checkSum = "";
    while (in.read() != -1) {
    // Read file in completely
    }
    if (in != null) {
        in.close();
    }
    if (file != null)
        file.close();
    if (check != null) {
        checkSum = String.valueOf(check.getChecksum().getValue());
        check.close();
    }
    return checkSum;
}
Also used : CRC32(java.util.zip.CRC32) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) CheckedInputStream(java.util.zip.CheckedInputStream)

Aggregations

CheckedInputStream (java.util.zip.CheckedInputStream)18 FileInputStream (java.io.FileInputStream)14 IOException (java.io.IOException)13 CRC32 (java.util.zip.CRC32)11 BufferedInputStream (java.io.BufferedInputStream)8 InputStream (java.io.InputStream)6 Adler32 (java.util.zip.Adler32)6 File (java.io.File)3 BinaryInputArchive (org.apache.jute.BinaryInputArchive)3 InputArchive (org.apache.jute.InputArchive)3 MimePartWithoutContentException (com.axway.ats.action.objects.model.MimePartWithoutContentException)1 NoSuchMimePackageException (com.axway.ats.action.objects.model.NoSuchMimePackageException)1 PackageException (com.axway.ats.action.objects.model.PackageException)1 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 SeekInputStream (com.axway.ats.core.io.SeekInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Enumeration (java.util.Enumeration)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1