List of usage examples for io.netty.buffer ByteBuf release
boolean release();
From source file:io.gatling.netty.util.ahc.Utf8ByteBufCharsetDecoderTest.java
License:Apache License
@Test void testByteBuf2BytesHasBackingArray() { byte[] inputBytes = "testdata".getBytes(US_ASCII); ByteBuf buf = Unpooled.wrappedBuffer(inputBytes); try {// w w w . jav a2 s . c om byte[] output = ByteBufUtils.byteBuf2Bytes(buf); assertArrayEquals(inputBytes, output); } finally { buf.release(); } }
From source file:io.gatling.netty.util.ahc.Utf8ByteBufCharsetDecoderTest.java
License:Apache License
@Test void testByteBuf2BytesNoBackingArray() { byte[] inputBytes = "testdata".getBytes(US_ASCII); ByteBuf buf = Unpooled.directBuffer(); try {/* w w w .jav a2 s .com*/ buf.writeBytes(inputBytes); byte[] output = ByteBufUtils.byteBuf2Bytes(buf); assertArrayEquals(inputBytes, output); } finally { buf.release(); } }
From source file:io.gatling.netty.util.ahc.Utf8ByteBufCharsetDecoderTest.java
License:Apache License
@Test void byteBufs2StringShouldBeAbleToDealWithCharsWithVariableBytesLength() { String inputString = ""; byte[] inputBytes = inputString.getBytes(UTF_8); for (int i = 1; i < inputBytes.length - 1; i++) { ByteBuf buf1 = Unpooled.wrappedBuffer(inputBytes, 0, i); ByteBuf buf2 = Unpooled.wrappedBuffer(inputBytes, i, inputBytes.length - i); try {//from w w w. j a v a 2s.c om String output = ByteBufUtils.byteBuf2String(UTF_8, buf1, buf2); assertEquals(inputString, output); } finally { buf1.release(); buf2.release(); } } }
From source file:io.gatling.netty.util.ahc.Utf8ByteBufCharsetDecoderTest.java
License:Apache License
@Test void byteBufs2StringShouldBeAbleToDealWithBrokenCharsTheSameWayAsJavaImpl() { String inputString = "foo bar"; byte[] inputBytes = inputString.getBytes(UTF_8); int droppedBytes = 1; for (int i = 1; i < inputBytes.length - 1 - droppedBytes; i++) { byte[] part1 = Arrays.copyOfRange(inputBytes, 0, i); byte[] part2 = Arrays.copyOfRange(inputBytes, i + droppedBytes, inputBytes.length); byte[] merged = new byte[part1.length + part2.length]; System.arraycopy(part1, 0, merged, 0, part1.length); System.arraycopy(part2, 0, merged, part1.length, part2.length); ByteBuf buf1 = Unpooled.wrappedBuffer(part1); ByteBuf buf2 = Unpooled.wrappedBuffer(part2); try {//w ww .j a va 2s. c o m String output = ByteBufUtils.byteBuf2String(UTF_8, buf1, buf2); String javaString = new String(merged, UTF_8); assertNotEquals(inputString, output); assertEquals(javaString, output); } finally { buf1.release(); buf2.release(); } } }
From source file:io.github.lxgaming.silentboss.listeners.SilentBossPacketHandler.java
License:Apache License
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) { ByteBuf byteBuf = null; try {/*from w w w .j av a 2 s . c om*/ if (!ByteBuf.class.isAssignableFrom(msg.getClass())) { super.write(ctx, msg, promise); return; } byteBuf = ((ByteBuf) msg).copy(); PacketBuffer packetBuffer = new PacketBuffer(byteBuf); int packetId = packetBuffer.readVarIntFromBuffer(); if (packetId != 33) { super.write(ctx, msg, promise); return; } int effectId = packetBuffer.readInt(); if (effectId == 1028 && SilentBoss.getInstance().getConfiguration().getConfig() .get("SilenceEnderDragon").getAsBoolean()) { process("EnderDragon"); return; } if (effectId == 1023 && SilentBoss.getInstance().getConfiguration().getConfig().get("SilenceWither") .getAsBoolean()) { process("Wither"); return; } super.write(ctx, msg, promise); } catch (Exception ex) { LogManager.error("Exception in SilentBossPacketHandler!"); ex.printStackTrace(); } finally { if (byteBuf != null) { byteBuf.release(); byteBuf = null; } } return; }
From source file:io.gomint.proxprox.network.AbstractConnection.java
License:BSD License
/** * Handles compressed batch packets directly by decoding their payload. * * @param buffer The buffer containing the batch packet's data (except packet ID) * @return decompressed and decrypted data *//*from w w w .jav a 2 s .c o m*/ byte[] handleBatchPacket(PacketBuffer buffer) { // Encrypted? byte[] input = new byte[buffer.getRemaining()]; System.arraycopy(buffer.getBuffer(), buffer.getPosition(), input, 0, input.length); if (this.encryptionHandler != null) { input = this.encryptionHandler.isEncryptionFromServerEnabled() ? this.encryptionHandler.decryptInputFromServer(input) : this.encryptionHandler.decryptInputFromClient(input); if (input == null) { // Decryption error disconnect("Checksum of encrypted packet was wrong"); return null; } } ByteBuf inBuf = PooledByteBufAllocator.DEFAULT.directBuffer(input.length); inBuf.writeBytes(input); ByteBuf outBuf = PooledByteBufAllocator.DEFAULT.directBuffer(8192); // We will write at least once so ensureWrite will realloc to 8192 so or so try { this.decompressor.process(inBuf, outBuf); } catch (DataFormatException e) { LOGGER.error("Failed to decompress batch packet", e); outBuf.release(); return null; } finally { inBuf.release(); } byte[] data = new byte[outBuf.readableBytes()]; outBuf.readBytes(data); outBuf.release(); return data; }
From source file:io.gomint.proxprox.network.EncryptionHandler.java
License:BSD License
private byte[] calcHash(byte[] input, byte[] key, AtomicLong counter) { Hash digest = getSHA256();//from w w w . j ava2 s.co m if (digest == null) { return new byte[8]; } ByteBuf buf = PooledByteBufAllocator.DEFAULT.directBuffer(8 + input.length + key.length); buf.writeLongLE(counter.getAndIncrement()); buf.writeBytes(input); buf.writeBytes(key); digest.update(buf); buf.release(); return digest.digest(); }
From source file:io.gomint.proxprox.network.EncryptionHandler.java
License:BSD License
private byte[] hashSHA256(byte[]... message) { Hash digest = getSHA256();/*from w w w .j a va 2 s. com*/ if (digest == null) { return null; } ByteBuf buf = PooledByteBufAllocator.DEFAULT.directBuffer(); for (byte[] bytes : message) { buf.writeBytes(bytes); } digest.update(buf); buf.release(); return digest.digest(); }
From source file:io.gomint.proxprox.network.tcp.PacketDecompressor.java
License:BSD License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { int size = in.readInt(); if (size == 0) { out.add(in.slice().retain());//from w w w . j a v a2s .c om in.skipBytes(in.readableBytes()); } else { ByteBuf decompressed = ctx.alloc().directBuffer(); try { zlib.process(in, decompressed); Preconditions.checkState(decompressed.readableBytes() == size, "Decompressed packet size mismatch"); out.add(decompressed); decompressed = null; } finally { if (decompressed != null) { decompressed.release(); } } } }
From source file:io.grpc.alts.internal.BufUnwrapperTest.java
License:Apache License
@Test public void closeEmptiesBuffers() { BufUnwrapper unwrapper = new BufUnwrapper(); ByteBuf buf = alloc.buffer(); try {/*from w ww .j a v a 2 s . c om*/ ByteBuffer[] readableBufs = unwrapper.readableNioBuffers(buf); ByteBuffer[] writableBufs = unwrapper.writableNioBuffers(buf); Truth.assertThat(readableBufs).hasLength(1); Truth.assertThat(readableBufs[0]).isNotNull(); Truth.assertThat(writableBufs).hasLength(1); Truth.assertThat(writableBufs[0]).isNotNull(); unwrapper.close(); Truth.assertThat(readableBufs[0]).isNull(); Truth.assertThat(writableBufs[0]).isNull(); } finally { buf.release(); } }