Example usage for java.util.zip CRC32 getValue

List of usage examples for java.util.zip CRC32 getValue

Introduction

In this page you can find the example usage for java.util.zip CRC32 getValue.

Prototype

@Override
public long getValue() 

Source Link

Document

Returns CRC-32 value.

Usage

From source file:org.exist.xquery.modules.compression.AbstractCompressFunction.java

/**
 * Adds a element to a archive/* w  w w .  ja v  a 2 s. co m*/
 *
 * @param os
 *            The Output Stream to add the element to
 * @param file
 *            The file to add to the archive
 * @param useHierarchy
 *            Whether to use a folder hierarchy in the archive file that
 *            reflects the collection hierarchy
 */
private void compressFile(OutputStream os, File file, boolean useHierarchy, String stripOffset, String method,
        String name) throws IOException {

    if (file.isFile()) {

        // create an entry in the Tar for the document
        Object entry = null;
        byte[] value = new byte[0];
        CRC32 chksum = new CRC32();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        if (name != null) {
            entry = newEntry(name);
        } else if (useHierarchy) {
            entry = newEntry(removeLeadingOffset(file.getPath(), stripOffset));
        } else {
            entry = newEntry(file.getName());
        }

        InputStream is = new FileInputStream(file);
        byte[] data = new byte[16384];
        int len = 0;
        while ((len = is.read(data, 0, data.length)) > 0) {
            baos.write(data, 0, len);
        }
        is.close();
        value = baos.toByteArray();
        // close the entry
        if (entry instanceof ZipEntry && "store".equals(method)) {
            ((ZipEntry) entry).setMethod(ZipOutputStream.STORED);
            chksum.update(value);
            ((ZipEntry) entry).setCrc(chksum.getValue());
            ((ZipEntry) entry).setSize(value.length);
        }

        putEntry(os, entry);
        os.write(value);
        closeEntry(os);

    } else {

        for (String i : file.list()) {
            compressFile(os, new File(file, i), useHierarchy, stripOffset, method, null);
        }

    }

}

From source file:org.apache.lucene.store.FSDirectory.java

public synchronized String getCacheKey(String[] filelist) {
    CacheKeyBuffer kkk = new CacheKeyBuffer(filelist, this.dir_uuid, System.currentTimeMillis() / 600000l,
            this.getP());

    String rtn = CACHE_BUFFER.get(kkk);
    if (rtn == null) {
        StringBuffer buff = new StringBuffer();
        buff.append(this.getClass().getName()).append("_");
        buff.append(this.directory.getAbsolutePath()).append("_");
        CRC32 crc32 = new CRC32();
        crc32.update(0);//from www.  jav a2 s.com
        long filesize = 0;
        long filemodify = 0;

        if (filelist != null) {
            buff.append(filelist.length).append("_");
            for (String s : filelist) {
                crc32.update(new String(s).getBytes());
                try {
                    filesize += this.fileLength(s);
                } catch (Throwable e) {
                    logger.error("filelength", e);
                }

                try {
                    filemodify = Math.max(filemodify, this.fileModified(s));
                } catch (Throwable e) {
                    logger.error("filelength", e);
                }
            }
        }
        long crcvalue = crc32.getValue();
        buff.append(crcvalue).append("_");
        buff.append(filesize).append("_");
        buff.append(filemodify).append("_");
        buff.append(format.format(new Date(filemodify)));
        rtn = buff.toString();
        CACHE_BUFFER.put(kkk, rtn);

    }

    return rtn;
}

From source file:bobs.is.compress.sevenzip.SevenZOutputFile.java

/**
 * Finishes the addition of entries to this archive, without closing it.
 * //from  w  w w.  jav a  2s. c o  m
 * @throws IOException if archive is already closed.
 */
public void finish() throws IOException {
    if (finished) {
        throw new IOException("This archive has already been finished");
    }
    finished = true;

    final long headerPosition = file.getFilePointer();

    final ByteArrayOutputStream headerBaos = new ByteArrayOutputStream();
    final DataOutputStream header = new DataOutputStream(headerBaos);

    writeHeader(header);
    header.flush();
    final byte[] headerBytes = headerBaos.toByteArray();
    file.write(headerBytes);

    final CRC32 crc32 = new CRC32();

    // signature header
    file.seek(0);
    file.write(SevenZFile.sevenZSignature);
    // version
    file.write(0);
    file.write(2);

    // start header
    final ByteArrayOutputStream startHeaderBaos = new ByteArrayOutputStream();
    final DataOutputStream startHeaderStream = new DataOutputStream(startHeaderBaos);
    startHeaderStream.writeLong(Long.reverseBytes(headerPosition - SevenZFile.SIGNATURE_HEADER_SIZE));
    startHeaderStream.writeLong(Long.reverseBytes(0xffffFFFFL & headerBytes.length));
    crc32.reset();
    crc32.update(headerBytes);
    startHeaderStream.writeInt(Integer.reverseBytes((int) crc32.getValue()));
    startHeaderStream.flush();
    final byte[] startHeaderBytes = startHeaderBaos.toByteArray();
    crc32.reset();
    crc32.update(startHeaderBytes);
    file.writeInt(Integer.reverseBytes((int) crc32.getValue()));
    file.write(startHeaderBytes);
}

From source file:com.magestore.app.pos.api.m1.config.POSConfigDataAccessM1.java

@Override
public boolean checkLicenseKey()
        throws DataAccessException, ConnectionException, ParseException, IOException, ParseException {
    // nu cha load config, cn khi to ch  default
    if (mConfig == null)
        mConfig = new PosConfigDefault();
    ActiveKey activeKey = new PosActiveKey();
    if (mConfig.getValue("webpos/general/active_key") == null)
        return false;

    String baseUrl = getHostUrl(POSDataAccessSessionM1.REST_BASE_URL);
    String extensionName = POSDataAccessSessionM1.REST_EXTENSION_NAME;
    String licensekey = (String) mConfig.getValue("webpos/general/active_key");
    if (licensekey.length() < 68)
        return false;
    CRC32 crc = new CRC32();
    String strExtensionName = licensekey.substring(0, 10) + extensionName;
    crc.update(strExtensionName.getBytes());
    int strDataCrc32 = (int) crc.getValue();
    int crc32Pos = (strDataCrc32 & 0x7FFFFFFF % 51) + 10;
    int md5Length = 32;
    String md5String = licensekey.substring(crc32Pos, (crc32Pos + md5Length));
    int md5StringLength = md5String.length();
    String key = licensekey.substring(0, crc32Pos)
            + licensekey.substring((crc32Pos + md5StringLength + 3), licensekey.length());
    try {/*from ww w  . j a  va 2s . com*/
        while ((key.length() % 4) != 0) {
            key += "=";
        }
        String licenseString = decryptRSAToString(key, POSDataAccessSessionM1.REST_PUBLIC_KEY);
        if (StringUtil.isNullOrEmpty(licenseString))
            return false;

        String strlicenseString = licenseString.substring(0, 3);
        String strlicensekey = licensekey.substring((crc32Pos + md5StringLength),
                (crc32Pos + md5StringLength + 3));
        if (!strlicenseString.equals(strlicensekey))
            return false;

        String type = licenseString.substring(0, 1);
        String strexpiredTime = licenseString.substring(1, 3);
        int expiredTime = Integer.parseInt(String.valueOf(strexpiredTime), 16);
        long extensionHash = -1;
        try {
            extensionHash = Long.parseLong(licenseString.substring(3, 13));
        } catch (Exception e) {
        }
        CRC32 crcExtensionName = new CRC32();
        crcExtensionName.update(extensionName.getBytes());
        long crc32ExtensionName = crcExtensionName.getValue();
        if (extensionHash != crc32ExtensionName)
            return false;

        String licenseDomain = licenseString.substring(17, licenseString.length()).replaceAll(" ", "");
        String checkCRc32 = licensekey.substring(0, crc32Pos)
                + licensekey.substring((crc32Pos + md5StringLength),
                        (crc32Pos + md5StringLength) + (licensekey.length() - crc32Pos - md5StringLength))
                + extensionName + licenseDomain;
        //            CRC32 crcCheck = new CRC32();
        //            crcCheck.update(checkCRc32.getBytes());
        //            long lcrc32String = -1;
        //            try {
        //                lcrc32String = Long.parseLong(crc32String);
        //            } catch (Exception e) {
        //            }
        String md5Check = EncryptUntil.HashMD5(checkCRc32);
        if (!md5Check.equals(md5String))
            return false;

        String strDate = licenseString.substring(11, 15);
        int resultDate = Integer.parseInt(String.valueOf(strDate), 16);
        String DATE_FORMAT = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        String createdDate = sdf.format(new Date(resultDate * 24 * 3600 * 1000L));
        if (!checkSameDomain(baseUrl, licenseDomain))
            return false;

        activeKey.setType(type);
        activeKey.setExpiredTime(expiredTime);
        activeKey.setCreatedDate(createdDate);
        activeKey.setLicenseDomain(licenseDomain);
        ConfigUtil.setActiveKey(activeKey);
        ConfigUtil.setIsDevLicense(type.equals("D") ? true : false);
        return true;
    } catch (Exception e) {
        String licenseDomain = "";
        if (baseUrl.contains("https://")) {
            baseUrl = baseUrl.replace("https://", "");
        } else if (baseUrl.contains("http://")) {
            baseUrl = baseUrl.replace("http://", "");
        }
        if (baseUrl.length() > 36) {
            licenseDomain = baseUrl.substring(0, 36);
        } else {
            licenseDomain = baseUrl;
        }
        String checkCRc32 = licensekey.substring(0, crc32Pos)
                + licensekey.substring((crc32Pos + md5StringLength),
                        (crc32Pos + md5StringLength) + (licensekey.length() - crc32Pos - md5StringLength))
                + extensionName + licenseDomain;
        //            CRC32 crcCheck = new CRC32();
        //            crcCheck.update(checkCRc32.getBytes());
        //            long lcrc32String = -1;
        //            try {
        //                lcrc32String = Long.parseLong(crc32String);
        //            } catch (Exception ex) {
        //            }
        String md5Check = EncryptUntil.HashMD5(checkCRc32);
        if (!md5Check.equals(md5String))
            return false;
        String type = licensekey.substring(crc32Pos + md5StringLength, crc32Pos + md5StringLength + 1);
        String strexpiredTime = licensekey.substring(crc32Pos + md5StringLength + 1,
                crc32Pos + md5StringLength + 1 + 2);
        int expiredTime = Integer.parseInt(String.valueOf(strexpiredTime), 16);
        if (!checkSameDomain(baseUrl, licenseDomain))
            return false;
        activeKey.setType(type);
        activeKey.setExpiredTime(expiredTime);
        activeKey.setLicenseDomain(licenseDomain);
        ConfigUtil.setActiveKey(activeKey);
        ConfigUtil.setIsDevLicense(type.equals("D") ? true : false);
        return true;
    }
}

From source file:srebrinb.compress.sevenzip.SevenZOutputFile.java

/**
 * Finishes the addition of entries to this archive, without closing it.
 * /*from   w  w w  .  j av a2  s  .  c o m*/
 * @throws IOException if archive is already closed.
 */
public void finish() throws IOException {
    if (finished) {
        throw new IOException("This archive has already been finished");
    }
    finished = true;

    final long headerPosition = channel.position();

    final ByteArrayOutputStream headerBaos = new ByteArrayOutputStream();
    final DataOutputStream header = new DataOutputStream(headerBaos);

    writeHeader(header);
    header.flush();
    final byte[] headerBytes = headerBaos.toByteArray();
    channel.write(ByteBuffer.wrap(headerBytes));

    final CRC32 crc32 = new CRC32();
    crc32.update(headerBytes);

    ByteBuffer bb = ByteBuffer.allocate(SevenZFile.sevenZSignature.length + 2 /* version */
            + 4 /* start header CRC */
            + 8 /* next header position */
            + 8 /* next header length */
            + 4 /* next header CRC */).order(ByteOrder.LITTLE_ENDIAN);
    // signature header
    channel.position(0);
    bb.put(SevenZFile.sevenZSignature);
    // version
    bb.put((byte) 0).put((byte) 2);

    // placeholder for start header CRC
    bb.putInt(0);

    // start header
    bb.putLong(headerPosition - SevenZFile.SIGNATURE_HEADER_SIZE).putLong(0xffffFFFFL & headerBytes.length)
            .putInt((int) crc32.getValue());
    crc32.reset();
    crc32.update(bb.array(), SevenZFile.sevenZSignature.length + 6, 20);
    bb.putInt(SevenZFile.sevenZSignature.length + 2, (int) crc32.getValue());
    bb.flip();
    channel.write(bb);
}

From source file:com.alexholmes.hdfsslurper.WorkerThread.java

private void process(FileStatus srcFileStatus) throws IOException, InterruptedException {

    Path stagingFile = null;/*  w  w  w  . java 2  s  .  c om*/
    FileSystem destFs = null;
    String filenameBatchidDelimiter = config.getFileNameBatchIdDelimiter();

    try {
        FileSystem srcFs = srcFileStatus.getPath().getFileSystem(config.getConfig());

        // run a script which can change the name of the file as well as
        // write out a new version of the file
        //
        if (config.getWorkScript() != null) {
            Path newSrcFile = stageSource(srcFileStatus);
            srcFileStatus = srcFileStatus.getPath().getFileSystem(config.getConfig()).getFileStatus(newSrcFile);
        }

        Path srcFile = srcFileStatus.getPath();

        // get the target HDFS file
        //
        Path destFile = getHdfsTargetPath(srcFileStatus);

        if (config.getCodec() != null) {
            String ext = config.getCodec().getDefaultExtension();
            if (!destFile.getName().endsWith(ext)) {
                destFile = new Path(destFile.toString() + ext);
            }
        }

        destFs = destFile.getFileSystem(config.getConfig());

        // get the staging HDFS file
        //
        stagingFile = fileSystemManager.getStagingFile(srcFileStatus, destFile);
        String batchId = srcFile.toString().substring(
                srcFile.toString().lastIndexOf(filenameBatchidDelimiter) + 1, srcFile.toString().length());

        log.info("event#Copying source file '" + srcFile + "' to staging destination '" + stagingFile + "'"
                + "$batchId#" + batchId);

        // if the directory of the target file doesn't exist, attempt to
        // create it
        //
        Path destParentDir = destFile.getParent();
        if (!destFs.exists(destParentDir)) {
            log.info("event#Attempting creation of target directory: " + destParentDir.toUri());
            if (!destFs.mkdirs(destParentDir)) {
                throw new IOException("event#Failed to create target directory: " + destParentDir.toUri());
            }
        }

        // if the staging directory doesn't exist, attempt to create it
        //
        Path destStagingParentDir = stagingFile.getParent();
        if (!destFs.exists(destStagingParentDir)) {
            log.info("event#Attempting creation of staging directory: " + destStagingParentDir.toUri());
            if (!destFs.mkdirs(destStagingParentDir)) {
                throw new IOException("event#Failed to create staging directory: " + destParentDir.toUri());
            }
        }

        // copy the file
        //
        InputStream is = null;
        OutputStream os = null;
        CRC32 crc = new CRC32();
        try {
            is = new BufferedInputStream(srcFs.open(srcFile));
            if (config.isVerify()) {
                is = new CheckedInputStream(is, crc);
            }
            os = destFs.create(stagingFile);

            if (config.getCodec() != null) {
                os = config.getCodec().createOutputStream(os);
            }

            IOUtils.copyBytes(is, os, 4096, false);
        } finally {
            IOUtils.closeStream(is);
            IOUtils.closeStream(os);
        }

        long srcFileSize = srcFs.getFileStatus(srcFile).getLen();
        long destFileSize = destFs.getFileStatus(stagingFile).getLen();
        if (config.getCodec() == null && srcFileSize != destFileSize) {
            throw new IOException(
                    "event#File sizes don't match, source = " + srcFileSize + ", dest = " + destFileSize);
        }

        log.info("event#Local file size = " + srcFileSize + ", HDFS file size = " + destFileSize + "$batchId#"
                + batchId);

        if (config.isVerify()) {
            verify(stagingFile, crc.getValue());
        }

        if (destFs.exists(destFile)) {
            destFs.delete(destFile, false);
        }

        log.info("event#Moving staging file '" + stagingFile + "' to destination '" + destFile + "'"
                + "$batchId#" + batchId);
        if (!destFs.rename(stagingFile, destFile)) {
            throw new IOException("event#Failed to rename file");
        }

        if (config.isCreateLzopIndex() && destFile.getName().endsWith(lzopExt)) {
            Path lzoIndexPath = new Path(destFile.toString() + LzoIndex.LZO_INDEX_SUFFIX);
            if (destFs.exists(lzoIndexPath)) {
                log.info("event#Deleting index file as it already exists");
                destFs.delete(lzoIndexPath, false);
            }
            indexer.index(destFile);
        }

        fileSystemManager.fileCopyComplete(srcFileStatus);

    } catch (Throwable t) {
        log.error("event#Caught exception working on file " + srcFileStatus.getPath(), t);

        // delete the staging file if it still exists
        //
        try {
            if (destFs != null && destFs.exists(stagingFile)) {
                destFs.delete(stagingFile, false);
            }
        } catch (Throwable t2) {
            log.error("event#Failed to delete staging file " + stagingFile, t2);
        }

        fileSystemManager.fileCopyError(srcFileStatus);
    }

}

From source file:cn.sinobest.jzpt.framework.utils.string.StringUtils.java

/**
 * CRC,8//from   w  w  w .j  a v a  2 s .c  o m
 */
public static String getCRC(InputStream in) {
    CRC32 crc32 = new CRC32();
    byte[] b = new byte[4096];
    int len = 0;
    try {
        while ((len = in.read(b)) != -1) {
            crc32.update(b, 0, len);
        }
        return Long.toHexString(crc32.getValue());
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:com.magestore.app.pos.api.m2.config.POSConfigDataAccess.java

@Override
public boolean checkLicenseKey()
        throws DataAccessException, ConnectionException, ParseException, IOException, ParseException {
    // nu cha load config, cn khi to ch  default
    if (mConfig == null)
        mConfig = new PosConfigDefault();
    ActiveKey activeKey = new PosActiveKey();
    if (mConfig.getValue("webpos/general/active_key") == null)
        return false;

    String baseUrl = getHostUrl(POSDataAccessSession.REST_BASE_URL);
    String extensionName = POSDataAccessSession.REST_EXTENSION_NAME;
    String licensekey = (String) mConfig.getValue("webpos/general/active_key");
    if (licensekey.length() < 68)
        return false;
    CRC32 crc = new CRC32();
    String strExtensionName = licensekey.substring(0, 10) + extensionName;
    crc.update(strExtensionName.getBytes());
    int strDataCrc32 = (int) crc.getValue();
    int crc32Pos = (strDataCrc32 & 0x7FFFFFFF % 51) + 10;
    int md5Length = 32;
    String md5String = licensekey.substring(crc32Pos, (crc32Pos + md5Length));
    int md5StringLength = md5String.length();
    String key = licensekey.substring(0, crc32Pos)
            + licensekey.substring((crc32Pos + md5StringLength + 3), licensekey.length());
    try {//from   ww w . j  a v a2  s . co  m
        while ((key.length() % 4) != 0) {
            key += "=";
        }
        String licenseString = decryptRSAToString(key, POSDataAccessSession.REST_PUBLIC_KEY);
        if (StringUtil.isNullOrEmpty(licenseString))
            return false;

        String strlicenseString = licenseString.substring(0, 3);
        String strlicensekey = licensekey.substring((crc32Pos + md5StringLength),
                (crc32Pos + md5StringLength + 3));
        if (!strlicenseString.equals(strlicensekey))
            return false;

        String type = licenseString.substring(0, 1);
        String strexpiredTime = licenseString.substring(1, 3);
        int expiredTime = Integer.parseInt(String.valueOf(strexpiredTime), 16);
        long extensionHash = -1;
        try {
            extensionHash = Long.parseLong(licenseString.substring(3, 13));
        } catch (Exception e) {
        }
        CRC32 crcExtensionName = new CRC32();
        crcExtensionName.update(extensionName.getBytes());
        long crc32ExtensionName = crcExtensionName.getValue();
        if (extensionHash != crc32ExtensionName)
            return false;

        String licenseDomain = licenseString.substring(17, licenseString.length()).replaceAll(" ", "");
        String checkCRc32 = licensekey.substring(0, crc32Pos)
                + licensekey.substring((crc32Pos + md5StringLength),
                        (crc32Pos + md5StringLength) + (licensekey.length() - crc32Pos - md5StringLength))
                + extensionName + licenseDomain;
        //            CRC32 crcCheck = new CRC32();
        //            crcCheck.update(checkCRc32.getBytes());
        //            long lcrc32String = -1;
        //            try {
        //                lcrc32String = Long.parseLong(crc32String);
        //            } catch (Exception e) {
        //            }
        String md5Check = EncryptUntil.HashMD5(checkCRc32);
        if (!md5Check.equals(md5String))
            return false;

        String strDate = licenseString.substring(11, 15);
        int resultDate = Integer.parseInt(String.valueOf(strDate), 16);
        String DATE_FORMAT = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        String createdDate = sdf.format(new Date(resultDate * 24 * 3600 * 1000L));
        if (!checkSameDomain(baseUrl, licenseDomain))
            return false;

        activeKey.setType(type);
        activeKey.setExpiredTime(expiredTime);
        activeKey.setCreatedDate(createdDate);
        activeKey.setLicenseDomain(licenseDomain);
        ConfigUtil.setActiveKey(activeKey);
        ConfigUtil.setIsDevLicense(type.equals("D") ? true : false);
        return true;
    } catch (Exception e) {
        String licenseDomain = "";
        if (baseUrl.contains("https://")) {
            baseUrl = baseUrl.replace("https://", "");
        } else if (baseUrl.contains("http://")) {
            baseUrl = baseUrl.replace("http://", "");
        }
        if (baseUrl.length() > 36) {
            licenseDomain = baseUrl.substring(0, 36);
        } else {
            licenseDomain = baseUrl;
        }
        String checkCRc32 = licensekey.substring(0, crc32Pos)
                + licensekey.substring((crc32Pos + md5StringLength),
                        (crc32Pos + md5StringLength) + (licensekey.length() - crc32Pos - md5StringLength))
                + extensionName + licenseDomain;
        //            CRC32 crcCheck = new CRC32();
        //            crcCheck.update(checkCRc32.getBytes());
        //            long lcrc32String = -1;
        //            try {
        //                lcrc32String = Long.parseLong(crc32String);
        //            } catch (Exception ex) {
        //            }
        String md5Check = EncryptUntil.HashMD5(checkCRc32);
        if (!md5Check.equals(md5String))
            return false;
        String type = licensekey.substring(crc32Pos + md5StringLength, crc32Pos + md5StringLength + 1);
        String strexpiredTime = licensekey.substring(crc32Pos + md5StringLength + 1,
                crc32Pos + md5StringLength + 1 + 2);
        int expiredTime = Integer.parseInt(String.valueOf(strexpiredTime), 16);
        if (!checkSameDomain(baseUrl, licenseDomain))
            return false;
        activeKey.setType(type);
        activeKey.setExpiredTime(expiredTime);
        activeKey.setLicenseDomain(licenseDomain);
        ConfigUtil.setActiveKey(activeKey);
        ConfigUtil.setIsDevLicense(type.equals("D") ? true : false);
        return true;
    }
}

From source file:org.phpmaven.phar.PharJavaPackager.java

private void packFile(final ByteArrayOutputStream fileEntriesBaos,
        final ByteArrayOutputStream compressedFilesBaos, final File fileToPack, String filePath)
        throws IOException {
    if (DEBUG) {//  ww w  . j a  v a 2 s.c om
        System.out.println("Packing file " + fileToPack + " with " + fileToPack.length() + " bytes.");
    }

    final byte[] fileBytes = filePath.getBytes("UTF-8");
    writeIntLE(fileEntriesBaos, fileBytes.length);
    fileEntriesBaos.write(fileBytes);
    // TODO Complain with files larger than 4 bytes file length
    writeIntLE(fileEntriesBaos, (int) fileToPack.length());
    writeIntLE(fileEntriesBaos, (int) (fileToPack.lastModified() / 1000));

    final byte[] uncompressed = FileUtils.readFileToByteArray(fileToPack);
    if (DEBUG) {
        System.out.println("read " + uncompressed.length + " bytes from file.");
    }
    final ByteArrayOutputStream compressedStream = new ByteArrayOutputStream();
    //        final GZIPOutputStream gzipStream = new GZIPOutputStream(compressedStream);
    //        gzipStream.write(uncompressed);
    //        gzipStream.flush();
    final CRC32 checksum = new CRC32();
    checksum.update(uncompressed);
    final Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
    deflater.setInput(uncompressed);
    deflater.finish();
    final byte[] buf = new byte[Short.MAX_VALUE];
    while (!deflater.needsInput()) {
        final int bytesRead = deflater.deflate(buf);
        compressedStream.write(buf, 0, bytesRead);
    }

    final byte[] compressed = compressedStream.toByteArray();
    if (DEBUG) {
        System.out.println("compressed to " + compressed.length + " bytes.");
    }

    //        final Inflater decompresser = new Inflater();
    //        decompresser.setInput(compressed);
    //        byte[] result = new byte[5000];
    //        try {
    //            int resultLength = decompresser.inflate(result);
    //            final String str = new String(result, 0, resultLength);
    //            int i = 42;
    //        } catch (DataFormatException e) {
    //            // TODO Auto-generated catch block
    //            e.printStackTrace();
    //        }
    //        decompresser.end();

    compressedFilesBaos.write(compressed);
    writeIntLE(fileEntriesBaos, compressed.length);

    writeIntLE(fileEntriesBaos, checksum.getValue());

    // bits: 0x00001000, gzip
    fileEntriesBaos.write(0);
    fileEntriesBaos.write(0x10);
    fileEntriesBaos.write(0);
    fileEntriesBaos.write(0);

    // 0 bytes manifest
    writeIntLE(fileEntriesBaos, 0);
}

From source file:org.smartfrog.services.www.bulkio.client.SunJavaBulkIOClient.java

@Override
public long doDownload(long ioSize) throws IOException, InterruptedException {
    validateURL();//from  w  ww  .  j ava  2 s.c o  m
    URL target = createFullURL(ioSize, format);
    getLog().info("Downloading " + ioSize + " bytes from " + target);
    CRC32 checksum = new CRC32();
    HttpURLConnection connection = openConnection(target);
    connection.setRequestMethod(HttpAttributes.METHOD_GET);
    connection.setDoOutput(false);
    connection.connect();
    checkStatusCode(target, connection, HttpURLConnection.HTTP_OK);
    String contentLengthHeader = connection.getHeaderField(HttpHeaders.CONTENT_LENGTH);
    long contentLength = Long.parseLong(contentLengthHeader);
    if (contentLength != ioSize) {
        throw new IOException("Wrong content length returned from " + target + " - expected " + ioSize
                + " but got " + contentLength);
    }
    String formatHeader = connection.getHeaderField(HttpHeaders.CONTENT_TYPE);
    if (!format.equals(formatHeader)) {
        throw new IOException("Wrong content type returned from " + target + " - expected " + format
                + " but got " + formatHeader);
    }
    InputStream stream = connection.getInputStream();
    long bytes = 0;
    try {
        for (bytes = 0; bytes < ioSize; bytes++) {
            int octet = stream.read();
            checksum.update(octet);
            if (interrupted) {
                throw new InterruptedException(
                        "Interrupted after reading" + bytes + " bytes" + " from " + target);
            }
        }
    } finally {
        closeQuietly(stream);
    }
    long actualChecksum = checksum.getValue();
    getLog().info("Download finished after " + bytes + " bytes, checksum=" + actualChecksum);
    if (bytes != ioSize) {
        throw new IOException("Wrong content length downloaded from " + target + " - requested " + ioSize
                + " but got " + bytes);
    }
    if (expectedChecksumFromGet >= 0 && expectedChecksumFromGet != actualChecksum) {
        throw new IOException("Wrong checksum from download of " + ioSize + " bytes " + " from " + target
                + " expected " + expectedChecksumFromGet + " but got " + actualChecksum);
    }
    return bytes;
}