NIO与Socket笔记 : CharBuffer 类的 API 使用

CharBuffer类提供一个字符( char)序列缓冲区 。

存储方式依旧为数组方式存储.数组类型为 char 类型.

新增依旧是根据下标直接对数组进行操作,通过 postion 指针进行指向.

public CharBuffer put(char x) {

    hb[ix(nextPutIndex())] = x;
    return this;
}

/**
 * Checks the current position against the limit, throwing a {@link
 * BufferOverflowException} if it is not smaller than the limit, and then
 * increments the position.
 *
 * @return  The current position value, before it is incremented
 */
final int nextPutIndex() {                          // package-private
    if (position >= limit)
        throw new BufferOverflowException();
    return position++;
}

public CharBuffer put(int i, char x) {

    hb[ix(checkIndex(i))] = x;
    return this;


}

创建方式依旧是两种:

1. CharBuffer cb = CharBuffer.allocate(100);

2. CharBuffer.wrap(charArray);

char[] charArray = {'1', '2', '3', '4', '5'};

CharBuffer cb = CharBuffer.wrap(charArray);

获取缓存信息:

postion:  charbuffer.position()

remaining:  charbuffer.remaining()

length:   charbuffer. length()

猜你喜欢

转载自blog.csdn.net/zhanglong_4444/article/details/88963625