Netty 学习之旅:ByteBuf 篇之 ByteBuf 内部结构与 API 学习

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip1024b (备注Java)
img

正文

  •     if {@code this.readableBytes} is less than {@code 4}
    

*/

public abstract int readInt();

/**

  • Gets an unsigned 32-bit integer at the current {@code readerIndex}

  • and increases the {@code readerIndex} by {@code 4} in this buffer.

  • @throws IndexOutOfBoundsException

  •     if {@code this.readableBytes} is less than {@code 4}
    

*/

public abstract long readUnsignedInt();

/**

  • Gets a 64-bit integer at the current {@code readerIndex}

  • and increases the {@code readerIndex} by {@code 8} in this buffer.

  • @throws IndexOutOfBoundsException

  •     if {@code this.readableBytes} is less than {@code 8}
    

*/

public abstract long readLong();

/**

  • Gets a 2-byte UTF-16 character at the current {@code readerIndex}

  • and increases the {@code readerIndex} by {@code 2} in this buffer.

  • @throws IndexOutOfBoundsException

  •     if {@code this.readableBytes} is less than {@code 2}
    

*/

public abstract char readChar();

/**

  • Gets a 32-bit floating point number at the current {@code readerIndex}

  • and increases the {@code readerIndex} by {@code 4} in this buffer.

  • @throws IndexOutOfBoundsException

  •     if {@code this.readableBytes} is less than {@code 4}
    

*/

public abstract float readFloat();

/**

  • Gets a 64-bit floating point number at the current {@code readerIndex}

  • and increases the {@code readerIndex} by {@code 8} in this buffer.

  • @throws IndexOutOfBoundsException

  •     if {@code this.readableBytes} is less than {@code 8}
    

*/

public abstract double readDouble();

/**

  • Transfers this buffer’s data to a newly created buffer starting at

  • the current {@code readerIndex} and increases the {@code readerIndex}

  • by the number of the transferred bytes (= {@code length}).

  • The returned buffer’s {@code readerIndex} and {@code writerIndex} are

  • {@code 0} and {@code length} respectively.

  • @param length the number of bytes to transfer

  • @return the newly created buffer which contains the transferred bytes

  • @throws IndexOutOfBoundsException

  •     if {@code length} is greater than {@code this.readableBytes}
    

*/

public abstract ByteBuf readBytes(int length);

/**

  • Returns a new slice of this buffer’s sub-region starting at the current

  • {@code readerIndex} and increases the {@code readerIndex} by the size

  • of the new slice (= {@code length}).

  • @param length the size of the new slice

  • @return the newly created slice

  • @throws IndexOutOfBoundsException

  •     if {@code length} is greater than {@code this.readableBytes}
    

*/

public abstract ByteBuf readSlice(int length);

/**

  • Transfers this buffer’s data to the specified destination starting at

  • the current {@code readerIndex} until the destination becomes

  • non-writable, and increases the {@code readerIndex} by the number of the

  • transferred bytes. This method is basically same with

  • {@link #readBytes(ByteBuf, int, int)}, except that this method

  • increases the {@code writerIndex} of the destination by the number of

  • the transferred bytes while {@link #readBytes(ByteBuf, int, int)}

  • does not.

  • @throws IndexOutOfBoundsException

  •     if {@code dst.writableBytes} is greater than
    
  •        {@code this.readableBytes}
    

*/

public abstract ByteBuf readBytes(ByteBuf dst);

/**

  • Transfers this buffer’s data to the specified destination starting at

  • the current {@code readerIndex} and increases the {@code readerIndex}

  • by the number of the transferred bytes (= {@code length}). This method

  • is basically same with {@link #readBytes(ByteBuf, int, int)},

  • except that this method increases the {@code writerIndex} of the

  • destination by the number of the transferred bytes (= {@code length})

  • while {@link #readBytes(ByteBuf, int, int)} does not.

  • @throws IndexOutOfBoundsException

  •     if {@code length} is greater than {@code this.readableBytes} or
    
  •     if {@code length} is greater than {@code dst.writableBytes}
    

*/

public abstract ByteBuf readBytes(ByteBuf dst, int length);

/**

  • Transfers this buffer’s data to the specified destination starting at

  • the current {@code readerIndex} and increases the {@code readerIndex}

  • by the number of the transferred bytes (= {@code length}).

  • @param dstIndex the first index of the destination

  • @param length the number of bytes to transfer

  • @throws IndexOutOfBoundsException

  •     if the specified {@code dstIndex} is less than {@code 0},
    
  •     if {@code length} is greater than {@code this.readableBytes}, or
    
  •     if {@code dstIndex + length} is greater than
    
  •        {@code dst.capacity}
    

*/

public abstract ByteBuf readBytes(ByteBuf dst, int dstIndex, int length);

/**

  • Transfers this buffer’s data to the specified destination starting at

  • the current {@code readerIndex} and increases the {@code readerIndex}

  • by the number of the transferred bytes (= {@code dst.length}).

  • @throws IndexOutOfBoundsException

  •     if {@code dst.length} is greater than {@code this.readableBytes}
    

*/

public abstract ByteBuf readBytes(byte[] dst);

/**

  • Transfers this buffer’s data to the specified destination starting at

  • the current {@code readerIndex} and increases the {@code readerIndex}

  • by the number of the transferred bytes (= {@code length}).

  • @param dstIndex the first index of the destination

  • @param length the number of bytes to transfer

  • @throws IndexOutOfBoundsException

  •     if the specified {@code dstIndex} is less than {@code 0},
    
  •     if {@code length} is greater than {@code this.readableBytes}, or
    
  •     if {@code dstIndex + length} is greater than {@code dst.length}
    

*/

public abstract ByteBuf readBytes(byte[] dst, int dstIndex, int length);

/**

  • Transfers this buffer’s data to the specified destination starting at

  • the current {@code readerIndex} until the destination’s position

  • reaches its limit, and increases the {@code readerIndex} by the

  • number of the transferred bytes.

  • @throws IndexOutOfBoundsException

  •     if {@code dst.remaining()} is greater than
    
  •        {@code this.readableBytes}
    

*/

public abstract ByteBuf readBytes(ByteBuffer dst);

/**

  • Transfers this buffer’s data to the specified stream starting at the

  • current {@code readerIndex}.

  • @param length the number of bytes to transfer

  • @throws IndexOutOfBoundsException

  •     if {@code length} is greater than {@code this.readableBytes}
    
  • @throws IOException

  •     if the specified stream threw an exception during I/O
    

*/

public abstract ByteBuf readBytes(OutputStream out, int length) throws IOException;

/**

  • Transfers this buffer’s data to the specified stream starting at the

  • current {@code readerIndex}.

  • @param length the maximum number of bytes to transfer

  • @return the actual number of bytes written out to the specified channel

  • @throws IndexOutOfBoundsException

  •     if {@code length} is greater than {@code this.readableBytes}
    
  • @throws IOException

  •     if the specified channel threw an exception during I/O
    

*/

public abstract int readBytes(GatheringByteChannel out, int length) throws IOException;

对于上面的方法,我挑如下三个进行重点说明一下,其他的类似:

  • public abstract ByteBuf readBytes(byte[] dst)

从 ByteBuf 缓存区中读取 dst.length 个字节到 dst 目标数组中,返回当前的 ByteBuf, 这是级联设计风格,如果 ByteBuf 可剩余可读字节小于 dst.length,则抛出异常。

  • public abstract ByteBuf readBytes(int length)

从 ByteBuf 缓存区读取 length 个字节到新创建的 ByteBuf, 注意返回的 ByteBuf 是先创建的,这不是级联设计风格。

  • public abstract ByteBuf readBytes(ByteBuf dst)

从 ByteBuf 缓存区读取 ds t可写字节长度到 dst 中,返回的 ByteBuf 是级联风格设计,如果 dst 的可剩余写长度大于操作的缓存区时,将抛出异常。

  • public abstract ByteBuf readSlice(int length)

从 ButeBuf 缓存区读取 length 个字节到 ByteBuf 中,这里的 ByteBuf 是原 ByteBuf 的视图,独立的 readIndex, writerIndex。

2.2.2 顺序写API

/**

  • Sets the specified boolean at the current {@code writerIndex}

  • and increases the {@code writerIndex} by {@code 1} in this buffer.

  • @throws IndexOutOfBoundsException

  •     if {@code this.writableBytes} is less than {@code 1}
    

*/

public abstract ByteBuf writeBoolean(boolean value);

/**

  • Sets the specified byte at the current {@code writerIndex}

  • and increases the {@code writerIndex} by {@code 1} in this buffer.

  • The 24 high-order bits of the specified value are ignored.

  • @throws IndexOutOfBoundsException

  •     if {@code this.writableBytes} is less than {@code 1}
    

*/

public abstract ByteBuf writeByte(int value);

/**

  • Sets the specified 16-bit short integer at the current

  • {@code writerIndex} and increases the {@code writerIndex} by {@code 2}

  • in this buffer. The 16 high-order bits of the specified value are ignored.

  • @throws IndexOutOfBoundsException

  •     if {@code this.writableBytes} is less than {@code 2}
    

*/

public abstract ByteBuf writeShort(int value);

/**

  • Sets the specified 24-bit medium integer at the current

  • {@code writerIndex} and increases the {@code writerIndex} by {@code 3}

  • in this buffer.

  • @throws IndexOutOfBoundsException

  •     if {@code this.writableBytes} is less than {@code 3}
    

*/

public abstract ByteBuf writeMedium(int value);

/**

  • Sets the specified 32-bit integer at the current {@code writerIndex}

  • and increases the {@code writerIndex} by {@code 4} in this buffer.

  • @throws IndexOutOfBoundsException

  •     if {@code this.writableBytes} is less than {@code 4}
    

*/

public abstract ByteBuf writeInt(int value);

/**

  • Sets the specified 64-bit long integer at the current

  • {@code writerIndex} and increases the {@code writerIndex} by {@code 8}

  • in this buffer.

  • @throws IndexOutOfBoundsException

  •     if {@code this.writableBytes} is less than {@code 8}
    

*/

public abstract ByteBuf writeLong(long value);

/**

  • Sets the specified 2-byte UTF-16 character at the current

  • {@code writerIndex} and increases the {@code writerIndex} by {@code 2}

  • in this buffer. The 16 high-order bits of the specified value are ignored.

  • @throws IndexOutOfBoundsException

  •     if {@code this.writableBytes} is less than {@code 2}
    

*/

public abstract ByteBuf writeChar(int value);

/**

  • Sets the specified 32-bit floating point number at the current

  • {@code writerIndex} and increases the {@code writerIndex} by {@code 4}

  • in this buffer.

  • @throws IndexOutOfBoundsException

  •     if {@code this.writableBytes} is less than {@code 4}
    

*/

public abstract ByteBuf writeFloat(float value);

/**

  • Sets the specified 64-bit floating point number at the current

  • {@code writerIndex} and increases the {@code writerIndex} by {@code 8}

  • in this buffer.

  • @throws IndexOutOfBoundsException

  •     if {@code this.writableBytes} is less than {@code 8}
    

*/

public abstract ByteBuf writeDouble(double value);

/**

  • Transfers the specified source buffer’s data to this buffer starting at

  • the current {@code writerIndex} until the source buffer becomes

  • unreadable, and increases the {@code writerIndex} by the number of

  • the transferred bytes. This method is basically same with

  • {@link #writeBytes(ByteBuf, int, int)}, except that this method

  • increases the {@code readerIndex} of the source buffer by the number of

  • the transferred bytes while {@link #writeBytes(ByteBuf, int, int)}

  • does not.

  • @throws IndexOutOfBoundsException

  •     if {@code src.readableBytes} is greater than
    
  •        {@code this.writableBytes}
    

*/

public abstract ByteBuf writeBytes(ByteBuf src);

/**

  • Transfers the specified source buffer’s data to this buffer starting at

  • the current {@code writerIndex} and increases the {@code writerIndex}

  • by the number of the transferred bytes (= {@code length}). This method

  • is basically same with {@link #writeBytes(ByteBuf, int, int)},

  • except that this method increases the {@code readerIndex} of the source

  • buffer by the number of the transferred bytes (= {@code length}) while

  • {@link #writeBytes(ByteBuf, int, int)} does not.

  • @param length the number of bytes to transfer

  • @throws IndexOutOfBoundsException

  •     if {@code length} is greater than {@code this.writableBytes} or
    
  •     if {@code length} is greater then {@code src.readableBytes}
    

*/

public abstract ByteBuf writeBytes(ByteBuf src, int length);

/**

  • Transfers the specified source buffer’s data to this buffer starting at

  • the current {@code writerIndex} and increases the {@code writerIndex}

  • by the number of the transferred bytes (= {@code length}).

  • @param srcIndex the first index of the source

  • @param length the number of bytes to transfer

  • @throws IndexOutOfBoundsException

  •     if the specified {@code srcIndex} is less than {@code 0},
    
  •     if {@code srcIndex + length} is greater than
    
  •        {@code src.capacity}, or
    
  •     if {@code length} is greater than {@code this.writableBytes}
    

*/

public abstract ByteBuf writeBytes(ByteBuf src, int srcIndex, int length);

/**

  • Transfers the specified source array’s data to this buffer starting at

  • the current {@code writerIndex} and increases the {@code writerIndex}

  • by the number of the transferred bytes (= {@code src.length}).

  • @throws IndexOutOfBoundsException

  •     if {@code src.length} is greater than {@code this.writableBytes}
    

*/

public abstract ByteBuf writeBytes(byte[] src);

/**

  • Transfers the specified source array’s data to this buffer starting at

  • the current {@code writerIndex} and increases the {@code writerIndex}

  • by the number of the transferred bytes (= {@code length}).

  • @param srcIndex the first index of the source

  • @param length the number of bytes to transfer

  • @throws IndexOutOfBoundsException

  •     if the specified {@code srcIndex} is less than {@code 0},
    
  •     if {@code srcIndex + length} is greater than
    
  •        {@code src.length}, or
    
  •     if {@code length} is greater than {@code this.writableBytes}
    

*/

public abstract ByteBuf writeBytes(byte[] src, int srcIndex, int length);

/**

  • Transfers the specified source buffer’s data to this buffer starting at

  • the current {@code writerIndex} until the source buffer’s position

  • reaches its limit, and increases the {@code writerIndex} by the

  • number of the transferred bytes.

  • @throws IndexOutOfBoundsException

  •     if {@code src.remaining()} is greater than
    
  •        {@code this.writableBytes}
    

*/

public abstract ByteBuf writeBytes(ByteBuffer src);

/**

  • Transfers the content of the specified stream to this buffer

  • starting at the current {@code writerIndex} and increases the

  • {@code writerIndex} by the number of the transferred bytes.

  • @param length the number of bytes to transfer

  • @return the actual number of bytes read in from the specified stream

  • @throws IndexOutOfBoundsException

  •     if {@code length} is greater than {@code this.writableBytes}
    
  • @throws IOException

  •     if the specified stream threw an exception during I/O
    

*/

public abstract int writeBytes(InputStream in, int length) throws IOException;

/**

  • Transfers the content of the specified channel to this buffer

  • starting at the current {@code writerIndex} and increases the

  • {@code writerIndex} by the number of the transferred bytes.

  • @param length the maximum number of bytes to transfer

  • @return the actual number of bytes read in from the specified channel

  • @throws IndexOutOfBoundsException

  •     if {@code length} is greater than {@code this.writableBytes}
    
  • @throws IOException

  •     if the specified channel threw an exception during I/O
    

*/

public abstract int writeBytes(ScatteringByteChannel in, int length) throws IOException;

/**

  • Fills this buffer with NUL (0x00) starting at the current

  • {@code writerIndex} and increases the {@code writerIndex} by the

  • specified {@code length}.

  • @param length the number of NULs to write to the buffer

  • @throws IndexOutOfBoundsException

  •     if {@code length} is greater than {@code this.writableBytes}
    

*/

public abstract ByteBuf writeZero(int length);

重点强调一下如下方法,其他的类似。

  • public abstract ByteBuf writeBytes(byte[] src)

将字节数组 src 全部写入到缓存区中,如果 src.length 大于 ByteBuf 可写区域的话,会抛出异常。

2.2.3 随机读API

/**

  • Gets a boolean at the specified absolute (@code index) in this buffer.

  • This method does not modify the {@code readerIndex} or {@code writerIndex}

  • of this buffer.

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     {@code index + 1} is greater than {@code this.capacity}
    

*/

public abstract boolean getBoolean(int index);

/**

  • Gets a byte at the specified absolute {@code index} in this buffer.

  • This method does not modify {@code readerIndex} or {@code writerIndex} of

  • this buffer.

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     {@code index + 1} is greater than {@code this.capacity}
    

*/

public abstract byte getByte(int index);

/**

  • Gets an unsigned byte at the specified absolute {@code index} in this

  • buffer. This method does not modify {@code readerIndex} or

  • {@code writerIndex} of this buffer.

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     {@code index + 1} is greater than {@code this.capacity}
    

*/

public abstract short getUnsignedByte(int index);

/**

  • Gets a 16-bit short integer at the specified absolute {@code index} in

  • this buffer. This method does not modify {@code readerIndex} or

  • {@code writerIndex} of this buffer.

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     {@code index + 2} is greater than {@code this.capacity}
    

*/

public abstract short getShort(int index);

/**

  • Gets an unsigned 16-bit short integer at the specified absolute

  • {@code index} in this buffer. This method does not modify

  • {@code readerIndex} or {@code writerIndex} of this buffer.

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     {@code index + 2} is greater than {@code this.capacity}
    

*/

public abstract int getUnsignedShort(int index);

/**

  • Gets a 24-bit medium integer at the specified absolute {@code index} in

  • this buffer. This method does not modify {@code readerIndex} or

  • {@code writerIndex} of this buffer.

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     {@code index + 3} is greater than {@code this.capacity}
    

*/

public abstract int getMedium(int index);

/**

  • Gets an unsigned 24-bit medium integer at the specified absolute

  • {@code index} in this buffer. This method does not modify

  • {@code readerIndex} or {@code writerIndex} of this buffer.

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     {@code index + 3} is greater than {@code this.capacity}
    

*/

public abstract int getUnsignedMedium(int index);

/**

  • Gets a 32-bit integer at the specified absolute {@code index} in

  • this buffer. This method does not modify {@code readerIndex} or

  • {@code writerIndex} of this buffer.

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     {@code index + 4} is greater than {@code this.capacity}
    

*/

public abstract int getInt(int index);

/**

  • Gets an unsigned 32-bit integer at the specified absolute {@code index}

  • in this buffer. This method does not modify {@code readerIndex} or

  • {@code writerIndex} of this buffer.

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     {@code index + 4} is greater than {@code this.capacity}
    

*/

public abstract long getUnsignedInt(int index);

/**

  • Gets a 64-bit long integer at the specified absolute {@code index} in

  • this buffer. This method does not modify {@code readerIndex} or

  • {@code writerIndex} of this buffer.

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     {@code index + 8} is greater than {@code this.capacity}
    

*/

public abstract long getLong(int index);

/**

  • Gets a 2-byte UTF-16 character at the specified absolute

  • {@code index} in this buffer. This method does not modify

  • {@code readerIndex} or {@code writerIndex} of this buffer.

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     {@code index + 2} is greater than {@code this.capacity}
    

*/

public abstract char getChar(int index);

/**

  • Gets a 32-bit floating point number at the specified absolute

  • {@code index} in this buffer. This method does not modify

  • {@code readerIndex} or {@code writerIndex} of this buffer.

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     {@code index + 4} is greater than {@code this.capacity}
    

*/

public abstract float getFloat(int index);

/**

  • Gets a 64-bit floating point number at the specified absolute

  • {@code index} in this buffer. This method does not modify

  • {@code readerIndex} or {@code writerIndex} of this buffer.

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     {@code index + 8} is greater than {@code this.capacity}
    

*/

public abstract double getDouble(int index);

/**

  • Transfers this buffer’s data to the specified destination starting at

  • the specified absolute {@code index} until the destination becomes

  • non-writable. This method is basically same with

  • {@link #getBytes(int, ByteBuf, int, int)}, except that this

  • method increases the {@code writerIndex} of the destination by the

  • number of the transferred bytes while

  • {@link #getBytes(int, ByteBuf, int, int)} does not.

  • This method does not modify {@code readerIndex} or {@code writerIndex} of

  • the source buffer (i.e. {@code this}).

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     if {@code index + dst.writableBytes} is greater than
    
  •        {@code this.capacity}
    

*/

public abstract ByteBuf getBytes(int index, ByteBuf dst);

/**

  • Transfers this buffer’s data to the specified destination starting at

  • the specified absolute {@code index}. This method is basically same

  • with {@link #getBytes(int, ByteBuf, int, int)}, except that this

  • method increases the {@code writerIndex} of the destination by the

  • number of the transferred bytes while

  • {@link #getBytes(int, ByteBuf, int, int)} does not.

  • This method does not modify {@code readerIndex} or {@code writerIndex} of

  • the source buffer (i.e. {@code this}).

  • @param length the number of bytes to transfer

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0},
    
  •     if {@code index + length} is greater than
    
  •        {@code this.capacity}, or
    
  •     if {@code length} is greater than {@code dst.writableBytes}
    

*/

public abstract ByteBuf getBytes(int index, ByteBuf dst, int length);

/**

  • Transfers this buffer’s data to the specified destination starting at

  • the specified absolute {@code index}.

  • This method does not modify {@code readerIndex} or {@code writerIndex}

  • of both the source (i.e. {@code this}) and the destination.

  • @param dstIndex the first index of the destination

  • @param length the number of bytes to transfer

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0},
    
  •     if the specified {@code dstIndex} is less than {@code 0},
    
  •     if {@code index + length} is greater than
    
  •        {@code this.capacity}, or
    
  •     if {@code dstIndex + length} is greater than
    
  •        {@code dst.capacity}
    

*/

public abstract ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length);

/**

  • Transfers this buffer’s data to the specified destination starting at

  • the specified absolute {@code index}.

  • This method does not modify {@code readerIndex} or {@code writerIndex} of

  • this buffer

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     if {@code index + dst.length} is greater than
    
  •        {@code this.capacity}
    

*/

public abstract ByteBuf getBytes(int index, byte[] dst);

/**

  • Transfers this buffer’s data to the specified destination starting at

  • the specified absolute {@code index}.

  • This method does not modify {@code readerIndex} or {@code writerIndex}

  • of this buffer.

  • @param dstIndex the first index of the destination

  • @param length the number of bytes to transfer

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0},
    
  •     if the specified {@code dstIndex} is less than {@code 0},
    
  •     if {@code index + length} is greater than
    
  •        {@code this.capacity}, or
    
  •     if {@code dstIndex + length} is greater than
    
  •        {@code dst.length}
    

*/

public abstract ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length);

/**

  • Transfers this buffer’s data to the specified destination starting at

  • the specified absolute {@code index} until the destination’s position

  • reaches its limit.

  • This method does not modify {@code readerIndex} or {@code writerIndex} of

  • this buffer while the destination’s {@code position} will be increased.

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     if {@code index + dst.remaining()} is greater than
    
  •        {@code this.capacity}
    

*/

public abstract ByteBuf getBytes(int index, ByteBuffer dst);

/**

  • Transfers this buffer’s data to the specified stream starting at the

  • specified absolute {@code index}.

  • This method does not modify {@code readerIndex} or {@code writerIndex} of

  • this buffer.

  • @param length the number of bytes to transfer

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     if {@code index + length} is greater than
    
  •        {@code this.capacity}
    
  • @throws IOException

  •     if the specified stream threw an exception during I/O
    

*/

public abstract ByteBuf getBytes(int index, OutputStream out, int length) throws IOException;

/**

  • Transfers this buffer’s data to the specified channel starting at the

  • specified absolute {@code index}.

  • This method does not modify {@code readerIndex} or {@code writerIndex} of

  • this buffer.

  • @param length the maximum number of bytes to transfer

  • @return the actual number of bytes written out to the specified channel

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     if {@code index + length} is greater than
    
  •        {@code this.capacity}
    
  • @throws IOException

  •     if the specified channel threw an exception during I/O
    

*/

public abstract int getBytes(int index, GatheringByteChannel out, int length) throws IOException;

重点解读如下API,其他类似。

  • public abstract ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length)

从源 ByteBuf 的 index 处开始读 length 个字节到目标 byte 中,从 dstIndex 开始。

2.2.4 随机写API

/**

  • Sets the specified boolean at the specified absolute {@code index} in this

  • buffer.

  • This method does not modify {@code readerIndex} or {@code writerIndex} of

  • this buffer.

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     {@code index + 1} is greater than {@code this.capacity}
    

*/

public abstract ByteBuf setBoolean(int index, boolean value);

/**

  • Sets the specified byte at the specified absolute {@code index} in this

  • buffer. The 24 high-order bits of the specified value are ignored.

  • This method does not modify {@code readerIndex} or {@code writerIndex} of

  • this buffer.

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     {@code index + 1} is greater than {@code this.capacity}
    

*/

public abstract ByteBuf setByte(int index, int value);

/**

  • Sets the specified 16-bit short integer at the specified absolute

  • {@code index} in this buffer. The 16 high-order bits of the specified

  • value are ignored.

  • This method does not modify {@code readerIndex} or {@code writerIndex} of

  • this buffer.

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     {@code index + 2} is greater than {@code this.capacity}
    

*/

public abstract ByteBuf setShort(int index, int value);

/**

  • Sets the specified 24-bit medium integer at the specified absolute

  • {@code index} in this buffer. Please note that the most significant

  • byte is ignored in the specified value.

  • This method does not modify {@code readerIndex} or {@code writerIndex} of

  • this buffer.

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     {@code index + 3} is greater than {@code this.capacity}
    

*/

public abstract ByteBuf setMedium(int index, int value);

/**

  • Sets the specified 32-bit integer at the specified absolute

  • {@code index} in this buffer.

  • This method does not modify {@code readerIndex} or {@code writerIndex} of

  • this buffer.

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     {@code index + 4} is greater than {@code this.capacity}
    

*/

public abstract ByteBuf setInt(int index, int value);

/**

  • Sets the specified 64-bit long integer at the specified absolute

  • {@code index} in this buffer.

  • This method does not modify {@code readerIndex} or {@code writerIndex} of

  • this buffer.

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     {@code index + 8} is greater than {@code this.capacity}
    

*/

public abstract ByteBuf setLong(int index, long value);

/**

  • Sets the specified 2-byte UTF-16 character at the specified absolute

  • {@code index} in this buffer.

  • The 16 high-order bits of the specified value are ignored.

  • This method does not modify {@code readerIndex} or {@code writerIndex} of

  • this buffer.

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     {@code index + 2} is greater than {@code this.capacity}
    

*/

public abstract ByteBuf setChar(int index, int value);

/**

  • Sets the specified 32-bit floating-point number at the specified

  • absolute {@code index} in this buffer.

  • This method does not modify {@code readerIndex} or {@code writerIndex} of

  • this buffer.

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     {@code index + 4} is greater than {@code this.capacity}
    

*/

最后

小编精心为大家准备了一手资料

以上Java高级架构资料、源码、笔记、视频。Dubbo、Redis、设计模式、Netty、zookeeper、Spring cloud、分布式、高并发等架构技术

【附】架构书籍

  1. BAT面试的20道高频数据库问题解析
  2. Java面试宝典
  3. Netty实战
  4. 算法

BATJ面试要点及Java架构师进阶资料

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Java)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
is greater than {@code this.capacity}

*/

public abstract ByteBuf setLong(int index, long value);

/**

  • Sets the specified 2-byte UTF-16 character at the specified absolute

  • {@code index} in this buffer.

  • The 16 high-order bits of the specified value are ignored.

  • This method does not modify {@code readerIndex} or {@code writerIndex} of

  • this buffer.

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     {@code index + 2} is greater than {@code this.capacity}
    

*/

public abstract ByteBuf setChar(int index, int value);

/**

  • Sets the specified 32-bit floating-point number at the specified

  • absolute {@code index} in this buffer.

  • This method does not modify {@code readerIndex} or {@code writerIndex} of

  • this buffer.

  • @throws IndexOutOfBoundsException

  •     if the specified {@code index} is less than {@code 0} or
    
  •     {@code index + 4} is greater than {@code this.capacity}
    

*/

最后

小编精心为大家准备了一手资料

[外链图片转存中…(img-xpUpbzAD-1713566965833)]

[外链图片转存中…(img-fzVSRsm2-1713566965833)]

以上Java高级架构资料、源码、笔记、视频。Dubbo、Redis、设计模式、Netty、zookeeper、Spring cloud、分布式、高并发等架构技术

【附】架构书籍

  1. BAT面试的20道高频数据库问题解析
  2. Java面试宝典
  3. Netty实战
  4. 算法

[外链图片转存中…(img-7ifC8IJe-1713566965834)]

BATJ面试要点及Java架构师进阶资料

[外链图片转存中…(img-wUSTCSws-1713566965834)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Java)
[外链图片转存中…(img-YKZyxFW3-1713566965834)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 27
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值