Example usage for java.util.zip CheckedOutputStream CheckedOutputStream

List of usage examples for java.util.zip CheckedOutputStream CheckedOutputStream

Introduction

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

Prototype

public CheckedOutputStream(OutputStream out, Checksum cksum) 

Source Link

Document

Creates an output stream with the specified Checksum.

Usage

From source file:org.celeria.minecraft.backup.ArchiveWorldTaskFactory.java

private ZipOutputStream zipStreamFor(final FileObject archiveFile) throws FileSystemException {
    final OutputStream checkedStream = new CheckedOutputStream(streamFor(archiveFile), checksum);
    return new ZipOutputStream(new BufferedOutputStream(checkedStream));
}

From source file:org.theospi.portfolio.presentation.export.PresentationExport.java

public void createZip(OutputStream out) throws IOException {
    File directory = new File(tempDirectory + webappName);

    CheckedOutputStream checksum = null;
    ZipOutputStream zos = null;//from   w  w w . java 2s. c o m
    try {
        checksum = new CheckedOutputStream(out, new Adler32());
        zos = new ZipOutputStream(new BufferedOutputStream(checksum));
        recurseDirectory("", directory, zos);

        zos.finish();
        zos.flush();
    } finally {
        if (zos != null) {
            try {
                zos.close();
            } catch (IOException e) {
            }
        }
        if (checksum != null) {
            try {
                checksum.close();
            } catch (IOException e) {
            }
        }
    }

}

From source file:com.sqli.liferay.imex.util.xml.ZipUtils.java

public void zipFiles(File archive, File inputDir) throws IOException {

    FileOutputStream dest = new FileOutputStream(archive);
    CheckedOutputStream checksum = new CheckedOutputStream(dest, new Adler32());
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(checksum));

    zipEntries(out, inputDir, inputDir);

    out.close();//from w w  w.java2s .  co m
    log.debug("checksum:" + checksum.getChecksum().getValue());

}

From source file:org.commoncrawl.service.listcrawler.CacheWriterThread.java

@Override
public void run() {

    boolean shutdown = false;

    while (!shutdown) {
        try {// ww w  .  j a v a 2 s.c  o m
            final CacheWriteRequest request = _writeRequestQueue.take();

            switch (request._requestType) {

            case ExitThreadRequest: {
                // shutdown condition ... 
                CacheManager.LOG.info("Disk Writer Thread Received Shutdown. Exiting!");
                shutdown = true;
            }
                break;

            case WriteRequest: {

                long timeStart = System.currentTimeMillis();

                try {
                    // reset crc calculator (single thread so no worries on synchronization)
                    _crc32Out.reset();

                    // figure out if we need to compress the item ... 
                    if ((request._item.getFlags() & CacheItem.Flags.Flag_IsCompressed) == 0
                            && request._item.getContent().getCount() != 0) {
                        LOG.info("Incoming Cache Request Content for:" + request._item.getUrl()
                                + " is not compressed. Compressing...");
                        ByteStream compressedBytesOut = new ByteStream(request._item.getContent().getCount());
                        ThriftyGZIPOutputStream gzipOutputStream = new ThriftyGZIPOutputStream(
                                compressedBytesOut);
                        gzipOutputStream.write(request._item.getContent().getReadOnlyBytes(), 0,
                                request._item.getContent().getCount());
                        gzipOutputStream.finish();
                        LOG.info("Finished Compressing Incoming Content for:" + request._item.getUrl()
                                + " BytesIn:" + request._item.getContent().getCount() + " BytesOut:"
                                + compressedBytesOut.size());
                        // replace buffer

                        request._item.setContent(
                                new FlexBuffer(compressedBytesOut.getBuffer(), 0, compressedBytesOut.size()));
                        request._item.setFlags((request._item.getFlags() | CacheItem.Flags.Flag_IsCompressed));
                    }

                    // create streams ...
                    ByteStream bufferOutputStream = new ByteStream(8192);

                    CheckedOutputStream checkedStream = new CheckedOutputStream(bufferOutputStream, _crc32Out);
                    DataOutputStream dataOutputStream = new DataOutputStream(checkedStream);

                    // remember if this item has content ... 
                    boolean hasContent = request._item.isFieldDirty(CacheItem.Field_CONTENT);
                    // now mark the content field as clean, so that it will not be serialized in our current serialization attempt ... 
                    request._item.setFieldClean(CacheItem.Field_CONTENT);
                    // and go ahead and write out the data to the intermediate buffer while also computing partial checksum 
                    request._item.write(dataOutputStream);

                    request._item.setFieldDirty(CacheItem.Field_CONTENT);

                    // ok, now ... write out file header ... 
                    CacheItemHeader itemHeader = new CacheItemHeader(_manager.getLocalLogSyncBytes());

                    itemHeader._status = CacheItemHeader.STATUS_ALIVE;
                    itemHeader._lastAccessTime = System.currentTimeMillis();
                    itemHeader._fingerprint = request._itemFingerprint;
                    // compute total length ... 

                    // first the header bytes in the cacheItem 
                    itemHeader._dataLength = bufferOutputStream.size();
                    // next the content length (encoded - as in size + bytes) ... 
                    itemHeader._dataLength += 4 + request._item.getContent().getCount();
                    // lastly the crc value iteself ... 
                    itemHeader._dataLength += 8;
                    // open the log file ... 
                    DataOutputBuffer logStream = new DataOutputBuffer();

                    // ok, go ahead and write the header 
                    itemHeader.writeHeader(logStream);
                    // ok now write out the item data minus content... 
                    logStream.write(bufferOutputStream.getBuffer(), 0, bufferOutputStream.size());
                    // now create a checked stream for the content ... 
                    CheckedOutputStream checkedStream2 = new CheckedOutputStream(logStream,
                            checkedStream.getChecksum());

                    dataOutputStream = new DataOutputStream(checkedStream2);

                    // content size 
                    dataOutputStream.writeInt(request._item.getContent().getCount());
                    // now write out the content (via checked stream so that we can calc checksum on content)
                    dataOutputStream.write(request._item.getContent().getReadOnlyBytes(), 0,
                            request._item.getContent().getCount());
                    // ok ... lastly write out the checksum bytes ... 
                    dataOutputStream.writeLong(checkedStream2.getChecksum().getValue());
                    // and FINALLY, write out the total item bytes (so that we can seek in reverse to read last request log 
                    logStream.writeInt(CacheItemHeader.SIZE + itemHeader._dataLength);

                    // ok flush everyting to the memory stream 
                    dataOutputStream.flush();

                    //ok - time to acquire the log semaphore 
                    //LOG.info("Acquiring Local Log Semaphore");
                    _manager.getLocalLogAccessSemaphore().acquireUninterruptibly();

                    try {

                        // now time to acquire the write semaphore ... 
                        _manager.getLocalLogWriteAccessSemaphore().acquireUninterruptibly();

                        // get the current file position 
                        long recordOffset = _manager.getLocalLogFilePos();

                        try {

                            long ioTimeStart = System.currentTimeMillis();

                            RandomAccessFile logFile = new RandomAccessFile(_manager.getActiveLogFilePath(),
                                    "rw");

                            try {
                                // seek to our known record offset 
                                logFile.seek(recordOffset);
                                // write out the data
                                logFile.write(logStream.getData(), 0, logStream.getLength());
                            } finally {
                                logFile.close();
                            }
                            // now we need to update the file header 
                            _manager.updateLogFileHeader(_manager.getActiveLogFilePath(), 1,
                                    CacheItemHeader.SIZE + itemHeader._dataLength + 4 /*trailing bytes*/);

                            CacheManager.LOG
                                    .info("#### Wrote Cache Item in:" + (System.currentTimeMillis() - timeStart)
                                            + " iotime:" + (System.currentTimeMillis() - ioTimeStart)
                                            + " QueueSize:" + _writeRequestQueue.size());

                        } finally {
                            // release write semaphore quickly 
                            _manager.getLocalLogWriteAccessSemaphore().release();
                        }

                        // now inform the manager of the completed request ... 
                        _manager.writeRequestComplete(request, recordOffset);
                    } finally {
                        //LOG.info("Releasing Local Log Semaphore");
                        _manager.getLocalLogAccessSemaphore().release();
                    }
                } catch (IOException e) {
                    CacheManager.LOG.error("### FUC# BATMAN! - GONNA LOSE THIS REQUEST!!!!:"
                            + CCStringUtils.stringifyException(e));
                    _manager.writeRequestFailed(request, e);
                }
            }
                break;
            }
        } catch (InterruptedException e) {

        }
    }
}

From source file:net.landora.video.info.file.FileInfoManager.java

private synchronized FileInfo createInfo(File file) {
    FileInfo result = new FileInfo();
    result.setFileSize(file.length());//from ww w  . j av a2 s .c o m
    result.setLastModified(file.lastModified());
    result.setFilename(file.getName());

    InputStream is = null;
    try {
        is = new BufferedInputStream(new FileInputStream(file));
        Edonkey e2dkChecksum = new Edonkey();

        IOUtils.copy(is, new CheckedOutputStream(new NullOutputStream(), e2dkChecksum));

        result.setE2dkHash(e2dkChecksum.getHexValue());
    } catch (Exception e) {
        log.error("Error hashing file.", e);
        return null;
    } finally {
        IOUtils.closeQuietly(is);
    }

    MetadataProvidersManager.getInstance().checkForMetadata(result);

    return result;
}

From source file:com.atolcd.web.scripts.ZipContents.java

public void createZipFile(List<String> nodeIds, OutputStream os, boolean noaccent) throws IOException {
    File zip = null;/*from  w  w w  .ja va2s. co m*/

    try {
        if (nodeIds != null && !nodeIds.isEmpty()) {
            zip = TempFileProvider.createTempFile(TEMP_FILE_PREFIX, ZIP_EXTENSION);
            FileOutputStream stream = new FileOutputStream(zip);
            CheckedOutputStream checksum = new CheckedOutputStream(stream, new Adler32());
            BufferedOutputStream buff = new BufferedOutputStream(checksum);
            ZipArchiveOutputStream out = new ZipArchiveOutputStream(buff);
            out.setEncoding(encoding);
            out.setMethod(ZipArchiveOutputStream.DEFLATED);
            out.setLevel(Deflater.BEST_COMPRESSION);

            if (logger.isDebugEnabled()) {
                logger.debug("Using encoding '" + encoding + "' for zip file.");
            }

            try {
                for (String nodeId : nodeIds) {
                    NodeRef node = new NodeRef(storeRef, nodeId);
                    addToZip(node, out, noaccent, "");
                }
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
            } finally {
                out.close();
                buff.close();
                checksum.close();
                stream.close();

                if (nodeIds.size() > 0) {
                    InputStream in = new FileInputStream(zip);
                    try {
                        byte[] buffer = new byte[BUFFER_SIZE];
                        int len;

                        while ((len = in.read(buffer)) > 0) {
                            os.write(buffer, 0, len);
                        }
                    } finally {
                        IOUtils.closeQuietly(in);
                    }
                }
            }
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
    } finally {
        // try and delete the temporary file
        if (zip != null) {
            zip.delete();
        }
    }
}

From source file:com.guye.baffle.decoder.ArscData.java

public CRC32 createObfuscateFile(StringBlock tableBlock, StringBlock keyBlock, File file) throws IOException {
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    CRC32 cksum = new CRC32();
    CheckedOutputStream checkedOutputStream = new CheckedOutputStream(fileOutputStream, cksum);
    LEDataOutputStream out = new LEDataOutputStream(checkedOutputStream);

    int tableStrChange = getmTableStrings().getSize() - tableBlock.getSize();
    int keyStrChange = getmSpecNames().getSize() - keyBlock.getSize();
    getmHeader().chunkSize -= (tableStrChange + keyStrChange);
    getmHeader().write(out);//from   w  ww  . j a  v  a  2s  . c  o  m
    out.writeInt(1);
    tableBlock.write(out);
    getmPkgHeader().header.chunkSize -= keyStrChange;
    getmPkgHeader().write(out);
    getTypeNames().write(out);
    keyBlock.write(out);

    byte[] buff = new byte[1024];
    FileInputStream in = new FileInputStream(getFile());
    in.skip(getmResIndex());
    int len;
    while (((len = in.read(buff)) != -1)) {
        out.write(buff, 0, len);
    }

    in.close();
    out.close();
    checkedOutputStream.close();
    fileOutputStream.close();
    return cksum;
}

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

/** Export to stream. */
// TODO: theStdout is kind of superfluous here, as you could just say that a
// null theFilePath means that
@CheckForNull/*from w ww.  j  a v a 2 s  .  com*/
private static void exportToStream(final String theFilePath, final String theFormat, final String theTable,
        final boolean theStdout) throws Exception {
    final boolean compress = CMD_LN.hasOption(COMPRESS_OPT.getOpt());
    OutputStream os = null;
    final DBInfo info = dBInfo(FALSE);
    try {
        if (!theStdout)
            os = new FileOutputStream(theFilePath);
        else
            os = System.out;
        CRC32 checksum = null;
        OutputStream chesumStr;
        if (compress)
            chesumStr = new CRCGZipOutputStream(os);
        else if (theStdout)
            chesumStr = os;
        else {
            checksum = new CRC32();
            chesumStr = new CheckedOutputStream(os, checksum);
        }
        final PrintWriter ouputWriter = new PrintWriter(new OutputStreamWriter(chesumStr, "UTF-8"));

        final ClientLoader loader = ClientLoader.newInstance(info).outputFormat(theFormat)
                .outWriter(ouputWriter).verbose(VERBOSE).verboseStream(System.err);

        final BackendClient client = loader.loadClient();
        client.export(theTable, getRowFilter());

        if (!theStdout)
            writeCRC(theFilePath, chesumStr, compress, checksum);

    } finally {
        DBUtils.closeQuitely(os);
    }
}

From source file:org.akvo.flow.service.DataSyncService.java

private ZipFileData formZip(long surveyInstanceId) {
    ZipFileData zipFileData = new ZipFileData();
    StringBuilder surveyBuf = new StringBuilder();

    // Hold the responses in the StringBuilder
    String uuid = processSurveyData(surveyInstanceId, surveyBuf, zipFileData.imagePaths);

    // THe filename will match the Survey Instance UUID
    File zipFile = new File(FileUtil.getFilesDir(FileType.DATA), uuid + ConstantUtil.ARCHIVE_SUFFIX);

    // Write the data into the zip file
    try {//w w w.  jav  a 2s  . c om
        String fileName = zipFile.getAbsolutePath();// Will normalize filename.
        zipFileData.filename = fileName;
        Log.i(TAG, "Creating zip file: " + fileName);
        FileOutputStream fout = new FileOutputStream(zipFile);
        CheckedOutputStream checkedOutStream = new CheckedOutputStream(fout, new Adler32());
        ZipOutputStream zos = new ZipOutputStream(checkedOutStream);

        writeTextToZip(zos, surveyBuf.toString(), SURVEY_DATA_FILE);
        String signingKeyString = mProps.getProperty(SIGNING_KEY_PROP);
        if (!StringUtil.isNullOrEmpty(signingKeyString)) {
            MessageDigest sha1Digest = MessageDigest.getInstance("SHA1");
            byte[] digest = sha1Digest.digest(surveyBuf.toString().getBytes("UTF-8"));
            SecretKeySpec signingKey = new SecretKeySpec(signingKeyString.getBytes("UTF-8"), SIGNING_ALGORITHM);
            Mac mac = Mac.getInstance(SIGNING_ALGORITHM);
            mac.init(signingKey);
            byte[] hmac = mac.doFinal(digest);
            String encodedHmac = Base64.encodeBytes(hmac);
            writeTextToZip(zos, encodedHmac, SIG_FILE_NAME);
        }

        final String checksum = "" + checkedOutStream.getChecksum().getValue();
        zos.close();
        Log.i(TAG, "Closed zip output stream for file: " + fileName + ". Checksum: " + checksum);
    } catch (IOException e) {
        PersistentUncaughtExceptionHandler.recordException(e);
        Log.e(TAG, e.getMessage());
        zipFileData = null;
    } catch (NoSuchAlgorithmException e) {
        PersistentUncaughtExceptionHandler.recordException(e);
        Log.e(TAG, e.getMessage());
        zipFileData = null;
    } catch (InvalidKeyException e) {
        PersistentUncaughtExceptionHandler.recordException(e);
        Log.e(TAG, e.getMessage());
        zipFileData = null;
    }

    return zipFileData;
}

From source file:org.spliffy.server.apps.calendar.CalendarManager.java

private void updateCtag(CalEvent event) {
    OutputStream nulOut = new NullOutputStream();
    CheckedOutputStream cout = new CheckedOutputStream(nulOut, new Adler32());
    HashUtils.appendLine(event.getDescription(), cout);
    HashUtils.appendLine(event.getSummary(), cout);
    HashUtils.appendLine(event.getTimezone(), cout);
    HashUtils.appendLine(event.getStartDate(), cout);
    HashUtils.appendLine(event.getEndDate(), cout);
    Checksum check = cout.getChecksum();
    long crc = check.getValue();
    event.setCtag(crc);// w  w w  . j ava2s . c  om
    updateCtag(event.getCalendar());
}