List of usage examples for io.netty.buffer ByteBuf nioBuffer
public abstract ByteBuffer nioBuffer();
From source file:alluxio.client.block.stream.GrpcDataWriter.java
License:Apache License
@Override public void writeChunk(final ByteBuf buf) throws IOException { mPosToQueue += buf.readableBytes();/*from w w w.j ava2 s . c o m*/ try { WriteRequest request = WriteRequest.newBuilder().setCommand(mPartialRequest) .setChunk(Chunk.newBuilder().setData(UnsafeByteOperations.unsafeWrap(buf.nioBuffer())).build()) .build(); if (mStream instanceof GrpcDataMessageBlockingStream) { ((GrpcDataMessageBlockingStream<WriteRequest, WriteResponse>) mStream) .sendDataMessage(new DataMessage<>(request, new NettyDataBuffer(buf)), mDataTimeoutMs); } else { mStream.send(request, mDataTimeoutMs); } } finally { buf.release(); } }
From source file:alluxio.network.protocol.databuffer.DataNettyBuffer.java
License:Apache License
/** * Constructor for creating a DataNettyBuffer, by passing a Netty ByteBuf. * This way we avoid one copy from ByteBuf to another ByteBuffer, * and making sure the buffer would not be recycled. * IMPORTANT: {@link #release()} must be called after * reading is finished. Otherwise the memory space for the ByteBuf might never be reclaimed. * * @param bytebuf The ByteBuf having the data * @param length The length of the underlying ByteBuffer data *//*from w ww .j a v a2s .c o m*/ public DataNettyBuffer(ByteBuf bytebuf, long length) { // throws exception if there are multiple nioBuffers, or reference count is not 1 Preconditions.checkArgument(bytebuf.nioBufferCount() == 1, "Number of nioBuffers of this bytebuf is %s (1 expected).", bytebuf.nioBufferCount()); Preconditions.checkArgument(bytebuf.refCnt() == 1, "Reference count of this bytebuf is %s (1 expected).", bytebuf.refCnt()); // increase the bytebuf reference count so it would not be recycled by Netty bytebuf.retain(); mNettyBuf = bytebuf; mBuffer = bytebuf.nioBuffer(); mLength = length; }
From source file:alluxio.worker.block.UnderFileSystemBlockReaderTest.java
License:Apache License
@Test public void transferFullBlock() throws Exception { mReader = UnderFileSystemBlockReader.create(mUnderFileSystemBlockMeta, 0, false, mAlluxioBlockStore); ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer((int) TEST_BLOCK_SIZE * 2, (int) TEST_BLOCK_SIZE * 2); try {//from w w w .jav a2 s . co m while (buf.writableBytes() > 0 && mReader.transferTo(buf) != -1) { } Assert.assertTrue(BufferUtils.equalIncreasingByteBuffer((int) TEST_BLOCK_SIZE, (int) TEST_BLOCK_SIZE, buf.nioBuffer())); mReader.close(); } finally { buf.release(); } }
From source file:alluxio.worker.block.UnderFileSystemBlockReaderTest.java
License:Apache License
@Test public void transferPartialBlock() throws Exception { mReader = UnderFileSystemBlockReader.create(mUnderFileSystemBlockMeta, 0, false, mAlluxioBlockStore); ByteBuf buf = PooledByteBufAllocator.DEFAULT.buffer((int) TEST_BLOCK_SIZE / 2, (int) TEST_BLOCK_SIZE / 2); try {/* w w w . j a v a 2 s .c om*/ while (buf.writableBytes() > 0 && mReader.transferTo(buf) != -1) { } Assert.assertTrue(BufferUtils.equalIncreasingByteBuffer((int) TEST_BLOCK_SIZE, (int) TEST_BLOCK_SIZE / 2, buf.nioBuffer())); mReader.close(); } finally { buf.release(); } }
From source file:c5db.codec.ProtostuffDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { ByteBufferInput input = new ByteBufferInput(in.nioBuffer(), false); T newMsg = schema.newMessage();// w ww . j av a2 s . c o m schema.mergeFrom(input, newMsg); out.add(newMsg); }
From source file:c5db.codec.UdpProtostuffDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, DatagramPacket dgram, List<Object> out) throws Exception { ByteBuf msg = dgram.content(); ByteBufferInput input = new ByteBufferInput(msg.nioBuffer(), protostuffEncoded); T newMsg = schema.newMessage();/*from w ww . jav a2 s. c o m*/ schema.mergeFrom(input, newMsg); out.add(newMsg); }
From source file:c5db.control.ByteBufInput.java
License:Apache License
public void transferByteRangeTo(Output output, boolean utf8String, int fieldNumber, boolean repeated) throws IOException { final int length = readRawVarint32(); if (length < 0) { throw negativeSize(); }// w ww. j ava 2 s . c om if (utf8String) { // if it is a UTF string, we have to call the writeByteRange. if (buffer.hasArray()) { output.writeByteRange(true, fieldNumber, buffer.array(), buffer.arrayOffset() + buffer.readerIndex(), length, repeated); buffer.skipBytes(length); } else { byte[] bytes = new byte[length]; buffer.readBytes(bytes); output.writeByteRange(true, fieldNumber, bytes, 0, bytes.length, repeated); } } else { // Do the potentially vastly more efficient potential splice call. if (!buffer.isReadable(length)) { throw misreportedSize(); } ByteBuf dup = buffer.slice(buffer.readerIndex(), length); output.writeBytes(fieldNumber, dup.nioBuffer(), repeated); buffer.skipBytes(length); } }
From source file:ch.ethz.globis.distindex.middleware.net.MiddlewareChannelHandler.java
License:Open Source License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; String clientHost = ctx.channel().remoteAddress().toString(); ByteBuffer response = ioHandler.handle(clientHost, buf.nioBuffer()); ByteBuf nettyBuf = Unpooled.wrappedBuffer(response); ByteBuf sizeBuf = Unpooled.copyInt(nettyBuf.readableBytes()); ctx.write(sizeBuf);/*from w w w . j a va 2 s . c om*/ ctx.write(nettyBuf); ctx.flush(); buf.release(); }
From source file:com.alibaba.rocketmq.remoting.netty.NettyDecoder.java
License:Apache License
@Override public Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { ByteBuf frame = null; try {/*from w w w . j a v a 2s.c o m*/ frame = (ByteBuf) super.decode(ctx, in); if (null == frame) { return null; } ByteBuffer byteBuffer = frame.nioBuffer(); return RemotingCommand.decode(byteBuffer); } catch (Exception e) { log.error("decode exception, " + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), e); RemotingUtil.closeChannel(ctx.channel()); } finally { if (null != frame) { frame.release(); } } return null; }
From source file:com.antsdb.saltedfish.server.mysql.packet.replication.StateIndicator.java
License:Open Source License
@Override public void read(MysqlClientHandler handler, ByteBuf in) { // ignore packet info, assure master is mysql 5.6 if (_log.isTraceEnabled()) { ByteBuf packet = (ByteBuf) in; ByteBuffer bbuf = packet.nioBuffer(); int i = bbuf.remaining(); byte[] bytes = new byte[i]; packet.readBytes(bytes);//ww w . ja v a 2s .c om String dump = '\n' + UberUtil.hexDump(bytes); _log.trace(dump); } }