List of usage examples for java.util.zip Deflater setInput
public void setInput(ByteBuffer input)
From source file:org.getspout.spout.packet.PacketBlockData.java
public void compress() { if (!compressed) { Deflater deflater = new Deflater(); deflater.setInput(data); deflater.setLevel(Deflater.BEST_COMPRESSION); deflater.finish();/*from ww w .j a v a2 s . c o m*/ ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length); byte[] buffer = new byte[1024]; while (!deflater.finished()) { int bytesCompressed = deflater.deflate(buffer); bos.write(buffer, 0, bytesCompressed); } try { bos.close(); } catch (IOException e) { e.printStackTrace(); } data = bos.toByteArray(); compressed = true; } }
From source file:org.sonar.microbenchmark.SerializationBenchmarkTest.java
private File zipFile(File input) throws Exception { File zipFile = new File(input.getAbsolutePath() + ".zip"); Deflater deflater = new Deflater(); byte[] content = FileUtils.readFileToByteArray(input); deflater.setInput(content); try (OutputStream outputStream = new FileOutputStream(zipFile)) { deflater.finish();/* w w w.j ava 2s .c o m*/ byte[] buffer = new byte[1024]; while (!deflater.finished()) { int count = deflater.deflate(buffer); // returns the generated code... index outputStream.write(buffer, 0, count); } } deflater.end(); return zipFile; }
From source file:com.moesol.keys.EncodeUidsTest.java
public void testCompress() { StringBuilder sb = new StringBuilder(); for (int i = 0; i < 10000; i++) { UUID id = UUID.randomUUID(); sb.append(id);/*from w ww . ja v a2 s .com*/ sb.append(','); } String result = sb.toString(); // System.out.println("val=" + result); Deflater deflate = new Deflater(); try { byte[] compressed = new byte[512000]; deflate.setInput(result.getBytes()); deflate.finish(); System.out.printf("in=%d out=%d%n", deflate.getBytesRead(), deflate.getBytesWritten()); deflate.deflate(compressed); System.out.printf("in=%d out=%d%n", deflate.getBytesRead(), deflate.getBytesWritten()); } finally { deflate.end(); } }
From source file:org.getspout.spout.packet.PacketCacheFile.java
public void compress() { if (!compressed) { Deflater deflater = new Deflater(); deflater.setInput(fileData); deflater.setLevel(Deflater.BEST_COMPRESSION); deflater.finish();//from w w w .j av a2 s .c o m ByteArrayOutputStream bos = new ByteArrayOutputStream(fileData.length); byte[] buffer = new byte[1024]; while (!deflater.finished()) { int bytesCompressed = deflater.deflate(buffer); bos.write(buffer, 0, bytesCompressed); } try { bos.close(); } catch (IOException e) { e.printStackTrace(); } fileData = bos.toByteArray(); compressed = true; } }
From source file:org.getspout.spoutapi.packet.PacketCustomBlockChunkOverride.java
@Override public void compress() { if (!compressed) { if (data != null && hasData) { Deflater deflater = new Deflater(); deflater.setInput(data); deflater.setLevel(Deflater.BEST_COMPRESSION); deflater.finish();/*from w ww . ja v a2 s .c o m*/ ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length); byte[] buffer = new byte[1024]; while (!deflater.finished()) { int bytesCompressed = deflater.deflate(buffer); bos.write(buffer, 0, bytesCompressed); } try { bos.close(); } catch (IOException e) { e.printStackTrace(); } data = bos.toByteArray(); } compressed = true; } }
From source file:org.getspout.spoutapi.packet.PacketAddonData.java
@Override public void compress() { if (!compressed) { if (data != null) { Deflater deflater = new Deflater(); deflater.setInput(data); deflater.setLevel(Deflater.BEST_COMPRESSION); deflater.finish();/*from ww w . j a v a 2 s. c o m*/ ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length); byte[] buffer = new byte[1024]; while (!deflater.finished()) { int bytesCompressed = deflater.deflate(buffer); bos.write(buffer, 0, bytesCompressed); } try { bos.close(); } catch (IOException e) { e.printStackTrace(); } data = bos.toByteArray(); } compressed = true; } }
From source file:de.triology.cas.logout.LogoutUriEnabledLogoutManagerImpl.java
/** * Create a logout message for front channel logout. * * @param logoutRequest the logout request. * @return a front SAML logout message.//from w w w .jav a 2 s . c o m */ public String createFrontChannelLogoutMessage(final LogoutRequest logoutRequest) { final String logoutMessage = this.logoutMessageBuilder.create(logoutRequest); final Deflater deflater = new Deflater(); deflater.setInput(logoutMessage.getBytes(ASCII)); deflater.finish(); final byte[] buffer = new byte[logoutMessage.length()]; final int resultSize = deflater.deflate(buffer); final byte[] output = new byte[resultSize]; System.arraycopy(buffer, 0, output, 0, resultSize); return Base64.encodeBase64String(output); }
From source file:org.getspout.spout.packet.PacketAddonData.java
public void compress() { if (!compressed) { if (data != null) { Deflater deflater = new Deflater(); deflater.setInput(data); deflater.setLevel(Deflater.BEST_COMPRESSION); deflater.finish();/* ww w. jav a2 s .c o m*/ ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length); byte[] buffer = new byte[1024]; while (!deflater.finished()) { int bytesCompressed = deflater.deflate(buffer); bos.write(buffer, 0, bytesCompressed); } try { bos.close(); } catch (IOException e) { e.printStackTrace(); } data = bos.toByteArray(); } compressed = true; } }
From source file:org.dragonet.net.ClientChunkManager.java
/** * Send a single chunk to the client/* w w w.j a va 2 s . c om*/ * * @param chunkX The chunk X coordinate * @param chunkZ The chunk Z coordinate */ private synchronized void sendChunk(int chunkX, int chunkZ) { try { GlowChunkSnapshot chunk = this.getSession().getPlayer().getWorld().getChunkAt(chunkX, chunkZ) .getChunkSnapshot(); ByteArrayOutputStream totalData = new ByteArrayOutputStream(); PEBinaryWriter writer = new PEBinaryWriter(totalData); if (writer.getEndianness() == PEBinaryUtils.BIG_ENDIAN) { writer.switchEndianness(); } writer.writeInt(chunkX); writer.writeInt(chunkZ); for (int x = 0; x < 16; x++) { for (int z = 0; z < 16; z++) { for (int y = 0; y < 128; y++) { writer.writeByte((byte) (this.getSession().getTranslator() .translateBlockToPE(chunk.getBlockTypeId(x, y, z)) & 0xFF)); } } } writer.write(new byte[16384]); for (int i = 0; i < 16384; i++) { writer.writeByte((byte) 0xF0); } for (int i = 0; i < 16384; i++) { writer.writeByte((byte) 0x11); } for (int i = 0; i < 256; i++) { writer.writeByte((byte) 0x00); } for (int i = 0; i < 256; i++) { writer.writeByte((byte) 0x00); writer.writeByte((byte) 0x85); writer.writeByte((byte) 0xB2); writer.writeByte((byte) 0x4A); } Deflater deflater = new Deflater(2); deflater.reset(); deflater.setInput(totalData.toByteArray()); deflater.finish(); byte[] bufferDeflate = new byte[65536]; int deflatedSize = deflater.deflate(bufferDeflate); FullChunkPacket packet = new FullChunkPacket(); packet.compressedData = ArrayUtils.subarray(bufferDeflate, 0, deflatedSize); this.getSession().send(packet); } catch (IOException e) { } }
From source file:Comman.Tool.java
public byte[] Image_compress(final byte[] data) { if (data == null || data.length == 0) { return new byte[0]; }/* ww w . ja v a 2s.com*/ try (final ByteArrayOutputStream out = new ByteArrayOutputStream(data.length)) { final Deflater deflater = new Deflater(); deflater.setInput(data); deflater.finish(); final byte[] buffer = new byte[1024]; while (!deflater.finished()) { out.write(buffer, 0, deflater.deflate(buffer)); } return out.toByteArray(); } catch (final IOException e) { System.err.println("Compression failed! Returning the original data..."); return data; } }