List of usage examples for java.util.zip Deflater finished
public boolean finished()
From source file:Main.java
License:asdf
public static void main(String[] argv) throws Exception { byte[] input = "asdf".getBytes(); Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); compressor.setInput(input);//from www. j a v a 2 s. c om compressor.finish(); ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length); byte[] buf = new byte[1024]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } bos.close(); byte[] compressedData = bos.toByteArray(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { byte[] input = "www.java2s.com".getBytes(); Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); compressor.setInput(input);//w w w .jav a 2 s . co m compressor.finish(); ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length); byte[] buf = new byte[1024]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } bos.close(); byte[] compressedData = bos.toByteArray(); System.out.println(Arrays.toString(compressedData)); }
From source file:Main.java
public static void main(String[] argv) throws Exception { byte[] input = "this is a test".getBytes(); Deflater compressor = new Deflater(); compressor.setLevel(Deflater.BEST_COMPRESSION); compressor.setInput(input);// w ww . j a v a 2 s . com compressor.finish(); ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length); byte[] buf = new byte[1024]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } bos.close(); byte[] compressedData = bos.toByteArray(); Inflater decompressor = new Inflater(); decompressor.setInput(compressedData); bos = new ByteArrayOutputStream(compressedData.length); buf = new byte[1024]; while (!decompressor.finished()) { int count = decompressor.inflate(buf); bos.write(buf, 0, count); } bos.close(); byte[] decompressedData = bos.toByteArray(); System.out.println(new String(decompressedData)); }
From source file:Main.java
public static byte[] compress(byte[] data, int level) throws IOException { if (data == null || data.length == 0) { return data; }//from w ww . java2 s .c om ByteArrayOutputStream bout = new ByteArrayOutputStream(data.length); Deflater deflater = new Deflater(); deflater.setLevel(level); deflater.setInput(data); deflater.finish(); byte[] buf = new byte[BUFFER_SIZE]; while (!deflater.finished()) { int count = deflater.deflate(buf); bout.write(buf, 0, count); } deflater.end(); bout.close(); return bout.toByteArray(); }
From source file:Main.java
public static byte[] compress(byte[] data) throws IOException { Deflater deflater = new Deflater(); deflater.setInput(data);/*from ww w .ja v a 2 s . com*/ ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); deflater.finish(); byte[] buffer = new byte[1024]; while (!deflater.finished()) { int count = deflater.deflate(buffer); // returns the generated code... index outputStream.write(buffer, 0, count); } outputStream.close(); byte[] output = outputStream.toByteArray(); System.out.println("Original: " + data.length / 1024 + " Kb"); System.out.println("Compressed: " + output.length / 1024 + " Kb"); return output; }
From source file:Main.java
private static byte[] compress(byte[] data) throws IOException { Deflater deflater = new Deflater(); deflater.setInput(data);// w w w.j a va 2 s.c o m ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); deflater.finish(); byte[] buffer = new byte[1024]; while (!deflater.finished()) { int count = deflater.deflate(buffer); // returns the generated code... index outputStream.write(buffer, 0, count); } outputStream.close(); byte[] output = outputStream.toByteArray(); deflater.end(); // System.out.println("Original: " + data.length + " bytes."); // System.out.println("Compressed: " + output.length + " bytes."); return output; }
From source file:Main.java
public static byte[] compress(byte[] input, int compressionLevel, boolean GZIPFormat) throws IOException { Deflater compressor = new Deflater(compressionLevel, GZIPFormat); compressor.setInput(input);//from w w w. j ava2 s .c o m compressor.finish(); ByteArrayOutputStream bao = new ByteArrayOutputStream(); byte[] readBuffer = new byte[1024]; int readCount = 0; while (!compressor.finished()) { readCount = compressor.deflate(readBuffer); if (readCount > 0) { bao.write(readBuffer, 0, readCount); } } compressor.end(); return bao.toByteArray(); }
From source file:Main.java
private static byte[] compressBytesInflateDeflate(byte[] inBytes) { Deflater deflater = new Deflater(Deflater.BEST_SPEED); deflater.setInput(inBytes);//w w w .jav a2 s. c o m ByteArrayOutputStream bos = new ByteArrayOutputStream(inBytes.length); deflater.finish(); byte[] buffer = new byte[1024 * 8]; while (!deflater.finished()) { int count = deflater.deflate(buffer); bos.write(buffer, 0, count); } byte[] output = bos.toByteArray(); return output; }
From source file:Main.java
public static byte[] compress(byte[] data) throws IOException { Deflater compresser = new Deflater(Deflater.BEST_COMPRESSION); compresser.setInput(data);/*from w w w . j ava 2s .co m*/ ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length); compresser.finish(); byte[] buffer = new byte[1024]; while (!compresser.finished()) { int count = compresser.deflate(buffer); outputStream.write(buffer, 0, count); } compresser.end(); outputStream.close(); return outputStream.toByteArray(); }
From source file:Main.java
public static byte[] compressInZlib(byte[] originalData, int offset, int length) throws IOException { Deflater compresser = new Deflater(); compresser.setInput(originalData, offset, length); compresser.finish();/* w ww.j a v a2 s. c om*/ ByteArrayOutputStream bos = new ByteArrayOutputStream(length); int count; byte[] buf = new byte[1024]; while (!compresser.finished()) { count = compresser.deflate(buf); bos.write(buf, 0, count); } compresser.end(); byte[] compressData = bos.toByteArray(); bos.close(); return compressData; }