List of usage examples for java.util.zip Deflater deflate
public int deflate(ByteBuffer output)
From source file:org.dragonet.net.ClientChunkManager.java
/** * Send a single chunk to the client/*www . j av a2 s . c o m*/ * * @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: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 . j a v a 2s . co 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.hyperic.hq.livedata.agent.commands.LiveData_result.java
private String compress(String s) throws IOException { Deflater compressor = new Deflater(); compressor.setInput(s.getBytes("UTF-8")); compressor.finish();//from ww w .j a v a 2 s . co m ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; while (!compressor.finished()) { int count = compressor.deflate(buf); bos.write(buf, 0, count); } bos.close(); byte[] compressedData = bos.toByteArray(); return Base64.encode(compressedData); }
From source file:com.ctriposs.r2.filter.compression.DeflateCompressor.java
@Override public byte[] deflate(InputStream data) throws CompressionException { byte[] input; try {/*from ww w . j a v a 2 s . c o m*/ input = IOUtils.toByteArray(data); } catch (IOException e) { throw new CompressionException(CompressionConstants.DECODING_ERROR + CompressionConstants.BAD_STREAM, e); } Deflater zlib = new Deflater(); zlib.setInput(input); zlib.finish(); ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] temp = new byte[CompressionConstants.BUFFER_SIZE]; int bytesRead; while (!zlib.finished()) { bytesRead = zlib.deflate(temp); if (bytesRead == 0) { if (!zlib.needsInput()) { throw new CompressionException(CompressionConstants.DECODING_ERROR + getContentEncodingName()); } else { break; } } output.write(temp, 0, bytesRead); } zlib.end(); return output.toByteArray(); }
From source file:net.fenyo.mail4hotspot.dns.Msg.java
private byte[] process(final AdvancedServices advancedServices, final Inet4Address address) throws GeneralException { // log.debug("processing message of type " + input_buffer[0]); // for (byte b : input_buffer) log.debug("received byte: [" + b + "]"); if (input_buffer.length == 0) throw new GeneralException("invalid size"); if (input_buffer[0] == 0) { // UTF-8 message type final ByteBuffer bb = ByteBuffer.allocate(input_buffer.length - 1); bb.put(input_buffer, 1, input_buffer.length - 1); bb.position(0);/*from w w w .j av a 2 s . co m*/ final String query = Charset.forName("UTF-8").decode(bb).toString(); // log.debug("RECEIVED query: [" + query + "]"); final String reply = advancedServices.processQueryFromClient(query, address); // this buffer may not be backed by an accessible byte array, so we do not use Charset.forName("UTF-8").encode(reply).array() to fill output_buffer final ByteBuffer ob = Charset.forName("UTF-8").encode(reply); ob.get(output_buffer = new byte[ob.limit()]); output_size = output_buffer.length; } else { // binary message type // log.debug("processing binary message"); final ByteBuffer bb = ByteBuffer.allocate(input_buffer[0]); bb.put(input_buffer, 1, input_buffer[0]); bb.position(0); final String query = Charset.forName("UTF-8").decode(bb).toString(); // log.debug("RECEIVED query: [" + query + "]"); final BinaryMessageReply reply = advancedServices.processBinaryQueryFromClient(query, Arrays.copyOfRange(input_buffer, input_buffer[0] + 1, input_buffer.length), address); // this buffer may not be backed by an accessible byte array, so we do not use Charset.forName("UTF-8").encode(reply).array() to fill string_part final ByteBuffer ob = Charset.forName("UTF-8").encode(reply.reply_string); final byte[] string_part = new byte[ob.limit()]; ob.get(string_part); if (string_part.length > 255) throw new GeneralException("string_part too long"); output_buffer = new byte[string_part.length + reply.reply_data.length + 1]; output_buffer[0] = (byte) string_part.length; for (int i = 0; i < string_part.length; i++) output_buffer[i + 1] = string_part[i]; for (int i = 0; i < reply.reply_data.length; i++) output_buffer[string_part.length + i + 1] = reply.reply_data[i]; output_size = output_buffer.length; } synchronized (compressed) { // http://docs.oracle.com/javase/7/docs/api/java/util/zip/Deflater.html#deflate(byte[]) // log.debug("processing binary message: length before compressing: " + output_buffer.length); final Deflater compresser = new Deflater(); compresser.setInput(output_buffer); compresser.finish(); final int nbytes = compresser.deflate(compressed); // log.debug("RET: " + nbytes); // log.debug("COMPRESSED: " + compressed.length); // log.debug("processing binary message: length after compressing: " + nbytes); if (compressed.length == nbytes) { log.error("compressed buffer too small..."); throw new GeneralException("compressed buffer too small..."); } output_buffer = Arrays.copyOf(compressed, nbytes); output_size = output_buffer.length; } synchronized (is_processed) { is_processed = true; } return new byte[] { 'E', 0 }; // 'E'rror 0 == OK }
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);//from w w w . j a va 2 s .co m try (OutputStream outputStream = new FileOutputStream(zipFile)) { 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); } } deflater.end(); return zipFile; }
From source file:Comman.Tool.java
public byte[] Image_compress(final byte[] data) { if (data == null || data.length == 0) { return new byte[0]; }//www. j a va2 s . c o m 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; } }
From source file:org.graylog.plugins.beats.BeatsFrameDecoderTest.java
private ChannelBuffer buildCompressedFrame(byte[] payload, int compressionLevel) { final Deflater deflater = new Deflater(compressionLevel); deflater.setInput(payload);//from www. j a va2s. c o m deflater.finish(); final byte[] compressedPayload = new byte[1024]; final int compressedPayloadLength = deflater.deflate(compressedPayload); deflater.end(); final ChannelBuffer buffer = ChannelBuffers.buffer(6 + compressedPayloadLength); buffer.writeByte('2'); buffer.writeByte('C'); // Compressed payload length buffer.writeInt(compressedPayloadLength); // Compressed payload buffer.writeBytes(compressedPayload, 0, compressedPayloadLength); return buffer; }
From source file:org.dragonet.proxy.protocol.packet.LoginPacket.java
@Override public void encode() { try {//from w w w . j ava 2s. c om setChannel(NetworkChannel.CHANNEL_PRIORITY); // Basic info JSONObject jsonBasic = new JSONObject(); String b64Signature; String b64User; { JSONObject jsonSignature = new JSONObject(); jsonSignature.put("alg", "ES384"); jsonSignature.put("x5u", "MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAE+SKWsy2I5ecBTPZEfNgwgvuin2/iqqi3haMxXPpknsVKINpp5E7jlygXqBmSJad5VG+1uq75V9irGEtmpUINP5xhYiOrlEma+2aBJIUe17UT/r50yTnDhDrPoOY/eAHL"); b64Signature = Base64.getEncoder().encodeToString(jsonSignature.toString().getBytes("UTF-8")); } { JSONObject jsonUser = new JSONObject(); jsonUser.put("exp", System.currentTimeMillis() / 1000L + (60 * 10)); JSONObject jsonUserInfo = new JSONObject(); jsonUserInfo.put("displayName", username); jsonUserInfo.put("identity", clientUuid.toString()); jsonUser.put("extraData", jsonUserInfo); jsonUser.put("identityPublicKey", ""); // publicKey jsonUser.put("nbf", System.currentTimeMillis() / 1000L); b64User = Base64.getEncoder().encodeToString(jsonUser.toString().getBytes("UTF-8")); System.out.println(jsonUser.toString()); } String b64Basic = b64Signature + "." + b64User; // Meta info JSONObject jsonMeta = new JSONObject(); String strMeta; { jsonMeta.put("ClientRandomId", clientID); jsonMeta.put("ServerAddress", serverAddress); jsonMeta.put("SkinId", skin.getModel()); jsonMeta.put("SkinData", Base64.getEncoder().encodeToString(skin.getData())); strMeta = Base64.getEncoder().encodeToString(jsonMeta.toString().getBytes("UTF-8")); } String b64Meta = b64Signature + "." + strMeta; byte[] chainData; { byte[] dataBasic = b64Basic.getBytes("UTF-8"); byte[] dataMeta = b64Meta.getBytes("UTF-8"); ByteArrayOutputStream bos = new ByteArrayOutputStream(); PEBinaryWriter writer = new PEBinaryWriter(bos); writer.switchEndianness(); writer.writeInt(dataBasic.length); writer.switchEndianness(); writer.write(dataBasic); writer.switchEndianness(); writer.writeInt(dataMeta.length); writer.switchEndianness(); writer.write(dataMeta); chainData = bos.toByteArray(); } JSONObject jsonChain = new JSONObject(); jsonChain.put("chain", chainData); String strChain = jsonChain.toString(); byte[] b64Chain = Base64.getEncoder().encode(strChain.getBytes("UTF-8")); Deflater deflater = new Deflater(7); deflater.setInput(b64Chain); deflater.finish(); byte[] buff = new byte[40960]; int deflated = deflater.deflate(buff); buff = ArrayUtils.subarray(buff, 0, deflated); ByteArrayOutputStream bos = new ByteArrayOutputStream(); PEBinaryWriter writer = new PEBinaryWriter(bos); writer.writeByte((byte) (this.pid() & 0xFF)); writer.writeInt(protocol); writer.writeInt(buff.length); writer.write(buff); this.setData(bos.toByteArray()); } catch (IOException | JSONException e) { } }
From source file:org.getspout.spout.packet.PacketBlockData.java
public void compress() { if (!compressed) { Deflater deflater = new Deflater(); deflater.setInput(data);//w ww. ja v a 2 s .c o m deflater.setLevel(Deflater.BEST_COMPRESSION); deflater.finish(); 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; } }