Interface ChannelBuffer
-
- All Superinterfaces:
- Comparable<ChannelBuffer>
- All Known Subinterfaces:
- WrappedChannelBuffer
- All Known Implementing Classes:
- AbstractChannelBuffer, BigEndianHeapChannelBuffer, ByteBufferBackedChannelBuffer, CompositeChannelBuffer, DuplicatedChannelBuffer, DynamicChannelBuffer, HeapChannelBuffer,LittleEndianHeapChannelBuffer, ReadOnlyChannelBuffer, SlicedChannelBuffer, TruncatedChannelBuffer
public interface ChannelBuffer extends Comparable<ChannelBuffer>
A random and sequential accessible sequence of zero or more bytes (octets). This interface provides an abstract view for one or more primitive byte arrays (byte[]
) and NIO buffers.Creation of a buffer
It is recommended to create a new buffer using the helper methods inChannelBuffers
rather than calling an individual implementation's constructor.Random Access Indexing
Just like an ordinary primitive byte array,ChannelBuffer
uses zero-based indexing. It means the index of the first byte is always0
and the index of the last byte is alwayscapacity - 1
. For example, to iterate all bytes of a buffer, you can do the following, regardless of its internal implementation:ChannelBuffer
buffer = ...; for (int i = 0; i < buffer.capacity(); i ++) { byte b = buffer.getByte(i); System.out.println((char) b); }Sequential Access Indexing
ChannelBuffer 由3部分组成,discardable 已经读了的,readable 还可读 ,writable还可写的
readerIndex是读指针,writerIndex写指针
ChannelBuffer
provides two pointer variables to support sequential read and write operations -readerIndex
for a read operation andwriterIndex
for a write operation respectively. The following diagram shows how a buffer is segmented into three areas by the two pointers: -
+-------------------+------------------+------------------+ | discardable bytes | readable bytes | writable bytes | | | (CONTENT) | | +-------------------+------------------+------------------+ | | | | 0 <= readerIndex <= writerIndex <= capacity
Readable bytes (the actual content)
This segment is where the actual data is stored. Any operation whose name starts withread
orskip
will get or skip the data at the currentreaderIndex
and increase it by the number of read bytes. If the argument of the read operation is also aChannelBuffer
and no destination index is specified, the specified buffer'sreaderIndex
is increased together.If there's not enough content left,
IndexOutOfBoundsException
is raised. The default value of newly allocated, wrapped or copied buffer'sreaderIndex
is0
.// Iterates the readable bytes of a buffer.
ChannelBuffer
buffer = ...; while (buffer.readable()) { System.out.println(buffer.readByte()); }Writable bytes 剩余多少可以写的
This segment is a undefined space which needs to be filled. Any operation whose name ends withwrite
will write the data at the currentwriterIndex
and increase it by the number of written bytes. If the argument of the write operation is also aChannelBuffer
, and no source index is specified, the specified buffer'sreaderIndex
is increased together.If there's not enough writable bytes left,
IndexOutOfBoundsException
is raised. The default value of newly allocated buffer'swriterIndex
is0
. The default value of wrapped or copied buffer'swriterIndex
is thecapacity
of the buffer.// Fills the writable bytes of a buffer with random integers.
ChannelBuffer
buffer = ...; while (buffer.writableBytes() >= 4) { buffer.writeInt(random.nextInt()); }Discardable bytes 已经读了多少
This segment contains the bytes which were read already by a read operation. Initially, the size of this segment is0
, but its size increases up to thewriterIndex
as read operations are executed. The read bytes can be discarded by callingdiscardReadBytes()
to reclaim unused area as depicted by the following diagram:BEFORE discardReadBytes() +-------------------+------------------+------------------+ | discardable bytes | readable bytes | writable bytes | +-------------------+------------------+------------------+ | | | | 0 <= readerIndex <= writerIndex <= capacity AFTER discardReadBytes() 释放已经读了部分 +------------------+--------------------------------------+ | readable bytes | writable bytes (got more space) | +------------------+--------------------------------------+ | | | readerIndex (0) <= writerIndex (decreased) <= capacity
Please note that there is no guarantee about the content of writable bytes after callingdiscardReadBytes()
. The writable bytes will not be moved in most cases and could even be filled with completely different data depending on the underlying buffer implementation.Clearing the buffer indexes
You can set bothreaderIndex
andwriterIndex
to0
by callingclear()
. It does not clear the buffer content (e.g. filling with0
) but just clears the two pointers. Please also note that the semantic of this operation is different fromBuffer.clear()
.BEFORE clear() +-------------------+------------------+------------------+ | discardable bytes | readable bytes | writable bytes | +-------------------+------------------+------------------+ | | | | 0 <= readerIndex <= writerIndex <= capacity AFTER clear() 把readerIndex和writerIndex =0 +---------------------------------------------------------+ | writable bytes (got more space) | +---------------------------------------------------------+ | | 0 = readerIndex = writerIndex <= capacity
Search operations
VariousindexOf(int, int, byte)
methods help you locate an index of a value which meets a certain criteria. Complicated dynamic sequential search can be done withChannelBufferIndexFinder
as well as simple static single byte search.If you are decoding variable length data such as NUL-terminated string, you will find
bytesBefore(byte)
also useful.Mark and reset
There are two marker indexes in every buffer. One is for storingreaderIndex
and the other is for storingwriterIndex
. You can always reposition one of the two indexes by calling a reset method. It works in a similar fashion to the mark and reset methods inInputStream
except that there's noreadlimit
.Derived buffers
You can create a view of an existing buffer by calling eitherduplicate()
,slice()
orslice(int, int)
. A derived buffer will have an independentreaderIndex
,writerIndex
and marker indexes, while it shares other internal data representation, just like a NIO buffer does.In case a completely fresh copy of an existing buffer is required, please call
copy()
method instead.Conversion to existing JDK types
Byte array
If aChannelBuffer
is backed by a byte array (i.e.byte[]
), you can access it directly via thearray()
method. To determine if a buffer is backed by a byte array,hasArray()
should be used.NIO Buffers
VarioustoByteBuffer()
andtoByteBuffers()
methods convert aChannelBuffer
into one or more NIO buffers. These methods avoid buffer allocation and memory copy whenever possible, but there's no guarantee that memory copy will not be involved.Strings
VarioustoString(String)
methods convert aChannelBuffer
into aString
. Please note thattoString()
is not a conversion method.I/O Streams
Please refer toChannelBufferInputStream
andChannelBufferOutputStream
.
Mark和Reset,可以和InputStream一样使用,但是有两个指针;
markReaderIndex() // 标记
markWriterIndex() //
resetReaderIndex() // 回到标记
resetWriterIndex()
相关推荐
Netty内部使用高效的缓冲数据结构(ChannelBuffer),支持快速的数据读写操作。 ##### 2. 统一的异步I/O API Netty提供了一个统一的异步I/O API,使得开发人员能够轻松地处理各种网络通信任务,无论是在NIO还是...
- **使用 POJO 替代 ChannelBuffer:**Netty 允许开发者使用普通的 Java 对象 (POJO) 来替代 ChannelBuffer 进行数据处理。 - **应用程序关闭:**介绍了如何优雅地关闭 Netty 应用程序,确保所有资源被正确释放。 #...
- **1.8 使用POJO代替`ChannelBuffer`**:学习如何使用普通的Java对象(POJOs)代替`ChannelBuffer`进行数据处理。 - **1.9 应用程序关闭**:了解如何优雅地关闭Netty应用程序。 - **1.10 总结**:回顾本章所学的...
Netty 是一个专门为快速开发高性能、高可靠性网络应用而设计的 Java 框架。它提供了异步事件驱动的网络应用程序框架,使得开发者能够轻松创建网络服务器和客户端。Netty 3.1 中文用户手册提供了详细的指南,帮助...
- **使用 POJO 代替 ChannelBuffer**:介绍如何使用普通的 Java 对象(POJOs)替代 ChannelBuffer 进行数据交换,提高代码的可读性和可维护性。 - **关闭应用**:正确的关闭顺序对于资源释放至关重要,Netty 提供...
- **高性能**:Netty的`ChannelBuffer`相比传统的`java.nio.ByteBuffer`具有更高的性能。 - **灵活性**:支持多种操作模式,如直接内存模式和堆内存模式。 - **安全性**:提供了更好的内存管理机制,避免了内存泄漏...
7. **Codec**:Netty 提供了一系列编解码器,用于将各种协议数据转换为 ChannelBuffer 和反向操作,如 HTTP、TCP、UDP 等。 通过对“Netty 权威指南第二版源码”的学习,你可以: - 理解 Netty 的事件模型和线程...
此外,手册还指导开发者如何用 POJO 替换 ChannelBuffer,以提高代码的可读性和易用性,以及如何优雅地关闭应用程序。 第二章则深入介绍了 Netty 的架构概览,包括其丰富的缓存数据结构、统一的异步 I/O API、基于...
Netty使用`ChannelBuffer`接口来封装缓冲区的功能,提供了比标准Java NIO `ByteBuffer`更高级的操作方式。 1. **HeapChannelBuffer**:这是Netty提供的一个实现`ChannelBuffer`接口的具体类。它的数据存储在Java堆...
首先,ChannelBuffer是Netty中用于网络数据交换的字节容器。在Netty中,所有的网络数据都是通过ChannelBuffer进行读写的。它的作用类似于Java NIO中的ByteBuffer,但它提供了更加丰富的功能。ChannelBuffer有...
### Netty NIO技术文档知识点概述 #### 一、Netty与NIO的基本概念 - **Netty**:一个高性能的异步事件驱动框架,专为服务器端开发设计,简化了网络应用程序的开发过程。 - **NIO (New I/O)**:Java平台的一种新的...
这个过程介绍了如何构建一个基础的Netty服务端和客户端,以及如何处理ChannelBuffer,即Netty中的数据容器。 手册接着提供了对ReceivedData的详解,通过编写Echo服务,即对收到的任何消息进行回显,来进一步解释...
Netty提供了一种称为`ChannelBuffer`的数据结构,用于管理和操作网络数据。这种数据结构比传统的Java NIO `ByteBuffer`更加灵活高效,可以更好地适应网络应用的需求。 ##### 3.2 异步I/O API Netty基于Java NIO构建...
netty pojo netty pojo替换 channelbuffer