Example usage for java.util.zip DeflaterOutputStream close

List of usage examples for java.util.zip DeflaterOutputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Writes remaining compressed data to the output stream and closes the underlying stream.

Usage

From source file:de.topobyte.livecg.core.painting.backend.ipe.IpeImageEncoder.java

public static IpeImage encode(Image image) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DeflaterOutputStream dos = new DeflaterOutputStream(baos);
    for (int j = 0; j < image.getHeight(); j++) {
        for (int i = 0; i < image.getWidth(); i++) {
            int rgb = image.getRGB(i, j);
            dos.write(rgb >> 16);/*from   w  w  w .  jav  a  2s.c  om*/
            dos.write(rgb >> 8);
            dos.write(rgb);
        }
    }
    dos.close();
    byte[] buffer = baos.toByteArray();
    int length = buffer.length;

    byte[] bbase64 = Base64.encodeBase64(buffer);
    String base64 = new String(bbase64);

    return new IpeImage(base64, length);
}

From source file:com.tremolosecurity.unison.u2f.util.U2fUtil.java

public static String encode(List<SecurityKeyData> devices, String encyrptionKeyName) throws Exception {
    ArrayList<KeyHolder> keys = new ArrayList<KeyHolder>();
    for (SecurityKeyData dr : devices) {
        KeyHolder kh = new KeyHolder();
        kh.setCounter(dr.getCounter());//from   ww  w  .j  ava2s .  c  om
        kh.setEnrollmentTime(dr.getEnrollmentTime());
        kh.setKeyHandle(dr.getKeyHandle());
        kh.setPublicKey(dr.getPublicKey());
        kh.setTransports(dr.getTransports());
        keys.add(kh);
    }

    String json = gson.toJson(keys);
    EncryptedMessage msg = new EncryptedMessage();

    SecretKey key = GlobalEntries.getGlobalEntries().getConfigManager().getSecretKey(encyrptionKeyName);
    if (key == null) {
        throw new Exception("Queue message encryption key not found");
    }

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    msg.setMsg(cipher.doFinal(json.getBytes("UTF-8")));
    msg.setIv(cipher.getIV());

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    DeflaterOutputStream compressor = new DeflaterOutputStream(baos,
            new Deflater(Deflater.BEST_COMPRESSION, true));

    compressor.write(gson.toJson(msg).getBytes("UTF-8"));
    compressor.flush();
    compressor.close();

    String b64 = new String(Base64.encodeBase64(baos.toByteArray()));

    return b64;

}

From source file:com.jzboy.couchdb.DatabaseDocUpdateTest.java

@BeforeClass
public static void setUpClass() throws Exception {
    dbName = "jzboy_test_db_" + System.currentTimeMillis();
    instance = new Database(dbName);
    instance.create();//  w  w w  .j  a v a  2s.co  m

    // create a gzipped attachment
    final String inputString = "blahblahblah??";
    byte[] input = inputString.getBytes("utf-8");
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    DeflaterOutputStream deflater = new DeflaterOutputStream(os);
    deflater.write(input, 0, input.length);
    deflater.close();
    attachment = os.toByteArray();
}

From source file:ZipUtil.java

public static void zipFileToFile(File flSource, File flTarget) throws IOException {
    Deflater oDeflate = new Deflater(Deflater.DEFLATED, false);

    FileInputStream stmFileIn = new FileInputStream(flSource);
    FileOutputStream stmFileOut = new FileOutputStream(flTarget);
    DeflaterOutputStream stmDeflateOut = new DeflaterOutputStream(stmFileOut, oDeflate);
    try {/*  w w  w  .  ja v  a  2  s .co  m*/
        // FileUtil.inputStreamToOutputStream(stmFileIn, stmDeflateOut);
    } //end try
    finally {
        stmDeflateOut.finish();
        stmDeflateOut.flush();
        stmDeflateOut.close();
        stmFileOut.close();
        stmFileIn.close();
    }
}

From source file:com.hundsun.jresplus.web.nosession.cookie.HessianZipSerializer.java

public static byte[] encode(Object object) throws SerializationException {
    if (object == null) {
        return null;
    }/*  w  ww  . j a  v a  2s  .  co  m*/
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    Deflater def = new Deflater(Deflater.BEST_COMPRESSION, false);
    DeflaterOutputStream dos = new DeflaterOutputStream(baos, def);

    Hessian2Output ho = null;

    try {
        ho = new Hessian2Output(dos);
        ho.writeObject(object);
    } catch (Exception e) {
        throw new SerializationException("Failed to encode date", e);
    } finally {
        if (ho != null) {
            try {
                ho.close();
            } catch (IOException e) {
            }
        }
        try {
            dos.close();
        } catch (IOException e) {
        }

        def.end();
    }

    return baos.toByteArray();
}

From source file:gov.nasa.ensemble.common.CommonUtils.java

public static byte[] compress(byte[] input) {
    try {/*from  ww w  . j a va2s . c  om*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DeflaterOutputStream dos = new DeflaterOutputStream(baos);
        dos.write(input);
        dos.close();
        return baos.toByteArray();
    } catch (IOException e) {
        throw new Error(e);
    }
}

From source file:com.uber.hoodie.common.HoodieJsonPayload.java

private byte[] compressData(String jsonData) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
    DeflaterOutputStream dos = new DeflaterOutputStream(baos, deflater, true);
    try {/* w  w  w .  j  ava 2 s  .com*/
        dos.write(jsonData.getBytes());
    } finally {
        dos.flush();
        dos.close();
        // Its important to call this.
        // Deflater takes off-heap native memory and does not release until GC kicks in
        deflater.end();
    }
    return baos.toByteArray();
}

From source file:com.linkedin.r2.filter.compression.stream.TestStreamingCompression.java

@Test
public void testDeflateCompressor()
        throws IOException, InterruptedException, CompressionException, ExecutionException {
    StreamingCompressor compressor = new DeflateCompressor(_executor);
    final byte[] origin = new byte[BUF_SIZE];
    Arrays.fill(origin, (byte) 'c');

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    DeflaterOutputStream zlib = new DeflaterOutputStream(out);
    IOUtils.write(origin, zlib);/*from   w  ww.j  a v  a  2  s . c  om*/
    zlib.close();
    byte[] compressed = out.toByteArray();

    testCompress(compressor, origin, compressed);
    testDecompress(compressor, origin, compressed);
    testCompressThenDecompress(compressor, origin);
}

From source file:com.uber.hoodie.common.TestRawTripPayload.java

private byte[] compressData(String jsonData) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DeflaterOutputStream dos = new DeflaterOutputStream(baos, new Deflater(Deflater.BEST_COMPRESSION), true);
    try {/*  w  ww .  j av  a2s .c  o m*/
        dos.write(jsonData.getBytes());
    } finally {
        dos.flush();
        dos.close();
    }
    return baos.toByteArray();
}

From source file:com.cloud.agent.api.SecurityIngressRulesCmd.java

public String compressStringifiedRules() {
    StringBuilder ruleBuilder = new StringBuilder();
    for (SecurityIngressRulesCmd.IpPortAndProto ipPandP : getRuleSet()) {
        ruleBuilder.append(ipPandP.getProto()).append(":").append(ipPandP.getStartPort()).append(":")
                .append(ipPandP.getEndPort()).append(":");
        for (String cidr : ipPandP.getAllowedCidrs()) {
            ruleBuilder.append(cidr).append(",");
        }/*from ww w  . jav  a  2 s .  co  m*/
        ruleBuilder.append("NEXT");
        ruleBuilder.append(" ");
    }
    String stringified = ruleBuilder.toString();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        //Note : not using GZipOutputStream since that is for files
        //GZipOutputStream gives a different header, although the compression is the same
        DeflaterOutputStream dzip = new DeflaterOutputStream(out);
        dzip.write(stringified.getBytes());
        dzip.close();
    } catch (IOException e) {
        s_logger.warn("Exception while compressing ingress rules");
        return null;
    }
    return Base64.encodeBase64String(out.toByteArray());
}