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

/**
 * @param _name name//from w ww  .  j a  v  a2  s . co m
 * @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.kuali.rice.krad.datadictionary.URLMonitor.java

private Long getCRC(URL zipUrl) {
    Long result = -1l;//from   ww w .  ja  v  a2s .  c  o m
    try {
        CRC32 crc = new CRC32();
        CheckedInputStream cis = new CheckedInputStream(zipUrl.openStream(), crc);

        byte[] buffer = new byte[1024];
        int length;

        //read the entry from zip file and extract it to disk
        while ((length = cis.read(buffer)) > 0)
            ;

        cis.close();

        result = crc.getValue();
    } catch (IOException e) {
        LOG.warn("Unable to calculate CRC, resource doesn't exist?", e);
    }
    return result;
}

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();
    }// www. j ava  2 s.c o m
    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.olat.upgrade.OLATUpgrade_9_0_0.java

private long checksum(byte[] binaryDatas) throws IOException {
    InputStream in = null;/*from   w  w  w  .j av  a  2 s .  c o  m*/
    Adler32 checksum = new Adler32();
    try {
        in = new CheckedInputStream(new ByteArrayInputStream(binaryDatas), checksum);
        IOUtils.copy(in, new NullOutputStream());
    } finally {
        IOUtils.closeQuietly(in);
    }
    return checksum.getValue();
}

From source file:org.openmrs.module.sync.server.ConnectionResponse.java

/**
 * /*from   w w w .j av a2  s  .  c o m*/
 * @param inputStream
 * @throws SyncException
 */
public ConnectionResponse(InputStream is, boolean useCompression) throws SyncException {
    try {
        this.useCompression = useCompression;
        this.cis = new CheckedInputStream(is, new CRC32());

        if (this.useCompression) {
            GZIPInputStream zis = new GZIPInputStream(new BufferedInputStream(cis));
            this.responsePayload = IOUtils.toString(zis, "UTF-8");
            IOUtils.closeQuietly(zis);
            IOUtils.closeQuietly(cis);
            log.info("**********************  CHECKSUM: " + cis.getChecksum().getValue());
            this.checksum = cis.getChecksum().getValue();
        } else {
            this.responsePayload = IOUtils.toString(cis, "UTF-8");
        }

        log.info("Response compressed: " + useCompression);
        //log.info("Response input: " + is.toString());
        //log.info("Response data: " + this.responsePayload);
        log.info("Response checksum: " + this.checksum);

        this.setState(ServerConnectionState.OK);

    } catch (IOException e) {
        //throw new SyncException(e);
        log.error("An error occurred while unzipping response", e);
    }

}

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

public static Long ChecksumCRC32(ValueMetaInterface metaA, Object dataA) {
    long checksum = 0;
    FileObject file = null;/*from w w w. j av  a 2 s .  co  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;//w w  w  .  j  a va2s  . c  o  m
    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 av a 2s.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;/*from w w  w.  j  ava2 s .  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  va2s  .  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;
}