Example usage for java.util.zip GZIPOutputStream flush

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

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes the compressed output stream.

Usage

From source file:yui.classes.utils.IOUtils.java

public static int gzipAndCopyContent(OutputStream out, byte[] bytes) throws IOException {
    ByteArrayOutputStream baos = null;
    GZIPOutputStream gzos = null;

    int length = 0;
    try {/*from w w  w .  j  a v a  2  s.c om*/
        baos = new ByteArrayOutputStream();
        gzos = new GZIPOutputStream(baos);

        gzos.write(bytes);
        gzos.finish();
        gzos.flush();
        gzos.close();

        byte[] gzippedBytes = baos.toByteArray();

        // Set the size of the file.
        length = gzippedBytes.length;
        // Write the binary context out

        copy(new ByteArrayInputStream(gzippedBytes), out);
        out.flush();
    } finally {
        try {
            if (gzos != null) {
                gzos.close();
            }
        } catch (Exception ignored) {
        }
        try {
            if (baos != null) {
                baos.close();
            }
        } catch (Exception ignored) {
        }
    }
    return length;
}

From source file:lapin.load.Loader.java

static private byte[] compress(byte[] bytes) throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    GZIPOutputStream gout = new GZIPOutputStream(bout);
    gout.write(bytes);/*from   ww w.  ja v  a 2 s .c o m*/
    gout.flush();
    IO.close(gout);
    IO.close(bout);
    return bout.toByteArray();
}

From source file:org.cruxframework.crux.core.server.rest.spi.HttpUtil.java

private static byte[] getResponseBytes(HttpRequest request, HttpResponse response, String responseContent)
        throws UnsupportedEncodingException, IOException {
    boolean gzipResponse = shouldGzipResponseContent(request, responseContent);
    byte[] responseBytes = (responseContent != null ? responseContent.getBytes("UTF-8") : new byte[0]);
    if (gzipResponse) {
        ByteArrayOutputStream output = null;
        GZIPOutputStream gzipOutputStream = null;
        try {/*from   ww w  .  java2  s.c  om*/
            output = new ByteArrayOutputStream(responseBytes.length);
            gzipOutputStream = new GZIPOutputStream(output);
            gzipOutputStream.write(responseBytes);
            gzipOutputStream.finish();
            gzipOutputStream.flush();
            response.getOutputHeaders().putSingle(HttpHeaderNames.CONTENT_ENCODING, "gzip");
            responseBytes = output.toByteArray();
        } catch (IOException e) {
            throw new InternalServerErrorException("Unable to compress response",
                    "Error processing requested service", e);
        } finally {
            if (null != gzipOutputStream) {
                gzipOutputStream.close();
            }
            if (null != output) {
                output.close();
            }
        }
    }
    return responseBytes;
}

From source file:org.mitre.opensextant.util.FileUtility.java

/**
 *
 * @param text/*from  w  w w .  ja va 2  s.  c o  m*/
 * @param fname
 * @return
 * @throws IOException
 */
public static boolean writeGzipFile(String text, String fname) throws IOException {
    if (fname == null || text == null) {
        return false;
    }

    FileOutputStream outstream = new FileOutputStream(fname);
    GZIPOutputStream gzout = new GZIPOutputStream(new BufferedOutputStream(outstream), default_buffer);

    gzout.write(text.getBytes(default_encoding));

    gzout.flush();
    gzout.finish();

    gzout.close();
    outstream.close();
    return true;

}

From source file:forge.quest.io.QuestDataIO.java

private static void savePacked(final String f, final XStream xStream, final QuestData qd) throws IOException {
    final BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(f));
    final GZIPOutputStream zout = new GZIPOutputStream(bout);
    xStream.toXML(qd, zout);//from w  w  w.  j  a  va 2 s  .co  m
    zout.flush();
    zout.close();
}

From source file:org.ofbiz.webapp.event.RestEventHandler.java

/** 
* ? /*  w  w w  . jav a2  s.  c  om*/
*  
* @param is 
* @param os 
* @throws Exception 
*/
public static void compress(InputStream is, OutputStream os) throws Exception {

    GZIPOutputStream gos = new GZIPOutputStream(os);

    int count;
    byte data[] = new byte[BUFFER];
    while ((count = is.read(data, 0, BUFFER)) != -1) {
        gos.write(data, 0, count);
    }

    gos.finish();

    gos.flush();
    gos.close();
}

From source file:gov.nih.nci.caarray.util.CaArrayUtils.java

/**
 * Serializes the given object (zipped) to a byte array.
 * /*w  w w  .j a v  a 2 s. c om*/
 * @param serializable object to serialize
 * @return the serialized object as a byte array.
 */
public static byte[] serialize(Serializable serializable) {
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    GZIPOutputStream gZipOutputStream = null;
    ObjectOutputStream objectOutputStream = null;
    try {
        gZipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
        objectOutputStream = new ObjectOutputStream(gZipOutputStream);
        objectOutputStream.writeObject(serializable);
        objectOutputStream.flush();
        gZipOutputStream.finish();
        gZipOutputStream.flush();
        byteArrayOutputStream.flush();
    } catch (final IOException e) {
        throw new IllegalStateException("Couldn't serialize object", e);
    } finally {
        IOUtils.closeQuietly(objectOutputStream);
        IOUtils.closeQuietly(gZipOutputStream);
        IOUtils.closeQuietly(byteArrayOutputStream);
    }
    return byteArrayOutputStream.toByteArray();
}

From source file:org.opensextant.util.FileUtility.java

/**
 *
 * @param text/*from   w ww . j a va 2s .  c o m*/
 *            buffer to write
 * @param filepath
 *            path to file
 * @return status true if file was written
 * @throws IOException
 *             on error
 */
public static boolean writeGzipFile(String text, String filepath) throws IOException {
    if (filepath == null || text == null) {
        return false;
    }

    final FileOutputStream outstream = new FileOutputStream(filepath);
    final GZIPOutputStream gzout = new GZIPOutputStream(new BufferedOutputStream(outstream), ioBufferSize);

    gzout.write(text.getBytes(default_encoding));

    gzout.flush();
    gzout.finish();

    gzout.close();
    outstream.close();
    return true;

}

From source file:com.orange.clara.cloud.servicedbdumper.filer.compression.GzipCompressing.java

@Async
public Future<Boolean> gziptIt(InputStream inputStream, OutputStream outputStream) throws IOException {
    logger.debug("Start compressing...");
    GZIPOutputStream gout = new GZIPOutputStream(outputStream);
    ByteStreams.copy(inputStream, gout);
    gout.flush();
    gout.close();/*from w  w w  . j  av  a 2  s  . c om*/
    outputStream.flush();
    outputStream.close();
    inputStream.close();
    logger.debug("Finish compressing");
    return new AsyncResult<Boolean>(true);
}

From source file:org.forgerock.openam.cts.utils.blob.strategies.CompressionStrategy.java

/**
 * Compress the Tokens binary object./*from ww  w  .ja va 2 s .c  om*/
 *
 * @param token Non null Token to modify.
 *
 * @throws org.forgerock.openam.cts.utils.blob.TokenStrategyFailedException {@inheritDoc}
 */
@Override
public void perform(Token token) throws TokenStrategyFailedException {
    bout.reset();
    try {
        GZIPOutputStream out = new GZIPOutputStream(bout);
        out.write(token.getBlob(), 0, token.getBlob().length);
        out.flush();
        out.close();
    } catch (IOException e) {
        throw new TokenStrategyFailedException(e);
    }
    token.setBlob(bout.toByteArray());
}