List of usage examples for java.util.zip Deflater setLevel
public void setLevel(int level)
From source file:org.ojbc.util.helper.ZipUtils.java
public static byte[] zip(byte[] originalData) { Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); compressor.setInput(originalData);//www. j av a 2s . c o m compressor.finish(); ByteArrayOutputStream bos = new ByteArrayOutputStream(originalData.length); byte[] buf = new byte[1024]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } try { bos.close(); } catch (IOException e) { log.error("Failed to zip data " + originalData, e); } byte[] compressedData = bos.toByteArray(); log.debug("Orignal data:" + originalData.length + " bytes"); log.debug("Compressed data:" + compressedData.length + " bytes"); return compressedData; }
From source file:com.qatickets.common.ZIPHelper.java
public static byte[] compress(byte[] data) { // Create the compressor with highest level of compression final Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); // Give the compressor the data to compress compressor.setInput(data);/*w ww . ja va 2 s .c o m*/ 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. final ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length); // Compress the data byte[] buf = new byte[1024]; while (!compressor.finished()) { final int count = compressor.deflate(buf); bos.write(buf, 0, count); } try { bos.close(); } catch (IOException e) { } // Get the compressed data final byte[] compressedData = bos.toByteArray(); compressor.end(); return compressedData; }
From source file:com.asual.lesscss.ResourcePackage.java
private static byte[] deflate(byte[] input) throws IOException { Deflater deflater = new Deflater(); deflater.setLevel(Deflater.BEST_COMPRESSION); deflater.setInput(input);/*from ww w. j a va 2 s. c o m*/ deflater.finish(); ByteArrayOutputStream baos = new ByteArrayOutputStream(input.length); byte[] buf = new byte[1024]; while (!deflater.finished()) { int count = deflater.deflate(buf); baos.write(buf, 0, count); } baos.close(); return baos.toByteArray(); }
From source file:radixcore.network.ByteBufIO.java
/** * Compresses the data in a byte array.// w w w . ja v a2s . co m * * @param input The byte array to be compressed. * @return The byte array in its compressed form. */ public static byte[] compress(byte[] input) { try { final Deflater deflater = new Deflater(); deflater.setLevel(Deflater.BEST_COMPRESSION); deflater.setInput(input); final ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(input.length); deflater.finish(); final byte[] buffer = new byte[1024]; while (!deflater.finished()) { final int count = deflater.deflate(buffer); byteOutput.write(buffer, 0, count); } deflater.end(); byteOutput.close(); return byteOutput.toByteArray(); } catch (final IOException e) { RadixExcept.logFatalCatch(e, "Error compressing byte array."); return null; } }
From source file:org.ow2.proactive.utils.ObjectByteConverter.java
/** * Convert the given Serializable Object into a byte array. * <p>/*w ww . j a v a2 s . co 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:edu.stanford.junction.addon.JSONObjWrapper.java
private static String compressString(String str) { byte[] input; try {// ww w .ja va2 s.c om 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:de.unidue.inf.is.ezdl.dlcore.utils.StringUtils.java
/** * Compresses a string.//from w ww .j av a 2 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 w w w .j av a 2 s. 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.bigdata.dastor.utils.FBUtilities.java
public static void compressToStream(byte[] input, ByteArrayOutputStream bos) throws IOException { // 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);//from www .j a v a2s . c o m compressor.finish(); // Write the compressed data to the stream byte[] buf = new byte[1024]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } }
From source file:nl.nn.adapterframework.util.Misc.java
public static byte[] compress(byte[] input) throws IOException { // 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);//from ww w .j a v a2 s . co m 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); } bos.close(); // Get the compressed data return bos.toByteArray(); }