jdk/src/java.base/share/classes/java/nio/Buffer.java
author zmajo
Fri, 03 Jul 2015 07:23:45 +0200
changeset 31671 362e0c0acece
parent 27292 7ff4b24b33ce
child 31873 87b015c2cd36
permissions -rw-r--r--
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics Summary: Annotate possibly intrinsified methods with @HotSpotIntrinsicCandidate. Add checks omitted by intrinsics to the library code. Add CheckIntrinsics flags to check consistency of intrinsics. Reviewed-by: jrose, kvn, thartmann, vlivanov, abuckley, darcy, ascarpino, briangoetz, alanb, aph, dnsimon
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 18574
diff changeset
     2
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.nio;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
17922
d56eec572de5 8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents: 10325
diff changeset
    28
import java.util.Spliterator;
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 27292
diff changeset
    29
import jdk.internal.HotSpotIntrinsicCandidate;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * A container for data of a specific primitive type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <p> A buffer is a linear, finite sequence of elements of a specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * primitive type.  Aside from its content, the essential properties of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * buffer are its capacity, limit, and position: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *   <p> A buffer's <i>capacity</i> is the number of elements it contains.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *   capacity of a buffer is never negative and never changes.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *   <p> A buffer's <i>limit</i> is the index of the first element that should
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *   not be read or written.  A buffer's limit is never negative and is never
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *   greater than its capacity.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *   <p> A buffer's <i>position</i> is the index of the next element to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *   read or written.  A buffer's position is never negative and is never
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *   greater than its limit.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p> There is one subclass of this class for each non-boolean primitive type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
    56
 * <h2> Transferring data </h2>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p> Each subclass of this class defines two categories of <i>get</i> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <i>put</i> operations: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *   <p> <i>Relative</i> operations read or write one or more elements starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *   at the current position and then increment the position by the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *   elements transferred.  If the requested transfer exceeds the limit then a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *   relative <i>get</i> operation throws a {@link BufferUnderflowException}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *   and a relative <i>put</i> operation throws a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *   BufferOverflowException}; in either case, no data is transferred.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *   <p> <i>Absolute</i> operations take an explicit element index and do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *   affect the position.  Absolute <i>get</i> and <i>put</i> operations throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *   an {@link IndexOutOfBoundsException} if the index argument exceeds the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *   limit.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <p> Data may also, of course, be transferred in to or out of a buffer by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * I/O operations of an appropriate channel, which are always relative to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * current position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
    82
 * <h2> Marking and resetting </h2>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <p> A buffer's <i>mark</i> is the index to which its position will be reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * when the {@link #reset reset} method is invoked.  The mark is not always
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * defined, but when it is defined it is never negative and is never greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * than the position.  If the mark is defined then it is discarded when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * position or the limit is adjusted to a value smaller than the mark.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * mark is not defined then invoking the {@link #reset reset} method causes an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * {@link InvalidMarkException} to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
    93
 * <h2> Invariants </h2>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <p> The following invariant holds for the mark, position, limit, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * capacity values:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *     <tt>0</tt> <tt>&lt;=</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *     <i>mark</i> <tt>&lt;=</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *     <i>position</i> <tt>&lt;=</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *     <i>limit</i> <tt>&lt;=</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *     <i>capacity</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * <p> A newly-created buffer always has a position of zero and a mark that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * undefined.  The initial limit may be zero, or it may be some other value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * that depends upon the type of the buffer and the manner in which it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * constructed.  Each element of a newly-allocated buffer is initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
   113
 * <h2> Clearing, flipping, and rewinding </h2>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * <p> In addition to methods for accessing the position, limit, and capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * values and for marking and resetting, this class also defines the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * operations upon buffers:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *   <li><p> {@link #clear} makes a buffer ready for a new sequence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *   channel-read or relative <i>put</i> operations: It sets the limit to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *   capacity and the position to zero.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *   <li><p> {@link #flip} makes a buffer ready for a new sequence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *   channel-write or relative <i>get</i> operations: It sets the limit to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *   current position and then sets the position to zero.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *   <li><p> {@link #rewind} makes a buffer ready for re-reading the data that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 *   it already contains: It leaves the limit unchanged and sets the position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 *   to zero.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
   136
 * <h2> Read-only buffers </h2>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * <p> Every buffer is readable, but not every buffer is writable.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * mutation methods of each buffer class are specified as <i>optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * operations</i> that will throw a {@link ReadOnlyBufferException} when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * invoked upon a read-only buffer.  A read-only buffer does not allow its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * content to be changed, but its mark, position, and limit values are mutable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * Whether or not a buffer is read-only may be determined by invoking its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * {@link #isReadOnly isReadOnly} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 *
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
   147
 * <h2> Thread safety </h2>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * <p> Buffers are not safe for use by multiple concurrent threads.  If a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * buffer is to be used by more than one thread then access to the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * should be controlled by appropriate synchronization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 *
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
   154
 * <h2> Invocation chaining </h2>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * <p> Methods in this class that do not otherwise have a value to return are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * specified to return the buffer upon which they are invoked.  This allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * method invocations to be chained; for example, the sequence of statements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * b.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * b.position(23);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * b.limit(42);</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * can be replaced by the single, more compact statement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * b.flip().position(23).limit(42);</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * @author JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
public abstract class Buffer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
17922
d56eec572de5 8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents: 10325
diff changeset
   178
    /**
d56eec572de5 8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents: 10325
diff changeset
   179
     * The characteristics of Spliterators that traverse and split elements
d56eec572de5 8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents: 10325
diff changeset
   180
     * maintained in Buffers.
d56eec572de5 8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents: 10325
diff changeset
   181
     */
d56eec572de5 8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents: 10325
diff changeset
   182
    static final int SPLITERATOR_CHARACTERISTICS =
d56eec572de5 8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents: 10325
diff changeset
   183
        Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.ORDERED;
d56eec572de5 8014854: (bf) CharBuffer.chars too slow with default implementation
alanb
parents: 10325
diff changeset
   184
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    // Invariants: mark <= position <= limit <= capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    private int mark = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    private int position = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private int limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private int capacity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    // Used only by direct buffers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    // NOTE: hoisted here for speed in JNI GetDirectBufferAddress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    long address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    // Creates a new buffer with the given mark, position, limit, and capacity,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    // after checking invariants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    Buffer(int mark, int pos, int lim, int cap) {       // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (cap < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            throw new IllegalArgumentException("Negative capacity: " + cap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        this.capacity = cap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        limit(lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        position(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (mark >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            if (mark > pos)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                throw new IllegalArgumentException("mark > position: ("
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                                                   + mark + " > " + pos + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            this.mark = mark;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 17922
diff changeset
   213
     * Returns this buffer's capacity.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @return  The capacity of this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public final int capacity() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return capacity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 17922
diff changeset
   222
     * Returns this buffer's position.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @return  The position of this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public final int position() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Sets this buffer's position.  If the mark is defined and larger than the
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 17922
diff changeset
   232
     * new position then it is discarded.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @param  newPosition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *         The new position value; must be non-negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *         and no larger than the current limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @return  This buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *          If the preconditions on <tt>newPosition</tt> do not hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
27292
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   243
    public Buffer position(int newPosition) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if ((newPosition > limit) || (newPosition < 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        position = newPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (mark > position) mark = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 17922
diff changeset
   252
     * Returns this buffer's limit.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @return  The limit of this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    public final int limit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        return limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Sets this buffer's limit.  If the position is larger than the new limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * then it is set to the new limit.  If the mark is defined and larger than
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 17922
diff changeset
   263
     * the new limit then it is discarded.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @param  newLimit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *         The new limit value; must be non-negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *         and no larger than this buffer's capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @return  This buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *          If the preconditions on <tt>newLimit</tt> do not hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
27292
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   274
    public Buffer limit(int newLimit) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if ((newLimit > capacity) || (newLimit < 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        limit = newLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (position > limit) position = limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (mark > limit) mark = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 17922
diff changeset
   284
     * Sets this buffer's mark at its position.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @return  This buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
27292
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   288
    public Buffer mark() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        mark = position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Resets this buffer's position to the previously-marked position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * <p> Invoking this method neither changes nor discards the mark's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * value. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @return  This buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @throws  InvalidMarkException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *          If the mark has not been set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
27292
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   304
    public Buffer reset() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        int m = mark;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (m < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            throw new InvalidMarkException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        position = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * Clears this buffer.  The position is set to zero, the limit is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * the capacity, and the mark is discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * <p> Invoke this method before using a sequence of channel-read or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * <i>put</i> operations to fill this buffer.  For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * buf.clear();     // Prepare buffer for reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * in.read(buf);    // Read data</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * <p> This method does not actually erase the data in the buffer, but it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * is named as if it did because it will most often be used in situations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * in which that might as well be the case. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @return  This buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     */
27292
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   329
    public Buffer clear() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        position = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        limit = capacity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        mark = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * Flips this buffer.  The limit is set to the current position and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * the position is set to zero.  If the mark is defined then it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * <p> After a sequence of channel-read or <i>put</i> operations, invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * this method to prepare for a sequence of channel-write or relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * <i>get</i> operations.  For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * buf.put(magic);    // Prepend header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * in.read(buf);      // Read data into rest of buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * buf.flip();        // Flip buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * out.write(buf);    // Write header + data to channel</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * <p> This method is often used in conjunction with the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * java.nio.ByteBuffer#compact compact} method when transferring data from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * one place to another.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @return  This buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     */
27292
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   357
    public Buffer flip() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        limit = position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        position = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        mark = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * Rewinds this buffer.  The position is set to zero and the mark is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * <p> Invoke this method before a sequence of channel-write or <i>get</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * operations, assuming that the limit has already been set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * appropriately.  For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * out.write(buf);    // Write remaining data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * buf.rewind();      // Rewind buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * buf.get(array);    // Copy data into array</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @return  This buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     */
27292
7ff4b24b33ce 4774077: Use covariant return types in the NIO buffer hierarchy
rwarburton
parents: 25859
diff changeset
   379
    public Buffer rewind() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        position = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        mark = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Returns the number of elements between the current position and the
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 17922
diff changeset
   387
     * limit.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @return  The number of elements remaining in this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    public final int remaining() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return limit - position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Tells whether there are any elements between the current position and
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 17922
diff changeset
   397
     * the limit.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @return  <tt>true</tt> if, and only if, there is at least one element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *          remaining in this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    public final boolean hasRemaining() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        return position < limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    /**
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 17922
diff changeset
   407
     * Tells whether or not this buffer is read-only.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @return  <tt>true</tt> if, and only if, this buffer is read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public abstract boolean isReadOnly();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * Tells whether or not this buffer is backed by an accessible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * <p> If this method returns <tt>true</tt> then the {@link #array() array}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * and {@link #arrayOffset() arrayOffset} methods may safely be invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @return  <tt>true</tt> if, and only if, this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *          is backed by an array and is not read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    public abstract boolean hasArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * Returns the array that backs this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * buffer&nbsp;&nbsp;<i>(optional operation)</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * <p> This method is intended to allow array-backed buffers to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * passed to native code more efficiently. Concrete subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * provide more strongly-typed return values for this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * <p> Modifications to this buffer's content will cause the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * array's content to be modified, and vice versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * <p> Invoke the {@link #hasArray hasArray} method before invoking this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * method in order to ensure that this buffer has an accessible backing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * array.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @return  The array that backs this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @throws  ReadOnlyBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *          If this buffer is backed by an array but is read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @throws  UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *          If this buffer is not backed by an accessible array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    public abstract Object array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * Returns the offset within this buffer's backing array of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * element of the buffer&nbsp;&nbsp;<i>(optional operation)</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * <p> If this buffer is backed by an array then buffer position <i>p</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * corresponds to array index <i>p</i>&nbsp;+&nbsp;<tt>arrayOffset()</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * <p> Invoke the {@link #hasArray hasArray} method before invoking this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * method in order to ensure that this buffer has an accessible backing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * array.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @return  The offset within this buffer's array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *          of the first element of the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @throws  ReadOnlyBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *          If this buffer is backed by an array but is read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * @throws  UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *          If this buffer is not backed by an accessible array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    public abstract int arrayOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * Tells whether or not this buffer is
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 17922
diff changeset
   481
     * <a href="ByteBuffer.html#direct"><i>direct</i></a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @return  <tt>true</tt> if, and only if, this buffer is direct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    public abstract boolean isDirect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    // -- Package-private methods for bounds checking, etc. --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * Checks the current position against the limit, throwing a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * BufferUnderflowException} if it is not smaller than the limit, and then
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 17922
diff changeset
   495
     * increments the position.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @return  The current position value, before it is incremented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
    final int nextGetIndex() {                          // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        if (position >= limit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            throw new BufferUnderflowException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        return position++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    final int nextGetIndex(int nb) {                    // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        if (limit - position < nb)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            throw new BufferUnderflowException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        int p = position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        position += nb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * Checks the current position against the limit, throwing a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * BufferOverflowException} if it is not smaller than the limit, and then
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 17922
diff changeset
   516
     * increments the position.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @return  The current position value, before it is incremented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    final int nextPutIndex() {                          // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        if (position >= limit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            throw new BufferOverflowException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        return position++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    final int nextPutIndex(int nb) {                    // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        if (limit - position < nb)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            throw new BufferOverflowException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        int p = position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        position += nb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * Checks the given index against the limit, throwing an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * IndexOutOfBoundsException} if it is not smaller than the limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * or is smaller than zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 27292
diff changeset
   539
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    final int checkIndex(int i) {                       // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        if ((i < 0) || (i >= limit))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    final int checkIndex(int i, int nb) {               // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        if ((i < 0) || (nb > limit - i))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    final int markValue() {                             // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        return mark;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
10325
b72c20cd583a 7047325: Internal API to improve management of direct buffers
coffeys
parents: 5506
diff changeset
   556
    final void truncate() {                             // package-private
b72c20cd583a 7047325: Internal API to improve management of direct buffers
coffeys
parents: 5506
diff changeset
   557
        mark = -1;
b72c20cd583a 7047325: Internal API to improve management of direct buffers
coffeys
parents: 5506
diff changeset
   558
        position = 0;
b72c20cd583a 7047325: Internal API to improve management of direct buffers
coffeys
parents: 5506
diff changeset
   559
        limit = 0;
b72c20cd583a 7047325: Internal API to improve management of direct buffers
coffeys
parents: 5506
diff changeset
   560
        capacity = 0;
b72c20cd583a 7047325: Internal API to improve management of direct buffers
coffeys
parents: 5506
diff changeset
   561
    }
b72c20cd583a 7047325: Internal API to improve management of direct buffers
coffeys
parents: 5506
diff changeset
   562
1634
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   563
    final void discardMark() {                          // package-private
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   564
        mark = -1;
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   565
    }
3871c2046043 6593946: (bf) X-Buffer.compact() does not discard mark as specified
alanb
parents: 2
diff changeset
   566
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    static void checkBounds(int off, int len, int size) { // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        if ((off | len | (off + len) | (size - (off + len))) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
}