List of usage examples for java.util.zip Deflater deflate
public int deflate(ByteBuffer output)
From source file:info.fetter.logstashforwarder.protocol.LumberjackClient.java
public int sendCompressedFrame(List<Map<String, byte[]>> keyValuesList) throws IOException { output.writeByte(PROTOCOL_VERSION);/*from w w w . j av a2 s. c om*/ output.writeByte(FRAME_COMPRESSED); ByteArrayOutputStream uncompressedBytes = new ByteArrayOutputStream(); DataOutputStream uncompressedOutput = new DataOutputStream(uncompressedBytes); for (Map<String, byte[]> keyValues : keyValuesList) { logger.trace("Adding data frame"); sendDataFrame(uncompressedOutput, keyValues); } uncompressedOutput.close(); Deflater compressor = new Deflater(); byte[] uncompressedData = uncompressedBytes.toByteArray(); if (logger.isDebugEnabled()) { logger.debug("Deflating data : " + uncompressedData.length + " bytes"); } if (logger.isTraceEnabled()) { HexDump.dump(uncompressedData, 0, System.out, 0); } compressor.setInput(uncompressedData); compressor.finish(); ByteArrayOutputStream compressedBytes = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; while (!compressor.finished()) { int count = compressor.deflate(buffer); compressedBytes.write(buffer, 0, count); } compressedBytes.close(); byte[] compressedData = compressedBytes.toByteArray(); if (logger.isDebugEnabled()) { logger.debug("Deflated data : " + compressor.getTotalOut() + " bytes"); } if (logger.isTraceEnabled()) { HexDump.dump(compressedData, 0, System.out, 0); } output.writeInt(compressor.getTotalOut()); output.write(compressedData); output.flush(); if (logger.isDebugEnabled()) { logger.debug("Sending compressed frame : " + keyValuesList.size() + " frames"); } return 6 + compressor.getTotalOut(); }
From source file:com.itude.mobile.android.util.DataUtil.java
/** * Compress byte array /* w w w. j av a 2s .c om*/ * * @param uncompressed byte array * @return compressed byte array */ public byte[] compress(byte[] uncompressed) { byte[] result = null; Deflater deflater = new Deflater(); deflater.setInput(uncompressed); deflater.finish(); // Create an expandable byte array to hold the compressed data. // You cannot use an array that's the same size as the original because // there is no guarantee that the compressed data will be smaller than // the uncompressed data. ByteArrayOutputStream bos = new ByteArrayOutputStream(uncompressed.length); // Compress the data byte[] buf = new byte[1024]; while (!deflater.finished()) { int count = deflater.deflate(buf); bos.write(buf, 0, count); } deflater.end(); try { bos.close(); } catch (IOException e) { MBLog.w(TAG, "Unable to close stream"); } // Get the compressed data result = bos.toByteArray(); return result; }
From source file:org.adeptnet.auth.saml.SAMLClient.java
private byte[] deflate(final byte[] input) throws IOException { // deflate and base-64 encode it final Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true); deflater.setInput(input);/* www . j a v a 2 s . co m*/ deflater.finish(); byte[] tmp = new byte[8192]; int count; final ByteArrayOutputStream bos = new ByteArrayOutputStream(); while (!deflater.finished()) { count = deflater.deflate(tmp); bos.write(tmp, 0, count); } bos.close(); deflater.end(); return bos.toByteArray(); }
From source file:com.eryansky.common.utils.SysUtils.java
/** * //from w ww. j ava2 s .co m * * @param in_str * * @return ? */ public static byte[] zip_Str(String in_str) { byte[] input = new byte[0]; try { input = in_str.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } ArrayList<Byte> al = new ArrayList<Byte>(); byte[] output; Deflater compresser = new Deflater(); compresser.setInput(input); compresser.finish(); for (; !compresser.finished();) { output = new byte[100]; compresser.deflate(output); for (int i = 0; i < output.length; i++) { al.add(new Byte(output[i])); } } output = new byte[al.size()]; for (int i = 0; i < al.size(); i++) { output[i] = (al.get(i)).byteValue(); } return output; }
From source file:com.funambol.transport.http.server.Sync4jServlet.java
/** * Sets the content of HTTP response./* w ww . j av a 2 s. co m*/ * * Compresses the response if the Accept-Encoding is gzip or deflate. * Sets the Content-Encoding according to the encoding used. * Sets the Content-Length with the length of the compressed response. * Sets the Uncompressed-Content-Length with the length of the uncompressed * response. * The response will be compressed only if the length of the uncompressed * response is greater than the give sizeThreashold. * * @param httpResponse the HttpServletResponse * @param requestAcceptEncoding the <code>Accept-Encoding</code> specified * in the request * @param sizeThreshold if the response is smaller of this value, it * should not be compressed * @param resp the SyncResponse object contains the response message * @param requestTime the time in which the request is arrived to servlet * @param sessionId the session identifier * @throws java.io.IOException if an error occurs * */ private void setResponseContent(HttpServletResponse httpResponse, String requestAcceptEncoding, String sizeThreshold, SyncResponse resp, long requestTime, String sessionId) throws IOException { byte[] responseContent = null; OutputStream out = null; try { out = httpResponse.getOutputStream(); responseContent = resp.getMessage(); int uncompressedContentLength = responseContent.length; if (supportedEncoding != null && !"".equals(supportedEncoding) && enableCompression) { if (log.isTraceEnabled()) { log.trace("Setting Accept-Encoding to " + supportedEncoding); } httpResponse.setHeader(HEADER_ACCEPT_ENCODING, supportedEncoding); } String encodingToUse = null; if (requestAcceptEncoding != null) { if (requestAcceptEncoding.indexOf(COMPRESSION_TYPE_GZIP) != -1 && requestAcceptEncoding.indexOf(COMPRESSION_TYPE_DEFLATE) != -1) { encodingToUse = preferredEncoding; } else if (requestAcceptEncoding.indexOf(COMPRESSION_TYPE_DEFLATE) != -1) { encodingToUse = COMPRESSION_TYPE_DEFLATE; } else if (requestAcceptEncoding.indexOf(COMPRESSION_TYPE_GZIP) != -1) { encodingToUse = COMPRESSION_TYPE_GZIP; } } int threshold = 0; try { if (sizeThreshold != null && sizeThreshold.length() != 0) { threshold = Integer.parseInt(sizeThreshold); } } catch (NumberFormatException ex) { // // Ignoring the specified value // if (log.isTraceEnabled()) { log.trace("The size threshold specified by the client (" + sizeThreshold + ") is not valid."); } } // // If the encodingToUse is null or the // uncompressed response length is less than // sizeThreshold, the response will not be compressed. // if (encodingToUse == null || uncompressedContentLength < threshold) { if (log.isTraceEnabled()) { if (enableCompression) { if (requestAcceptEncoding == null) { log.trace( "The client doesn't support any encoding. " + "The response is not compressed"); } else if (encodingToUse == null) { log.trace("The specified Accept-Encoding (" + requestAcceptEncoding + ") is not recognized. The response is not compressed"); } else if (uncompressedContentLength < threshold) { log.trace("The response is not compressed because smaller than " + threshold); } } } if (log.isTraceEnabled()) { log.trace("Setting Content-Length to: " + uncompressedContentLength); } httpResponse.setContentLength(uncompressedContentLength); out.write(responseContent); out.flush(); return; } if (encodingToUse != null) { if (log.isTraceEnabled()) { log.trace("Compressing the response using: " + encodingToUse); log.trace("Setting Uncompressed-Content-Length to: " + uncompressedContentLength); } httpResponse.setHeader(HEADER_UNCOMPRESSED_CONTENT_LENGTH, String.valueOf(uncompressedContentLength)); if (encodingToUse.equals(COMPRESSION_TYPE_GZIP)) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); GZIPOutputStream outTmp = new GZIPOutputStream(bos); outTmp.write(responseContent, 0, uncompressedContentLength); outTmp.flush(); outTmp.close(); // // Get the compressed data // responseContent = bos.toByteArray(); int compressedLength = responseContent.length; if (log.isTraceEnabled()) { log.trace("Setting Content-Length to: " + compressedLength); log.trace("Setting Content-Encoding to: " + COMPRESSION_TYPE_GZIP); } httpResponse.setContentLength(compressedLength); httpResponse.setHeader(HEADER_CONTENT_ENCODING, COMPRESSION_TYPE_GZIP); out.write(responseContent); out.flush(); } else if (encodingToUse.equals(COMPRESSION_TYPE_DEFLATE)) { // // Create the compressor with specificated level of compression // Deflater compressor = new Deflater(); compressor.setLevel(compressionLevel); compressor.setInput(responseContent); 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(uncompressedContentLength); // // Compress the response // byte[] buf = new byte[SIZE_INPUT_BUFFER]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } // // Get the compressed data // responseContent = bos.toByteArray(); int compressedLength = responseContent.length; if (log.isTraceEnabled()) { log.trace("Setting Content-Length to: " + compressedLength); log.trace("Setting Content-Encoding to: " + COMPRESSION_TYPE_DEFLATE); } httpResponse.setContentLength(compressedLength); httpResponse.setHeader(HEADER_CONTENT_ENCODING, COMPRESSION_TYPE_DEFLATE); out.write(responseContent); out.flush(); } } } finally { if (out != null) { out.close(); } if (logMessages) { logResponse(responseContent, requestTime, sessionId); } } }
From source file:edu.umass.cs.gigapaxos.SQLPaxosLogger.java
/** * @param data/* ww w . j av a 2s . com*/ * @return Compressed form. * @throws IOException */ public static byte[] deflate(byte[] data) throws IOException { if (!DB_COMPRESSION) return data; byte[] compressed = null; double inflation = 1; int compressedLength = data.length; do { Deflater deflator = new Deflater(); compressed = new byte[(int) ((inflation *= 1.1) * data.length + 16)]; deflator.setInput(data); deflator.finish(); compressedLength = deflator.deflate(compressed); deflator.end(); } while (compressedLength == compressed.length); return Arrays.copyOf(compressed, compressedLength); }