List of usage examples for java.util.zip Deflater setInput
public void setInput(ByteBuffer input)
From source file:com.itude.mobile.android.util.DataUtil.java
/** * Compress byte array /*w ww . ja v a 2s . co m*/ * * @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.phpmaven.phar.PharJavaPackager.java
private void packFile(final ByteArrayOutputStream fileEntriesBaos, final ByteArrayOutputStream compressedFilesBaos, final File fileToPack, String filePath) throws IOException { if (DEBUG) {/*from w w w. j av a 2s . c o m*/ System.out.println("Packing file " + fileToPack + " with " + fileToPack.length() + " bytes."); } final byte[] fileBytes = filePath.getBytes("UTF-8"); writeIntLE(fileEntriesBaos, fileBytes.length); fileEntriesBaos.write(fileBytes); // TODO Complain with files larger than 4 bytes file length writeIntLE(fileEntriesBaos, (int) fileToPack.length()); writeIntLE(fileEntriesBaos, (int) (fileToPack.lastModified() / 1000)); final byte[] uncompressed = FileUtils.readFileToByteArray(fileToPack); if (DEBUG) { System.out.println("read " + uncompressed.length + " bytes from file."); } final ByteArrayOutputStream compressedStream = new ByteArrayOutputStream(); // final GZIPOutputStream gzipStream = new GZIPOutputStream(compressedStream); // gzipStream.write(uncompressed); // gzipStream.flush(); final CRC32 checksum = new CRC32(); checksum.update(uncompressed); final Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true); deflater.setInput(uncompressed); deflater.finish(); final byte[] buf = new byte[Short.MAX_VALUE]; while (!deflater.needsInput()) { final int bytesRead = deflater.deflate(buf); compressedStream.write(buf, 0, bytesRead); } final byte[] compressed = compressedStream.toByteArray(); if (DEBUG) { System.out.println("compressed to " + compressed.length + " bytes."); } // final Inflater decompresser = new Inflater(); // decompresser.setInput(compressed); // byte[] result = new byte[5000]; // try { // int resultLength = decompresser.inflate(result); // final String str = new String(result, 0, resultLength); // int i = 42; // } catch (DataFormatException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // decompresser.end(); compressedFilesBaos.write(compressed); writeIntLE(fileEntriesBaos, compressed.length); writeIntLE(fileEntriesBaos, checksum.getValue()); // bits: 0x00001000, gzip fileEntriesBaos.write(0); fileEntriesBaos.write(0x10); fileEntriesBaos.write(0); fileEntriesBaos.write(0); // 0 bytes manifest writeIntLE(fileEntriesBaos, 0); }
From source file:com.sixt.service.framework.registry.consul.RegistrationManager.java
private String binaryEncode(String tag) throws IOException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Deflater deflater = new Deflater(); deflater.setInput(tag.getBytes()); deflater.finish();//from w ww . j ava2 s. co m byte[] buffer = new byte[1024]; while (!deflater.finished()) { int count = deflater.deflate(buffer); outputStream.write(buffer, 0, count); } outputStream.close(); byte compressed[] = outputStream.toByteArray(); return bytesToHex(compressed); }
From source file:org.jwebsocket.util.Tools.java
/** * Deflate a byte array with Zip compression * * @param aUncompressedData The uncompressed data * @return The compressed data/* w w w. j a v a 2s . c om*/ * @throws Exception */ public static byte[] deflate(byte[] aUncompressedData) throws Exception { Deflater lDeflater = new Deflater(); lDeflater.setInput(aUncompressedData); lDeflater.finish(); byte[] lOut = new byte[1024 * 1000 * 5]; int lWritten = lDeflater.deflate(lOut); byte[] lResult = new byte[lWritten]; System.arraycopy(lOut, 0, lResult, 0, lWritten); return lResult; }
From source file:de.tudarmstadt.ukp.wikipedia.revisionmachine.difftool.data.codec.RevisionEncoder.java
@Override public String encodeDiff(final RevisionCodecData codecData, final Diff diff) throws UnsupportedEncodingException, EncodingException { String sEncoding;// w w w. ja v a 2s .c o m byte[] bData = encode(codecData, diff); if (MODE_ZIP_COMPRESSION) { Deflater compresser = new Deflater(); compresser.setInput(bData); compresser.finish(); byte[] output = new byte[1000]; ByteArrayOutputStream stream = new ByteArrayOutputStream(); int cLength; do { cLength = compresser.deflate(output); stream.write(output, 0, cLength); } while (cLength == 1000); output = stream.toByteArray(); if (bData.length + 1 < output.length) { sEncoding = Base64.encodeBase64String(bData); } else { sEncoding = "_" + Base64.encodeBase64String(output); } } else { sEncoding = Base64.encodeBase64String(bData); } return sEncoding; }
From source file:de.tudarmstadt.ukp.wikipedia.revisionmachine.difftool.data.codec.RevisionEncoder.java
@Override public byte[] binaryDiff(final RevisionCodecData codecData, final Diff diff) throws UnsupportedEncodingException, EncodingException { byte[] bData = encode(codecData, diff); if (MODE_ZIP_COMPRESSION) { Deflater compresser = new Deflater(); compresser.setInput(bData); compresser.finish();/*from www. j av a2 s .c o m*/ byte[] output = new byte[1000]; ByteArrayOutputStream stream = new ByteArrayOutputStream(); int cLength; do { cLength = compresser.deflate(output); stream.write(output, 0, cLength); } while (cLength == 1000); output = stream.toByteArray(); if (bData.length + 1 < output.length) { return bData; } else { stream = new ByteArrayOutputStream(); stream.write(new byte[] { -128 }, 0, 1); stream.write(output, 0, output.length); return stream.toByteArray(); } } return bData; }
From source file:com.gargoylesoftware.htmlunit.WebClient3Test.java
private void doTestDeflateCompression(final boolean gzipCompatibleCompression) throws Exception { final byte[] input = "document.title = 'modified';".getBytes("UTF-8"); final byte[] buffer = new byte[100]; final Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, gzipCompatibleCompression); deflater.setInput(input); deflater.finish();// w ww . jav a2 s . c o m final int compressedDataLength = deflater.deflate(buffer); final byte[] content = new byte[compressedDataLength]; System.arraycopy(buffer, 0, content, 0, compressedDataLength); final List<NameValuePair> headers = new ArrayList<>(); headers.add(new NameValuePair("Content-Encoding", "deflate")); headers.add(new NameValuePair("Content-Length", String.valueOf(compressedDataLength))); final MockWebConnection conn = getMockWebConnection(); conn.setResponse(URL_SECOND, content, 200, "OK", "text/javascript", headers); final String html = "<html><head>" + "<title>Hello world</title>" + "<script src='" + URL_SECOND + "'></script>" + "</head><body><script>alert(document.title)</script></body></html>"; loadPageWithAlerts2(html); }
From source file:se.kth.infosys.lumberjack.protocol.LumberjackClient.java
public int sendCompressedFrame(List<Map<String, byte[]>> keyValuesList) throws IOException { output.writeByte(PROTOCOL_VERSION);/*www .ja v a2s.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(); 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: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. j a v a 2s . co 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; }