io.netty.buffer.ByteBuf.internalNioBuffer()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.4k)|赞(0)|评价(0)|浏览(206)

本文整理了Java中io.netty.buffer.ByteBuf.internalNioBuffer()方法的一些代码示例,展示了ByteBuf.internalNioBuffer()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ByteBuf.internalNioBuffer()方法的具体详情如下:
包路径:io.netty.buffer.ByteBuf
类名称:ByteBuf
方法名:internalNioBuffer

ByteBuf.internalNioBuffer介绍

[英]Internal use only: Exposes the internal NIO buffer.
[中]仅限内部使用:公开内部NIO缓冲区。

代码示例

代码示例来源:origin: netty/netty

  1. @Override
  2. public ByteBuffer internalNioBuffer(int index, int length) {
  3. return buf.internalNioBuffer(index, length);
  4. }

代码示例来源:origin: redisson/redisson

  1. @Override
  2. public ByteBuffer internalNioBuffer(int index, int length) {
  3. return buf.internalNioBuffer(index, length);
  4. }

代码示例来源:origin: netty/netty

  1. @Override
  2. public ByteBuffer internalNioBuffer(int index, int length) {
  3. checkIndex(index, length);
  4. return buffer.internalNioBuffer(index, length);
  5. }

代码示例来源:origin: netty/netty

  1. static ByteBuffer safeNioBuffer(ByteBuf buffer) {
  2. return buffer.nioBufferCount() == 1 ? buffer.internalNioBuffer(buffer.readerIndex(), buffer.readableBytes())
  3. : buffer.nioBuffer();
  4. }
  5. }

代码示例来源:origin: netty/netty

  1. @Override
  2. public ByteBuffer internalNioBuffer(int index, int length) {
  3. if (buffers.length == 1) {
  4. return buffer(0).internalNioBuffer(index, length);
  5. }
  6. throw new UnsupportedOperationException();
  7. }

代码示例来源:origin: netty/netty

  1. @Override
  2. public ByteBuffer internalNioBuffer(int index, int length) {
  3. switch (componentCount) {
  4. case 0:
  5. return EMPTY_NIO_BUFFER;
  6. case 1:
  7. Component c = components[0];
  8. return c.buf.internalNioBuffer(c.idx(index), length);
  9. default:
  10. throw new UnsupportedOperationException();
  11. }
  12. }

代码示例来源:origin: redisson/redisson

  1. static ByteBuffer safeNioBuffer(ByteBuf buffer) {
  2. return buffer.nioBufferCount() == 1 ? buffer.internalNioBuffer(buffer.readerIndex(), buffer.readableBytes())
  3. : buffer.nioBuffer();
  4. }
  5. }

代码示例来源:origin: redisson/redisson

  1. @Override
  2. public ByteBuffer internalNioBuffer(int index, int length) {
  3. switch (componentCount) {
  4. case 0:
  5. return EMPTY_NIO_BUFFER;
  6. case 1:
  7. Component c = components[0];
  8. return c.buf.internalNioBuffer(c.idx(index), length);
  9. default:
  10. throw new UnsupportedOperationException();
  11. }
  12. }

代码示例来源:origin: redisson/redisson

  1. @Override
  2. public ByteBuffer internalNioBuffer(int index, int length) {
  3. checkIndex(index, length);
  4. return buffer.internalNioBuffer(index, length);
  5. }

代码示例来源:origin: redisson/redisson

  1. @Override
  2. public ByteBuffer internalNioBuffer(int index, int length) {
  3. if (buffers.length == 1) {
  4. return buffer(0).internalNioBuffer(index, length);
  5. }
  6. throw new UnsupportedOperationException();
  7. }

代码示例来源:origin: redisson/redisson

  1. private static ByteBuffer toByteBuffer(ByteBuf out, int index, int len) {
  2. return out.nioBufferCount() == 1 ? out.internalNioBuffer(index, len) :
  3. out.nioBuffer(index, len);
  4. }

代码示例来源:origin: redisson/redisson

  1. @Override
  2. public Object decode(ByteBuf buf, State state) throws IOException {
  3. int decompressSize = buf.readInt();
  4. ByteBuf out = ByteBufAllocator.DEFAULT.buffer(decompressSize);
  5. try {
  6. LZ4SafeDecompressor decompressor = factory.safeDecompressor();
  7. ByteBuffer outBuffer = out.internalNioBuffer(out.writerIndex(), out.writableBytes());
  8. int pos = outBuffer.position();
  9. decompressor.decompress(buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()), outBuffer);
  10. int compressedLength = outBuffer.position() - pos;
  11. out.writerIndex(compressedLength);
  12. return innerCodec.getValueDecoder().decode(out, state);
  13. } finally {
  14. out.release();
  15. }
  16. }
  17. };

代码示例来源:origin: redisson/redisson

  1. @Override
  2. public Object decode(ByteBuf buf, State state) throws IOException {
  3. int decompressSize = buf.readInt();
  4. ByteBuf out = ByteBufAllocator.DEFAULT.buffer(decompressSize);
  5. try {
  6. LZ4SafeDecompressor decompressor = factory.safeDecompressor();
  7. ByteBuffer outBuffer = out.internalNioBuffer(out.writerIndex(), out.writableBytes());
  8. int pos = outBuffer.position();
  9. decompressor.decompress(buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()), outBuffer);
  10. int compressedLength = outBuffer.position() - pos;
  11. out.writerIndex(compressedLength);
  12. return innerCodec.getValueDecoder().decode(out, state);
  13. } finally {
  14. out.release();
  15. }
  16. }
  17. };

代码示例来源:origin: AsyncHttpClient/async-http-client

  1. private void decodeHeap0(ByteBuf buf) {
  2. int length = buf.readableBytes();
  3. ensureCapacity(length);
  4. if (buf.nioBufferCount() == 1) {
  5. decodeSingleNioBuffer(buf.internalNioBuffer(buf.readerIndex(), length).duplicate());
  6. } else {
  7. decode(buf.nioBuffers());
  8. }
  9. charBuffer.flip();
  10. }

代码示例来源:origin: redisson/redisson

  1. protected final int doWriteBytes(ChannelOutboundBuffer in, ByteBuf buf) throws Exception {
  2. if (buf.hasMemoryAddress()) {
  3. int localFlushedAmount = socket.writeAddress(buf.memoryAddress(), buf.readerIndex(), buf.writerIndex());
  4. if (localFlushedAmount > 0) {
  5. in.removeBytes(localFlushedAmount);
  6. return 1;
  7. }
  8. } else {
  9. final ByteBuffer nioBuf = buf.nioBufferCount() == 1 ?
  10. buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()) : buf.nioBuffer();
  11. int localFlushedAmount = socket.write(nioBuf, nioBuf.position(), nioBuf.limit());
  12. if (localFlushedAmount > 0) {
  13. nioBuf.position(nioBuf.position() + localFlushedAmount);
  14. in.removeBytes(localFlushedAmount);
  15. return 1;
  16. }
  17. }
  18. return WRITE_STATUS_SNDBUF_FULL;
  19. }

代码示例来源:origin: redisson/redisson

  1. protected final int doWriteBytes(ChannelOutboundBuffer in, ByteBuf buf) throws Exception {
  2. if (buf.hasMemoryAddress()) {
  3. int localFlushedAmount = socket.writeAddress(buf.memoryAddress(), buf.readerIndex(), buf.writerIndex());
  4. if (localFlushedAmount > 0) {
  5. in.removeBytes(localFlushedAmount);
  6. return 1;
  7. }
  8. } else {
  9. final ByteBuffer nioBuf = buf.nioBufferCount() == 1 ?
  10. buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()) : buf.nioBuffer();
  11. int localFlushedAmount = socket.write(nioBuf, nioBuf.position(), nioBuf.limit());
  12. if (localFlushedAmount > 0) {
  13. nioBuf.position(nioBuf.position() + localFlushedAmount);
  14. in.removeBytes(localFlushedAmount);
  15. return 1;
  16. }
  17. }
  18. return WRITE_STATUS_SNDBUF_FULL;
  19. }

代码示例来源:origin: netty/netty

  1. @Override
  2. protected boolean doWriteMessage(Object msg, ChannelOutboundBuffer in) throws Exception {
  3. final SocketAddress remoteAddress;
  4. final ByteBuf data;
  5. if (msg instanceof AddressedEnvelope) {
  6. @SuppressWarnings("unchecked")
  7. AddressedEnvelope<ByteBuf, SocketAddress> envelope = (AddressedEnvelope<ByteBuf, SocketAddress>) msg;
  8. remoteAddress = envelope.recipient();
  9. data = envelope.content();
  10. } else {
  11. data = (ByteBuf) msg;
  12. remoteAddress = null;
  13. }
  14. final int dataLen = data.readableBytes();
  15. if (dataLen == 0) {
  16. return true;
  17. }
  18. final ByteBuffer nioData = data.nioBufferCount() == 1 ? data.internalNioBuffer(data.readerIndex(), dataLen)
  19. : data.nioBuffer(data.readerIndex(), dataLen);
  20. final int writtenBytes;
  21. if (remoteAddress != null) {
  22. writtenBytes = javaChannel().send(nioData, remoteAddress);
  23. } else {
  24. writtenBytes = javaChannel().write(nioData);
  25. }
  26. return writtenBytes > 0;
  27. }

代码示例来源:origin: redisson/redisson

  1. /**
  2. * Read bytes into the given {@link ByteBuf} and return the amount.
  3. */
  4. protected final int doReadBytes(ByteBuf byteBuf) throws Exception {
  5. int writerIndex = byteBuf.writerIndex();
  6. int localReadAmount;
  7. unsafe().recvBufAllocHandle().attemptedBytesRead(byteBuf.writableBytes());
  8. if (byteBuf.hasMemoryAddress()) {
  9. localReadAmount = socket.readAddress(byteBuf.memoryAddress(), writerIndex, byteBuf.capacity());
  10. } else {
  11. ByteBuffer buf = byteBuf.internalNioBuffer(writerIndex, byteBuf.writableBytes());
  12. localReadAmount = socket.read(buf, buf.position(), buf.limit());
  13. }
  14. if (localReadAmount > 0) {
  15. byteBuf.writerIndex(writerIndex + localReadAmount);
  16. }
  17. return localReadAmount;
  18. }

代码示例来源:origin: redisson/redisson

  1. /**
  2. * Read bytes into the given {@link ByteBuf} and return the amount.
  3. */
  4. protected final int doReadBytes(ByteBuf byteBuf) throws Exception {
  5. int writerIndex = byteBuf.writerIndex();
  6. int localReadAmount;
  7. unsafe().recvBufAllocHandle().attemptedBytesRead(byteBuf.writableBytes());
  8. if (byteBuf.hasMemoryAddress()) {
  9. localReadAmount = socket.readAddress(byteBuf.memoryAddress(), writerIndex, byteBuf.capacity());
  10. } else {
  11. ByteBuffer buf = byteBuf.internalNioBuffer(writerIndex, byteBuf.writableBytes());
  12. localReadAmount = socket.read(buf, buf.position(), buf.limit());
  13. }
  14. if (localReadAmount > 0) {
  15. byteBuf.writerIndex(writerIndex + localReadAmount);
  16. }
  17. return localReadAmount;
  18. }

代码示例来源:origin: netty/netty

  1. @Override
  2. protected int doReadMessages(List<Object> buf) throws Exception {
  3. DatagramChannel ch = javaChannel();
  4. DatagramChannelConfig config = config();
  5. RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
  6. ByteBuf data = allocHandle.allocate(config.getAllocator());
  7. allocHandle.attemptedBytesRead(data.writableBytes());
  8. boolean free = true;
  9. try {
  10. ByteBuffer nioData = data.internalNioBuffer(data.writerIndex(), data.writableBytes());
  11. int pos = nioData.position();
  12. InetSocketAddress remoteAddress = (InetSocketAddress) ch.receive(nioData);
  13. if (remoteAddress == null) {
  14. return 0;
  15. }
  16. allocHandle.lastBytesRead(nioData.position() - pos);
  17. buf.add(new DatagramPacket(data.writerIndex(data.writerIndex() + allocHandle.lastBytesRead()),
  18. localAddress(), remoteAddress));
  19. free = false;
  20. return 1;
  21. } catch (Throwable cause) {
  22. PlatformDependent.throwException(cause);
  23. return -1;
  24. } finally {
  25. if (free) {
  26. data.release();
  27. }
  28. }
  29. }

相关文章

ByteBuf类方法