下面列出了java.util.zip.DeflaterOutputStream#flush ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
}
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;
}
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;
}
}
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;
}
}
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;
}
}
@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[]{};
}
}
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;
}
}
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);
}
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();
}
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);
}
}
/**
* 压缩 这是一个测试
* @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();
}
}
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());
}
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());
}
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);
}
}
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);
}
}
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();
}
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();
}
/**
* 压缩
*
* @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();
}
/**
* 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;
}
}
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();
}