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