Java Code Examples for java.util.zip.Deflater#end()

The following examples show how to use java.util.zip.Deflater#end() . 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: Zlib.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
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 2
Source File: DeflateCompressor.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public StreamOutput streamOutput(StreamOutput out) throws IOException {
    out.writeBytes(HEADER);
    final boolean nowrap = true;
    final Deflater deflater = new Deflater(LEVEL, nowrap);
    final boolean syncFlush = true;
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(out, deflater, BUFFER_SIZE, syncFlush);
    OutputStream compressedOut = new BufferedOutputStream(deflaterOutputStream, BUFFER_SIZE);
    return new OutputStreamStreamOutput(compressedOut) {
        final AtomicBoolean closed = new AtomicBoolean(false);

        public void close() throws IOException {
            try {
                super.close();
            } finally {
                if (closed.compareAndSet(false, true)) {
                    // important to release native memory
                    deflater.end();
                }
            }
        }
    };
}
 
Example 3
Source File: DeflaterOutputStreamTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Override
protected void setUp() {
    // setting up a deflater to be used
    byte byteArray[] = { 1, 3, 4, 7, 8 };
    int x = 0;
    Deflater deflate = new Deflater(1);
    deflate.setInput(byteArray);
    while (!(deflate.needsInput())) {
        x += deflate.deflate(outputBuf, x, outputBuf.length - x);
    }
    deflate.finish();
    while (!(deflate.finished())) {
        x = x + deflate.deflate(outputBuf, x, outputBuf.length - x);
    }
    deflate.end();
}
 
Example 4
Source File: SecureUtil.java    From pay with Apache License 2.0 6 votes vote down vote up
public static byte[] deflater(byte[] inputByte) throws IOException {
    boolean compressedDataLength = false;
    Deflater compresser = new Deflater();
    compresser.setInput(inputByte);
    compresser.finish();
    ByteArrayOutputStream o = new ByteArrayOutputStream(inputByte.length);
    byte[] result = new byte[1024];

    try {
        while(!compresser.finished()) {
            int compressedDataLength1 = compresser.deflate(result);
            o.write(result, 0, compressedDataLength1);
        }
    } finally {
        o.close();
    }

    compresser.end();
    return o.toByteArray();
}
 
Example 5
Source File: ManualConfig.java    From hermes with Apache License 2.0 6 votes vote down vote up
private byte[] compress(byte[] bytes) {
	ByteArrayInputStream input = new ByteArrayInputStream(bytes);
	ByteArrayOutputStream bout = new ByteArrayOutputStream();
	Deflater def = new Deflater(5, true);
	try {
		DeflaterOutputStream gout = new DeflaterOutputStream(bout, def);
		IO.INSTANCE.copy(input, gout, AutoClose.INPUT_OUTPUT);
		return bout.toByteArray();
	} catch (IOException e) {
		throw new RuntimeException("Exception occurred while compressing manual config.", e);
	} finally {
		if (def != null) {
			def.end();
		}
	}
}
 
Example 6
Source File: DeflaterTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * @throws DataFormatException
 * @throws UnsupportedEncodingException
 * java.util.zip.Deflater#getBytesRead()
 */
public void test_getBytesWritten() throws DataFormatException,
        UnsupportedEncodingException {
    // Regression test for HARMONY-158
    Deflater def = new Deflater();
    assertEquals(0, def.getTotalIn());
    assertEquals(0, def.getTotalOut());
    assertEquals(0, def.getBytesWritten());
    // Encode a String into bytes
    String inputString = "blahblahblah??";
    byte[] input = inputString.getBytes("UTF-8");

    // Compress the bytes
    byte[] output = new byte[100];
    def.setInput(input);
    def.finish();
    int compressedDataLength = def.deflate(output);
    assertEquals(14, def.getTotalIn());
    assertEquals(compressedDataLength, def.getTotalOut());
    assertEquals(compressedDataLength, def.getBytesWritten());
    def.end();
}
 
Example 7
Source File: FlateFilter.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@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 8
Source File: SDKUtil.java    From unionpay with MIT License 6 votes vote down vote up
/**
 * 压缩.
 * 
 * @param inputByte
 *            需要解压缩的byte[]数组
 * @return 压缩后的数据
 * @throws IOException
 */
public static byte[] deflater(final byte[] inputByte) throws IOException {
	int compressedDataLength = 0;
	Deflater compresser = new Deflater();
	compresser.setInput(inputByte);
	compresser.finish();
	ByteArrayOutputStream o = new ByteArrayOutputStream(inputByte.length);
	byte[] result = new byte[1024];
	try {
		while (!compresser.finished()) {
			compressedDataLength = compresser.deflate(result);
			o.write(result, 0, compressedDataLength);
		}
	} finally {
		o.close();
	}
	compresser.end();
	return o.toByteArray();
}
 
Example 9
Source File: Zlib.java    From krpc with Apache License 2.0 5 votes vote down vote up
public byte[] zip(byte[] input) throws IOException {
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();) {
        byte[] buff = tlBuff.get();
        Deflater defl = new Deflater();
        defl.setInput(input);
        defl.finish();
        while (!defl.finished()) {
            int len = defl.deflate(buff);
            bos.write(buff, 0, len);
        }
        defl.end();
        bos.flush();
        return bos.toByteArray();
    }
}
 
Example 10
Source File: AstSerializer.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
static byte[] serialize(final FunctionNode fn) {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final Deflater deflater = new Deflater(COMPRESSION_LEVEL);
    try (final ObjectOutputStream oout = new ObjectOutputStream(new DeflaterOutputStream(out, deflater))) {
        oout.writeObject(fn);
    } catch (final IOException e) {
        throw new AssertionError("Unexpected exception serializing function", e);
    } finally {
        deflater.end();
    }
    return out.toByteArray();
}
 
Example 11
Source File: CompressedWritable.java    From hadoop-gpu with Apache License 2.0 5 votes vote down vote up
public final void write(DataOutput out) throws IOException {
  if (compressed == null) {
    ByteArrayOutputStream deflated = new ByteArrayOutputStream();
    Deflater deflater = new Deflater(Deflater.BEST_SPEED);
    DataOutputStream dout =
      new DataOutputStream(new DeflaterOutputStream(deflated, deflater));
    writeCompressed(dout);
    dout.close();
    deflater.end();
    compressed = deflated.toByteArray();
  }
  out.writeInt(compressed.length);
  out.write(compressed);
}
 
Example 12
Source File: ZipFileSystem.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private void releaseDeflater(Deflater def) {
    synchronized (deflaters) {
        if (inflaters.size() < MAX_FLATER) {
           def.reset();
           deflaters.add(def);
        } else {
           def.end();
        }
    }
}
 
Example 13
Source File: ZipFileSystem.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private void releaseDeflater(Deflater def) {
    synchronized (deflaters) {
        if (inflaters.size() < MAX_FLATER) {
           def.reset();
           deflaters.add(def);
        } else {
           def.end();
        }
    }
}
 
Example 14
Source File: SlowInflateTestCase.java    From vespa with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    value = "000000000000000000000000000000000000000000000000000000000000000";
    raw = Utf8.toBytesStd(value);
    output = new byte[raw.length * 2];
    Deflater compresser = new Deflater();
    compresser.setInput(raw);
    compresser.finish();
    compressedDataLength = compresser.deflate(output);
    compresser.end();
    compressed = Arrays.copyOf(output, compressedDataLength);
}
 
Example 15
Source File: ZipFileSystem.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void close() throws IOException {
    beginWrite();
    try {
        if (!isOpen)
            return;
        isOpen = false;             // set closed
    } finally {
        endWrite();
    }
    if (!streams.isEmpty()) {       // unlock and close all remaining streams
        Set<InputStream> copy = new HashSet<>(streams);
        for (InputStream is: copy)
            is.close();
    }
    beginWrite();                   // lock and sync
    try {
        sync();
        ch.close();                 // close the ch just in case no update
    } finally {                     // and sync dose not close the ch
        endWrite();
    }

    synchronized (inflaters) {
        for (Inflater inf : inflaters)
            inf.end();
    }
    synchronized (deflaters) {
        for (Deflater def : deflaters)
            def.end();
    }

    IOException ioe = null;
    synchronized (tmppaths) {
        for (Path p: tmppaths) {
            try {
                Files.deleteIfExists(p);
            } catch (IOException x) {
                if (ioe == null)
                    ioe = x;
                else
                    ioe.addSuppressed(x);
            }
        }
    }
    provider.removeFileSystem(zfpath, this);
    if (ioe != null)
       throw ioe;
}
 
Example 16
Source File: ZipFileSystem.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void close() throws IOException {
    beginWrite();
    try {
        if (!isOpen)
            return;
        isOpen = false;             // set closed
    } finally {
        endWrite();
    }
    if (!streams.isEmpty()) {       // unlock and close all remaining streams
        Set<InputStream> copy = new HashSet<>(streams);
        for (InputStream is: copy)
            is.close();
    }
    beginWrite();                   // lock and sync
    try {
        sync();
        ch.close();                 // close the ch just in case no update
    } finally {                     // and sync dose not close the ch
        endWrite();
    }

    synchronized (inflaters) {
        for (Inflater inf : inflaters)
            inf.end();
    }
    synchronized (deflaters) {
        for (Deflater def : deflaters)
            def.end();
    }

    IOException ioe = null;
    synchronized (tmppaths) {
        for (Path p: tmppaths) {
            try {
                Files.deleteIfExists(p);
            } catch (IOException x) {
                if (ioe == null)
                    ioe = x;
                else
                    ioe.addSuppressed(x);
            }
        }
    }
    provider.removeFileSystem(zfpath, this);
    if (ioe != null)
       throw ioe;
}
 
Example 17
Source File: ZipFileSystem.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void close() throws IOException {
    beginWrite();
    try {
        if (!isOpen)
            return;
        isOpen = false;             // set closed
    } finally {
        endWrite();
    }
    if (!streams.isEmpty()) {       // unlock and close all remaining streams
        Set<InputStream> copy = new HashSet<>(streams);
        for (InputStream is: copy)
            is.close();
    }
    beginWrite();                   // lock and sync
    try {
        sync();
        ch.close();                 // close the ch just in case no update
    } finally {                     // and sync dose not close the ch
        endWrite();
    }

    synchronized (inflaters) {
        for (Inflater inf : inflaters)
            inf.end();
    }
    synchronized (deflaters) {
        for (Deflater def : deflaters)
            def.end();
    }

    IOException ioe = null;
    synchronized (tmppaths) {
        for (Path p: tmppaths) {
            try {
                Files.deleteIfExists(p);
            } catch (IOException x) {
                if (ioe == null)
                    ioe = x;
                else
                    ioe.addSuppressed(x);
            }
        }
    }
    provider.removeFileSystem(zfpath, this);
    if (ioe != null)
       throw ioe;
}
 
Example 18
Source File: CompressionCodecZLib.java    From pulsar with Apache License 2.0 4 votes vote down vote up
@Override
protected void onRemoval(Deflater deflater) throws Exception {
    deflater.end();
}
 
Example 19
Source File: DeflaterTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * java.util.zip.Deflater#setStrategy(int)
 */
public void test_setStrategyI() throws Exception {
    byte[] byteArray = new byte[100];
    InputStream inFile = Support_Resources.getStream("hyts_checkInput.txt");
    inFile.read(byteArray);
    inFile.close();

    for (int i = 0; i < 3; i++) {
        byte outPutBuf[] = new byte[500];
        MyDeflater mdefl = new MyDeflater();

        if (i == 0) {
            mdefl.setStrategy(mdefl.getDefStrategy());
        } else if (i == 1) {
            mdefl.setStrategy(mdefl.getHuffman());
        } else {
            mdefl.setStrategy(mdefl.getFiltered());
        }

        mdefl.setInput(byteArray);
        while (!mdefl.needsInput()) {
            mdefl.deflate(outPutBuf);
        }
        mdefl.finish();
        while (!mdefl.finished()) {
            mdefl.deflate(outPutBuf);
        }

        if (i == 0) {
            // System.out.println(mdefl.getTotalOut());
            // ran JDK and found that getTotalOut() = 86 for this particular
            // file
            assertEquals("getTotalOut() for the default strategy did not correspond with JDK",
                    86, mdefl.getTotalOut());
        } else if (i == 1) {
            // System.out.println(mdefl.getTotalOut());
            // ran JDK and found that getTotalOut() = 100 for this
            // particular file
            assertEquals("getTotalOut() for the Huffman strategy did not correspond with JDK",
                    100, mdefl.getTotalOut());
        } else {
            // System.out.println(mdefl.getTotalOut());
            // ran JDK and found that totalOut = 93 for this particular file
            assertEquals("Total Out for the Filtered strategy did not correspond with JDK",
                    93, mdefl.getTotalOut());
        }
        mdefl.end();
    }

    // Attempting to setStrategy to an invalid value
    Deflater defl = new Deflater();
    try {
        defl.setStrategy(-412);
        fail("IllegalArgumentException not thrown when setting strategy to an invalid value.");
    } catch (IllegalArgumentException e) {
    }
    defl.end();
}
 
Example 20
Source File: ZipFileSystem.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void close() throws IOException {
    beginWrite();
    try {
        if (!isOpen)
            return;
        isOpen = false;             // set closed
    } finally {
        endWrite();
    }
    if (!streams.isEmpty()) {       // unlock and close all remaining streams
        Set<InputStream> copy = new HashSet<>(streams);
        for (InputStream is: copy)
            is.close();
    }
    beginWrite();                   // lock and sync
    try {
        sync();
        ch.close();                 // close the ch just in case no update
    } finally {                     // and sync dose not close the ch
        endWrite();
    }

    synchronized (inflaters) {
        for (Inflater inf : inflaters)
            inf.end();
    }
    synchronized (deflaters) {
        for (Deflater def : deflaters)
            def.end();
    }

    beginWrite();                // lock and sync
    try {
        // Clear the map so that its keys & values can be garbage collected
        inodes = null;
    } finally {
        endWrite();
    }

    IOException ioe = null;
    synchronized (tmppaths) {
        for (Path p: tmppaths) {
            try {
                Files.deleteIfExists(p);
            } catch (IOException x) {
                if (ioe == null)
                    ioe = x;
                else
                    ioe.addSuppressed(x);
            }
        }
    }
    provider.removeFileSystem(zfpath, this);
    if (ioe != null)
       throw ioe;
}