Example usage for java.util.zip GZIPOutputStream write

List of usage examples for java.util.zip GZIPOutputStream write

Introduction

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

Prototype

public synchronized void write(byte[] buf, int off, int len) throws IOException 

Source Link

Document

Writes array of bytes to the compressed output stream.

Usage

From source file:com.microsoft.tfs.core.clients.versioncontrol.engines.internal.workers.BaselineUpdaterWorker.java

/**
 * {@inheritDoc}//from  w w w  .  ja  va  2 s .c  o  m
 */
@Override
public WorkerStatus call() throws Exception {
    /*
     * Try not to run any code outside this try block, so our Throwable
     * catch below can report every error (even RuntimeExceptions).
     */
    try {
        boolean completedSuccessfully = false;
        final int token = state.getBaselineFolderCollection().lockForRead();

        try {
            // Present the read lock token that we already hold to prevent
            // it from attempting to acquire another read lock for the call
            // to GetNewBaselineLocation.
            String baselineFilePath = state.getBaselineFolderCollection().getNewBaselineLocation(
                    request.getBaselineFileGUID(), request.getBaselinePartitionLocalItem(), token);

            // Get the uncompressed file size.
            final File fi = new File(baselineFilePath);
            final long uncompressedFileSize = fi.length();

            // Set status message for the monitor.
            monitor.setCurrentWorkDescription(
                    MessageFormat.format(Messages.getString("BaselineUpdaterWorker.UpdatingBaselineFormat"), //$NON-NLS-1$
                            baselineFilePath));

            // Add the .gz extension to this baseline.
            baselineFilePath = baselineFilePath + BaselineFolder.getGzipExtension();

            if (uncompressedFileSize < Worker.MAX_GZIP_INPUT_SIZE) {
                final byte[] buffer = new byte[4096];
                byte[] hashValue = null;

                MessageDigest md5Digest = null;
                if (request.getHashValue() != null) {
                    md5Digest = MessageDigest.getInstance("MD5"); //$NON-NLS-1$
                }

                final GZIPOutputStream outputStream = new GZIPOutputStream(
                        new FileOutputStream(baselineFilePath));
                final String sourceLocalItem = request.getSourceLocalItem();
                final FileSystemUtils util = FileSystemUtils.getInstance();
                final FileSystemAttributes attrs = util.getAttributes(sourceLocalItem);

                InputStream inputStream;
                if (attrs.isSymbolicLink()) {
                    final String linkTarget = util.getSymbolicLink(sourceLocalItem);
                    inputStream = new ByteArrayInputStream(linkTarget.getBytes("UTF-8")); //$NON-NLS-1$
                } else {
                    inputStream = new FileInputStream(request.getSourceLocalItem());
                }

                try {
                    int bytesRead;

                    while (true) {
                        bytesRead = inputStream.read(buffer, 0, buffer.length);

                        if (bytesRead <= 0) {
                            break;
                        }

                        if (null != md5Digest) {
                            md5Digest.update(buffer, 0, bytesRead);
                        }

                        outputStream.write(buffer, 0, bytesRead);
                    }

                    if (null != md5Digest) {
                        hashValue = md5Digest.digest();
                    }
                } finally {
                    if (outputStream != null) {
                        IOUtils.closeSafely(outputStream);
                    }
                    if (inputStream != null) {
                        IOUtils.closeSafely(inputStream);
                    }
                }

                if (null != hashValue && 16 == hashValue.length && null != request.getHashValue()
                        && 16 == request.getHashValue().length
                        && !Arrays.equals(request.getHashValue(), hashValue)) {
                    // The hash value didn't match the provided hash value.
                    // Delete the baseline from disk.
                    FileHelpers.deleteFileWithoutException(baselineFilePath);
                } else {
                    completedSuccessfully = true;
                }
            } else {
                // TODO: We didn't attempt to gzip.
            }
        } catch (final Exception ex) {
            log.trace("BaselineUpdater", ex); //$NON-NLS-1$
        } finally {
            state.getBaselineFolderCollection().unlockForRead(token);

            if (!completedSuccessfully) {
                state.addFailedRequest(request);
            }
        }
    } catch (final Throwable t) {
        /*
         * An actual error happened. We have to communicate this problem to
         * the thread submitting tasks so it can take the correct action
         * (shut down other workers).
         */
        state.setFatalError(t);
        return new WorkerStatus(this, FinalState.ERROR);
    }

    return new WorkerStatus(this, FinalState.NORMAL);
}

From source file:com.microsoft.tfs.core.clients.versioncontrol.engines.internal.workers.CheckinWorker.java

/**
 * Compress the source file to a new temporary file, and return the absolute
 * path to the new temporary file. The algorithm used is gzip.
 *
 * @param sourceFile//from   w  ww .  java  2s .co  m
 *        the source file to compress (must not be <code>null</code> or
 *        empty).
 * @return the full path to the new compressed temp file that was created.
 * @throws CoreCancelException
 *         if the compression was cancelled by the user via Core's
 *         TaskMonitor. The output file is removed before this exception is
 *         thrown.
 */
private File compressToTempFile(final String sourceFile) throws CoreCancelException {
    Check.notNullOrEmpty(sourceFile, "sourceFile"); //$NON-NLS-1$

    FileInputStream is = null;
    FileOutputStream os = null;
    GZIPOutputStream gzos = null;

    final String messageFormat = MessageFormat.format(
            Messages.getString("CheckinWorker.CompressFIleProgressFormat_SKIPVALIDATE"), //$NON-NLS-1$
            change.getServerItem());
    final FileProcessingProgressMonitorAdapter monitor = new FileProcessingProgressMonitorAdapter(
            userCancellationMonitor, new File(sourceFile).length(), messageFormat);

    TaskMonitorService.pushTaskMonitor(monitor);

    try {
        final File temp = File.createTempFile("teamexplorer", ".tmp"); //$NON-NLS-1$ //$NON-NLS-2$

        final String tempFileName = temp.getAbsolutePath();

        is = new FileInputStream(sourceFile);
        os = new FileOutputStream(tempFileName);
        gzos = new GZIPOutputStream(os);

        final byte[] buffer = new byte[GZIP_COMPRESS_READ_BUFFER];
        int read = 0;
        while ((read = is.read(buffer)) != -1) {
            if (TaskMonitorService.getTaskMonitor().isCanceled()) {
                temp.delete();
                throw new CoreCancelException();
            }

            gzos.write(buffer, 0, read);
            TaskMonitorService.getTaskMonitor().worked(read);
        }

        return temp;
    } catch (final IOException e) {
        throw new VersionControlException(e);
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (final IOException e) {
        }

        try {
            if (gzos != null) {
                gzos.close();
            }
        } catch (final IOException e) {
        }

        TaskMonitorService.popTaskMonitor();
    }
}

From source file:gov.nih.nci.ncicb.tcga.dcc.qclive.common.util.ArchiveCompressorTarGzImpl.java

/**
 * Compress the given files into an archive with the given name, plus the file extension.  Put the compressed
 * archive into the given directory.  (So if you pass in /test/ as the destinationDirectory and 'anArchive' as
 * the archiveName, the compressed archives will be /test/anArchive.tar.gz)
 *
 * @param files                the files to include in the archive
 * @param archiveName          the name of the archive, minus extension
 * @param destinationDirectory the location to put the new compressed archive
 * @param compress             flag to compress the archive
 * @return the File representing the created compressed archive
 * @throws IOException if it needs to/*from w  w w  . jav  a2  s.co m*/
 */
public File createArchive(final List<File> files, final String archiveName, final File destinationDirectory,
        final Boolean compress) throws IOException {
    File archive = null;
    TarOutputStream tarOutputStream = null;
    FileInputStream in = null;
    GZIPOutputStream gzipOutputStream = null;

    try {
        // first make a tar archive with all the given files
        final File tarFile = new File(destinationDirectory, archiveName + TAR_EXTENSION);
        //noinspection IOResourceOpenedButNotSafelyClosed
        tarOutputStream = new TarOutputStream(new FileOutputStream(tarFile));
        tarOutputStream.setLongFileMode(TarOutputStream.LONGFILE_GNU);
        final byte[] buf = new byte[1024];
        for (final File file : files) {
            try {
                //noinspection IOResourceOpenedButNotSafelyClosed
                in = new FileInputStream(file);
                // name of entry should be archiveName/fileName
                final TarEntry tarEntry = new TarEntry(file);
                tarEntry.setName(archiveName + File.separator + file.getName());
                tarOutputStream.putNextEntry(tarEntry);
                int len;
                while ((len = in.read(buf)) > 0) {
                    tarOutputStream.write(buf, 0, len);
                }
                tarOutputStream.closeEntry();
                in.close();
            } finally {
                IOUtils.closeQuietly(in);
            }
        }
        tarOutputStream.close();

        if (compress) {
            final File outputFile = new File(destinationDirectory, archiveName + TAR_GZIP_EXTENSION);
            // then compress it using gzip
            //noinspection IOResourceOpenedButNotSafelyClosed
            gzipOutputStream = new GZIPOutputStream(new FileOutputStream(outputFile));
            //noinspection IOResourceOpenedButNotSafelyClosed
            in = new FileInputStream(tarFile);
            int len;
            while ((len = in.read(buf)) > 0) {
                gzipOutputStream.write(buf, 0, len);
            }
            // this was a temp file so delete it
            //noinspection ResultOfMethodCallIgnored
            tarFile.delete();
            archive = outputFile;
        } else {
            archive = tarFile;
        }
    } finally {
        IOUtils.closeQuietly(tarOutputStream);
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(gzipOutputStream);
    }

    return archive;
}

From source file:de.innovationgate.utils.WGUtils.java

/**
 * Zips bytes to compressed bytes//from www.  j a v a 2s .co m
 * @param uncompressedBytes Input
 * @throws IOException
 */
public static byte[] zip(byte[] uncompressedBytes) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream zos = new GZIPOutputStream(baos);
    zos.write(uncompressedBytes, 0, uncompressedBytes.length);
    zos.close();

    return baos.toByteArray();
}

From source file:com.meetup.memcached.MemcachedClient.java

/** 
 * Stores data to cache./*from   w  ww.  ja  va 2  s .  c  o m*/
 *
 * If data does not already exist for this key on the server, or if the key is being<br/>
 * deleted, the specified value will not be stored.<br/>
 * The server will automatically delete the value when the expiration time has been reached.<br/>
 * <br/>
 * If compression is enabled, and the data is longer than the compression threshold<br/>
 * the data will be stored in compressed form.<br/>
 * <br/>
 * As of the current release, all objects stored will use java serialization.
 * 
 * @param cmdname action to take (set, add, replace)
 * @param key key to store cache under
 * @param value object to cache
 * @param expiry expiration
 * @param hashCode if not null, then the int hashcode to use
 * @param asString store this object as a string?
 * @return true/false indicating success
 */
private boolean set(String cmdname, String key, Object value, Date expiry, Integer hashCode, boolean asString) {

    if (cmdname == null || cmdname.trim().equals("") || key == null) {
        log.error("key is null or cmd is null/empty for set()");
        return false;
    }

    try {
        key = sanitizeKey(key);
    } catch (UnsupportedEncodingException e) {

        // if we have an errorHandler, use its hook
        if (errorHandler != null)
            errorHandler.handleErrorOnSet(this, e, key);

        log.error("failed to sanitize your key!", e);
        return false;
    }

    if (value == null) {
        log.warn("trying to store a null value to cache: " + key);
        return false;
    }

    // get SockIO obj
    SockIOPool.SockIO sock = pool.getSock(key, hashCode);

    if (sock == null) {
        if (errorHandler != null)
            errorHandler.handleErrorOnSet(this, new IOException("no socket to server available"), key);
        return false;
    }

    if (expiry == null)
        expiry = new Date(0);

    // store flags
    int flags = 0;

    // byte array to hold data
    byte[] val;

    if (NativeHandler.isHandled(value)) {

        if (asString) {
            // useful for sharing data between java and non-java
            // and also for storing ints for the increment method
            try {
                if (log.isInfoEnabled())
                    log.info("++++ storing data as a string for key: " + key + " for class: "
                            + value.getClass().getName());
                val = value.toString().getBytes(defaultEncoding);
            } catch (UnsupportedEncodingException ue) {

                // if we have an errorHandler, use its hook
                if (errorHandler != null)
                    errorHandler.handleErrorOnSet(this, ue, key);

                log.error("invalid encoding type used: " + defaultEncoding, ue);
                sock.close();
                sock = null;
                return false;
            }
        } else {
            try {
                if (log.isInfoEnabled())
                    log.info("Storing with native handler...");
                flags |= NativeHandler.getMarkerFlag(value);
                val = NativeHandler.encode(value);
            } catch (Exception e) {

                // if we have an errorHandler, use its hook
                if (errorHandler != null)
                    errorHandler.handleErrorOnSet(this, e, key);

                log.error("Failed to native handle obj", e);

                sock.close();
                sock = null;
                return false;
            }
        }
    } else {
        // always serialize for non-primitive types
        try {
            if (log.isInfoEnabled())
                log.info("++++ serializing for key: " + key + " for class: " + value.getClass().getName());
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            (new ObjectOutputStream(bos)).writeObject(value);
            val = bos.toByteArray();
            flags |= F_SERIALIZED;
        } catch (IOException e) {

            // if we have an errorHandler, use its hook
            if (errorHandler != null)
                errorHandler.handleErrorOnSet(this, e, key);

            // if we fail to serialize, then
            // we bail
            log.error("failed to serialize obj", e);
            log.error(value.toString());

            // return socket to pool and bail
            sock.close();
            sock = null;
            return false;
        }
    }

    // now try to compress if we want to
    // and if the length is over the threshold 
    if (compressEnable && val.length > compressThreshold) {

        try {
            if (log.isInfoEnabled()) {
                log.info("++++ trying to compress data");
                log.info("++++ size prior to compression: " + val.length);
            }
            ByteArrayOutputStream bos = new ByteArrayOutputStream(val.length);
            GZIPOutputStream gos = new GZIPOutputStream(bos);
            gos.write(val, 0, val.length);
            gos.finish();

            // store it and set compression flag
            val = bos.toByteArray();
            flags |= F_COMPRESSED;

            if (log.isInfoEnabled())
                log.info("++++ compression succeeded, size after: " + val.length);
        } catch (IOException e) {

            // if we have an errorHandler, use its hook
            if (errorHandler != null)
                errorHandler.handleErrorOnSet(this, e, key);

            log.error("IOException while compressing stream: " + e.getMessage());
            log.error("storing data uncompressed");
        }
    }

    // now write the data to the cache server
    try {
        String cmd = String.format("%s %s %d %d %d\r\n", cmdname, key, flags, (expiry.getTime() / 1000),
                val.length);
        sock.write(cmd.getBytes());
        sock.write(val);
        sock.write("\r\n".getBytes());
        sock.flush();

        // get result code
        String line = sock.readLine();
        if (log.isInfoEnabled())
            log.info("++++ memcache cmd (result code): " + cmd + " (" + line + ")");

        if (STORED.equals(line)) {
            if (log.isInfoEnabled())
                log.info("++++ data successfully stored for key: " + key);
            sock.close();
            sock = null;
            return true;
        } else if (NOTSTORED.equals(line)) {
            if (log.isInfoEnabled())
                log.info("++++ data not stored in cache for key: " + key);
        } else {
            log.error("++++ error storing data in cache for key: " + key + " -- length: " + val.length);
            log.error("++++ server response: " + line);
        }
    } catch (IOException e) {

        // if we have an errorHandler, use its hook
        if (errorHandler != null)
            errorHandler.handleErrorOnSet(this, e, key);

        // exception thrown
        log.error("++++ exception thrown while writing bytes to server on set");
        log.error(e.getMessage(), e);

        try {
            sock.trueClose();
        } catch (IOException ioe) {
            log.error("++++ failed to close socket : " + sock.toString());
        }

        sock = null;
    }

    if (sock != null) {
        sock.close();
        sock = null;
    }

    return false;
}