< prev index next >

src/java.base/share/classes/java/nio/Buffer.java

Print this page




  94  * <h2> Invariants </h2>
  95  *
  96  * <p> The following invariant holds for the mark, position, limit, and
  97  * capacity values:
  98  *
  99  * <blockquote>
 100  *     {@code 0} {@code <=}
 101  *     <i>mark</i> {@code <=}
 102  *     <i>position</i> {@code <=}
 103  *     <i>limit</i> {@code <=}
 104  *     <i>capacity</i>
 105  * </blockquote>
 106  *
 107  * <p> A newly-created buffer always has a position of zero and a mark that is
 108  * undefined.  The initial limit may be zero, or it may be some other value
 109  * that depends upon the type of the buffer and the manner in which it is
 110  * constructed.  Each element of a newly-allocated buffer is initialized
 111  * to zero.
 112  *
 113  *
 114  * <h2> Clearing, flipping, and rewinding </h2>
 115  *
 116  * <p> In addition to methods for accessing the position, limit, and capacity
 117  * values and for marking and resetting, this class also defines the following
 118  * operations upon buffers:
 119  *
 120  * <ul>
 121  *
 122  *   <li><p> {@link #clear} makes a buffer ready for a new sequence of
 123  *   channel-read or relative <i>put</i> operations: It sets the limit to the
 124  *   capacity and the position to zero.  </p></li>
 125  *
 126  *   <li><p> {@link #flip} makes a buffer ready for a new sequence of
 127  *   channel-write or relative <i>get</i> operations: It sets the limit to the
 128  *   current position and then sets the position to zero.  </p></li>
 129  *
 130  *   <li><p> {@link #rewind} makes a buffer ready for re-reading the data that
 131  *   it already contains: It leaves the limit unchanged and sets the position
 132  *   to zero.  </p></li>
 133  *






 134  * </ul>
 135  *
 136  *
 137  * <h2> Read-only buffers </h2>
 138  *
 139  * <p> Every buffer is readable, but not every buffer is writable.  The
 140  * mutation methods of each buffer class are specified as <i>optional
 141  * operations</i> that will throw a {@link ReadOnlyBufferException} when
 142  * invoked upon a read-only buffer.  A read-only buffer does not allow its
 143  * content to be changed, but its mark, position, and limit values are mutable.
 144  * Whether or not a buffer is read-only may be determined by invoking its
 145  * {@link #isReadOnly isReadOnly} method.
 146  *
 147  *
 148  * <h2> Thread safety </h2>
 149  *
 150  * <p> Buffers are not safe for use by multiple concurrent threads.  If a
 151  * buffer is to be used by more than one thread then access to the buffer
 152  * should be controlled by appropriate synchronization.
 153  *


 550      * @throws  ReadOnlyBufferException
 551      *          If this buffer is backed by an array but is read-only
 552      *
 553      * @throws  UnsupportedOperationException
 554      *          If this buffer is not backed by an accessible array
 555      *
 556      * @since 1.6
 557      */
 558     public abstract int arrayOffset();
 559 
 560     /**
 561      * Tells whether or not this buffer is
 562      * <a href="ByteBuffer.html#direct"><i>direct</i></a>.
 563      *
 564      * @return  {@code true} if, and only if, this buffer is direct
 565      *
 566      * @since 1.6
 567      */
 568     public abstract boolean isDirect();
 569 








































 570 
 571     // -- Package-private methods for bounds checking, etc. --
 572 
 573     /**
 574      * Checks the current position against the limit, throwing a {@link
 575      * BufferUnderflowException} if it is not smaller than the limit, and then
 576      * increments the position.
 577      *
 578      * @return  The current position value, before it is incremented
 579      */
 580     final int nextGetIndex() {                          // package-private
 581         if (position >= limit)
 582             throw new BufferUnderflowException();
 583         return position++;
 584     }
 585 
 586     final int nextGetIndex(int nb) {                    // package-private
 587         if (limit - position < nb)
 588             throw new BufferUnderflowException();
 589         int p = position;




  94  * <h2> Invariants </h2>
  95  *
  96  * <p> The following invariant holds for the mark, position, limit, and
  97  * capacity values:
  98  *
  99  * <blockquote>
 100  *     {@code 0} {@code <=}
 101  *     <i>mark</i> {@code <=}
 102  *     <i>position</i> {@code <=}
 103  *     <i>limit</i> {@code <=}
 104  *     <i>capacity</i>
 105  * </blockquote>
 106  *
 107  * <p> A newly-created buffer always has a position of zero and a mark that is
 108  * undefined.  The initial limit may be zero, or it may be some other value
 109  * that depends upon the type of the buffer and the manner in which it is
 110  * constructed.  Each element of a newly-allocated buffer is initialized
 111  * to zero.
 112  *
 113  *
 114  * <h2> Additional operations </h2>
 115  *
 116  * <p> In addition to methods for accessing the position, limit, and capacity
 117  * values and for marking and resetting, this class also defines the following
 118  * operations upon buffers:
 119  *
 120  * <ul>
 121  *
 122  *   <li><p> {@link #clear} makes a buffer ready for a new sequence of
 123  *   channel-read or relative <i>put</i> operations: It sets the limit to the
 124  *   capacity and the position to zero.  </p></li>
 125  *
 126  *   <li><p> {@link #flip} makes a buffer ready for a new sequence of
 127  *   channel-write or relative <i>get</i> operations: It sets the limit to the
 128  *   current position and then sets the position to zero.  </p></li>
 129  *
 130  *   <li><p> {@link #rewind} makes a buffer ready for re-reading the data that
 131  *   it already contains: It leaves the limit unchanged and sets the position
 132  *   to zero.  </p></li>
 133  *
 134  *   <li><p> {@link #slice} creates a subsequence of a buffer: It leaves the
 135  *   limit and the position unchanged. </p></li>
 136  *
 137  *   <li><p> {@link #duplicate} creates a shallow copy of a buffer: It leaves
 138  *   the limit and the position unchanged. </p></li>
 139  *
 140  * </ul>
 141  *
 142  *
 143  * <h2> Read-only buffers </h2>
 144  *
 145  * <p> Every buffer is readable, but not every buffer is writable.  The
 146  * mutation methods of each buffer class are specified as <i>optional
 147  * operations</i> that will throw a {@link ReadOnlyBufferException} when
 148  * invoked upon a read-only buffer.  A read-only buffer does not allow its
 149  * content to be changed, but its mark, position, and limit values are mutable.
 150  * Whether or not a buffer is read-only may be determined by invoking its
 151  * {@link #isReadOnly isReadOnly} method.
 152  *
 153  *
 154  * <h2> Thread safety </h2>
 155  *
 156  * <p> Buffers are not safe for use by multiple concurrent threads.  If a
 157  * buffer is to be used by more than one thread then access to the buffer
 158  * should be controlled by appropriate synchronization.
 159  *


 556      * @throws  ReadOnlyBufferException
 557      *          If this buffer is backed by an array but is read-only
 558      *
 559      * @throws  UnsupportedOperationException
 560      *          If this buffer is not backed by an accessible array
 561      *
 562      * @since 1.6
 563      */
 564     public abstract int arrayOffset();
 565 
 566     /**
 567      * Tells whether or not this buffer is
 568      * <a href="ByteBuffer.html#direct"><i>direct</i></a>.
 569      *
 570      * @return  {@code true} if, and only if, this buffer is direct
 571      *
 572      * @since 1.6
 573      */
 574     public abstract boolean isDirect();
 575 
 576     /**
 577      * Creates a new buffer whose content is a shared subsequence of
 578      * this buffer's content.
 579      *
 580      * <p> The content of the new buffer will start at this buffer's current
 581      * position.  Changes to this buffer's content will be visible in the new
 582      * buffer, and vice versa; the two buffers' position, limit, and mark
 583      * values will be independent.
 584      *
 585      * <p> The new buffer's position will be zero, its capacity and its limit
 586      * will be the number of elements remaining in this buffer, its mark will be
 587      * undefined. The new buffer will be direct if, and only if, this buffer is
 588      * direct, and it will be read-only if, and only if, this buffer is
 589      * read-only.  </p>
 590      *
 591      * @return  The new buffer
 592      *
 593      * @since 9
 594      */
 595     public abstract Buffer slice();
 596 
 597     /**
 598      * Creates a new buffer that shares this buffer's content.
 599      *
 600      * <p> The content of the new buffer will be that of this buffer.  Changes
 601      * to this buffer's content will be visible in the new buffer, and vice
 602      * versa; the two buffers' position, limit, and mark values will be
 603      * independent.
 604      *
 605      * <p> The new buffer's capacity, limit, position and mark values will be
 606      * identical to those of this buffer. The new buffer will be direct if, and
 607      * only if, this buffer is direct, and it will be read-only if, and only if,
 608      * this buffer is read-only.  </p>
 609      *
 610      * @return  The new buffer
 611      *
 612      * @since 9
 613      */
 614     public abstract Buffer duplicate();
 615 
 616 
 617     // -- Package-private methods for bounds checking, etc. --
 618 
 619     /**
 620      * Checks the current position against the limit, throwing a {@link
 621      * BufferUnderflowException} if it is not smaller than the limit, and then
 622      * increments the position.
 623      *
 624      * @return  The current position value, before it is incremented
 625      */
 626     final int nextGetIndex() {                          // package-private
 627         if (position >= limit)
 628             throw new BufferUnderflowException();
 629         return position++;
 630     }
 631 
 632     final int nextGetIndex(int nb) {                    // package-private
 633         if (limit - position < nb)
 634             throw new BufferUnderflowException();
 635         int p = position;


< prev index next >