List of usage examples for java.util.zip Deflater setInput
public void setInput(ByteBuffer input)
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); compressor.finish();/*ww w .j a va 2 s .c o m*/ 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:Main.java
public static byte[] compress(byte[] input) { if (input == null || input.length == 0) return input; Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); // Give the compressor the data to compress compressor.setInput(input); compressor.finish();/*from w ww. j a va2s. c om*/ // Create an expandable byte array to hold the compressed data. // It is not necessary 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); } // Get the compressed data return bos.toByteArray(); }
From source file:Main.java
public static String compressAndB64EncodeUTF8Bytes(byte[] bytes) throws Exception { byte[] input = bytes; // 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();/*w w w . j a v a 2 s . c o m*/ // Create an expandable byte array to hold the compressed data. // It is not necessary that the compressed data will be smaller than // the uncompressed data. ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length); // Compress the data byte[] buf = new byte[32]; 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 new String(Base64.encode(compressedData), "UTF-8"); }
From source file:org.jasig.cas.util.CompressionUtils.java
/** * Deflate the given string via a {@link java.util.zip.Deflater}. * The result will be base64 encoded with {@link #UTF8_ENCODING}. * * @param data the data/* w w w. ja v a 2s. c o m*/ * @return base64 encoded string */ public static String deflate(final String data) { try { final Deflater deflater = new Deflater(); deflater.setInput(data.getBytes(UTF8_ENCODING)); deflater.finish(); final byte[] buffer = new byte[data.length()]; final int resultSize = deflater.deflate(buffer); final byte[] output = new byte[resultSize]; System.arraycopy(buffer, 0, output, 0, resultSize); return encodeBase64(output); } catch (final UnsupportedEncodingException e) { throw new RuntimeException("Cannot find encoding:" + UTF8_ENCODING, e); } }
From source file:Main.java
/** * Compress the byte array passed//from w w w. ja v a 2s .c o m * <p> * @param input byte array * @param bufferLength buffer length * @return compressed byte array * @throws IOException thrown if we can't close the output stream */ public static byte[] compressByteArray(byte[] input, int bufferLength) throws IOException { // 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. // It is not necessary that the compressed data will be smaller than // the uncompressed data. ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length); // Compress the data byte[] buf = new byte[bufferLength]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } // JCS-136 ( Details here : http://www.devguli.com/blog/eng/java-deflater-and-outofmemoryerror/ ) compressor.end(); bos.close(); // Get the compressed data return bos.toByteArray(); }
From source file:org.jahia.utils.Url.java
/** * Encode facet filter URL parameter//from w w w.ja v a2 s .c o m * @param inputString facet filter parameter * @return filter encoded for URL query parameter usage */ public static String encodeUrlParam(String inputString) { if (StringUtils.isEmpty(inputString)) { return inputString; } // Compress the bytes byte[] output = new byte[2048]; Deflater compresser = new Deflater(); try { compresser.setInput(inputString.getBytes("UTF-8")); compresser.finish(); int compressedDataLength = compresser.deflate(output); byte[] copy = new byte[compressedDataLength]; System.arraycopy(output, 0, copy, 0, Math.min(output.length, compressedDataLength)); return Base64.encodeBase64URLSafeString(copy); } catch (UnsupportedEncodingException e) { logger.warn("Not able to encode facet URL: " + inputString, e); } return inputString; }
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); deflater.finish();/*from w w w .j av a 2s.c o m*/ 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./*from ww w . j av a 2s . 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: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); compressor.finish();/*from w w w.j a v a 2 s. c o m*/ // 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.igormaznitsa.jcp.expression.functions.FunctionBINFILE.java
@Nonnull private static byte[] deflate(@Nonnull final byte[] data) throws IOException { final Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION); deflater.setInput(data); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); deflater.finish();/*from w ww .jav a 2 s .co m*/ final byte[] buffer = new byte[1024]; while (!deflater.finished()) { final int count = deflater.deflate(buffer); outputStream.write(buffer, 0, count); } outputStream.close(); final byte[] output = outputStream.toByteArray(); deflater.end(); return output; }