java.util.zip.Deflater Java Examples
The following examples show how to use
java.util.zip.Deflater.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PressUtility.java From jstarcraft-core with Apache License 2.0 | 6 votes |
/** * 按照指定的级别压缩指定的数据 * * @param datas * @param level * @return */ public static byte[] zip(byte[] datas, int level) { if (level < Deflater.NO_COMPRESSION || level > Deflater.BEST_COMPRESSION) { String message = StringUtility.format("非法的压缩等级[{}]", level); LOGGER.error(message); throw new IllegalArgumentException(message); } Deflater deflater = new Deflater(level); deflater.setInput(datas); deflater.finish(); try (ByteArrayOutputStream stream = new ByteArrayOutputStream(BUFFER_SIZE)) { byte[] bytes = new byte[BUFFER_SIZE]; while (!deflater.finished()) { int count = deflater.deflate(bytes); stream.write(bytes, 0, count); } deflater.end(); return stream.toByteArray(); } catch (IOException exception) { throw new IllegalStateException("压缩异常", exception); } }
Example #2
Source File: PacketCustom.java From CodeChickenLib with GNU Lesser General Public License v2.1 | 6 votes |
/** * Compresses the payload ByteBuf after the type byte */ private void do_compress() { Deflater deflater = new Deflater(); try { readerIndex(1); int len = readableBytes(); deflater.setInput(array(), readerIndex(), len); deflater.finish(); byte[] out = new byte[len]; int clen = deflater.deflate(out); if (clen >= len - 5 || !deflater.finished())//not worth compressing, gets larger return; clear(); writeByte(type | 0x80); writeVarInt(len); writeByteArray(out); } catch (Exception e) { throw new EncoderException(e); } finally { readerIndex(0); deflater.end(); } }
Example #3
Source File: ZipPlugin.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
static byte[] compress(byte[] bytesIn) { Deflater deflater = new Deflater(); deflater.setInput(bytesIn); ByteArrayOutputStream stream = new ByteArrayOutputStream(bytesIn.length); byte[] buffer = new byte[1024]; deflater.finish(); while (!deflater.finished()) { int count = deflater.deflate(buffer); stream.write(buffer, 0, count); } try { stream.close(); } catch (IOException ex) { return bytesIn; } byte[] bytesOut = stream.toByteArray(); deflater.end(); return bytesOut; }
Example #4
Source File: ZipContentItem.java From pentaho-reporting with GNU Lesser General Public License v2.1 | 6 votes |
public ZipContentItem( final ZipRepository repository, final ZipContentLocation parent, final String name ) { if ( repository == null ) { throw new NullPointerException(); } if ( parent == null ) { throw new NullPointerException(); } if ( name == null ) { throw new NullPointerException(); } this.repository = repository; this.parent = parent; this.entryName = name; this.name = RepositoryUtilities.buildName( this, "/" ); this.time = System.currentTimeMillis(); this.comment = null; this.size = 0; this.rawData = EMPTY_BYTES; this.method = LibRepositoryBoot.ZIP_METHOD_DEFLATED; this.compression = Deflater.DEFAULT_COMPRESSION; }
Example #5
Source File: OldAndroidDeflateTest.java From j2objc with Apache License 2.0 | 6 votes |
private void bigTest(int step, int expectedAdler) throws UnsupportedEncodingException, DataFormatException { byte[] input = new byte[128 * 1024]; byte[] comp = new byte[128 * 1024 + 512]; byte[] output = new byte[128 * 1024 + 512]; Inflater inflater = new Inflater(false); Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION, false); createSample(input, step); compress(deflater, input, comp); expand(inflater, comp, (int) deflater.getBytesWritten(), output); assertEquals(inflater.getBytesWritten(), input.length); assertEquals(deflater.getAdler(), inflater.getAdler()); assertEquals(deflater.getAdler(), expectedAdler); }
Example #6
Source File: CliUtil.java From gemfirexd-oss with Apache License 2.0 | 6 votes |
public static DeflaterInflaterData compressBytes(byte[] input) { Deflater compresser = new Deflater(); compresser.setInput(input); compresser.finish(); byte[] buffer = new byte[100]; byte[] result = new byte[0]; int compressedDataLength = 0; int totalCompressedDataLength = 0; do { byte[] newResult = new byte[result.length + buffer.length]; System.arraycopy(result, 0, newResult, 0, result.length); compressedDataLength = compresser.deflate(buffer); totalCompressedDataLength += compressedDataLength; // System.out.println(compressedDataLength); // System.out.println("uc: b "+buffer.length); // System.out.println("uc: r "+result.length); // System.out.println("uc: nr "+newResult.length); // System.out.println(); System.arraycopy(buffer, 0, newResult, result.length, buffer.length); result = newResult; } while (compressedDataLength != 0); return new DeflaterInflaterData(totalCompressedDataLength, result); }
Example #7
Source File: PerMessageDeflateExtension.java From WebSocket-for-Android with Apache License 2.0 | 6 votes |
@Override public byte[] compress(byte[] b, int offset, int length) { int len; int position = 0; if (_deflaterReset) { _deflater.reset(); } _deflater.setInput(b, offset, length); while ((len = _deflater.deflate(_buffer, position, _capcacity - position, Deflater.SYNC_FLUSH)) > 0) { if ((position += len) == _capcacity) { _buffer = Arrays.copyOf(_buffer, _capcacity <<= 1); } } return Arrays.copyOf(_buffer, position - TAIL_LENGTH); }
Example #8
Source File: FlateFilter.java From gcs with Mozilla Public License 2.0 | 6 votes |
@Override protected void encode(InputStream input, OutputStream encoded, COSDictionary parameters) throws IOException { int compressionLevel = getCompressionLevel(); Deflater deflater = new Deflater(compressionLevel); DeflaterOutputStream out = new DeflaterOutputStream(encoded, deflater); int amountRead; int mayRead = input.available(); if (mayRead > 0) { byte[] buffer = new byte[Math.min(mayRead,BUFFER_SIZE)]; while ((amountRead = input.read(buffer, 0, Math.min(mayRead,BUFFER_SIZE))) != -1) { out.write(buffer, 0, amountRead); } } out.close(); encoded.flush(); deflater.end(); }
Example #9
Source File: CompressedBatchEncoder.java From logstash-input-beats with Apache License 2.0 | 6 votes |
@Override protected ByteBuf getPayload(ChannelHandlerContext ctx, Batch batch) throws IOException { ByteBuf payload = super.getPayload(ctx, batch); try { try (ByteBufOutputStream output = new ByteBufOutputStream(ctx.alloc().buffer())) { DeflaterOutputStream outputDeflater = new DeflaterOutputStream(output, new Deflater()); byte[] chunk = new byte[payload.readableBytes()]; payload.readBytes(chunk); outputDeflater.write(chunk); outputDeflater.close(); ByteBuf content = ctx.alloc().buffer(output.writtenBytes()); content.writeByte(batch.getProtocol()); content.writeByte('C'); content.writeInt(output.writtenBytes()); content.writeBytes(output.buffer()); return content; } } finally { payload.release(); } }
Example #10
Source File: Slice.java From Telegram-FOSS with GNU General Public License v2.0 | 6 votes |
private void storeData(ByteBuffer data) { try { final byte[] input = data.array(); FileOutputStream fos = new FileOutputStream(file); final Deflater deflater = new Deflater(Deflater.BEST_SPEED, true); deflater.setInput(input, data.arrayOffset(), data.remaining()); deflater.finish(); byte[] buf = new byte[1024]; while (!deflater.finished()) { int byteCount = deflater.deflate(buf); fos.write(buf, 0, byteCount); } deflater.end(); fos.close(); } catch (Exception e) { FileLog.e(e); } }
Example #11
Source File: Java7ZlibInputOutputStream.java From Smack with Apache License 2.0 | 6 votes |
@Override public OutputStream getOutputStream(OutputStream outputStream) { final int flushMethodInt; switch (flushMethod) { case SYNC_FLUSH: flushMethodInt = Deflater.SYNC_FLUSH; break; case FULL_FLUSH: flushMethodInt = Deflater.FULL_FLUSH; break; default: throw new AssertionError(); } return new DeflaterOutputStream(outputStream, new Deflater(compressionLevel)) { @Override public void flush() throws IOException { int count; while ((count = def.deflate(buf, 0, buf.length, flushMethodInt)) > 0) { out.write(buf, 0, count); } super.flush(); } }; }
Example #12
Source File: ZipContentItem.java From pentaho-reporting with GNU Lesser General Public License v2.1 | 6 votes |
public ZipContentItem( final String name, final ZipRepository repository, final ZipContentLocation parent ) { if ( name == null ) { throw new NullPointerException(); } if ( repository == null ) { throw new NullPointerException(); } if ( parent == null ) { throw new NullPointerException(); } this.time = System.currentTimeMillis(); this.name = name; this.repository = repository; this.parent = parent; this.contentId = RepositoryUtilities.buildName( this, "/" ); this.newItem = true; this.method = LibRepositoryBoot.ZIP_METHOD_DEFLATED; this.compression = Deflater.DEFAULT_COMPRESSION; }
Example #13
Source File: WriteFxImageBenchmark.java From chart-fx with Apache License 2.0 | 6 votes |
public static void testCompressionPerformance(final Image image, final String description) { LOGGER.atInfo().addArgument(description).log("Test compression level performance with image with {}"); // precompute typical palette final PaletteQuantizer userPaletteRGBA = WriteFxImage.estimatePalette(image, true, DEFAULT_PALETTE_COLOR_COUNT); final PaletteQuantizer userPaletteRGB = WriteFxImage.estimatePalette(image, false, DEFAULT_PALETTE_COLOR_COUNT); for (final boolean alpha : new boolean[] { true, false }) { LOGGER.atInfo().addArgument(alpha ? "with" : "without").log("{} alpha channel"); for (int compressionLevel = Deflater.NO_COMPRESSION; compressionLevel <= Deflater.BEST_COMPRESSION; compressionLevel++) { writeFxImage(image, alpha, true, compressionLevel, OLDREF); writeFxImage(image, alpha, true, compressionLevel, NEWREF); // compute palette on-the-fly writeFxImage(image, alpha, true, compressionLevel, PALETTE); // use pre-computed palette writeFxImage(image, alpha, true, compressionLevel, PALETTE, alpha ? userPaletteRGBA : userPaletteRGB); LOGGER.atInfo().log(" "); // deliberatly empty line for better readability } } }
Example #14
Source File: CassandraOutputData.java From learning-hadoop with Apache License 2.0 | 6 votes |
/** * Compress a CQL query * * @param queryStr * the CQL query * @param compression * compression option (GZIP is the only option - so far) * @return an array of bytes containing the compressed query */ public static byte[] compressQuery(String queryStr, Compression compression) { byte[] data = queryStr.getBytes(Charset .forName(CassandraColumnMetaData.UTF8)); Deflater compressor = new Deflater(); compressor.setInput(data); compressor.finish(); ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; while (!compressor.finished()) { int size = compressor.deflate(buffer); byteArray.write(buffer, 0, size); } return byteArray.toByteArray(); }
Example #15
Source File: Compression.java From xyz-hub with Apache License 2.0 | 6 votes |
/** * * @param bytearray * @return * @throws DataFormatException */ public static byte[] compressUsingInflate(byte[] bytearray) throws DataFormatException { if (bytearray == null) return new byte[0]; try (final ByteArrayOutputStream bos = new ByteArrayOutputStream()) { final Deflater deflater = new Deflater(); final byte[] buff = new byte[1024]; deflater.setInput(bytearray); deflater.finish(); while (!deflater.finished()) { int count = deflater.deflate(buff); bos.write(buff, 0, count); } deflater.end(); bos.flush(); return bos.toByteArray(); } catch (IOException e) { throw new DataFormatException(e.getMessage()); } }
Example #16
Source File: FlateFilter.java From sambox with Apache License 2.0 | 6 votes |
@Override public void encode(InputStream input, OutputStream encoded, COSDictionary parameters) throws IOException { int compressionLevel = getCompressionLevel(); Deflater deflater = new Deflater(compressionLevel); try (DeflaterOutputStream out = new DeflaterOutputStream(encoded, deflater)) { int amountRead; int mayRead = input.available(); if (mayRead > 0) { byte[] buffer = new byte[Math.min(mayRead, BUFFER_SIZE)]; while ((amountRead = input.read(buffer, 0, Math.min(mayRead, BUFFER_SIZE))) != -1) { out.write(buffer, 0, amountRead); } } } encoded.flush(); }
Example #17
Source File: TIFFDeflater.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
public TIFFDeflater(String compressionType, int compressionTagValue, ImageWriteParam param, int predictorValue) { super(compressionType, compressionTagValue, true); this.predictor = predictorValue; // Set the deflate level. int deflateLevel; if(param != null && param.getCompressionMode() == ImageWriteParam.MODE_EXPLICIT) { float quality = param.getCompressionQuality(); deflateLevel = (int)(1 + 8*quality); } else { deflateLevel = Deflater.DEFAULT_COMPRESSION; } this.deflater = new Deflater(deflateLevel); }
Example #18
Source File: Zlib.java From Jupiter with GNU General Public License v3.0 | 6 votes |
public static byte[] deflate(byte[] data, int level) throws Exception { Deflater deflater = new Deflater(level); deflater.reset(); deflater.setInput(data); deflater.finish(); ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length); byte[] buf = new byte[1024]; try { while (!deflater.finished()) { int i = deflater.deflate(buf); bos.write(buf, 0, i); } } finally { deflater.end(); bos.close(); } return bos.toByteArray(); }
Example #19
Source File: Slice.java From TelePlus-Android with GNU General Public License v2.0 | 6 votes |
private void storeData(ByteBuffer data) { try { final byte[] input = data.array(); FileOutputStream fos = new FileOutputStream(file); final Deflater deflater = new Deflater(Deflater.BEST_SPEED, true); deflater.setInput(input, data.arrayOffset(), data.remaining()); deflater.finish(); byte[] buf = new byte[1024]; while (!deflater.finished()) { int byteCount = deflater.deflate(buf); fos.write(buf, 0, byteCount); } deflater.end(); fos.close(); } catch (Exception e) { FileLog.e(e); } }
Example #20
Source File: ZipUtilTest.java From gocd with Apache License 2.0 | 6 votes |
@Test void shouldZipFileContentsOnly() throws IOException { zipFile = zipUtil.zipFolderContents(srcDir, temporaryFolder.newFile(), Deflater.NO_COMPRESSION); assertThat(zipFile.isFile()).isTrue(); zipUtil.unzip(zipFile, destDir); assertIsDirectory(new File(destDir, emptyDir.getName())); assertIsDirectory(new File(destDir, childDir1.getName())); File actual1 = new File(destDir, file1.getName()); assertThat(actual1.isFile()).isTrue(); assertThat(fileContent(actual1)).isEqualTo(fileContent(file1)); File actual2 = new File(destDir, childDir1.getName() + File.separator + file2.getName()); assertThat(actual2.isFile()).isTrue(); assertThat(fileContent(actual2)).isEqualTo(fileContent(file2)); }
Example #21
Source File: StringCompressUtils.java From opencards with BSD 2-Clause "Simplified" License | 6 votes |
private static String compress2(String s) { Deflater defl = new Deflater(Deflater.BEST_COMPRESSION); defl.setInput(s.getBytes()); defl.finish(); boolean done = false; ByteArrayOutputStream bos = new ByteArrayOutputStream(); while (!done) { byte[] buf = new byte[256]; int bufnum = defl.deflate(buf); bos.write(buf, 0, bufnum); if (bufnum < buf.length) done = true; } try { bos.flush(); bos.close(); } catch (IOException ioe) { System.err.println(ioe.toString()); } return toHexString(bos.toByteArray()); }
Example #22
Source File: FlushableGZIPOutputStream.java From Tomcat7.0.67 with Apache License 2.0 | 6 votes |
@Override public synchronized void flush() throws IOException { if (hasLastByte) { // - do not allow the gzip header to be flushed on its own // - do not do anything if there is no data to send // trick the deflater to flush /** * Now this is tricky: We force the Deflater to flush its data by * switching compression level. As yet, a perplexingly simple workaround * for * http://developer.java.sun.com/developer/bugParade/bugs/4255743.html */ if (!def.finished()) { def.setLevel(Deflater.NO_COMPRESSION); flushLastByte(); flagReenableCompression = true; } } out.flush(); }
Example #23
Source File: JdkZlibEncoder.java From netty-4.1.22 with Apache License 2.0 | 6 votes |
/** * Creates a new zlib encoder with the specified {@code compressionLevel} * and the specified wrapper.使用指定的压缩级别和指定的包装器创建一个新的zlib编码器。 * * @param compressionLevel * {@code 1} yields the fastest compression and {@code 9} yields the * best compression. {@code 0} means no compression. The default * compression level is {@code 6}. * * @throws CompressionException if failed to initialize zlib */ public JdkZlibEncoder(ZlibWrapper wrapper, int compressionLevel) { if (compressionLevel < 0 || compressionLevel > 9) { throw new IllegalArgumentException( "compressionLevel: " + compressionLevel + " (expected: 0-9)"); } if (wrapper == null) { throw new NullPointerException("wrapper"); } if (wrapper == ZlibWrapper.ZLIB_OR_NONE) { throw new IllegalArgumentException( "wrapper '" + ZlibWrapper.ZLIB_OR_NONE + "' is not " + "allowed for compression."); } this.wrapper = wrapper; deflater = new Deflater(compressionLevel, wrapper != ZlibWrapper.ZLIB); }
Example #24
Source File: JoH.java From xDrip-plus with GNU General Public License v3.0 | 6 votes |
public static String compressString(String source) { try { Deflater deflater = new Deflater(); deflater.setInput(source.getBytes(Charset.forName("UTF-8"))); deflater.finish(); byte[] buf = new byte[source.length() + 256]; int count = deflater.deflate(buf); // check count deflater.end(); return Base64.encodeToString(buf, 0, count, Base64.NO_WRAP); } catch (Exception e) { return null; } }
Example #25
Source File: JdkZlibEncoder.java From netty4.0.27Learn with Apache License 2.0 | 6 votes |
/** * Creates a new zlib encoder with the specified {@code compressionLevel} * and the specified wrapper. * * @param compressionLevel * {@code 1} yields the fastest compression and {@code 9} yields the * best compression. {@code 0} means no compression. The default * compression level is {@code 6}. * * @throws CompressionException if failed to initialize zlib */ public JdkZlibEncoder(ZlibWrapper wrapper, int compressionLevel) { if (compressionLevel < 0 || compressionLevel > 9) { throw new IllegalArgumentException( "compressionLevel: " + compressionLevel + " (expected: 0-9)"); } if (wrapper == null) { throw new NullPointerException("wrapper"); } if (wrapper == ZlibWrapper.ZLIB_OR_NONE) { throw new IllegalArgumentException( "wrapper '" + ZlibWrapper.ZLIB_OR_NONE + "' is not " + "allowed for compression."); } this.wrapper = wrapper; deflater = new Deflater(compressionLevel, wrapper != ZlibWrapper.ZLIB); }
Example #26
Source File: ZlibOriginal.java From Nukkit with GNU General Public License v3.0 | 6 votes |
@Override public byte[] deflate(byte[] data, int level) throws Exception { Deflater deflater = new Deflater(level); deflater.reset(); deflater.setInput(data); deflater.finish(); ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length); byte[] buf = new byte[1024]; try { while (!deflater.finished()) { int i = deflater.deflate(buf); bos.write(buf, 0, i); } } finally { deflater.end(); } return bos.toByteArray(); }
Example #27
Source File: PNGEncoder.java From tn5250j with GNU General Public License v2.0 | 5 votes |
/** * @param outarray * @param inarray * @return * @throws EncoderException */ private int compressInternal(byte[] outarray, byte[] inarray) throws EncoderException { Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION); try { deflater.setInput(inarray, 0,inarray.length); deflater.finish(); deflater.deflate(outarray); if (!deflater.finished()) { error("PNG encoding error: Deflater could not compress image data."); } return (deflater.getTotalOut()); } finally { deflater.end(); } }
Example #28
Source File: TezUtils.java From incubator-tez with Apache License 2.0 | 5 votes |
/** * Convert a Configuration to compressed ByteString using Protocol buffer * * @param conf * : Configuration to be converted * @return PB ByteString (compressed) * @throws IOException */ public static ByteString createByteStringFromConf(Configuration conf) throws IOException { Preconditions.checkNotNull(conf, "Configuration must be specified"); ByteString.Output os = ByteString.newOutput(); DeflaterOutputStream compressOs = new DeflaterOutputStream(os, new Deflater(Deflater.BEST_SPEED)); try { writeConfInPB(compressOs, conf); } finally { if (compressOs != null) { compressOs.close(); } } return os.toByteString(); }
Example #29
Source File: ZipFileSystem.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
private Deflater getDeflater() { synchronized (deflaters) { int size = deflaters.size(); if (size > 0) { Deflater def = deflaters.remove(size - 1); return def; } else { return new Deflater(Deflater.DEFAULT_COMPRESSION, true); } } }
Example #30
Source File: CompressionOutputStream.java From nifi with Apache License 2.0 | 5 votes |
public CompressionOutputStream(final OutputStream outStream, final int bufferSize, final int level, final int strategy) { if (bufferSize < MIN_BUFFER_SIZE) { throw new IllegalArgumentException("Buffer size must be at least " + MIN_BUFFER_SIZE); } this.out = outStream; this.deflater = new Deflater(level); this.deflater.setStrategy(strategy); buffer = new byte[bufferSize]; compressed = new byte[bufferSize + 64]; }