zoukankan      html  css  js  c++  java
  • java.nio.Buffer

     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:

    一。 三个位置

    • A buffer's position is the index of the next element to be read or written
    • A buffer's limit is the index of the first element that should not be read or written
    • A buffer's capacity is the number of elements it contains

     在其子类中都有一个static allocate(int size)方法,用来分配对应类型的缓冲区。The new buffer's position will be zero, its limit will be its capacity

    二。 两个模式


    三。 五个操作

    3.1 clear()


     makes a buffer ready for a new sequence of channel-read or relative put operations   

    Clears this buffer. The position is set to zero, the limit is set to the capacity, and the mark is discarded.
    Invoke this method before using a sequence of channel-read or put operations to fill this buffer. For example:
     buf.clear();     // Prepare buffer for reading
     in.read(buf);    // Read data
    This method does not actually erase the data in the buffer, but it is named as if it did because it will most often be used in situations in which that might as well be the case.
    public final Buffer clear() {
            position = 0;
            limit = capacity;
            mark = -1;
            return this;

    1             FileChannel in = new FileInputStream("data.txt").getChannel(),
    2                        out = new FileOutputStream("data2.txt").getChannel();
    4             ByteBuffer buffer = ByteBuffer.allocate(BSIZE);
    5             while(in.read(buffer) != -1) {
    6                   buffer.flip(); // Prepare for writing
    7                   out.write(buffer);
    8                   buffer.clear();  // Prepare for reading
    9             }

    3.2 flip()


    makes a buffer ready for a new sequence of channel-write or relative get operations:

        public final Buffer flip() {
            limit = position;
            position = 0;
            mark = -1;
            return this;

    1             // Read the file:
    2             FileChannel fc = new FileInputStream("data.txt").getChannel();
    3             ByteBuffer buff = ByteBuffer.allocate(BSIZE);
    4             fc.read(buff);
    5             buff.flip();
    6             while(buff.hasRemaining())
    7                 System.out.print((char)buff.get());
    8             }

    3.3 rewind()


    makes a buffer ready for re-reading the data that it already contains:

    Flips this buffer. The limit is set to the current position and then the position is set to zero. If the mark is defined then it is discarded.
    After a sequence of channel-read or put operations, invoke this method to prepare for a sequence of channel-write or relative get operations. For example:
     buf.put(magic);    // Prepend header
     in.read(buf);      // Read data into rest of buffer
     buf.flip();        // Flip buffer
     out.write(buf);    // Write header + data to channel
    This method is often used in conjunction with the compact method when transferring data from one place to another.
        public final Buffer rewind() {
            position = 0;
            mark = -1;
            return this;

    3.4 mark()


    public final Buffer mark() {
        mark = position;
        return this;

    3.5 reset()


    public final Buffer reset() {
        int m = mark;
        if (m < 0)
            throw new InvalidMarkException();
        position = m;
        return this;

    (1)A buffer's mark is the index to which its position will be reset when the reset method is invoked.

    (2)The mark is not always defined, but when it is defined it is never negative and is never greater than the position.

    (3)If the mark is defined then it is discarded when the position or the limit is adjusted to a value smaller than the mark.

    (4)If the mark is not defined then invoking the reset method causes an InvalidMarkException to be thrown.


      0 <= mark <= position <= limit <= capacity


    Buffer类用来提供为基本数据类型(primitive) 的数据提供一个容器。




    Java NIO学习笔记之二-图解ByteBuffer


    深入浅出NIO Channel和Buffer

    Java Buffer编程基础


    Cache VS Buffer

  • 相关阅读:
    [蓝桥] 基础练习 数列排序(java)
    Java 大数任意进制转换
  • 原文地址:https://www.cnblogs.com/yuyutianxia/p/3859086.html
Copyright © 2011-2022 走看看