List of usage examples for io.netty.buffer ByteBuf readerIndex
public abstract int readerIndex();
From source file:io.grpc.alts.internal.BufUnwrapperTest.java
License:Apache License
@Test public void writableNioBuffers_indexesPreserved() { ByteBuf buf = alloc.buffer(1); int ridx = buf.readerIndex(); int widx = buf.writerIndex(); int cap = buf.capacity(); try (BufUnwrapper unwrapper = new BufUnwrapper()) { ByteBuffer[] internalBufs = unwrapper.writableNioBuffers(buf); Truth.assertThat(internalBufs).hasLength(1); internalBufs[0].put((byte) 'a'); assertEquals(ridx, buf.readerIndex()); assertEquals(widx, buf.writerIndex()); assertEquals(cap, buf.capacity()); } finally {//from w w w . j a va 2s . co m buf.release(); } }
From source file:io.grpc.alts.internal.ChannelCrypterNettyTestBase.java
License:Apache License
FrameDecrypt frameDecryptOfEncrypt(FrameEncrypt frameEncrypt) { int tagLen = client.getSuffixLength(); FrameDecrypt frameDecrypt = new FrameDecrypt(); ByteBuf out = frameEncrypt.out; frameDecrypt.ciphertext = Collections .singletonList(out.slice(out.readerIndex(), out.readableBytes() - tagLen)); frameDecrypt.tag = out.slice(out.readerIndex() + out.readableBytes() - tagLen, tagLen); frameDecrypt.out = getDirectBuffer(out.readableBytes(), ref); return frameDecrypt; }
From source file:io.grpc.alts.internal.ChannelCrypterNettyTestBase.java
License:Apache License
@Test public void encryptDecryptComposite() throws GeneralSecurityException { String message = "Hello world"; int lastLen = 2; byte[] messageBytes = message.getBytes(UTF_8); FrameEncrypt frameEncrypt = new FrameEncrypt(); ByteBuf plain1 = getDirectBuffer(messageBytes.length - lastLen, ref); ByteBuf plain2 = getDirectBuffer(lastLen, ref); plain1.writeBytes(messageBytes, 0, messageBytes.length - lastLen); plain2.writeBytes(messageBytes, messageBytes.length - lastLen, lastLen); ByteBuf plain = Unpooled.wrappedBuffer(plain1, plain2); frameEncrypt.plain = Collections.singletonList(plain); frameEncrypt.out = getDirectBuffer(messageBytes.length + client.getSuffixLength(), ref); client.encrypt(frameEncrypt.out, frameEncrypt.plain); int tagLen = client.getSuffixLength(); FrameDecrypt frameDecrypt = new FrameDecrypt(); ByteBuf out = frameEncrypt.out; int outLen = out.readableBytes(); ByteBuf cipher1 = getDirectBuffer(outLen - lastLen - tagLen, ref); ByteBuf cipher2 = getDirectBuffer(lastLen, ref); cipher1.writeBytes(out, 0, outLen - lastLen - tagLen); cipher2.writeBytes(out, outLen - tagLen - lastLen, lastLen); ByteBuf cipher = Unpooled.wrappedBuffer(cipher1, cipher2); frameDecrypt.ciphertext = Collections.singletonList(cipher); frameDecrypt.tag = out.slice(out.readerIndex() + out.readableBytes() - tagLen, tagLen); frameDecrypt.out = getDirectBuffer(out.readableBytes(), ref); server.decrypt(frameDecrypt.out, frameDecrypt.tag, frameDecrypt.ciphertext); assertThat(frameEncrypt.plain.get(0).slice(0, frameDecrypt.out.readableBytes())) .isEqualTo(frameDecrypt.out); }
From source file:io.grpc.netty.GrpcHpackDecoder.java
License:Apache License
/** * Unsigned Little Endian Base 128 Variable-Length Integer Encoding * <p>/* w w w. j a v a2 s .co m*/ * Visible for testing only! */ static int decodeULE128(ByteBuf in, int result) throws Http2Exception { final int readerIndex = in.readerIndex(); final long v = decodeULE128(in, (long) result); if (v > Integer.MAX_VALUE) { // the maximum value that can be represented by a signed 32 bit number is: // [0x1,0x7f] + 0x7f + (0x7f << 7) + (0x7f << 14) + (0x7f << 21) + (0x6 << 28) // OR // 0x0 + 0x7f + (0x7f << 7) + (0x7f << 14) + (0x7f << 21) + (0x7 << 28) // we should reset the readerIndex if we overflowed the int type. in.readerIndex(readerIndex); throw DECODE_ULE_128_TO_INT_DECOMPRESSION_EXCEPTION; } return (int) v; }
From source file:io.grpc.netty.GrpcHpackDecoder.java
License:Apache License
/** * Unsigned Little Endian Base 128 Variable-Length Integer Encoding * <p>/*from ww w . j a v a2 s . co m*/ * Visible for testing only! */ static long decodeULE128(ByteBuf in, long result) throws Http2Exception { assert result <= 0x7f && result >= 0; final boolean resultStartedAtZero = result == 0; final int writerIndex = in.writerIndex(); for (int readerIndex = in.readerIndex(), shift = 0; readerIndex < writerIndex; ++readerIndex, shift += 7) { byte b = in.getByte(readerIndex); if (shift == 56 && (((b & 0x80) != 0 || b == 0x7F) && !resultStartedAtZero)) { // the maximum value that can be represented by a signed 64 bit number is: // [0x01L, 0x7fL] + 0x7fL + (0x7fL << 7) + (0x7fL << 14) + (0x7fL << 21) + (0x7fL << 28) + (0x7fL << 35) // + (0x7fL << 42) + (0x7fL << 49) + (0x7eL << 56) // OR // 0x0L + 0x7fL + (0x7fL << 7) + (0x7fL << 14) + (0x7fL << 21) + (0x7fL << 28) + (0x7fL << 35) + // (0x7fL << 42) + (0x7fL << 49) + (0x7fL << 56) // this means any more shifts will result in overflow so we should break out and throw an error. throw DECODE_ULE_128_TO_LONG_DECOMPRESSION_EXCEPTION; } if ((b & 0x80) == 0) { in.readerIndex(readerIndex + 1); return result + ((b & 0x7FL) << shift); } result += (b & 0x7FL) << shift; } throw DECODE_ULE_128_DECOMPRESSION_EXCEPTION; }
From source file:io.grpc.netty.GrpcHpackHuffmanDecoder.java
License:Apache License
/** * Decompresses the given Huffman coded string literal. * * @param buf the string literal to be decoded * @return the output stream for the compressed data * @throws Http2Exception EOS Decoded//from ww w. ja v a 2 s . co m */ public AsciiString decode(ByteBuf buf, int length) throws Http2Exception { processor.reset(); buf.forEachByte(buf.readerIndex(), length, processor); buf.skipBytes(length); return processor.end(); }
From source file:io.hekate.network.netty.NettyMessage.java
License:Apache License
static String utf(ByteBuf buf) throws IOException { try {//from w w w.jav a 2s . co m int len = buf.readInt(); String string = buf.toString(buf.readerIndex(), len, StandardCharsets.UTF_8); buf.skipBytes(len); return string; } catch (IndexOutOfBoundsException e) { throw endOfStream(e); } }
From source file:io.hekate.network.netty.NettyMessageTest.java
License:Apache License
@Test public void testPreview() throws Exception { ByteBuf buf = Unpooled.buffer(); buf.writeByte(10);//from w ww . ja v a2 s .c o m buf.writeBoolean(true); buf.writeInt(100); buf.writeLong(1000); buf.writeDouble(1.01); NettyMessage msg = new NettyMessage(buf, new Codec<Object>() { @Override public boolean isStateful() { return false; } @Override public Class<Object> baseType() { return Object.class; } @Override public Object decode(DataReader in) throws IOException { throw new AssertionError("Should not be called."); } @Override public void encode(Object obj, DataWriter out) throws IOException { throw new AssertionError("Should not be called."); } }); assertEquals((byte) 10, (byte) msg.preview(DataInput::readByte)); assertEquals(0, buf.readerIndex()); assertTrue(msg.previewBoolean(m -> { m.skipBytes(1); return m.readBoolean(); })); assertEquals(0, buf.readerIndex()); assertEquals(100, msg.previewInt(m -> { m.skipBytes(2); return m.readInt(); })); assertEquals(0, buf.readerIndex()); assertEquals(1000, msg.previewLong(m -> { m.skipBytes(6); return m.readLong(); })); assertEquals(0, buf.readerIndex()); assertEquals(1.01, msg.previewDouble(m -> { m.skipBytes(14); return m.readDouble(); }), 1000); assertEquals(0, buf.readerIndex()); assertEquals(1, buf.refCnt()); msg.release(); assertEquals(0, buf.refCnt()); }
From source file:io.hydramq.core.type.converters.StringConverter.java
License:Open Source License
@Override public String read(final ConversionContext context, final ByteBuf buffer) { int size = buffer.readInt(); String value = buffer.toString(buffer.readerIndex(), size, CharsetUtil.UTF_8); buffer.skipBytes(size);// ww w . java 2s. c o m return value; }
From source file:io.jsync.dns.impl.netty.decoder.TextDecoder.java
License:Open Source License
/** * Returns a decoded TXT (text) resource record, stored as an * {@link ArrayList} of {@code String}s. * * @param response the DNS response that contains the resource record being * decoded//from w w w . ja v a2 s . c o m * @param resource the resource record being decoded */ @Override public List<String> decode(DnsResponse response, DnsResource resource) { List<String> list = new ArrayList<String>(); ByteBuf data = resource.content().readerIndex(response.originalIndex()); int index = data.readerIndex(); while (index < data.writerIndex()) { int len = data.getUnsignedByte(index++); list.add(data.toString(index, len, CharsetUtil.UTF_8)); index += len; } return list; }