美文网首页
NIO编程之buffer重要属性详见

NIO编程之buffer重要属性详见

作者: 柴崎越 | 来源:发表于2019-02-26 21:33 被阅读0次

一,实例代码

    public static void main(String[] args) {
        IntBuffer buffer= IntBuffer.allocate(10);
        for(int i=0;i<5;i++)
        {
            int randomNumber=new SecureRandom().nextInt(20);
            buffer.put(randomNumber);
        }
        //读写切换
        buffer.flip();
        while(buffer.hasRemaining())
        {
            System.out.println(buffer.get());
        }
    }
}
运行结果: 图片.png

二,源代码

图片.png
在buffer的类文件中,翻到第39行及其附近(以下翻译不是源码的内容)
/**
 * A container for data of a specific primitive type.
 *一个存放特定原生数据的容器
 * <p> A buffer is a linear, finite sequence of elements of a specific
 * primitive type.  Aside from its content, the essential properties of a
 * buffer are its capacity, limit, and position: </p>
 *最重要的除了内容,最基本的三个属性为capacity,limit,position
 * <blockquote>
 *
 *   <p> A buffer's <i>capacity</i> is the number of elements it contains.  The
 *   capacity of a buffer is never negative and never changes.  </p>
 *   capacity是元素的容量,不为负数且从不改变
 *   <p> A buffer's <i>limit</i> is the index of the first element that should
 *   not be read or written.  A buffer's limit is never negative and is never
 *   greater than its capacity.  </p>
 *   第一个不能读的下标,不能为空,且不能大于容量(capacity)
 *   <p> A buffer's <i>position</i> is the index of the next element to be
 *   read or written.  A buffer's position is never negative and is never
 *   greater than its limit.  </p>
 *  position指向下一个读或者写的位置,不能大于limit
 * </blockquote>

三,图解

图片.png
图片.png 图片.png

相关文章

网友评论

      本文标题:NIO编程之buffer重要属性详见

      本文链接:https://www.haomeiwen.com/subject/tkaoyqtx.html