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:org.efaps.db.databases.OracleDatabase.java

/**
 * @param _name name/*from w w w.  j a v a  2 s . com*/
 * @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;
}

From source file:org.mule.transports.vfs.VFSReceiver.java

protected boolean hasChanged(FileObject fileObject) {
    boolean changed = false;
    String key = fileObject.getName().getPath();
    long checksum = 0;
    if (checksumMap.containsKey(key)) {
        checksum = ((Long) checksumMap.get(key)).longValue();
    }/*from w  ww .j a  v  a  2s  .c  om*/
    long newChecksum = 0;
    CheckedInputStream checkedInputStream = null;
    try {
        InputStream inputStream = fileObject.getContent().getInputStream();
        checkedInputStream = new CheckedInputStream(inputStream, new Adler32());
        int bufferSize = 1;
        if (inputStream.available() > 0) {
            bufferSize = inputStream.available();
        }
        byte[] buffer = new byte[bufferSize];
        while (checkedInputStream.read(buffer) > -1) {
            ;
        }
        newChecksum = checkedInputStream.getChecksum().getValue();
        if (newChecksum != checksum) {
            if (logger.isDebugEnabled()) {
                logger.debug("calculated a new checksum of " + newChecksum);
            }
            checksumMap.put(key, new Long(newChecksum));
            changed = true;
        }
    } catch (IOException e) {
        connector.handleException(e);
    }
    return changed;
}

From source file:org.openossad.util.core.row.ValueDataUtil.java

public static Long ChecksumCRC32(ValueMetaInterface metaA, Object dataA) {
    long checksum = 0;
    FileObject file = null;/* www  .j a  v  a  2  s  . c o  m*/
    try {
        file = OpenDESIGNERVFS.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;
}

From source file:org.openossad.util.core.row.ValueDataUtil.java

public static Long ChecksumAdler32(ValueMetaInterface metaA, Object dataA) {
    long checksum = 0;
    FileObject file = null;//from  w ww .j  a  v a2  s.c om
    try {
        file = OpenDESIGNERVFS.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:org.pentaho.di.core.row.ValueDataUtil.java

public static Long ChecksumCRC32(ValueMetaInterface metaA, Object dataA) {
    long checksum = 0;
    FileObject file = null;// w w w  .j a  va2s.  c om
    try {
        file = KettleVFS.getFileObject(dataA.toString());
        CheckedInputStream cis = null;

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

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

    } catch (Exception e) {
        // ignore - should likely log the exception
    } finally {
        if (file != null) {
            try {
                file.close();
                file = null;
            } catch (Exception e) {
                // Ignore
            }
        }
    }
    return checksum;
}

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

public static Long ChecksumAdler32(ValueMetaInterface metaA, Object dataA) {
    long checksum = 0;
    FileObject file = null;/*w w w  .  ja  va 2s. co  m*/
    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:org.talend.commons.runtime.utils.io.IOUtils.java

public static long computeCRC(InputStream in) {
    long unitCRC = 0;

    BufferedInputStream bufferedInputStream = null;

    try {/*w  w  w  .  j  a  v  a2s .  c o m*/
        bufferedInputStream = new BufferedInputStream(in);

        // Compute Adler-32 checksum
        CheckedInputStream cis = new CheckedInputStream(bufferedInputStream, new Adler32());
        byte[] tempBuf = new byte[128];
        while (cis.read(tempBuf) >= 0) {
            // do nothing
        }
        unitCRC = cis.getChecksum().getValue();
    } catch (IOException e) {
        return -1;
    } finally {
        try {
            bufferedInputStream.close();
        } catch (Exception e) {
            // ignore me even if i'm null
        }
    }
    return unitCRC;
}

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

public static long getChecksumAlder32(File file) throws IOException {
    BufferedInputStream bufferedInputStream = null;
    CheckedInputStream cis = null;
    try {//from   ww  w. j  ava 2 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:org.yccheok.jstock.analysis.Utils.java

public static long getChecksum(File file) {
    FileInputStream stream = null;
    CheckedInputStream cis = null;
    try {//  ww  w . j  a v  a  2s .c  o  m
        // Compute Adler-32 checksum
        stream = new FileInputStream(file);
        cis = new CheckedInputStream(stream, new Adler32());
        byte[] tempBuf = new byte[128];
        while (cis.read(tempBuf) >= 0) {
        }
        long checksum = cis.getChecksum().getValue();
        return checksum;
    } catch (IOException ex) {
        log.error(null, ex);
    } finally {
        org.yccheok.jstock.file.Utils.close(cis);
        org.yccheok.jstock.file.Utils.close(stream);
    }
    return 0;
}

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

/**
 * Compute the Adler-32 checksum of the specified file. Code based on
 * "e457. Calculating the Checksum of a File", "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.
 *///from   ww  w  . j a  v  a 2 s.c  om
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;
}