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:NGzipCompressingEntity.java

public void writeTo(final OutputStream outstream) throws IOException {
    if (outstream == null) {
        throw new IllegalArgumentException("Output stream may not be null");
    }//w w  w .j a  va 2 s.c o  m
    System.out.println("Writing gzip");
    GZIPOutputStream gzip = new GZIPOutputStream(outstream);
    InputStream in = wrappedEntity.getContent();
    byte[] tmp = new byte[2048];
    int l;
    while ((l = in.read(tmp)) != -1) {
        gzip.write(tmp, 0, l);
    }
    gzip.close();
}

From source file:NGzipCompressingEntity.java

public void produceContent(ContentEncoder encoder, IOControl ioctrl) throws IOException {
    if (baos == null) {
        baos = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(baos);
        InputStream in = wrappedEntity.getContent();
        byte[] tmp = new byte[2048];
        int l;/*from  ww w.j a v a 2s  .c om*/
        while ((l = in.read(tmp)) != -1) {
            gzip.write(tmp, 0, l);
        }
        gzip.close();

        buffer = ByteBuffer.wrap(baos.toByteArray());
    }

    encoder.write(buffer);
    if (!buffer.hasRemaining())
        encoder.complete();
}

From source file:org.mule.util.compression.GZipCompression.java

/**
 * Used for compressing a byte array into a new byte array using GZIP
 * /*from   w ww.  j a v a  2  s.c  o  m*/
 * @param bytes An array of bytes to compress
 * @return a compressed byte array
 * @throws java.io.IOException if it fails to write to a GZIPOutputStream
 * @see java.util.zip.GZIPOutputStream
 */
public byte[] compressByteArray(byte[] bytes) throws IOException {
    // TODO add strict behaviour as option
    if (bytes == null || isCompressed(bytes)) {
        // nothing to compress
        if (logger.isDebugEnabled()) {
            logger.debug("Data already compressed; doing nothing");
        }
        return bytes;
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Compressing message of size: " + bytes.length);
    }

    ByteArrayOutputStream baos = null;
    GZIPOutputStream gzos = null;

    try {
        baos = new ByteArrayOutputStream(DEFAULT_BUFFER_SIZE);
        gzos = new GZIPOutputStream(baos);

        gzos.write(bytes, 0, bytes.length);
        gzos.finish();
        gzos.close();

        byte[] compressedByteArray = baos.toByteArray();
        baos.close();

        if (logger.isDebugEnabled()) {
            logger.debug("Compressed message to size: " + compressedByteArray.length);
        }

        return compressedByteArray;
    } catch (IOException ioex) {
        throw ioex;
    } finally {
        IOUtils.closeQuietly(gzos);
        IOUtils.closeQuietly(baos);
    }
}

From source file:org.wso2.carbon.logging.summarizer.scriptCreator.OutputFileHandler.java

public void compressLogFile(String temp) throws IOException {
    File file = new File(temp);
    FileOutputStream outputStream = new FileOutputStream(file + ".gz");
    GZIPOutputStream gzos = new GZIPOutputStream(outputStream);
    FileInputStream inputStream = new FileInputStream(temp);
    BufferedInputStream in = new BufferedInputStream(inputStream);
    byte[] buffer = new byte[1024];
    int i;//from w ww. j a v  a 2s .co  m
    while ((i = in.read(buffer)) >= 0) {
        gzos.write(buffer, 0, i);
    }
    in.close();
    gzos.close();
}

From source file:eu.domibus.ebms3.common.CompressionService.java

/**
 * Compress given Stream via GZIP [RFC1952].
 *
 * @param sourceStream Stream of uncompressed data
 * @param targetStream Stream of compressed data
 * @throws IOException//from w  w  w. ja  v  a2s . c  o m
 */
private void compress(final InputStream sourceStream, final GZIPOutputStream targetStream) throws IOException {
    final byte[] buffer = new byte[1024];

    try {
        int i;
        while ((i = sourceStream.read(buffer)) > 0) {
            targetStream.write(buffer, 0, i);
        }

        sourceStream.close();

        targetStream.finish();
        targetStream.close();

    } catch (IOException e) {
        CompressionService.LOG.error(
                "I/O exception during gzip compression. method: compress(Inputstream, GZIPOutputStream)", e);
        throw e;
    }
}

From source file:org.broadleafcommerce.common.sitemap.service.SiteMapServiceImpl.java

/**
 * Gzip a file and then delete the file//from w ww .  ja  v  a  2s.  c o  m
 * 
 * @param fileName
 */
protected void gzipAndDeleteFiles(FileWorkArea fileWorkArea, List<String> fileNames) {
    for (String fileName : fileNames) {
        try {
            String fileNameWithPath = FilenameUtils
                    .normalize(fileWorkArea.getFilePathLocation() + File.separator + fileName);

            FileInputStream fis = new FileInputStream(fileNameWithPath);
            FileOutputStream fos = new FileOutputStream(fileNameWithPath + ENCODING_EXTENSION);
            GZIPOutputStream gzipOS = new GZIPOutputStream(fos);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fis.read(buffer)) != -1) {
                gzipOS.write(buffer, 0, len);
            }
            //close resources
            gzipOS.close();
            fos.close();
            fis.close();

            File originalFile = new File(fileNameWithPath);
            originalFile.delete();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:edu.umn.cs.spatialHadoop.visualization.FrequencyMap.java

@Override
public void write(DataOutput out) throws IOException {
    super.write(out);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzos = new GZIPOutputStream(baos);
    ByteBuffer bbuffer = ByteBuffer.allocate(getHeight() * 4 + 8);
    bbuffer.putInt(getWidth());//from w ww  . j a v a2 s.  co m
    bbuffer.putInt(getHeight());
    gzos.write(bbuffer.array(), 0, bbuffer.position());
    for (int x = 0; x < getWidth(); x++) {
        bbuffer.clear();
        for (int y = 0; y < getHeight(); y++) {
            bbuffer.putFloat(frequencies[x][y]);
        }
        gzos.write(bbuffer.array(), 0, bbuffer.position());
    }
    gzos.close();

    byte[] serializedData = baos.toByteArray();
    out.writeInt(serializedData.length);
    out.write(serializedData);
}

From source file:net.nifheim.beelzebu.coins.common.utils.FileManager.java

private void gzipFile(InputStream in, String to) throws IOException {
    GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(to));
    byte[] buffer = new byte[4096];
    int bytesRead;
    while ((bytesRead = in.read(buffer)) != -1) {
        out.write(buffer, 0, bytesRead);
    }//from  ww  w .  j  ava 2s  .  com
    in.close();
    out.close();
}

From source file:org.efaps.admin.program.bundle.TempFileBundle.java

/**
 * @param _gziped zip the file/*  ww  w .  j  a  v  a2  s  . c om*/
 * @return File
 * @throws EFapsException on error
 */
private File setFile(final boolean _gziped) throws EFapsException {
    final String filename = _gziped ? this.key + "GZIP" : this.key;
    final File ret = FileUtils.getFile(TempFileBundle.getTempFolder(), filename);
    try {
        final FileOutputStream out = new FileOutputStream(ret);
        final byte[] buffer = new byte[1024];
        int bytesRead;
        if (_gziped) {
            final GZIPOutputStream zout = new GZIPOutputStream(out);
            for (final String oid : this.oids) {
                final Checkout checkout = new Checkout(oid);
                final InputStream bis = checkout.execute();
                while ((bytesRead = bis.read(buffer)) != -1) {
                    zout.write(buffer, 0, bytesRead);
                }
            }
            zout.close();
        } else {
            for (final String oid : this.oids) {
                final Checkout checkout = new Checkout(oid);
                final InputStream bis = checkout.execute();
                while ((bytesRead = bis.read(buffer)) != -1) {
                    out.write(buffer, 0, bytesRead);
                }
            }
        }
        out.close();
    } catch (final IOException e) {
        throw new EFapsException(this.getClass(), "setFile", e, filename);
    }
    return ret;
}

From source file:org.pentaho.amazon.client.impl.S3ClientImplTest.java

private void createGzArchive() throws Exception {
    FileInputStream fileInputStream = null;
    FileOutputStream fileOutputStream = null;
    GZIPOutputStream gzipOutputStream = null;

    try {//w  ww .j  ava  2 s.  c  o  m
        fileInputStream = new FileInputStream(logFileName);
        fileOutputStream = new FileOutputStream(gzArchName);
        gzipOutputStream = new GZIPOutputStream(fileOutputStream);
        byte[] buffer = new byte[1024];
        int len;
        while ((len = fileInputStream.read(buffer)) != -1) {
            gzipOutputStream.write(buffer, 0, len);
        }
    } finally {
        gzipOutputStream.close();
        fileOutputStream.close();
        fileInputStream.close();
    }
}