Example usage for java.util.zip CheckedInputStream read

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

Introduction

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

Prototype

public int read(byte b[]) throws IOException 

Source Link

Document

Reads up to b.length bytes of data from this input stream into an array of bytes.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    CheckedInputStream cis = new CheckedInputStream(new FileInputStream("filename"), new Adler32());
    byte[] tempBuf = new byte[128];
    while (cis.read(tempBuf) >= 0) {
    }/* ww w  .j  av  a  2s .  com*/
    long checksum = cis.getChecksum().getValue();
}

From source file:Main.java

public static String getCRC32(File file) {
    CRC32 crc32 = new CRC32();
    CheckedInputStream checkedinputstream = null;
    String crc = null;//w  w  w .j  a va2s  .c  o  m
    try {
        checkedinputstream = new CheckedInputStream(new FileInputStream(file), crc32);
        byte[] buf = new byte[1024];
        while (checkedinputstream.read(buf) >= 0) {
        }
        crc = Long.toHexString(crc32.getValue()).toUpperCase();
    } catch (Exception e) {
        Log.w(TAG, e);
        Log.e("WxException", e.getMessage(), e);
    } finally {
        if (checkedinputstream != null) {
            try {
                checkedinputstream.close();
            } catch (IOException e) {
            }
        }
    }
    return crc;
}

From source file:AbstractFileSystemHelper.java

public static long getChecksum(InputStream is) {
    CheckedInputStream cis = null;
    long checksum = 0;
    try {//w w w  .  java 2s. c om
        cis = new CheckedInputStream(is, new Adler32());
        byte[] tempBuf = new byte[128];
        while (cis.read(tempBuf) >= 0) {
        }
        checksum = cis.getChecksum().getValue();
    } catch (IOException e) {
        checksum = 0;
    } finally {
        if (cis != null) {
            try {
                cis.close();
            } catch (IOException ioe) {
            }
        }
    }
    return checksum;
}

From source file:com.yifanlu.PSXperiaTool.ZpakCreate.java

public static long getCRC32(File file) throws IOException {
    CheckedInputStream cis = null;
    long fileSize = 0;
    // Computer CRC32 checksum
    cis = new CheckedInputStream(new FileInputStream(file), new CRC32());

    fileSize = file.length();/*from  ww w .j  a va  2 s . c om*/

    byte[] buf = new byte[128];
    while (cis.read(buf) != -1)
        ;

    long checksum = cis.getChecksum().getValue();
    cis.close();
    Logger.verbose("CRC32 of %s is %d", file.getPath(), checksum);
    return checksum;
}

From source file:MultiFileChecksumHelper.java

public static long getChecksum(File[] files) {
    CheckedInputStream cis = null;
    FileInputStream is = null;/* w  ww  .  j  a  v a 2s .  co m*/
    Checksum checksum = new Adler32();
    byte[] tempBuf = new byte[128];

    for (int i = 0; i < files.length && files[i] != null && files[i].exists() && files[i].isFile(); i++) {
        try {
            is = new FileInputStream(files[i]);
            cis = new CheckedInputStream(is, checksum);
            while (cis.read(tempBuf) >= 0) {
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (cis != null) {
                try {
                    cis.close();
                } catch (IOException ioe) {
                }
                cis = null;
            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ioe) {
                }
                is = null;
            }
        }
    }
    return checksum.getValue();
}

From source file:com.blockwithme.longdb.tools.Utils.java

/** Get checksum CRC32 for the file content.
 * /*from w  ww .  java2  s  .c om*/
 * @param theFile
 *        the file
 * @param isCommpressed
 *        true if commpressed
 * @return the checksum (CRC32) of the file.
 * @throws Exception */
@SuppressWarnings("resource")
public static long getCRC32(final File theFile, final boolean isCommpressed) throws Exception {
    CheckedInputStream cis = null;
    try {
        final CRC32 checksum = new CRC32();
        // TODO: Would a buffered input stream make this faster?
        cis = new CheckedInputStream((isCommpressed ? new GZIPInputStream(new FileInputStream(theFile))
                : new FileInputStream(theFile)), checksum);
        final byte[] tempBuf = new byte[ONE_K];
        while (cis.read(tempBuf) >= 0)
            ; // just read the full stream. // $codepro.audit.disable
        return checksum.getValue();
    } finally {
        if (cis != null)
            cis.close();
    }
}

From source file:net.sourceforge.jaulp.file.checksum.ChecksumUtils.java

/**
 * Gets the checksum from the given file. If the flag crc is true than the CheckedInputStream is
 * constructed with an instance of <code>java.util.zip.CRC32</code> otherwise with an instance
 * of <code>java.util.zip.Adler32</code>.
 *
 * @param file/*from   ww w. ja va 2s . c  o m*/
 *            The file The file from what to get the checksum.
 * @param crc
 *            The crc If the flag crc is true than the CheckedInputStream is constructed with an
 *            instance of {@link java.util.zip.CRC32} object otherwise it is constructed with an
 *            instance of
 * @return The checksum from the given file as long.
 * @throws FileNotFoundException
 *             Is thrown if the file is not found.
 * @throws IOException
 *             Signals that an I/O exception has occurred. {@link java.util.zip.CRC32} object
 *             otherwise it is constructed with an instance of {@link java.util.zip.Adler32}
 *             object. {@link java.util.zip.Adler32} object.
 */
public static long getChecksum(File file, boolean crc) throws FileNotFoundException, IOException {
    CheckedInputStream cis = null;
    if (crc) {
        cis = new CheckedInputStream(new FileInputStream(file), new CRC32());
    } else {
        cis = new CheckedInputStream(new FileInputStream(file), new Adler32());
    }
    int length = (int) file.length();
    byte[] buffer = new byte[length];
    long checksum = 0;
    while (cis.read(buffer) >= 0) {
        checksum = cis.getChecksum().getValue();
    }
    checksum = cis.getChecksum().getValue();
    StreamUtils.closeInputStream(cis);
    return checksum;
}

From source file:de.alpharogroup.file.checksum.ChecksumExtensions.java

/**
 * Gets the checksum from the given file. If the flag crc is true than the CheckedInputStream is
 * constructed with an instance of <code>java.util.zip.CRC32</code> otherwise with an instance
 * of <code>java.util.zip.Adler32</code>.
 *
 * @param file/*from   w  w  w . j  a va2  s  . c o  m*/
 *            The file The file from what to get the checksum.
 * @param crc
 *            The crc If the flag crc is true than the CheckedInputStream is constructed with an
 *            instance of {@link java.util.zip.CRC32} object otherwise it is constructed with an
 *            instance of
 * @return The checksum from the given file as long.
 * @throws FileNotFoundException
 *             Is thrown if the file is not found.
 * @throws IOException
 *             Signals that an I/O exception has occurred. {@link java.util.zip.CRC32} object
 *             otherwise it is constructed with an instance of {@link java.util.zip.Adler32}
 *             object. {@link java.util.zip.Adler32} object.
 */
public static long getChecksum(final File file, final boolean crc) throws FileNotFoundException, IOException {
    CheckedInputStream cis = null;
    if (crc) {
        cis = new CheckedInputStream(new FileInputStream(file), new CRC32());
    } else {
        cis = new CheckedInputStream(new FileInputStream(file), new Adler32());
    }
    final int length = (int) file.length();
    final byte[] buffer = new byte[length];
    long checksum = 0;
    while (cis.read(buffer) >= 0) {
        checksum = cis.getChecksum().getValue();
    }
    checksum = cis.getChecksum().getValue();
    StreamExtensions.closeInputStream(cis);
    return checksum;
}

From source file:de.alpharogroup.file.checksum.ChecksumUtils.java

/**
 * Gets the checksum from the given file. If the flag crc is true than the CheckedInputStream is
 * constructed with an instance of <code>java.util.zip.CRC32</code> otherwise with an instance
 * of <code>java.util.zip.Adler32</code>.
 *
 * @param file/*from w ww  .  j  a va2  s.c  o m*/
 *            The file The file from what to get the checksum.
 * @param crc
 *            The crc If the flag crc is true than the CheckedInputStream is constructed with an
 *            instance of {@link java.util.zip.CRC32} object otherwise it is constructed with an
 *            instance of
 * @return The checksum from the given file as long.
 * @throws FileNotFoundException
 *             Is thrown if the file is not found.
 * @throws IOException
 *             Signals that an I/O exception has occurred. {@link java.util.zip.CRC32} object
 *             otherwise it is constructed with an instance of {@link java.util.zip.Adler32}
 *             object. {@link java.util.zip.Adler32} object.
 */
public static long getChecksum(final File file, final boolean crc) throws FileNotFoundException, IOException {
    CheckedInputStream cis = null;
    if (crc) {
        cis = new CheckedInputStream(new FileInputStream(file), new CRC32());
    } else {
        cis = new CheckedInputStream(new FileInputStream(file), new Adler32());
    }
    final int length = (int) file.length();
    final byte[] buffer = new byte[length];
    long checksum = 0;
    while (cis.read(buffer) >= 0) {
        checksum = cis.getChecksum().getValue();
    }
    checksum = cis.getChecksum().getValue();
    StreamUtils.closeInputStream(cis);
    return checksum;
}

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

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

        // Computer CRC32 checksum
        cis = new CheckedInputStream((FileInputStream) ((LocalFile) file).getInputStream(), new CRC32());
        byte[] buf = new byte[128];
        while (cis.read(buf) >= 0) {
        }

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

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