今天看gateway 实现的时候看到个哥们基于的netty实现的gateway。so,解析一下Netty。
废话少说,maven pom 引入,down 下jar包。看了下netty的包结构,还是挺明确的,不像spring包那么多。
buffer,channel,是包装的JDK 的nio Buffer,Channel等类。
在io.netty.buffer.ByteBuf找到了如下的注释,解释了 limit ,flip等原生JDK api 的含义。
1 /** 2 * A random and sequential accessible sequence of zero or more bytes (octets). 3 * This interface provides an abstract view for one or more primitive byte 4 * arrays ({@code byte[]}) and {@linkplain ByteBuffer NIO buffers}. 5 * 6 *27 * 28 *Creation of a buffer
7 * 8 * It is recommended to create a new buffer using the helper methods in 9 * {@link Unpooled} rather than calling an individual implementation's 10 * constructor. 11 * 12 *Random Access Indexing
13 * 14 * Just like an ordinary primitive byte array, {@link ByteBuf} uses 15 * http://en.wikipedia.org/wiki/Zero-based_numbering">zero-based indexing. 16 * It means the index of the first byte is always {@code 0} and the index of the last byte is 17 * always {@link #capacity() capacity - 1}. For example, to iterate all bytes of a buffer, you 18 * can do the following, regardless of its internal implementation: 19 * 20 *21 * {@link ByteBuf} buffer = ...; 22 * for (int i = 0; i < buffer.capacity(); i ++) { 23 * byte b = buffer.getByte(i); 24 * System.out.println((char) b); 25 * } 26 *
Sequential Access Indexing
29 * 30 * {@link ByteBuf} provides two pointer variables to support sequential 31 * read and write operations - {@link #readerIndex() readerIndex} for a read 32 * operation and {@link #writerIndex() writerIndex} for a write operation 33 * respectively. The following diagram shows how a buffer is segmented into 34 * three areas by the two pointers: 35 * 36 *37 * +-------------------+------------------+------------------+ 38 * | discardable bytes | readable bytes | writable bytes | 39 * | | (CONTENT) | | 40 * +-------------------+------------------+------------------+ 41 * | | | | 42 * 0 <= readerIndex <= writerIndex <= capacity 43 * 44 * 45 *
Readable bytes (the actual content)
46 * 47 * This segment is where the actual data is stored. Any operation whose name 48 * starts with {@code read} or {@code skip} will get or skip the data at the 49 * current {@link #readerIndex() readerIndex} and increase it by the number of 50 * read bytes. If the argument of the read operation is also a 51 * {@link ByteBuf} and no destination index is specified, the specified 52 * buffer's {@link #writerIndex() writerIndex} is increased together. 53 *54 * If there's not enough content left, {@link IndexOutOfBoundsException} is 55 * raised. The default value of newly allocated, wrapped or copied buffer's 56 * {@link #readerIndex() readerIndex} is {@code 0}. 57 * 58 *
59 * // Iterates the readable bytes of a buffer. 60 * {@link ByteBuf} buffer = ...; 61 * while (buffer.readable()) { 62 * System.out.println(buffer.readByte()); 63 * } 64 * 65 * 66 *
Writable bytes
67 * 68 * This segment is a undefined space which needs to be filled. Any operation 69 * whose name ends with {@code write} will write the data at the current 70 * {@link #writerIndex() writerIndex} and increase it by the number of written 71 * bytes. If the argument of the write operation is also a {@link ByteBuf}, 72 * and no source index is specified, the specified buffer's 73 * {@link #readerIndex() readerIndex} is increased together. 74 *75 * If there's not enough writable bytes left, {@link IndexOutOfBoundsException} 76 * is raised. The default value of newly allocated buffer's 77 * {@link #writerIndex() writerIndex} is {@code 0}. The default value of 78 * wrapped or copied buffer's {@link #writerIndex() writerIndex} is the 79 * {@link #capacity() capacity} of the buffer. 80 * 81 *
82 * // Fills the writable bytes of a buffer with random integers. 83 * {@link ByteBuf} buffer = ...; 84 * while (buffer.maxWritableBytes() >= 4) { 85 * buffer.writeInt(random.nextInt()); 86 * } 87 * 88 * 89 *
Discardable bytes
90 * 91 * This segment contains the bytes which were read already by a read operation. 92 * Initially, the size of this segment is {@code 0}, but its size increases up 93 * to the {@link #writerIndex() writerIndex} as read operations are executed. 94 * The read bytes can be discarded by calling {@link #discardReadBytes()} to 95 * reclaim unused area as depicted by the following diagram: 96 * 97 *98 * BEFORE discardReadBytes() 99 * 100 * +-------------------+------------------+------------------+ 101 * | discardable bytes | readable bytes | writable bytes | 102 * +-------------------+------------------+------------------+ 103 * | | | | 104 * 0 <= readerIndex <= writerIndex <= capacity 105 * 106 * 107 * AFTER discardReadBytes() 108 * 109 * +------------------+--------------------------------------+ 110 * | readable bytes | writable bytes (got more space) | 111 * +------------------+--------------------------------------+ 112 * | | | 113 * readerIndex (0) <= writerIndex (decreased) <= capacity 114 * 115 * 116 * Please note that there is no guarantee about the content of writable bytes 117 * after calling {@link #discardReadBytes()}. The writable bytes will not be 118 * moved in most cases and could even be filled with completely different data 119 * depending on the underlying buffer implementation. 120 * 121 *
Clearing the buffer indexes
122 * 123 * You can set both {@link #readerIndex() readerIndex} and 124 * {@link #writerIndex() writerIndex} to {@code 0} by calling {@link #clear()}. 125 * It does not clear the buffer content (e.g. filling with {@code 0}) but just 126 * clears the two pointers. Please also note that the semantic of this 127 * operation is different from {@link ByteBuffer#clear()}. 128 * 129 *130 * BEFORE clear() 131 * 132 * +-------------------+------------------+------------------+ 133 * | discardable bytes | readable bytes | writable bytes | 134 * +-------------------+------------------+------------------+ 135 * | | | | 136 * 0 <= readerIndex <= writerIndex <= capacity 137 * 138 * 139 * AFTER clear() 140 * 141 * +---------------------------------------------------------+ 142 * | writable bytes (got more space) | 143 * +---------------------------------------------------------+ 144 * | | 145 * 0 = readerIndex = writerIndex <= capacity 146 * 147 * 148 *
Search operations
149 * 150 * For simple single-byte searches, use {@link #indexOf(int, int, byte)} and {@link #bytesBefore(int, int, byte)}. 151 * {@link #bytesBefore(byte)} is especially useful when you deal with a {@code NUL}-terminated string. 152 * For complicated searches, use {@link #forEachByte(int, int, ByteBufProcessor)} with a {@link ByteBufProcessor} 153 * implementation. 154 * 155 *Mark and reset
156 * 157 * There are two marker indexes in every buffer. One is for storing 158 * {@link #readerIndex() readerIndex} and the other is for storing 159 * {@link #writerIndex() writerIndex}. You can always reposition one of the 160 * two indexes by calling a reset method. It works in a similar fashion to 161 * the mark and reset methods in {@link InputStream} except that there's no 162 * {@code readlimit}. 163 * 164 *Derived buffers
165 * 166 * You can create a view of an existing buffer by calling either 167 * {@link #duplicate()}, {@link #slice()} or {@link #slice(int, int)}. 168 * A derived buffer will have an independent {@link #readerIndex() readerIndex}, 169 * {@link #writerIndex() writerIndex} and marker indexes, while it shares 170 * other internal data representation, just like a NIO buffer does. 171 *172 * In case a completely fresh copy of an existing buffer is required, please 173 * call {@link #copy()} method instead. 174 * 175 *
Conversion to existing JDK types
176 * 177 *Byte array
178 * 179 * If a {@link ByteBuf} is backed by a byte array (i.e. {@code byte[]}), 180 * you can access it directly via the {@link #array()} method. To determine 181 * if a buffer is backed by a byte array, {@link #hasArray()} should be used. 182 * 183 *NIO Buffers
184 * 185 * If a {@link ByteBuf} can be converted into an NIO {@link ByteBuffer} which shares its 186 * content (i.e. view buffer), you can get it via the {@link #nioBuffer()} method. To determine 187 * if a buffer can be converted into an NIO buffer, use {@link #nioBufferCount()}. 188 * 189 *Strings
190 * 191 * Various {@link #toString(Charset)} methods convert a {@link ByteBuf} 192 * into a {@link String}. Please note that {@link #toString()} is not a 193 * conversion method. 194 * 195 *I/O Streams
196 * 197 * Please refer to {@link ByteBufInputStream} and 198 * {@link ByteBufOutputStream}. 199 */