List of usage examples for java.util.zip Deflater finish
boolean finish
To view the source code for java.util.zip Deflater finish.
Click Source Link
From source file:NCDSearch.NCDSearch.java
public double NCD(byte[] file, byte[] target, int compression) { Deflater compressor = new Deflater(compression); byte[] outputtrash = new byte[file.length + target.length]; int bothcompressedsize; int filecompressedsize; int targetcompressedsize; byte[] both = new byte[file.length + target.length]; for (int i = 0; i < file.length; i++) { both[i] = file[i];/*w w w. ja v a2s . c o m*/ } for (int i = 0; i < target.length; i++) { both[i + file.length] = target[i]; } compressor.setInput(file); compressor.finish(); filecompressedsize = compressor.deflate(outputtrash); compressor.reset(); compressor.setInput(target); compressor.finish(); targetcompressedsize = compressor.deflate(outputtrash); compressor.reset(); compressor.setInput(both); compressor.finish(); bothcompressedsize = compressor.deflate(outputtrash); compressor.reset(); return (double) (bothcompressedsize - filecompressedsize) / (double) targetcompressedsize; }
From source file:se.kth.infosys.lumberjack.protocol.LumberjackClient.java
public int sendCompressedFrame(List<Map<String, byte[]>> keyValuesList) throws IOException { output.writeByte(PROTOCOL_VERSION);//from www .ja v a 2s.c o m 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(); logger.trace("Deflating data: {} bytes", uncompressedData.length); 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(); logger.trace("Deflated data: {} bytes", compressor.getTotalOut()); if (logger.isTraceEnabled()) { HexDump.dump(compressedData, 0, System.out, 0); } output.writeInt(compressor.getTotalOut()); output.write(compressedData); output.flush(); logger.trace("Sending compressed frame: {} frames", keyValuesList.size()); return 6 + compressor.getTotalOut(); }
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 ww.j a va 2 s .c o m*/ 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:org.ajax4jsf.resource.ResourceBuilderImpl.java
protected byte[] encrypt(byte[] src) { try {//from ww w . j a v a2 s.c o m Deflater compressor = new Deflater(Deflater.BEST_SPEED); byte[] compressed = new byte[src.length + 100]; compressor.setInput(src); compressor.finish(); int totalOut = compressor.deflate(compressed); byte[] zipsrc = new byte[totalOut]; System.arraycopy(compressed, 0, zipsrc, 0, totalOut); compressor.end(); return codec.encode(zipsrc); } catch (Exception e) { throw new FacesException("Error encode resource data", e); } }
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);// w w w . j a va 2 s . c o 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
/** * // w w w . j a v a 2s. c o 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.//from w w w.j a v a 2s .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/* www . j a v a 2s. c o m*/ * @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); }
From source file:PngEncoder.java
/** * Write the image data into the pngBytes array. * This will write one or more PNG "IDAT" chunks. In order * to conserve memory, this method grabs as many rows as will * fit into 32K bytes, or the whole image; whichever is less. * * * @return true if no errors; false if error grabbing pixels *///from ww w . j av a 2 s .c o m protected boolean writeImageData() { int rowsLeft = this.height; // number of rows remaining to write int startRow = 0; // starting row to process this time through int nRows; // how many rows to grab at a time byte[] scanLines; // the scan lines to be compressed int scanPos; // where we are in the scan lines int startPos; // where this line's actual pixels start (used // for filtering) byte[] compressedLines; // the resultant compressed lines int nCompressed; // how big is the compressed area? //int depth; // color depth ( handle only 8 or 32 ) PixelGrabber pg; this.bytesPerPixel = (this.encodeAlpha) ? 4 : 3; Deflater scrunch = new Deflater(this.compressionLevel); ByteArrayOutputStream outBytes = new ByteArrayOutputStream(1024); DeflaterOutputStream compBytes = new DeflaterOutputStream(outBytes, scrunch); try { while (rowsLeft > 0) { nRows = Math.min(32767 / (this.width * (this.bytesPerPixel + 1)), rowsLeft); nRows = Math.max(nRows, 1); int[] pixels = new int[this.width * nRows]; pg = new PixelGrabber(this.image, 0, startRow, this.width, nRows, pixels, 0, this.width); try { pg.grabPixels(); } catch (Exception e) { System.err.println("interrupted waiting for pixels!"); return false; } if ((pg.getStatus() & ImageObserver.ABORT) != 0) { System.err.println("image fetch aborted or errored"); return false; } /* * Create a data chunk. scanLines adds "nRows" for * the filter bytes. */ scanLines = new byte[this.width * nRows * this.bytesPerPixel + nRows]; if (this.filter == FILTER_SUB) { this.leftBytes = new byte[16]; } if (this.filter == FILTER_UP) { this.priorRow = new byte[this.width * this.bytesPerPixel]; } scanPos = 0; startPos = 1; for (int i = 0; i < this.width * nRows; i++) { if (i % this.width == 0) { scanLines[scanPos++] = (byte) this.filter; startPos = scanPos; } scanLines[scanPos++] = (byte) ((pixels[i] >> 16) & 0xff); scanLines[scanPos++] = (byte) ((pixels[i] >> 8) & 0xff); scanLines[scanPos++] = (byte) ((pixels[i]) & 0xff); if (this.encodeAlpha) { scanLines[scanPos++] = (byte) ((pixels[i] >> 24) & 0xff); } if ((i % this.width == this.width - 1) && (this.filter != FILTER_NONE)) { if (this.filter == FILTER_SUB) { filterSub(scanLines, startPos, this.width); } if (this.filter == FILTER_UP) { filterUp(scanLines, startPos, this.width); } } } /* * Write these lines to the output area */ compBytes.write(scanLines, 0, scanPos); startRow += nRows; rowsLeft -= nRows; } compBytes.close(); /* * Write the compressed bytes */ compressedLines = outBytes.toByteArray(); nCompressed = compressedLines.length; this.crc.reset(); this.bytePos = writeInt4(nCompressed, this.bytePos); this.bytePos = writeBytes(IDAT, this.bytePos); this.crc.update(IDAT); this.bytePos = writeBytes(compressedLines, nCompressed, this.bytePos); this.crc.update(compressedLines, 0, nCompressed); this.crcValue = this.crc.getValue(); this.bytePos = writeInt4((int) this.crcValue, this.bytePos); scrunch.finish(); scrunch.end(); return true; } catch (IOException e) { System.err.println(e.toString()); return false; } }