Java Code Examples for java.util.zip.DeflaterOutputStream#finish()

The following examples show how to use java.util.zip.DeflaterOutputStream#finish() . 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: BenchmarkRunner.java    From dubbox with Apache License 2.0 6 votes vote down vote up
private static byte[] compressDeflate(byte[] data)
{
	try {
		ByteArrayOutputStream bout = new ByteArrayOutputStream(500);
		DeflaterOutputStream compresser = new DeflaterOutputStream(bout);
		compresser.write(data, 0, data.length);
		compresser.finish();
		compresser.flush();
		return bout.toByteArray();
	}
	catch (IOException ex) {
		AssertionError ae = new AssertionError("IOException while writing to ByteArrayOutputStream!");
		ae.initCause(ex);
		throw ae;
	}
}
 
Example 2
Source File: UtilAll.java    From DDMQ with Apache License 2.0 6 votes vote down vote up
public static byte[] compress(final byte[] src, final int level) throws IOException {
    byte[] result = src;
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(src.length);
    java.util.zip.Deflater defeater = new java.util.zip.Deflater(level);
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, defeater);
    try {
        deflaterOutputStream.write(src);
        deflaterOutputStream.finish();
        deflaterOutputStream.close();
        result = byteArrayOutputStream.toByteArray();
    } catch (IOException e) {
        defeater.end();
        throw e;
    } finally {
        try {
            byteArrayOutputStream.close();
        } catch (IOException ignored) {
        }

        defeater.end();
    }

    return result;
}
 
Example 3
Source File: IOUtils.java    From mpush with Apache License 2.0 6 votes vote down vote up
public static byte[] compress(byte[] data) {

        Profiler.enter("time cost on [compress]");

        ByteArrayOutputStream out = new ByteArrayOutputStream(data.length / 4);
        DeflaterOutputStream zipOut = new DeflaterOutputStream(out);
        try {
            zipOut.write(data);
            zipOut.finish();
            zipOut.close();
        } catch (IOException e) {
            LOGGER.error("compress ex", e);
            return Constants.EMPTY_BYTES;
        } finally {
            close(zipOut);
            Profiler.release();
        }
        return out.toByteArray();
    }
 
Example 4
Source File: UtilAll.java    From rocketmq-read with Apache License 2.0 6 votes vote down vote up
/**
 * 压缩
 * @param src ;
 * @param level ;
 * @return ;
 * @throws IOException ;
 */
public static byte[] compress(final byte[] src, final int level) throws IOException {
    byte[] result = src;
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(src.length);
    java.util.zip.Deflater defeater = new java.util.zip.Deflater(level);
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, defeater);
    try {
        deflaterOutputStream.write(src);
        deflaterOutputStream.finish();
        deflaterOutputStream.close();
        result = byteArrayOutputStream.toByteArray();
    } catch (IOException e) {
        defeater.end();
        throw e;
    } finally {
        try {
            byteArrayOutputStream.close();
        } catch (IOException ignored) {
        }

        defeater.end();
    }

    return result;
}
 
Example 5
Source File: BenchmarkRunner.java    From dubbox with Apache License 2.0 6 votes vote down vote up
private static byte[] compressDeflate(byte[] data)
{
	try {
		ByteArrayOutputStream bout = new ByteArrayOutputStream(500);
		DeflaterOutputStream compresser = new DeflaterOutputStream(bout);
		compresser.write(data, 0, data.length);
		compresser.finish();
		compresser.flush();
		return bout.toByteArray();
	}
	catch (IOException ex) {
		AssertionError ae = new AssertionError("IOException while writing to ByteArrayOutputStream!");
		ae.initCause(ex);
		throw ae;
	}
}
 
Example 6
Source File: UtilAll.java    From rocketmq-all-4.1.0-incubating with Apache License 2.0 6 votes vote down vote up
public static byte[] compress(final byte[] src, final int level) throws IOException {
    byte[] result = src;
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(src.length);
    java.util.zip.Deflater defeater = new java.util.zip.Deflater(level);
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, defeater);
    try {
        deflaterOutputStream.write(src);
        deflaterOutputStream.finish();
        deflaterOutputStream.close();
        result = byteArrayOutputStream.toByteArray();
    } catch (IOException e) {
        defeater.end();
        throw e;
    } finally {
        try {
            byteArrayOutputStream.close();
        } catch (IOException ignored) {
        }

        defeater.end();
    }

    return result;
}
 
Example 7
Source File: UtilAll.java    From rocketmq_trans_message with Apache License 2.0 6 votes vote down vote up
public static byte[] compress(final byte[] src, final int level) throws IOException {
    byte[] result = src;
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(src.length);
    java.util.zip.Deflater defeater = new java.util.zip.Deflater(level);
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, defeater);
    try {
        deflaterOutputStream.write(src);
        deflaterOutputStream.finish();
        deflaterOutputStream.close();
        result = byteArrayOutputStream.toByteArray();
    } catch (IOException e) {
        defeater.end();
        throw e;
    } finally {
        try {
            byteArrayOutputStream.close();
        } catch (IOException ignored) {
        }

        defeater.end();
    }

    return result;
}
 
Example 8
Source File: CKit.java    From FxDock with Apache License 2.0 6 votes vote down vote up
public static String compressString(String s) throws Exception
{
	if(s == null)
	{
		return "";
	}
	
	ByteArrayOutputStream ba = new ByteArrayOutputStream(s.length() * 2 + 20);
	DeflaterOutputStream out = new DeflaterOutputStream(ba);
	byte[] bytes = s.getBytes(CHARSET_UTF8);
	out.write(bytes);
	out.finish();
	out.flush();
	
	byte[] compressed = ba.toByteArray();
	return Hex.toHexString(compressed);
}
 
Example 9
Source File: UtilAll.java    From rocketmq with Apache License 2.0 6 votes vote down vote up
public static byte[] compress(final byte[] src, final int level) throws IOException {
    byte[] result = src;
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(src.length);
    java.util.zip.Deflater defeater = new java.util.zip.Deflater(level);
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, defeater);
    try {
        deflaterOutputStream.write(src);
        deflaterOutputStream.finish();
        deflaterOutputStream.close();
        result = byteArrayOutputStream.toByteArray();
    } catch (IOException e) {
        defeater.end();
        throw e;
    } finally {
        try {
            byteArrayOutputStream.close();
        } catch (IOException ignored) {
        }

        defeater.end();
    }

    return result;
}
 
Example 10
Source File: r.java    From MiBandDecompiled with Apache License 2.0 6 votes vote down vote up
public static byte[] a(byte abyte0[])
{
    if (abyte0 == null)
    {
        return null;
    }
    ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
    DeflaterOutputStream deflateroutputstream = new DeflaterOutputStream(bytearrayoutputstream);
    try
    {
        deflateroutputstream.write(abyte0, 0, abyte0.length);
        deflateroutputstream.finish();
        deflateroutputstream.flush();
        deflateroutputstream.close();
    }
    catch (Exception exception)
    {
        return null;
    }
    return bytearrayoutputstream.toByteArray();
}
 
Example 11
Source File: BenchmarkRunner.java    From dubbox with Apache License 2.0 6 votes vote down vote up
private static byte[] compressDeflate(byte[] data)
{
	try {
		ByteArrayOutputStream bout = new ByteArrayOutputStream(500);
		DeflaterOutputStream compresser = new DeflaterOutputStream(bout);
		compresser.write(data, 0, data.length);
		compresser.finish();
		compresser.flush();
		return bout.toByteArray();
	}
	catch (IOException ex) {
		AssertionError ae = new AssertionError("IOException while writing to ByteArrayOutputStream!");
		ae.initCause(ex);
		throw ae;
	}
}
 
Example 12
Source File: BenchmarkRunner.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
private static byte[] compressDeflate(byte[] data)
{
	try {
		ByteArrayOutputStream bout = new ByteArrayOutputStream(500);
		DeflaterOutputStream compresser = new DeflaterOutputStream(bout);
		compresser.write(data, 0, data.length);
		compresser.finish();
		compresser.flush();
		return bout.toByteArray();
	}
	catch (IOException ex) {
		AssertionError ae = new AssertionError("IOException while writing to ByteArrayOutputStream!");
		ae.initCause(ex);
		throw ae;
	}
}
 
Example 13
Source File: DeflateCompressor.java    From archive-patcher with Apache License 2.0 5 votes vote down vote up
@Override
public void compress(InputStream uncompressedIn, OutputStream compressedOut) throws IOException {
  byte[] buffer = new byte[inputBufferSize];
  DeflaterOutputStream deflaterOut =
      new DeflaterOutputStream(compressedOut, createOrResetDeflater(), outputBufferSize);
  int numRead = 0;
  while ((numRead = uncompressedIn.read(buffer)) >= 0) {
    deflaterOut.write(buffer, 0, numRead);
  }
  deflaterOut.finish();
  deflaterOut.flush();
}
 
Example 14
Source File: IOUtils.java    From mpush-client-java with Apache License 2.0 5 votes vote down vote up
public static byte[] compress(byte[] data) {
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream(data.length / 4);
    DeflaterOutputStream zipOut = new DeflaterOutputStream(byteStream);
    try {
        zipOut.write(data);
        zipOut.finish();
        zipOut.close();
    } catch (IOException e) {
        return Constants.EMPTY_BYTES;
    } finally {
        close(zipOut);
    }
    return byteStream.toByteArray();
}
 
Example 15
Source File: DeflateUncompressorTest.java    From archive-patcher with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws IOException {
  ByteArrayOutputStream compressedContentBuffer = new ByteArrayOutputStream();
  Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
  DeflaterOutputStream deflateOut = new DeflaterOutputStream(compressedContentBuffer, deflater);
  deflateOut.write(CONTENT);
  deflateOut.finish();
  deflateOut.close();
  deflater.end();
  compressedContent = compressedContentBuffer.toByteArray();
  uncompressor = new DeflateUncompressor();
  compressedContentIn = new ByteArrayInputStream(compressedContent);
  uncompressedContentOut = new ByteArrayOutputStream();
}
 
Example 16
Source File: ObjectUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
public static byte[] toCompressedBytes(@Nonnull final Object obj) throws IOException {
    FastMultiByteArrayOutputStream bos = new FastMultiByteArrayOutputStream();
    DeflaterOutputStream dos = new DeflaterOutputStream(bos);
    try {
        toStream(obj, dos);
        dos.finish();
        dos.flush();
        return bos.toByteArray_clear();
    } finally {
        IOUtils.closeQuietly(dos);
    }
}
 
Example 17
Source File: Compresser.java    From zstack with Apache License 2.0 5 votes vote down vote up
public static byte[] deflate(byte[] input, int bufferSize) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream(input.length);
    Deflater def = new Deflater();
    DeflaterOutputStream dos = new DeflaterOutputStream(out, def, bufferSize);
    dos.write(input, 0, input.length);
    dos.finish();
    dos.close();
    byte[] ret = out.toByteArray();
    out.close();
    return ret;
}
 
Example 18
Source File: ZLibUtils.java    From QQRobot with GNU General Public License v3.0 5 votes vote down vote up
/** 
 * 压缩 
 *  
 * @param data 
 *            待压缩数据 
 *  
 * @param os 
 *            输出流 
 */  
public static void compress(byte[] data, OutputStream os) {  
    DeflaterOutputStream dos = new DeflaterOutputStream(os);  

    try {  
        dos.write(data, 0, data.length);  

        dos.finish();  

        dos.flush();  
    } catch (IOException e) {  
        e.printStackTrace();  
    }  
}
 
Example 19
Source File: LogoutRequestGenerator.java    From development with Apache License 2.0 5 votes vote down vote up
private ByteArrayOutputStream deflateBytes(ByteArrayOutputStream baos) throws IOException {
    ByteArrayOutputStream deflatedBytes = new ByteArrayOutputStream();
    Deflater deflater = new Deflater(Deflater.DEFLATED, true);
    DeflaterOutputStream deflaterStream = new DeflaterOutputStream(deflatedBytes, deflater);
    deflaterStream.write(baos.toByteArray());
    deflaterStream.finish();
    return deflatedBytes;
}
 
Example 20
Source File: ZLibUtils.java    From android-project-wo2b with Apache License 2.0 5 votes vote down vote up
/**
 * 压缩
 * 
 * @param data 待压缩的数据
 * @param os 输出流
 * @throws IOException
 */
public static void compress(byte[] data, OutputStream os) throws IOException
{
	DeflaterOutputStream dos = new DeflaterOutputStream(os);

	dos.write(data, 0, data.length);
	dos.finish();
	dos.flush();
}