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