List of usage examples for java.util.zip Deflater Deflater
public Deflater()
From source file:org.diorite.impl.connection.packets.PacketCompression.java
public PacketCompression(final int threshold) { this.threshold = threshold; this.deflater = new Deflater(); this.inflater = new Inflater(); }
From source file:org.jahia.utils.Url.java
/** * Encode facet filter URL parameter/* w ww . j ava2 s. c om*/ * @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:org.ow2.proactive.utils.ObjectByteConverter.java
/** * Convert the given Serializable Object into a byte array. * <p>//from w w w . j ava2 s .c om * 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:org.jmangos.realm.network.packet.wow.client.CMSG_REQUEST_ACCOUNT_DATA.java
@Override protected void runImpl() { if (this.type > AccountDataType.NUM_ACCOUNT_DATA_TYPES.getValue()) { return;/*ww w. j av a2 s . com*/ } final HashMap<Integer, AccountData> adata = new HashMap<Integer, AccountData>(); // getAccount().getAccountData(); // /* // disabled // by // paalgyula // */ if (adata.containsKey(this.type)) { final Deflater compressor = new Deflater(); final byte[] dataToCompress = adata.get(this.type).getData().getBytes(Charset.forName("UTF-8")); compressor.setInput(dataToCompress); compressor.finish(); final ByteArrayOutputStream bos = new ByteArrayOutputStream(dataToCompress.length); final byte[] buf = new byte[1024]; while (!compressor.finished()) { final int count = compressor.deflate(buf); bos.write(buf, 0, count); } try { bos.close(); } catch (final IOException e) { } // FIXME NEED COMPLETE @SuppressWarnings("unused") final byte[] compressedData = bos.toByteArray(); } }
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//from w ww. java 2 s . 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:com.aliyun.odps.commons.proto.ProtobufRecordStreamWriter.java
public ProtobufRecordStreamWriter(TableSchema schema, OutputStream out, CompressOption option) throws IOException { columns = schema.getColumns().toArray(new Column[0]); OutputStream tmpOut;/*from w ww . j a v a 2 s . c o m*/ if (option != null) { if (option.algorithm.equals(CompressOption.CompressAlgorithm.ODPS_ZLIB)) { def = new Deflater(); def.setLevel(option.level); def.setStrategy(option.strategy); tmpOut = new DeflaterOutputStream(out, def); } else if (option.algorithm.equals(CompressOption.CompressAlgorithm.ODPS_SNAPPY)) { tmpOut = new SnappyFramedOutputStream(out); } else if (option.algorithm.equals(CompressOption.CompressAlgorithm.ODPS_RAW)) { tmpOut = out; } else { throw new IOException("invalid compression option."); } } else { tmpOut = out; } bou = new CountingOutputStream(tmpOut); this.out = CodedOutputStream.newInstance(bou); }
From source file:com.ctriposs.r2.filter.compression.DeflateCompressor.java
@Override public byte[] deflate(InputStream data) throws CompressionException { byte[] input; try {// w w w. java2 s .c om input = IOUtils.toByteArray(data); } catch (IOException e) { throw new CompressionException(CompressionConstants.DECODING_ERROR + CompressionConstants.BAD_STREAM, e); } Deflater zlib = new Deflater(); zlib.setInput(input); zlib.finish(); ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] temp = new byte[CompressionConstants.BUFFER_SIZE]; int bytesRead; while (!zlib.finished()) { bytesRead = zlib.deflate(temp); if (bytesRead == 0) { if (!zlib.needsInput()) { throw new CompressionException(CompressionConstants.DECODING_ERROR + getContentEncodingName()); } else { break; } } output.write(temp, 0, bytesRead); } zlib.end(); return output.toByteArray(); }
From source file:org.sonar.microbenchmark.SerializationBenchmarkTest.java
private File zipFile(File input) throws Exception { File zipFile = new File(input.getAbsolutePath() + ".zip"); Deflater deflater = new Deflater(); byte[] content = FileUtils.readFileToByteArray(input); deflater.setInput(content);//w ww. j a v a 2 s. com try (OutputStream outputStream = new FileOutputStream(zipFile)) { deflater.finish(); byte[] buffer = new byte[1024]; while (!deflater.finished()) { int count = deflater.deflate(buffer); // returns the generated code... index outputStream.write(buffer, 0, count); } } deflater.end(); return zipFile; }
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);// ww w.j a v a 2s . com 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) { 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:org.apache.fop.render.ps.ImageEncoderPNG.java
/** {@inheritDoc} */ public void writeTo(OutputStream out) throws IOException { // TODO: refactor this code with equivalent PDF code InputStream in = ((ImageRawStream) image).createInputStream(); try {//from ww w . j a v a 2 s . c o m if (numberOfInterleavedComponents == 1 || numberOfInterleavedComponents == 3) { // means we have Gray, RGB, or Palette IOUtils.copy(in, out); } else { // means we have Gray + alpha or RGB + alpha int numBytes = numberOfInterleavedComponents - 1; // 1 for Gray, 3 for RGB int numColumns = image.getSize().getWidthPx(); InflaterInputStream infStream = new InflaterInputStream(in, new Inflater()); DataInputStream dataStream = new DataInputStream(infStream); int offset = 0; int bytesPerRow = numberOfInterleavedComponents * numColumns; int filter; // here we need to inflate the PNG pixel data, which includes alpha, separate the alpha // channel and then deflate the RGB channels back again // TODO: not using the baos below and using the original out instead (as happens in PDF) // would be preferable but that does not work with the rest of the postscript code; this // needs to be revisited ByteArrayOutputStream baos = new ByteArrayOutputStream(); DeflaterOutputStream dos = new DeflaterOutputStream(/* out */baos, new Deflater()); while ((filter = dataStream.read()) != -1) { byte[] bytes = new byte[bytesPerRow]; dataStream.readFully(bytes, 0, bytesPerRow); dos.write((byte) filter); for (int j = 0; j < numColumns; j++) { dos.write(bytes, offset, numBytes); offset += numberOfInterleavedComponents; } offset = 0; } dos.close(); IOUtils.copy(new ByteArrayInputStream(baos.toByteArray()), out); } } finally { IOUtils.closeQuietly(in); } }