java.util.zip.DeflaterOutputStream#flush ( )源码实例Demo

下面列出了java.util.zip.DeflaterOutputStream#flush ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: dubbox   文件: BenchmarkRunner.java
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;
	}
}
 
源代码2 项目: iot-mqtt   文件: MixAll.java
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;
}
 
源代码3 项目: letv   文件: j.java
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;
    }
}
 
源代码4 项目: ache   文件: FilesTargetRepository.java
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;
    }
}
 
源代码5 项目: dubbox-hystrix   文件: BenchmarkRunner.java
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;
	}
}
 
源代码6 项目: Decoder-Improved   文件: ZlibEncoder.java
@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[]{};
    }
}
 
源代码7 项目: dubbox   文件: BenchmarkRunner.java
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;
	}
}
 
源代码8 项目: FxDock   文件: CKit.java
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);
}
 
源代码9 项目: MiBandDecompiled   文件: r.java
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();
}
 
源代码10 项目: database   文件: RecordCompressor.java
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);

    }

}
 
源代码11 项目: MonitorClient   文件: ZLibUtil.java
/** 
 * 压缩  这是一个测试
 * @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();  
    }  
}
 
源代码12 项目: hadoop   文件: TestZlibCompressorDecompressor.java
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());
}
 
源代码13 项目: big-c   文件: TestZlibCompressorDecompressor.java
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());
}
 
源代码14 项目: incubator-hivemall   文件: ObjectUtils.java
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);
    }
}
 
源代码15 项目: incubator-hivemall   文件: ObjectUtils.java
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);
    }
}
 
源代码16 项目: hudi   文件: HoodieJsonPayload.java
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();
}
 
源代码17 项目: hudi   文件: TestRawTripPayload.java
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();
}
 
源代码18 项目: android-project-wo2b   文件: ZLibUtils.java
/**
 * 压缩
 * 
 * @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();
}
 
源代码19 项目: Slide   文件: Usernotes.java
/**
 * 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;
    }
}
 
源代码20 项目: database   文件: TestHuffmanEncoder.java
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();

}