List of usage examples for io.netty.buffer ByteBuf writerIndex
public abstract int writerIndex();
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 {// w w w . j a v a 2s. c o m buf.release(); } }
From source file:io.grpc.alts.internal.ByteBufTestUtils.java
License:Apache License
static ByteBuf writeSlice(ByteBuf in, int len) { Preconditions.checkArgument(len <= in.writableBytes()); ByteBuf out = in.slice(in.writerIndex(), len); in.writerIndex(in.writerIndex() + len); return out.writerIndex(0); }
From source file:io.grpc.alts.internal.TsiTest.java
License:Apache License
/** Test corrupted ciphertext. */ public static void corruptedCiphertextTest(Handshakers handshakers, RegisterRef ref) throws GeneralSecurityException { performHandshake(DEFAULT_TRANSPORT_BUFFER_SIZE, handshakers); TsiFrameProtector sender = handshakers.getClient().createFrameProtector(alloc); TsiFrameProtector receiver = handshakers.getServer().createFrameProtector(alloc); String message = "hello world"; ByteBuf plaintextBuffer = Unpooled.wrappedBuffer(message.getBytes(UTF_8)); final List<ByteBuf> protectOut = new ArrayList<>(); List<Object> unprotectOut = new ArrayList<>(); sender.protectFlush(Collections.singletonList(plaintextBuffer), new Consumer<ByteBuf>() { @Override//from w w w .ja v a 2s . c o m public void accept(ByteBuf buf) { protectOut.add(buf); } }, alloc); assertThat(protectOut.size()).isEqualTo(1); ByteBuf protect = ref.register(protectOut.get(0)); int ciphertextIdx = protect.writerIndex() - FakeChannelCrypter.getTagBytes() - 2; protect.setByte(ciphertextIdx, protect.getByte(ciphertextIdx) + 1); try { receiver.unprotect(protect, unprotectOut, alloc); fail("Exception expected"); } catch (AEADBadTagException ex) { assertThat(ex).hasMessageThat().contains(DECRYPTION_FAILURE_RE); } sender.destroy(); receiver.destroy(); }
From source file:io.grpc.alts.internal.TsiTest.java
License:Apache License
/** Test corrupted tag. */ public static void corruptedTagTest(Handshakers handshakers, RegisterRef ref) throws GeneralSecurityException { performHandshake(DEFAULT_TRANSPORT_BUFFER_SIZE, handshakers); TsiFrameProtector sender = handshakers.getClient().createFrameProtector(alloc); TsiFrameProtector receiver = handshakers.getServer().createFrameProtector(alloc); String message = "hello world"; ByteBuf plaintextBuffer = Unpooled.wrappedBuffer(message.getBytes(UTF_8)); final List<ByteBuf> protectOut = new ArrayList<>(); List<Object> unprotectOut = new ArrayList<>(); sender.protectFlush(Collections.singletonList(plaintextBuffer), new Consumer<ByteBuf>() { @Override/* w ww . j a va 2 s . com*/ public void accept(ByteBuf buf) { protectOut.add(buf); } }, alloc); assertThat(protectOut.size()).isEqualTo(1); ByteBuf protect = ref.register(protectOut.get(0)); int tagIdx = protect.writerIndex() - 1; protect.setByte(tagIdx, protect.getByte(tagIdx) + 1); try { receiver.unprotect(protect, unprotectOut, alloc); fail("Exception expected"); } catch (AEADBadTagException ex) { assertThat(ex).hasMessageThat().contains(DECRYPTION_FAILURE_RE); } sender.destroy(); receiver.destroy(); }
From source file:io.grpc.netty.GrpcHpackDecoder.java
License:Apache License
/** * Unsigned Little Endian Base 128 Variable-Length Integer Encoding * <p>//from w ww . ja v a 2 s .c o 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.hekate.network.netty.ByteBufDataWriter.java
License:Apache License
public void setOut(ByteBuf out) { this.out = out; this.initSize = out != null ? out.writerIndex() : 0; }
From source file:io.hekate.network.netty.NetworkProtocolCodec.java
License:Apache License
private static void doEncode(Object msg, ByteBufDataWriter out, Codec<Object> codec) throws CodecException { ByteBuf buf = out.buffer(); try {//from w w w . j a va2 s .c o m // Header indexes. int headStartIdx = buf.writerIndex(); int headEndIdx = headStartIdx + HEADER_LENGTH; // Placeholder for the header. buf.ensureWritable(HEADER_LENGTH).writerIndex(headEndIdx); boolean internalMsg; if (msg instanceof NetworkProtocol) { // Encode internal message. internalMsg = true; NetworkProtocol netMsg = (NetworkProtocol) msg; encodeInternal(out, codec, netMsg); } else { // Encode user-defined message. internalMsg = false; codec.encode(msg, out); } // Calculate real message length. int len = buf.writerIndex() - headStartIdx; // Magic length value: // negative - for protocol messages // positive - for user messages if (internalMsg) { len = -len; } // Update length header. buf.setInt(headStartIdx, len); } catch (CodecException e) { throw e; } catch (Throwable t) { throw new CodecException("Failed to encode message [message=" + msg + ']', t); } }
From source file:io.horizondb.io.buffers.NettyBuffer.java
License:Apache License
/** * Creates a new <code>NettyBuffer</code> that wraps the specified <code>ByteBuf</code>. * /* ww w . jav a2 s . c o m*/ * @param buffer the <code>ByteBuf</code> */ NettyBuffer(ByteBuf buffer) { notNull(buffer, "the buffer parameter must not be null."); this.buffer = buffer; subRegion(0, buffer.capacity()); writerIndex(buffer.writerIndex()); }
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// www. ja va2s.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; }
From source file:io.jsync.http.impl.DefaultHttpClientRequest.java
License:Open Source License
private synchronized DefaultHttpClientRequest write(ByteBuf buff, boolean end) { int readableBytes = buff.readableBytes(); if (readableBytes == 0 && !end) { // nothing to write to the connection just return return this; }/*w w w. j ava 2 s . com*/ if (end) { completed = true; } written += buff.readableBytes(); if (!end && !raw && !chunked && !contentLengthSet()) { throw new IllegalStateException( "You must set the Content-Length header to be the total size of the message " + "body BEFORE sending any data if you are not using HTTP chunked encoding."); } if (conn == null) { if (pendingChunks == null) { pendingChunks = buff; } else { CompositeByteBuf pending; if (pendingChunks instanceof CompositeByteBuf) { pending = (CompositeByteBuf) pendingChunks; } else { pending = Unpooled.compositeBuffer(); pending.addComponent(pendingChunks).writerIndex(pendingChunks.writerIndex()); pendingChunks = pending; } pending.addComponent(buff).writerIndex(pending.writerIndex() + buff.writerIndex()); } connect(); } else { if (!headWritten) { writeHeadWithContent(buff, end); } else { if (end) { writeEndChunk(buff); } else { sendChunk(buff); } } if (end) { conn.endRequest(); } } return this; }