/**
* Tells whether there are any elements between the current position and
* the limit.
*
* @return <tt>true</tt> if, and only if, there is at least one element
* remaining in this buffer
*/
public final boolean hasRemaining() {
return position < limit;
}
final char[] hb; // Non-null only for heap buffers
protected int ix(int i) { //java.nio.HeapCharBuffer
return i + offset;
}
public char get() {
return hb[ix(nextGetIndex())];
}
/**
* Returns the number of elements between the current position and the
* limit.
*
* @return The number of elements remaining in this buffer
*/
public final int remaining() {
return limit - position;
}
for (int i=0; buffer.hasRemaining(), i++) {
myByteArray[i] = buffer.get();
}
int count = buffer.remaining();
for (int i=0; i<count , i++) {
myByteArray[i] = buffer.get();
}
以前总觉得是第二段 for 循环的代码好,因为它没有重复计算 buffer.remaining()
现在才知道原来还有另一个角度,当使用多线程时,就是第一段的性能更快了。
另外增加了 hasRemaining,get 方法的源码,然后因为第二段 for 循环的终止条件是固定值,所以它是单线程跑的,
但是第一段的 for 循环的终止值是每次算出来的,所以可以多线程调用。
java+NIO 中文版