Java 的 SocketChannel.read(ByteBuffer)
Posted
技术标签:
【中文标题】Java 的 SocketChannel.read(ByteBuffer)【英文标题】:Java's SocketChannel.read(ByteBuffer) 【发布时间】:2014-02-24 22:49:18 【问题描述】:似乎无法在 java 文档中找到答案:
SocketChannel.read(ByteBuffer) 是追加还是覆盖当前的ByteBuffer?
【问题讨论】:
Reads a sequence of bytes from this channel into the given buffer. Suppose that a byte sequence of length n is read, where 0 <= n <= r. This byte sequence will be transferred into the buffer so that the first byte in the sequence is at index p and the last byte is at index p + n - 1, where p is the buffer's position at the moment this method is invoked. Upon return the buffer's position will be equal to p + n; its limit will not have changed.
【参考方案1】:
答案在 ReadableByteChannel
的 Javadoc 中,SocketChannel
实现了:
尝试从通道读取最多 r 个字节,其中 r 是在调用此方法时缓冲区中剩余的字节数,即 dst.remaining()。
因此,不,它不会覆盖。
【讨论】:
以上是关于Java 的 SocketChannel.read(ByteBuffer)的主要内容,如果未能解决你的问题,请参考以下文章
折腾了我一周,原来Netty网络编程就是这么个破玩意儿!!!