List of usage examples for io.netty.buffer ByteBuf slice
public abstract ByteBuf slice(int index, int length);
From source file:io.datty.msgpack.core.ValueMessageReader.java
License:Apache License
public ByteBuf readBinary(ByteBuf source, boolean copy) { int length = readBinaryHeader(source); if (length > source.readableBytes()) { throw new MessageParseException( "insufficient buffer length: " + source.readableBytes() + ", required length: " + length); }//from ww w . j a v a 2s . c o m if (copy) { ByteBuf dst = source.alloc().buffer(length); source.readBytes(dst, length); return dst; } else { ByteBuf slice = source.slice(source.readerIndex(), length); source.skipBytes(length); return slice; } }
From source file:io.grpc.alts.internal.AltsChannelCrypter.java
License:Apache License
@Override public void encrypt(ByteBuf outBuf, List<ByteBuf> plainBufs) throws GeneralSecurityException { checkArgument(outBuf.nioBufferCount() == 1); // Copy plaintext buffers into outBuf for in-place encryption on single direct buffer. ByteBuf plainBuf = outBuf.slice(outBuf.writerIndex(), outBuf.writableBytes()); plainBuf.writerIndex(0);//from w ww . j av a 2s.c o m for (ByteBuf inBuf : plainBufs) { plainBuf.writeBytes(inBuf); } verify(outBuf.writableBytes() == plainBuf.readableBytes() + TAG_LENGTH); ByteBuffer out = outBuf.internalNioBuffer(outBuf.writerIndex(), outBuf.writableBytes()); ByteBuffer plain = out.duplicate(); plain.limit(out.limit() - TAG_LENGTH); byte[] counter = incrementOutCounter(); int outPosition = out.position(); aeadCrypter.encrypt(out, plain, counter); int bytesWritten = out.position() - outPosition; outBuf.writerIndex(outBuf.writerIndex() + bytesWritten); verify(!outBuf.isWritable()); }
From source file:io.grpc.alts.internal.AltsChannelCrypter.java
License:Apache License
@Override public void decrypt(ByteBuf out, ByteBuf tag, List<ByteBuf> ciphertextBufs) throws GeneralSecurityException { ByteBuf cipherTextAndTag = out.slice(out.writerIndex(), out.writableBytes()); cipherTextAndTag.writerIndex(0);/*w ww. j a va 2 s. c o m*/ for (ByteBuf inBuf : ciphertextBufs) { cipherTextAndTag.writeBytes(inBuf); } cipherTextAndTag.writeBytes(tag); decrypt(out, cipherTextAndTag); }
From source file:io.grpc.alts.internal.AltsTsiFrameProtector.java
License:Apache License
private static ByteBuf writeSlice(ByteBuf in, int len) { 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.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.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.moquette.parser.netty.TestUtils.java
License:Open Source License
/** * Verify the presence of the given string starting from the current position * inside the buffer./*w ww . java 2 s. c om*/ */ static void verifyString(String str, ByteBuf buff) throws UnsupportedEncodingException { ByteBuf tmpBuff = Unpooled.buffer(2); byte[] raw = str.getBytes("UTF-8"); tmpBuff.writeShort(raw.length); tmpBuff.writeBytes(raw); int buffLen = raw.length + 2; verifyByteBuf(tmpBuff, buff.slice(buff.readerIndex(), buffLen)); buff.skipBytes(buffLen); }
From source file:io.nebo.thrift.DefaultThriftFrameDecoder.java
License:Apache License
protected ByteBuf extractFrame(ByteBuf buffer, int index, int length) { // Slice should be sufficient here (and avoids the copy in LengthFieldBasedFrameDecoder) // because we know no one is going to modify the contents in the read buffers. return buffer.slice(index, length); }
From source file:io.nodyn.smalloc.Smalloc.java
License:Apache License
public static Object sliceOnto(JSObject src, JSObject dest, int start, int end) { ByteBuf srcBuf = ((NettyExternalIndexedData) src.getExternalIndexedData()).buffer(); int len = end - start; ByteBuf destBuf = srcBuf.slice(start, len); destBuf.writerIndex(0);/*from w ww . ja v a 2s . c o m*/ dest.setExternalIndexedData(new NettyExternalIndexedData(destBuf)); return src; }