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 void write(int b) throws IOException 

Source Link

Document

Writes a byte to the compressed output stream.

Usage

From source file:nl.nn.adapterframework.util.Misc.java

public static byte[] gzip(byte[] input) throws IOException {

    // Create an expandable byte array to hold the compressed data.
    // You cannot use an array that's the same size as the orginal because
    // there is no guarantee that the compressed data will be smaller than
    // the uncompressed data.
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
    GZIPOutputStream gz = new GZIPOutputStream(bos);
    gz.write(input);
    gz.close();/* ww w.ja va2  s.c o  m*/
    bos.close();

    // Get the compressed data
    return bos.toByteArray();
}

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

/**
 *
 * @param text//from   w  ww .j  a  v a  2s . co 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:it.evilsocket.dsploit.core.System.java

public static String saveSession(String sessionName) throws IOException {
    StringBuilder builder = new StringBuilder();
    String filename = mStoragePath + '/' + sessionName + ".dss", session = null;

    builder.append(SESSION_MAGIC + "\n");

    // skip the network target
    builder.append((mTargets.size() - 1) + "\n");
    for (Target target : mTargets) {
        if (target.getType() != Target.Type.NETWORK)
            target.serialize(builder);/*w  w w .j a v a2 s  .c o  m*/
    }
    builder.append(mCurrentTarget + "\n");

    session = builder.toString();

    FileOutputStream ostream = new FileOutputStream(filename);
    GZIPOutputStream gzip = new GZIPOutputStream(ostream);

    gzip.write(session.getBytes());

    gzip.close();

    mSessionName = sessionName;

    return filename;
}

From source file:it.evilsocket.dsploit.core.System.java

public static String saveHijackerSession(String sessionName, Session session) throws IOException {
    StringBuilder builder = new StringBuilder();
    String filename = mStoragePath + '/' + sessionName + ".dhs", buffer = null;

    builder.append(SESSION_MAGIC + "\n");

    builder.append((session.mUserName == null ? "null" : session.mUserName) + "\n");
    builder.append(session.mHTTPS + "\n");
    builder.append(session.mAddress + "\n");
    builder.append(session.mDomain + "\n");
    builder.append(session.mUserAgent + "\n");
    builder.append(session.mCookies.size() + "\n");
    for (BasicClientCookie cookie : session.mCookies.values()) {
        builder.append(cookie.getName() + "=" + cookie.getValue() + "; domain=" + cookie.getDomain()
                + "; path=/" + (session.mHTTPS ? ";secure" : "") + "\n");
    }//from  w  ww  . j a v  a2 s . co  m

    buffer = builder.toString();

    FileOutputStream ostream = new FileOutputStream(filename);
    GZIPOutputStream gzip = new GZIPOutputStream(ostream);

    gzip.write(buffer.getBytes());

    gzip.close();

    mSessionName = sessionName;

    return filename;
}

From source file:io.restassured.internal.http.GZIPDecompressingEntityTest.java

private byte[] gzipCompress(String string) throws IOException {
    byte[] bytes = string.getBytes("UTF-8");
    ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);
    GZIPOutputStream gzip = new GZIPOutputStream(bos);
    gzip.write(bytes);// w w w  . j  a  v  a2  s  . c  om
    gzip.close();
    byte[] compressed = bos.toByteArray();
    bos.close();
    return compressed;
}

From source file:org.apache.oozie.compression.GzipCompressionCodec.java

public byte[] compressBytes(byte[] header, byte[] data) throws IOException {
    ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(2000);
    byteOutput.write(header);/*from w  w w.  jav  a 2  s  . c o  m*/
    GZIPOutputStream gzipOut = new GZIPOutputStream(byteOutput);
    gzipOut.write(data);
    gzipOut.close();
    return byteOutput.toByteArray();
}

From source file:org.apache.oozie.compression.GzipCompressionCodec.java

public byte[] compressString(byte[] header, String data) throws IOException {
    ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(1000);
    byteOutput.write(header);/*from  w w  w .j  a  va2s .  c  om*/
    GZIPOutputStream gzipOut = new GZIPOutputStream(byteOutput);
    gzipOut.write(data.getBytes(CodecFactory.UTF_8_ENCODING));
    gzipOut.close();
    return byteOutput.toByteArray();
}

From source file:org.slc.sli.api.security.oauth.TokenGeneratorTest.java

/**
 * Not enabled, but would be a quick and dirty way of verifying some amount
 * of entropy in our pseudo-randomness by checking out well it compresses
 *//*from ww w  . java 2 s. com*/
public void testEntropy() throws Exception {
    String string = TokenGenerator.generateToken(1000);
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    GZIPOutputStream out = new GZIPOutputStream(bytes);
    out.write(string.getBytes());
    out.close();
    assertTrue("Entropy was too low..." + bytes.toByteArray().length, bytes.toByteArray().length > 700);
}

From source file:com.pinterest.terrapin.zookeeper.ViewInfoTest.java

@Test
public void testCompressedSerialization() throws Exception {
    // Check that compressed json serialization works correctly.
    byte[] compressedJson = viewInfo.toCompressedJson();
    GZIPInputStream zipIn = new GZIPInputStream(new ByteArrayInputStream(compressedJson));
    assertEquals(JSON, new String(IOUtils.toByteArray(zipIn)));

    // Check that compressed json deserialization works correctly.
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream zipOut = new GZIPOutputStream(out);
    zipOut.write(JSON.getBytes());
    zipOut.close();//  w  w  w .jav  a2s.c o  m
    assertEquals(viewInfo, ViewInfo.fromCompressedJson(out.toByteArray()));
}

From source file:org.pentaho.di.cluster.HttpUtilTest.java

/**
 * https://www.securecoding.cert.org/confluence/display/java/IDS12-J.+Perform+lossless+conversion+
 * of+String+data+between+differing+character+encodings
 *
 * @param in//from   w  w w . j a  v  a2  s. c  om
 *          string to encode
 * @return
 * @throws IOException
 */
private String canonicalBase64Encode(String in) throws IOException {
    Charset charset = Charset.forName(DEFAULT_ENCODING);
    CharsetEncoder encoder = charset.newEncoder();
    encoder.reset();
    ByteBuffer baosbf = encoder.encode(CharBuffer.wrap(in));
    byte[] bytes = new byte[baosbf.limit()];
    baosbf.get(bytes);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzos = new GZIPOutputStream(baos);
    gzos.write(bytes);
    gzos.close();
    String encoded = new String(Base64.encodeBase64(baos.toByteArray()));

    return encoded;
}