Java Code Examples for java.util.zip.Deflater#finished()
The following examples show how to use
java.util.zip.Deflater#finished() .
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: DeflaterCompress.java From compress with MIT License | 6 votes |
@Override public byte[] compress(byte[] data) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Deflater compressor = new Deflater(1); try { compressor.setInput(data); compressor.finish(); final byte[] buf = new byte[2048]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } } finally { compressor.end(); } return bos.toByteArray(); }
Example 2
Source File: ZlibThreadLocal.java From Nukkit with GNU General Public License v3.0 | 6 votes |
@Override public byte[] deflate(byte[] data, int level) throws Exception { Deflater deflater = getDef(level); if (deflater == null) throw new IllegalArgumentException("No deflate for level " + level + " !"); deflater.reset(); deflater.setInput(data); deflater.finish(); FastByteArrayOutputStream bos = ThreadCache.fbaos.get(); bos.reset(); byte[] buffer = buf.get(); while (!deflater.finished()) { int i = deflater.deflate(buffer); bos.write(buffer, 0, i); } //Deflater::end is called the time when the process exits. return bos.toByteArray(); }
Example 3
Source File: Slice.java From Telegram 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 4
Source File: DeflaterTest.java From j2objc with Apache License 2.0 | 6 votes |
/** * java.util.zip.Deflater#getAdler() */ public void test_getAdler() { byte byteArray[] = { 'a', 'b', 'c', 1, 2, 3 }; byte outPutBuf[] = new byte[100]; Deflater defl = new Deflater(); // getting the checkSum value using the Adler defl.setInput(byteArray); defl.finish(); while (!defl.finished()) { defl.deflate(outPutBuf); } long checkSumD = defl.getAdler(); defl.end(); // getting the checkSum value through the Adler32 class Adler32 adl = new Adler32(); adl.update(byteArray); long checkSumR = adl.getValue(); assertEquals( "The checksum value returned by getAdler() is not the same as the checksum returned by creating the adler32 instance", checkSumD, checkSumR); }
Example 5
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 6
Source File: DeflateCompression.java From tiff-java with MIT License | 6 votes |
/** * {@inheritDoc} */ @Override public byte[] encode(byte[] bytes, ByteOrder byteOrder) { try { Deflater deflater = new Deflater(); deflater.setInput(bytes); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(bytes.length); 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); } outputStream.close(); byte[] output = outputStream.toByteArray(); return output; } catch (IOException e) { throw new TiffException("Failed close encoded stream", e); } }
Example 7
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 8
Source File: bs.java From letv with Apache License 2.0 | 5 votes |
public static byte[] a(byte[] bArr) throws IOException { ByteArrayOutputStream byteArrayOutputStream; Throwable th; if (bArr == null || bArr.length <= 0) { return null; } Deflater deflater = new Deflater(); deflater.setInput(bArr); deflater.finish(); byte[] bArr2 = new byte[8192]; a = 0; try { byteArrayOutputStream = new ByteArrayOutputStream(); while (!deflater.finished()) { try { int deflate = deflater.deflate(bArr2); a += deflate; byteArrayOutputStream.write(bArr2, 0, deflate); } catch (Throwable th2) { th = th2; } } deflater.end(); if (byteArrayOutputStream != null) { byteArrayOutputStream.close(); } return byteArrayOutputStream.toByteArray(); } catch (Throwable th3) { Throwable th4 = th3; byteArrayOutputStream = null; th = th4; if (byteArrayOutputStream != null) { byteArrayOutputStream.close(); } throw th; } }
Example 9
Source File: CompressionManager.java From Angelia-core with GNU General Public License v3.0 | 5 votes |
/** * Compresses the given data with zlib (as used by minecrafts protocol) * * @param data Data to compress * @return Compressed data * @throws IOException In case something goes wrong with the stream internally * used */ public static byte[] compressZLib(byte[] data) throws IOException { Deflater deflater = new Deflater(); deflater.setInput(data); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); deflater.finish(); byte[] buffer = new byte[1024]; while (!deflater.finished()) { int count = deflater.deflate(buffer); outputStream.write(buffer, 0, count); } outputStream.close(); byte[] output = outputStream.toByteArray(); return output; }
Example 10
Source File: PackedTransaction.java From EosProxyServer with GNU Lesser General Public License v3.0 | 5 votes |
private byte[] compress( byte[] uncompressedBytes, CompressType compressType) { if ( compressType == null || !CompressType.zlib.equals( compressType)) { return uncompressedBytes; } // zip! Deflater deflater = new Deflater( Deflater.BEST_COMPRESSION ); deflater.setInput( uncompressedBytes ); ByteArrayOutputStream outputStream = new ByteArrayOutputStream( uncompressedBytes.length); 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); } try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); return uncompressedBytes; } return outputStream.toByteArray(); }
Example 11
Source File: ByteTools.java From o2oa with GNU Affero General Public License v3.0 | 5 votes |
public static String compressBase64String(byte[] data) throws Exception { try (ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length)) { Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); compressor.setInput(data); compressor.finish(); byte[] buf = new byte[1024]; while (!compressor.finished()) { int count = compressor.deflate(buf); baos.write(buf, 0, count); } return Base64.encodeBase64String(baos.toByteArray()); } }
Example 12
Source File: DeflaterTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * java.util.zip.Deflater#needsInput() */ public void test_needsInput() { Deflater defl = new Deflater(); assertTrue( "needsInput give the wrong boolean value as a result of no input buffer", defl.needsInput()); byte byteArray[] = { 1, 2, 3 }; defl.setInput(byteArray); assertFalse( "needsInput give wrong boolean value as a result of a full input buffer", defl.needsInput()); byte[] outPutBuf = new byte[50]; while (!defl.needsInput()) { defl.deflate(outPutBuf); } byte emptyByteArray[] = new byte[0]; defl.setInput(emptyByteArray); assertTrue( "needsInput give wrong boolean value as a result of an empty input buffer", defl.needsInput()); defl.setInput(byteArray); defl.finish(); while (!defl.finished()) { defl.deflate(outPutBuf); } // needsInput should NOT return true after finish() has been // called. if (System.getProperty("java.vendor").startsWith("IBM")) { assertFalse( "needsInput gave wrong boolean value as a result of finish() being called", defl.needsInput()); } defl.end(); }
Example 13
Source File: CompressUtil.java From s3-bucket-loader with Apache License 2.0 | 5 votes |
public static String compressAndB64EncodeUTF8Bytes(byte[] bytes) throws Exception{ byte[] input = bytes; // Compressor with highest level of compression Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); // Give the compressor the data to compress compressor.setInput(input); compressor.finish(); // Create an expandable byte array to hold the compressed data. // It is not necessary that the compressed data will be smaller than // the uncompressed data. ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length); // Compress the data byte[] buf = new byte[32]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } try { bos.close(); } catch (IOException e) { } // Get the compressed data byte[] compressedData = bos.toByteArray(); return new String(Base64.encode(compressedData),"UTF-8"); }
Example 14
Source File: BiliDanmukuCompressionTools.java From HeroVideo-master with Apache License 2.0 | 5 votes |
public static byte[] compress(byte[] value, int offset, int length, int compressionLevel) { ByteArrayOutputStream bos = new ByteArrayOutputStream(length); Deflater compressor = new Deflater(); try { compressor.setLevel(compressionLevel); // 将当前压缩级别设置为指定值。 compressor.setInput(value, offset, length); compressor.finish(); // 调用时,指示压缩应当以输入缓冲区的当前内容结尾。 // Compress the data final byte[] buf = new byte[1024]; while (!compressor.finished()) { // 如果已到达压缩数据输出流的结尾,则返回 true。 int count = compressor.deflate(buf); // 使用压缩数据填充指定缓冲区。 bos.write(buf, 0, count); } } finally { compressor.end(); // 关闭解压缩器并放弃所有未处理的输入。 } return bos.toByteArray(); }
Example 15
Source File: DeflaterTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * java.util.zip.Deflater#Deflater() */ public void test_Constructor() throws Exception { byte[] byteArray = new byte[100]; InputStream inFile = Support_Resources.getStream("hyts_checkInput.txt"); inFile.read(byteArray); inFile.close(); Deflater defl = new Deflater(); byte[] outPutBuf = new byte[500]; defl.setInput(byteArray); while (!defl.needsInput()) { defl.deflate(outPutBuf); } defl.finish(); while (!defl.finished()) { defl.deflate(outPutBuf); } int totalOut = defl.getTotalOut(); defl.end(); // creating a Deflater using the DEFAULT_COMPRESSION as the int MyDeflater mdefl = new MyDeflater(); mdefl.end(); mdefl = new MyDeflater(mdefl.getDefCompression()); outPutBuf = new byte[500]; mdefl.setInput(byteArray); while (!mdefl.needsInput()) { mdefl.deflate(outPutBuf); } mdefl.finish(); while (!mdefl.finished()) { mdefl.deflate(outPutBuf); } assertEquals(totalOut, mdefl.getTotalOut()); mdefl.end(); }
Example 16
Source File: TezUtils.java From incubator-tez with Apache License 2.0 | 5 votes |
private static byte[] compressBytesInflateDeflate(byte[] inBytes) { Deflater deflater = new Deflater(Deflater.BEST_SPEED); deflater.setInput(inBytes); ByteArrayOutputStream bos = new ByteArrayOutputStream(inBytes.length); deflater.finish(); byte[] buffer = new byte[1024 * 8]; while (!deflater.finished()) { int count = deflater.deflate(buffer); bos.write(buffer, 0, count); } byte[] output = bos.toByteArray(); return output; }
Example 17
Source File: CompressionManager.java From minecraft-world-downloader with GNU General Public License v3.0 | 5 votes |
/** * Compresses the stream if the size is greater than the compression limit. * Source: https://dzone.com/articles/how-compress-and-uncompress */ public static byte[] zlibCompress(byte[] input) throws IOException { Deflater deflater = new Deflater(); deflater.setInput(input); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(input.length); 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); } outputStream.close(); return outputStream.toByteArray(); }
Example 18
Source File: DeflaterTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * java.util.zip.Deflater#setInput(byte[]) */ public void test_setInput$B() { byte[] byteArray = { 1, 2, 3 }; byte[] outPutBuf = new byte[50]; byte[] outPutInf = new byte[50]; Deflater defl = new Deflater(); defl.setInput(byteArray); assertTrue("the array buffer in setInput() is empty", !defl .needsInput()); // The second setInput() should be ignored since needsInput() return // false defl.setInput(byteArray); defl.finish(); while (!defl.finished()) { defl.deflate(outPutBuf); } defl.end(); Inflater infl = new Inflater(); try { infl.setInput(outPutBuf); while (!infl.finished()) { infl.inflate(outPutInf); } } catch (DataFormatException e) { fail("Invalid input to be decompressed"); } for (int i = 0; i < byteArray.length; i++) { assertEquals(byteArray[i], outPutInf[i]); } assertEquals(byteArray.length, infl.getTotalOut()); infl.end(); }
Example 19
Source File: IntelInflaterDeflaterIntegrationTest.java From gatk with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Test public void deflateInflateWithIntel() throws DataFormatException { if (!isIntelInflaterDeflaterSupported()) { throw new SkipException("IntelInflater/IntelDeflater not available on this platform"); } // create buffers and random input final int LEN = 64 * 1024; final byte[] input = new RandomDNA().nextBases(LEN); final byte[] compressed = new byte[2 * LEN]; final byte[] result = new byte[LEN]; final IntelInflaterFactory intelInflaterFactory = new IntelInflaterFactory(); final IntelDeflaterFactory intelDeflaterFactory = new IntelDeflaterFactory(); Assert.assertTrue(intelInflaterFactory.usingIntelInflater()); Assert.assertTrue(intelDeflaterFactory.usingIntelDeflater()); for (int i = 0; i < 10; i++) { // create deflater with compression level i final Deflater deflater = intelDeflaterFactory.makeDeflater(i, true); // setup deflater deflater.setInput(input); deflater.finish(); // compress data int compressedBytes = 0; // buffer for compressed data is 2x the size of the data we are compressing // so this loop should always finish in one iteration while (!deflater.finished()) { compressedBytes = deflater.deflate(compressed, 0, compressed.length); } deflater.end(); // decompress and check output == input Inflater inflater = intelInflaterFactory.makeInflater(true); inflater.setInput(compressed, 0, compressedBytes); inflater.inflate(result); inflater.end(); Assert.assertEquals(input, result); // clear compressed and result buffers for next iteration Arrays.fill(compressed, (byte)0); Arrays.fill(result, (byte)0); } }
Example 20
Source File: ZookeeperDiscoveryImpl.java From ignite with Apache License 2.0 | 4 votes |
/** * @param bytes Bytes to compress. * @return Zip-compressed bytes. */ private static byte[] zip(byte[] bytes) { Deflater deflater = new Deflater(); deflater.setInput(bytes); deflater.finish(); GridByteArrayOutputStream out = new GridByteArrayOutputStream(bytes.length); final byte[] buf = new byte[bytes.length]; while (!deflater.finished()) { int cnt = deflater.deflate(buf); out.write(buf, 0, cnt); } return out.toByteArray(); }