java.nio.Bufferクラスの詳細

11493 ワード

1.属性


ツールバーの
  • capacity(容量)
  • limit(制限)
  • position(位置)
  • mark(マーク)
  • mark <= position <= limit <= capacity
    ソースコード
     // Invariants: mark <= position <= limit <= capacity
        private int mark = -1;
        private int position = 0;
        private int limit;
        private int capacity
    

    説明:
  • capacityは、要素を含む数、すなわち容量
  • を表す.
  • limitは、バッファ内の最初の要素の読み取りまたは書き込みを制限index
  • を表す.
  • positionは、次の要素を読み出すか書き込むindex
  • を表す.
  • markタグを設定し、現在のpositionの位置をタグし、reset()メソッドに従って前回タグのposition位置をリセットします.

  • 2.非抽象的な方法


    コンストラクタ
        // Creates a new buffer with the given mark, position, limit, and capacity,
        // after checking invariants.
        //
        Buffer(int mark, int pos, int lim, int cap) {       // package-private
            if (cap < 0)
                throw new IllegalArgumentException("Negative capacity: " + cap);
            this.capacity = cap;
            limit(lim);
            position(pos);
            if (mark >= 0) {
                if (mark > pos)
                    throw new IllegalArgumentException("mark > position: ("
                                                       + mark + " > " + pos + ")");
                this.mark = mark;
            }
        }
    
  • は、mark、position、limit、and capacity属性を持つBufferを作成し、このメソッドパッケージ内に表示されます.
  • は、capacity属性が0未満であるか否かを判断し、0未満であると不正パラメータ異常を放出する.

  • 取得容量(capacity)
        /**
         * Returns this buffer's capacity.
         *
         * @return  The capacity of this buffer
         */
        public final int capacity() {
            return capacity;
        }
    
  • はcapacity値を返します.

  • 取得位置(position)
        /**
         * Returns this buffer's position.
         *
         * @return  The position of this buffer
         */
        public final int position() {
            return position;
        }
    
  • はposition値を返します.

  • 設置位置(position)
        /**
         * Sets this buffer's position.  If the mark is defined and larger than the
         * new position then it is discarded.
         *
         * @param  newPosition
         *         The new position value; must be non-negative
         *         and no larger than the current limit
         *
         * @return  This buffer
         *
         * @throws  IllegalArgumentException
         *          If the preconditions on newPosition do not hold
         */
        public final Buffer position(int newPosition) {
            if ((newPosition > limit) || (newPosition < 0))
                throw new IllegalArgumentException();
            position = newPosition;
            if (mark > position) mark = -1;
            return this;
        }
    
  • position値を設定します.
  • から入力された新しいposition値がlimit(制限)より大きいか0より小さい場合、不正なパラメータ異常が放出される.
  • mark(タグ)が付与されたposition値より大きい場合、廃棄フラグ(mark=−1).

  • 取得制限(limit)
        /**
         * Returns this buffer's limit.
         *
         * @return  The limit of this buffer
         */
        public final int limit() {
            return limit;
        }
    
  • はlimit値を返します.

  • 制限の設定(limit)
        /**
         * Sets this buffer's limit.  If the position is larger than the new limit
         * then it is set to the new limit.  If the mark is defined and larger than
         * the new limit then it is discarded.
         *
         * @param  newLimit
         *         The new limit value; must be non-negative
         *         and no larger than this buffer's capacity
         *
         * @return  This buffer
         *
         * @throws  IllegalArgumentException
         *          If the preconditions on newLimit do not hold
         */
        public final Buffer limit(int newLimit) {
            if ((newLimit > capacity) || (newLimit < 0))
                throw new IllegalArgumentException();
            limit = newLimit;
            if (position > limit) position = limit;
            if (mark > limit) mark = -1;
            return this;
        }
    
  • limit値を設定します.
  • から入力新しいlimit値がcapacityより大きいか0より小さい場合、不正パラメータ異常
  • が投げ出される.
  • positionがlimitより大きい場合、position値はlimitの値
  • に設定.
  • markがlimitより大きい場合、mark
  • を廃止する
    タグの設定(mark)
         /**
         * Sets this buffer's mark at its position.
         *
         * @return  This buffer
         */
        public final Buffer mark() {
            mark = position;
            return this;
        }
    
  • 設定フラグの値はpositionであり、positionを記録する値に相当し、reset()メソッドを使用する場合にposition値をリセットする.

  • reset()リセット方法
        /**
         * Resets this buffer's position to the previously-marked position.
         *
         * 

    Invoking this method neither changes nor discards the mark's * value.

    * * @return This buffer * * @throws InvalidMarkException * If the mark has not been set */ public final Buffer reset() { int m = mark; if (m < 0) throw new InvalidMarkException(); position = m; return this; }
  • この方法は、タグが有効な場合にのみ使用できます.すなわち、mrakは0
  • 以上です.
  • positionの値をリセットし、markの値をposition
  • に割り当てる
    clear()消去方法
        /**
         * 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.
    *
    * @return This buffer
    */
    public final Buffer clear() {
    position = 0;
    limit = capacity;
    mark = -1;
    return this;
    }
  • Bufferの 、すなわちposition=0、limit=capacity、mark=-1
  • をクリア
    flip()
       /**
         * 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 {@link
    * java.nio.ByteBuffer#compact compact} method when transferring data from
    * one place to another.
    *
    * @return This buffer
    */
    public final Buffer flip() {
    limit = position;
    position = 0;
    mark = -1;
    return this;
    }
  • Bufferを し、Bufferを します.
  • position をlimitに し、position を0に し、タグを する.

  • rewind()ロールバックメソッド
        /**
         * Rewinds this buffer.  The position is set to zero and the mark is
         * discarded.
         *
         * 

    Invoke this method before a sequence of channel-write or get * operations, assuming that the limit has already been set * appropriately. For example: * *

    
         * out.write(buf);    // Write remaining data
         * buf.rewind();      // Rewind buffer
         * buf.get(array);    // Copy data into array
    *
    * @return This buffer
    */
    public final Buffer rewind() {
    position = 0;
    mark = -1;
    return this;
    }
  • Bufferをロールバックし、Bufferを します.
  • position を0に し、フラグを します.

  • remaining() り
        /**
         * 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;
        }
    
  • は、 りの の を します:limit-position
  • hasRemaining()に りの があるかどうか
        /**
         * Tells whether there are any elements between the current position and
         * the limit.
         *
         * @return  true if, and only if, there is at least one element
         *          remaining in this buffer
         */
        public final boolean hasRemaining() {
            return position < limit;
        }
    
  • Bufferに があるか かを する
  • .

    3. な


    isReadOnly() み り かどうか
        /**
         * Tells whether or not this buffer is read-only.
         *
         * @return  true if, and only if, this buffer is read-only
         */
        public abstract boolean isReadOnly();
    

    hasArray() を むかどうか
        /**
         * Tells whether or not this buffer is read-only.
         *
         * @return  true if, and only if, this buffer is read-only
         */
        public abstract boolean isReadOnly();
    

    Array()
        /**
         * Returns the array that backs this
         * buffer  (optional operation).
         *
         * 

    This method is intended to allow array-backed buffers to be * passed to native code more efficiently. Concrete subclasses * provide more strongly-typed return values for this method. * *

    Modifications to this buffer's content will cause the returned * array's content to be modified, and vice versa. * *

    Invoke the {@link #hasArray hasArray} method before invoking this * method in order to ensure that this buffer has an accessible backing * array.

    * * @return The array that backs this buffer * * @throws ReadOnlyBufferException * If this buffer is backed by an array but is read-only * * @throws UnsupportedOperationException * If this buffer is not backed by an accessible array * * @since 1.6 */ public abstract Object array();

    ArrayOffset() オフセットの
        /**
         * Returns the offset within this buffer's backing array of the first
         * element of the buffer  (optional operation).
         *
         * 

    If this buffer is backed by an array then buffer position p * corresponds to array index p + arrayOffset(). * *

    Invoke the {@link #hasArray hasArray} method before invoking this * method in order to ensure that this buffer has an accessible backing * array.

    * * @return The offset within this buffer's array * of the first element of the buffer * * @throws ReadOnlyBufferException * If this buffer is backed by an array but is read-only * * @throws UnsupportedOperationException * If this buffer is not backed by an accessible array * * @since 1.6 */ public abstract int arrayOffset();

    isDirect() バッファ
        /**
         * Tells whether or not this buffer is
         * direct.
         *
         * @return  true if, and only if, this buffer is direct
         *
         * @since 1.6
         */
        public abstract boolean isDirect();
    

    :
  • [ ]『Javaマルチスレッドプログラミングコア 』
  • [ ]『Java プログラミング:コアメソッドとフレームワーク』
  • [ ]『NIOとSocketプログラミング ガイド』