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

The following examples show how to use java.util.zip.DeflaterOutputStream#flush() . 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: MixAll.java    From iot-mqtt 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);
    Deflater deflater = new Deflater(level);
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream,deflater);
    try{
        deflaterOutputStream.write(src);
        deflaterOutputStream.flush();
        deflaterOutputStream.close();
        result = byteArrayOutputStream.toByteArray();
    }catch (IOException e){
        deflater.end();
        throw e;
    }finally {
        try {
            byteArrayOutputStream.close();
        } catch (IOException ignored) {
        }
        deflater.end();
    }
    return result;
}
 
Example 2
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 3
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 4
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 5
Source File: ZlibEncoder.java    From Decoder-Improved with GNU General Public License v3.0 6 votes vote down vote up
@Override
public byte[] modifyBytes(byte[] input) {
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
        DeflaterOutputStream deflater = new DeflaterOutputStream(bos);
        deflater.write(input, 0, input.length);
        deflater.finish();
        deflater.flush();
        bos.flush();
        deflater.close();
        bos.close();
        return bos.toByteArray();
    } catch (IOException e) {
        return new byte[]{};
    }
}
 
Example 6
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 7
Source File: FilesTargetRepository.java    From ache with Apache License 2.0 6 votes vote down vote up
public boolean insert(TargetModelJson target) {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        jsonMapper.writeValue(baos, target);
        baos.write("\n".getBytes());
        synchronized (this) {
            DeflaterOutputStream currentFile = getCurrentFile(baos);
            baos.writeTo(currentFile);
            currentFile.flush();
        }
        return true;
    } catch (IOException e) {
        logger.error("Failed to store object in repository.", e);
        return false;
    }
}
 
Example 8
Source File: j.java    From letv with Apache License 2.0 6 votes vote down vote up
public static byte[] a(byte[] bArr) {
    if (bArr == null) {
        return null;
    }
    OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream);
    try {
        deflaterOutputStream.write(bArr, 0, bArr.length);
        deflaterOutputStream.finish();
        deflaterOutputStream.flush();
        deflaterOutputStream.close();
        return byteArrayOutputStream.toByteArray();
    } catch (Exception e) {
        return null;
    }
}
 
Example 9
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 10
Source File: TestZlibCompressorDecompressor.java    From hadoop with Apache License 2.0 5 votes vote down vote up
private void compressDecompressLoop(int rawDataSize) throws IOException {
  byte[] rawData = null;
  rawData = generate(rawDataSize);
  ByteArrayOutputStream baos = new ByteArrayOutputStream(rawDataSize+12);
  DeflaterOutputStream dos = new DeflaterOutputStream(baos);
  dos.write(rawData);
  dos.flush();
  dos.close();
  byte[] compressedResult = baos.toByteArray();
  int compressedSize = compressedResult.length;
  ZlibDirectDecompressor decompressor = new ZlibDirectDecompressor();
 
  ByteBuffer inBuf = ByteBuffer.allocateDirect(compressedSize);
  ByteBuffer outBuf = ByteBuffer.allocateDirect(rawDataSize);

  inBuf.put(compressedResult, 0, compressedSize);
  inBuf.flip();    

  ByteBuffer expected = ByteBuffer.wrap(rawData);
  
  outBuf.clear();
  while(!decompressor.finished()) {
    decompressor.decompress(inBuf, outBuf);
    if (outBuf.remaining() == 0) {
      outBuf.flip();
      while (outBuf.remaining() > 0) {        
        assertEquals(expected.get(), outBuf.get());
      }
      outBuf.clear();
    }
  }
  outBuf.flip();
  while (outBuf.remaining() > 0) {        
    assertEquals(expected.get(), outBuf.get());
  }
  outBuf.clear();
  
  assertEquals(0, expected.remaining());
}
 
Example 11
Source File: ZLibUtil.java    From MonitorClient with Apache License 2.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(); 
        dos.close();
    } catch (IOException e) {  
        e.printStackTrace();  
    }  
}
 
Example 12
Source File: TestZlibCompressorDecompressor.java    From big-c with Apache License 2.0 5 votes vote down vote up
private void compressDecompressLoop(int rawDataSize) throws IOException {
  byte[] rawData = null;
  rawData = generate(rawDataSize);
  ByteArrayOutputStream baos = new ByteArrayOutputStream(rawDataSize+12);
  DeflaterOutputStream dos = new DeflaterOutputStream(baos);
  dos.write(rawData);
  dos.flush();
  dos.close();
  byte[] compressedResult = baos.toByteArray();
  int compressedSize = compressedResult.length;
  ZlibDirectDecompressor decompressor = new ZlibDirectDecompressor();
 
  ByteBuffer inBuf = ByteBuffer.allocateDirect(compressedSize);
  ByteBuffer outBuf = ByteBuffer.allocateDirect(rawDataSize);

  inBuf.put(compressedResult, 0, compressedSize);
  inBuf.flip();    

  ByteBuffer expected = ByteBuffer.wrap(rawData);
  
  outBuf.clear();
  while(!decompressor.finished()) {
    decompressor.decompress(inBuf, outBuf);
    if (outBuf.remaining() == 0) {
      outBuf.flip();
      while (outBuf.remaining() > 0) {        
        assertEquals(expected.get(), outBuf.get());
      }
      outBuf.clear();
    }
  }
  outBuf.flip();
  while (outBuf.remaining() > 0) {        
    assertEquals(expected.get(), outBuf.get());
  }
  outBuf.clear();
  
  assertEquals(0, expected.remaining());
}
 
Example 13
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 14
Source File: ObjectUtils.java    From incubator-hivemall with Apache License 2.0 5 votes vote down vote up
public static byte[] toCompressedBytes(@Nonnull final Externalizable 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 15
Source File: HoodieJsonPayload.java    From hudi with Apache License 2.0 5 votes vote down vote up
private byte[] compressData(String jsonData) throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
  DeflaterOutputStream dos = new DeflaterOutputStream(baos, deflater, true);
  try {
    dos.write(jsonData.getBytes());
  } finally {
    dos.flush();
    dos.close();
    // Its important to call this.
    // Deflater takes off-heap native memory and does not release until GC kicks in
    deflater.end();
  }
  return baos.toByteArray();
}
 
Example 16
Source File: TestRawTripPayload.java    From hudi with Apache License 2.0 5 votes vote down vote up
private byte[] compressData(String jsonData) throws IOException {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  DeflaterOutputStream dos = new DeflaterOutputStream(baos, new Deflater(Deflater.BEST_COMPRESSION), true);
  try {
    dos.write(jsonData.getBytes());
  } finally {
    dos.flush();
    dos.close();
  }
  return baos.toByteArray();
}
 
Example 17
Source File: RecordCompressor.java    From database with GNU General Public License v2.0 5 votes vote down vote up
public void compress(final byte[] bytes, final int off, final int len,
        final OutputStream os) {

    _deflater.reset(); // required w/ instance reuse.

    final DeflaterOutputStream dos = new DeflaterOutputStream(os, _deflater);

    try {

        /*
         * Write onto deflator that writes onto the output stream.
         */
        dos.write(bytes, off, len);

        /*
         * Flush and close the deflator instance.
         * 
         * Note: The caller is unable to do this as they do not have access
         * to the {@link Deflator}. However, if this flushes through to the
         * underlying sink then that could drive IOs without the application
         * being aware that synchronous IO was occurring.
         */
        dos.flush();

        dos.close();

    } catch (IOException ex) {

        throw new RuntimeException(ex);

    }

}
 
Example 18
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();
}
 
Example 19
Source File: Usernotes.java    From Slide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Converts a JSON string into a zlib compressed and base64 encoded blog
 *
 * @param json JSON to turn into blob
 * @return Blob
 */
public static String jsonToBlob(String json) {
    // Adapted from https://stackoverflow.com/a/33022277
    try {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        DeflaterOutputStream deflater = new DeflaterOutputStream(output);
        deflater.write(json.getBytes());
        deflater.flush();
        deflater.close();

        return Base64.encodeToString(output.toByteArray(), Base64.NO_WRAP);
    } catch (IOException e) {
        return null;
    }
}
 
Example 20
Source File: TestHuffmanEncoder.java    From database with GNU General Public License v2.0 3 votes vote down vote up
public byte[] compress(byte[] uncompressed, int off, int len) {
    
    /*
     * The deflater MUST be reset between invocations.
     */
    deflater.reset();
    
    /*
     * Clear the output buffer.
     */
    baos.reset();
    
    DeflaterOutputStream dos = new DeflaterOutputStream(baos, deflater/* ,size */);

    try {

        dos.write(uncompressed,off,len);

        dos.flush();

        dos.close();
        
    } catch (IOException ex) {
        
        throw new RuntimeException(ex);
        
    }

    return baos.toByteArray();

}