List of usage examples for java.util.zip Deflater setInput
public void setInput(ByteBuffer input)
From source file:ZipUtil.java
/** * Deflates the file and returns the deflated file. *//*w ww. j a v a 2 s . co m*/ public static byte[] zipByteArray(byte[] file) throws IOException { byte[] byReturn = null; Deflater oDeflate = new Deflater(Deflater.DEFLATED, false); oDeflate.setInput(file); oDeflate.finish(); ByteArrayOutputStream oZipStream = new ByteArrayOutputStream(); try { while (!oDeflate.finished()) { byte[] byRead = new byte[ZIP_BUFFER_SIZE]; int iBytesRead = oDeflate.deflate(byRead); if (iBytesRead == byRead.length) { oZipStream.write(byRead); } else { oZipStream.write(byRead, 0, iBytesRead); } } oDeflate.end(); byReturn = oZipStream.toByteArray(); } finally { oZipStream.close(); } return byReturn; }
From source file:fr.eo.util.dumper.Dumper.java
private static String getCompressedString(String value) { byte[] output = new byte[8096]; try {//from ww w. j a v a 2s . c om byte[] input = value.getBytes("UTF-8"); Deflater compresser = new Deflater(Deflater.BEST_COMPRESSION, true); compresser.setInput(input); compresser.finish(); int compressedDataLength = compresser.deflate(output); return "X'" + Hex.encodeHexString(Arrays.copyOf(output, compressedDataLength)) + "'"; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; }
From source file:com.simiacryptus.text.CompressionUtil.java
/** * Encode lz byte [ ]./* w w w . j a va2 s .co m*/ * * @param bytes the bytes * @param dictionary the dictionary * @return the byte [ ] */ public static byte[] encodeLZ(byte[] bytes, String dictionary) { byte[] output = new byte[(int) (bytes.length * 1.05 + 32)]; Deflater compresser = new Deflater(); try { compresser.setInput(bytes); if (null != dictionary && !dictionary.isEmpty()) { byte[] bytes2 = dictionary.getBytes("UTF-8"); compresser.setDictionary(bytes2); } } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } compresser.finish(); int compressedDataLength = compresser.deflate(output); compresser.end(); return Arrays.copyOf(output, compressedDataLength); }
From source file:org.atricore.idbus.capabilities.sso.main.binding.SamlR2HttpRedirectBinding.java
public static String deflateForRedirect(String redirStr, boolean encode) { int n = redirStr.length(); byte[] redirIs = null; try {// ww w . j a v a 2s. c o m redirIs = redirStr.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } byte[] deflated = new byte[n]; Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true); deflater.setInput(redirIs); deflater.finish(); int len = deflater.deflate(deflated); deflater.end(); byte[] exact = new byte[len]; System.arraycopy(deflated, 0, exact, 0, len); if (encode) { byte[] base64Str = new Base64().encode(exact); return new String(base64Str); } return new String(exact); }
From source file:edu.stanford.junction.addon.JSONObjWrapper.java
private static String compressString(String str) { byte[] input; try {//from w ww . j ava 2 s. com input = str.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { input = str.getBytes(); } // Create the compressor with highest level of compression Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); // Give the compressor the data to compress compressor.setInput(input); compressor.finish(); // 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); // Compress the data byte[] buf = new byte[1024]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } try { bos.close(); } catch (IOException e) { } // Get the compressed data byte[] compressedData = bos.toByteArray(); return Base64.encodeBytes(compressedData); }
From source file:gov.niem.ws.util.SecurityUtil.java
/** * Encode data with DEFLATE and then Base64 without chunking, so it can * safely be placed in an HTTP header./*w w w.j a va 2 s . co m*/ * * @param data * @return the compressed, b64 encoded data * @throws DataFormatException */ public static String encodeHeader(byte[] data) throws DataFormatException { // TODO: length limit on encoded? ByteArrayOutputStream out = new ByteArrayOutputStream(data.length); Deflater deflater = new Deflater(); deflater.setInput(data); deflater.finish(); byte[] buffer = new byte[1024]; while (!deflater.finished()) { int count = deflater.deflate(buffer); if (count == 0) break; out.write(buffer, 0, count); } try { deflater.end(); out.close(); } catch (IOException e) { } return new String(Base64.encodeBase64(out.toByteArray(), false)); }
From source file:org.ow2.proactive.utils.ObjectByteConverter.java
/** * Convert the given Serializable Object into a byte array. * <p>/*from w w w. j av a 2s . c o m*/ * The returned byteArray can be compressed by setting compress boolean argument value to <code>true</code>. * * @param obj the Serializable object to be compressed * @param compress true if the returned byteArray must be also compressed, false if no compression is required. * @return a compressed (or not) byteArray representing the Serialization of the given object. * @throws IOException if an I/O exception occurs when writing the output byte array */ public static final byte[] objectToByteArray(Object obj, boolean compress) throws IOException { ByteArrayOutputStream baos = null; ObjectOutputStream oos = null; if (obj == null) { return null; } try { baos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(baos); oos.writeObject(obj); oos.flush(); if (!compress) { // Return the UNCOMPRESSED data return baos.toByteArray(); } else { // Compressor with highest level of compression Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); // Give the compressor the data to compress compressor.setInput(baos.toByteArray()); compressor.finish(); ByteArrayOutputStream bos = null; try { // Create an expandable byte array to hold the compressed data. bos = new ByteArrayOutputStream(); // Compress the data byte[] buf = new byte[512]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } // Return the COMPRESSED data return bos.toByteArray(); } finally { if (bos != null) { bos.close(); } } } } finally { if (oos != null) { oos.close(); } if (baos != null) { baos.close(); } } }
From source file:de.unidue.inf.is.ezdl.dlcore.utils.StringUtils.java
/** * Compresses a string.//from www . j a v a2 s. c o m * * @param s * The string to compress * @return The compressed string */ public static String compress(String s) { ByteArrayOutputStream baos = null; try { byte[] input = s.getBytes("UTF-8"); Deflater compresser = new Deflater(); compresser.setLevel(Deflater.BEST_COMPRESSION); compresser.setInput(input); compresser.finish(); baos = new ByteArrayOutputStream(); while (!compresser.finished()) { byte[] output = new byte[1024]; int compressedDataLength = compresser.deflate(output); baos.write(output, 0, compressedDataLength); } baos.flush(); return Base64.encodeBase64String(baos.toByteArray()); } catch (UnsupportedEncodingException e) { logger.error(e.getMessage(), e); } catch (IOException e) { logger.error(e.getMessage(), e); } finally { ClosingUtils.close(baos); } return ""; }
From source file:com.android.server.wifi.WifiLogger.java
private static String compressToBase64(byte[] input) { String result;/*from ww w . j a va 2s . c o m*/ //compress Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); compressor.setInput(input); compressor.finish(); ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length); final byte[] buf = new byte[1024]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } try { compressor.end(); bos.close(); } catch (IOException e) { Log.e(TAG, "ByteArrayOutputStream close error"); result = android.util.Base64.encodeToString(input, Base64.DEFAULT); return result; } byte[] compressed = bos.toByteArray(); if (DBG) { Log.d(TAG, " length is:" + (compressed == null ? "0" : compressed.length)); } //encode result = android.util.Base64.encodeToString(compressed.length < input.length ? compressed : input, Base64.DEFAULT); if (DBG) { Log.d(TAG, "FwMemoryDump length is :" + result.length()); } return result; }
From source file:com.nary.Debug.java
public static void saveClass(OutputStream _out, Object _class, boolean _compress) throws IOException { if (_compress) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream OOS = new ObjectOutputStream(bos); OOS.writeObject(_class); byte[] dataArray = bos.toByteArray(); byte[] test = new byte[dataArray.length]; // this is where the byte array gets compressed to Deflater def = new Deflater(Deflater.BEST_COMPRESSION); def.setInput(dataArray); def.finish();//from w ww . j a v a 2 s . co m def.deflate(test); _out.write(test, 0, def.getTotalOut()); } else { ObjectOutputStream OS = new ObjectOutputStream(_out); OS.writeObject(_class); } }