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