jdk/src/share/classes/java/nio/X-Buffer.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 1224 8c1bc7c5dd00
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#warn This file is preprocessed before being compiled
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
package java.nio;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#if[char]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#end[char]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * $A$ $fulltype$ buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p> This class defines {#if[byte]?six:four} categories of operations upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * $fulltype$ buffers:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *   <li><p> Absolute and relative {@link #get() </code><i>get</i><code>} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *   {@link #put($type$) </code><i>put</i><code>} methods that read and write
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *   single $fulltype$s; </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *   <li><p> Relative {@link #get($type$[]) </code><i>bulk get</i><code>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *   methods that transfer contiguous sequences of $fulltype$s from this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *   into an array; {#if[!byte]?and}</p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *   <li><p> Relative {@link #put($type$[]) </code><i>bulk put</i><code>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *   methods that transfer contiguous sequences of $fulltype$s from $a$
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *   $fulltype$ array{#if[char]?,&#32;a&#32;string,} or some other $fulltype$
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *   buffer into this buffer;{#if[!byte]?&#32;and} </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
#if[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *   <li><p> Absolute and relative {@link #getChar() </code><i>get</i><code>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *   and {@link #putChar(char) </code><i>put</i><code>} methods that read and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *   write values of other primitive types, translating them to and from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *   sequences of bytes in a particular byte order; </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *   <li><p> Methods for creating <i><a href="#views">view buffers</a></i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *   which allow a byte buffer to be viewed as a buffer containing values of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *   some other primitive type; and </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
#end[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *   <li><p> Methods for {@link #compact </code>compacting<code>}, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *   #duplicate </code>duplicating<code>}, and {@link #slice
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *   </code>slicing<code>} $a$ $fulltype$ buffer.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <p> $Fulltype$ buffers can be created either by {@link #allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * </code><i>allocation</i><code>}, which allocates space for the buffer's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
#if[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * content, or by {@link #wrap($type$[]) </code><i>wrapping</i><code>} an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * existing $fulltype$ array {#if[char]?or&#32;string} into a buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
#else[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * content, by {@link #wrap($type$[]) </code><i>wrapping</i><code>} an existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * $fulltype$ array {#if[char]?or&#32;string} into a buffer, or by creating a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <a href="ByteBuffer.html#views"><i>view</i></a> of an existing byte buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
#end[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
#if[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <a name="direct">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <h4> Direct <i>vs.</i> non-direct buffers </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <p> A byte buffer is either <i>direct</i> or <i>non-direct</i>.  Given a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * direct byte buffer, the Java virtual machine will make a best effort to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * perform native I/O operations directly upon it.  That is, it will attempt to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * avoid copying the buffer's content to (or from) an intermediate buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * before (or after) each invocation of one of the underlying operating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * system's native I/O operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <p> A direct byte buffer may be created by invoking the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * #allocateDirect(int) allocateDirect} factory method of this class.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * buffers returned by this method typically have somewhat higher allocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * and deallocation costs than non-direct buffers.  The contents of direct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * buffers may reside outside of the normal garbage-collected heap, and so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * their impact upon the memory footprint of an application might not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * obvious.  It is therefore recommended that direct buffers be allocated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * primarily for large, long-lived buffers that are subject to the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * system's native I/O operations.  In general it is best to allocate direct
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * buffers only when they yield a measureable gain in program performance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * <p> A direct byte buffer may also be created by {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * java.nio.channels.FileChannel#map </code>mapping<code>} a region of a file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * directly into memory.  An implementation of the Java platform may optionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * support the creation of direct byte buffers from native code via JNI.  If an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * instance of one of these kinds of buffers refers to an inaccessible region
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * of memory then an attempt to access that region will not change the buffer's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * content and will cause an unspecified exception to be thrown either at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * time of the access or at some later time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * <p> Whether a byte buffer is direct or non-direct may be determined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * invoking its {@link #isDirect isDirect} method.  This method is provided so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * that explicit buffer management can be done in performance-critical code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * <a name="bin">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * <h4> Access to binary data </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * <p> This class defines methods for reading and writing values of all other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * primitive types, except <tt>boolean</tt>.  Primitive values are translated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * to (or from) sequences of bytes according to the buffer's current byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * order, which may be retrieved and modified via the {@link #order order}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * methods.  Specific byte orders are represented by instances of the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * ByteOrder} class.  The initial order of a byte buffer is always {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * ByteOrder#BIG_ENDIAN BIG_ENDIAN}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * <p> For access to heterogeneous binary data, that is, sequences of values of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * different types, this class defines a family of absolute and relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * <i>get</i> and <i>put</i> methods for each type.  For 32-bit floating-point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * values, for example, this class defines:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * float  {@link #getFloat()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * float  {@link #getFloat(int) getFloat(int index)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 *  void  {@link #putFloat(float) putFloat(float f)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 *  void  {@link #putFloat(int,float) putFloat(int index, float f)}</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * <p> Corresponding methods are defined for the types <tt>char</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * <tt>short</tt>, <tt>int</tt>, <tt>long</tt>, and <tt>double</tt>.  The index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * parameters of the absolute <i>get</i> and <i>put</i> methods are in terms of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * bytes rather than of the type being read or written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * <a name="views">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * <p> For access to homogeneous binary data, that is, sequences of values of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * the same type, this class defines methods that can create <i>views</i> of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * given byte buffer.  A <i>view buffer</i> is simply another buffer whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * content is backed by the byte buffer.  Changes to the byte buffer's content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * will be visible in the view buffer, and vice versa; the two buffers'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * position, limit, and mark values are independent.  The {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * #asFloatBuffer() asFloatBuffer} method, for example, creates an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * the {@link FloatBuffer} class that is backed by the byte buffer upon which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * the method is invoked.  Corresponding view-creation methods are defined for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * the types <tt>char</tt>, <tt>short</tt>, <tt>int</tt>, <tt>long</tt>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * <tt>double</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * <p> View buffers have three important advantages over the families of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * type-specific <i>get</i> and <i>put</i> methods described above:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 *   <li><p> A view buffer is indexed not in terms of bytes but rather in terms
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 *   of the type-specific size of its values;  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 *   <li><p> A view buffer provides relative bulk <i>get</i> and <i>put</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 *   methods that can transfer contiguous sequences of values between a buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 *   and an array or some other buffer of the same type; and  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 *   <li><p> A view buffer is potentially much more efficient because it will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 *   be direct if, and only if, its backing byte buffer is direct.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * <p> The byte order of a view buffer is fixed to be that of its byte buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 * at the time that the view is created.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
#end[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
#if[!byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 * <p> Like a byte buffer, $a$ $fulltype$ buffer is either <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 * href="ByteBuffer.html#direct"><i>direct</i> or <i>non-direct</i></a>.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 * $fulltype$ buffer created via the <tt>wrap</tt> methods of this class will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 * be non-direct.  $A$ $fulltype$ buffer created as a view of a byte buffer will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 * be direct if, and only if, the byte buffer itself is direct.  Whether or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 * $a$ $fulltype$ buffer is direct may be determined by invoking the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 * #isDirect isDirect} method.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
#end[!byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
#if[char]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * <p> This class implements the {@link CharSequence} interface so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 * character buffers may be used wherever character sequences are accepted, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * example in the regular-expression package <tt>{@link java.util.regex}</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
#end[char]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
#if[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 * <h4> Invocation chaining </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
#end[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 * <p> Methods in this class that do not otherwise have a value to return are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 * specified to return the buffer upon which they are invoked.  This allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 * method invocations to be chained.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
#if[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 * The sequence of statements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 * bb.putInt(0xCAFEBABE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 * bb.putShort(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 * bb.putShort(45);</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 * can, for example, be replaced by the single statement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 * bb.putInt(0xCAFEBABE).putShort(3).putShort(45);</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
#end[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
#if[char]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 * The sequence of statements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
 * cb.put("text/");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 * cb.put(subtype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 * cb.put("; charset=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 * cb.put(enc);</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 * can, for example, be replaced by the single statement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 * cb.put("text/").put(subtype).put("; charset=").put(enc);</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
#end[char]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 * @author JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
public abstract class $Type$Buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    extends Buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    implements Comparable<$Type$Buffer>{#if[char]?, Appendable, CharSequence, Readable}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    // These fields are declared here rather than in Heap-X-Buffer in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    // reduce the number of virtual method invocations needed to access these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    // values, which is especially costly when coding small buffers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    final $type$[] hb;                  // Non-null only for heap buffers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    final int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    boolean isReadOnly;                 // Valid only for heap buffers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    // Creates a new buffer with the given mark, position, limit, capacity,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    // backing array, and array offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    $Type$Buffer(int mark, int pos, int lim, int cap,   // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                 $type$[] hb, int offset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        super(mark, pos, lim, cap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        this.hb = hb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        this.offset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    // Creates a new buffer with the given mark, position, limit, and capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    $Type$Buffer(int mark, int pos, int lim, int cap) { // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        this(mark, pos, lim, cap, null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
#if[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Allocates a new direct $fulltype$ buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * <p> The new buffer's position will be zero, its limit will be its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * capacity, its mark will be undefined, and each of its elements will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * initialized to zero.  Whether or not it has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * {@link #hasArray </code>backing array<code>} is unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @param  capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *         The new buffer's capacity, in $fulltype$s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @return  The new $fulltype$ buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *          If the <tt>capacity</tt> is a negative integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public static $Type$Buffer allocateDirect(int capacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        return new Direct$Type$Buffer(capacity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
#end[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * Allocates a new $fulltype$ buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * <p> The new buffer's position will be zero, its limit will be its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * capacity, its mark will be undefined, and each of its elements will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * initialized to zero.  It will have a {@link #array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * </code>backing array<code>}, and its {@link #arrayOffset </code>array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * offset<code>} will be zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @param  capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *         The new buffer's capacity, in $fulltype$s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @return  The new $fulltype$ buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *          If the <tt>capacity</tt> is a negative integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public static $Type$Buffer allocate(int capacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (capacity < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return new Heap$Type$Buffer(capacity, capacity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Wraps $a$ $fulltype$ array into a buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * <p> The new buffer will be backed by the given $fulltype$ array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * that is, modifications to the buffer will cause the array to be modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * and vice versa.  The new buffer's capacity will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * <tt>array.length</tt>, its position will be <tt>offset</tt>, its limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * will be <tt>offset + length</tt>, and its mark will be undefined.  Its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * {@link #array </code>backing array<code>} will be the given array, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * its {@link #arrayOffset </code>array offset<code>} will be zero.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @param  array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *         The array that will back the new buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *         The offset of the subarray to be used; must be non-negative and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *         no larger than <tt>array.length</tt>.  The new buffer's position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *         will be set to this value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @param  length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *         The length of the subarray to be used;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *         must be non-negative and no larger than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *         <tt>array.length - offset</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *         The new buffer's limit will be set to <tt>offset + length</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @return  The new $fulltype$ buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *          If the preconditions on the <tt>offset</tt> and <tt>length</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *          parameters do not hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    public static $Type$Buffer wrap($type$[] array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                                    int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            return new Heap$Type$Buffer(array, offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        } catch (IllegalArgumentException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            throw new IndexOutOfBoundsException();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * Wraps $a$ $fulltype$ array into a buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * <p> The new buffer will be backed by the given $fulltype$ array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * that is, modifications to the buffer will cause the array to be modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * and vice versa.  The new buffer's capacity and limit will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * <tt>array.length</tt>, its position will be zero, and its mark will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * undefined.  Its {@link #array </code>backing array<code>} will be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * given array, and its {@link #arrayOffset </code>array offset<code>} will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * be zero.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @param  array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *         The array that will back this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @return  The new $fulltype$ buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    public static $Type$Buffer wrap($type$[] array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return wrap(array, 0, array.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
#if[char]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * Attempts to read characters into the specified character buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * The buffer is used as a repository of characters as-is: the only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * changes made are the results of a put operation. No flipping or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * rewinding of the buffer is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @param target the buffer to read characters into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @return The number of characters added to the buffer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *         -1 if this source of characters is at its end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @throws IOException if an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @throws NullPointerException if target is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @throws ReadOnlyBufferException if target is a read only buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public int read(CharBuffer target) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        // Determine the number of bytes n that can be transferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        int targetRemaining = target.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        int remaining = remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        if (remaining == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        int n = Math.min(remaining, targetRemaining);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        int limit = limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        // Set source limit to prevent target overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        if (targetRemaining < remaining)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            limit(position() + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            if (n > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                target.put(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            limit(limit); // restore real limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * Wraps a character sequence into a buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * <p> The content of the new, read-only buffer will be the content of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * given character sequence.  The buffer's capacity will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * <tt>csq.length()</tt>, its position will be <tt>start</tt>, its limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * will be <tt>end</tt>, and its mark will be undefined.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @param  csq
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *         The character sequence from which the new character buffer is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *         be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @param  start
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *         The index of the first character to be used;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *         must be non-negative and no larger than <tt>csq.length()</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *         The new buffer's position will be set to this value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @param  end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *         The index of the character following the last character to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *         used; must be no smaller than <tt>start</tt> and no larger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *         than <tt>csq.length()</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *         The new buffer's limit will be set to this value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @return  The new character buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *          If the preconditions on the <tt>start</tt> and <tt>end</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *          parameters do not hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    public static CharBuffer wrap(CharSequence csq, int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            return new StringCharBuffer(csq, start, end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        } catch (IllegalArgumentException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * Wraps a character sequence into a buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * <p> The content of the new, read-only buffer will be the content of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * given character sequence.  The new buffer's capacity and limit will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * <tt>csq.length()</tt>, its position will be zero, and its mark will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * undefined.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * @param  csq
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *         The character sequence from which the new character buffer is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *         be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @return  The new character buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    public static CharBuffer wrap(CharSequence csq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        return wrap(csq, 0, csq.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
#end[char]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * Creates a new $fulltype$ buffer whose content is a shared subsequence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * this buffer's content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * <p> The content of the new buffer will start at this buffer's current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * position.  Changes to this buffer's content will be visible in the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * buffer, and vice versa; the two buffers' position, limit, and mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * values will be independent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * <p> The new buffer's position will be zero, its capacity and its limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * will be the number of $fulltype$s remaining in this buffer, and its mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * will be undefined.  The new buffer will be direct if, and only if, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * buffer is direct, and it will be read-only if, and only if, this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * is read-only.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @return  The new $fulltype$ buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    public abstract $Type$Buffer slice();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * Creates a new $fulltype$ buffer that shares this buffer's content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * <p> The content of the new buffer will be that of this buffer.  Changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * to this buffer's content will be visible in the new buffer, and vice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * versa; the two buffers' position, limit, and mark values will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * independent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * <p> The new buffer's capacity, limit, position, and mark values will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * identical to those of this buffer.  The new buffer will be direct if,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * and only if, this buffer is direct, and it will be read-only if, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * only if, this buffer is read-only.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @return  The new $fulltype$ buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    public abstract $Type$Buffer duplicate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * Creates a new, read-only $fulltype$ buffer that shares this buffer's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * <p> The content of the new buffer will be that of this buffer.  Changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * to this buffer's content will be visible in the new buffer; the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * buffer itself, however, will be read-only and will not allow the shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * content to be modified.  The two buffers' position, limit, and mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * values will be independent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * <p> The new buffer's capacity, limit, position, and mark values will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * identical to those of this buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * <p> If this buffer is itself read-only then this method behaves in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * exactly the same way as the {@link #duplicate duplicate} method.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @return  The new, read-only $fulltype$ buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    public abstract $Type$Buffer asReadOnlyBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    // -- Singleton get/put methods --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * Relative <i>get</i> method.  Reads the $fulltype$ at this buffer's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * current position, and then increments the position. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @return  The $fulltype$ at the buffer's current position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * @throws  BufferUnderflowException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *          If the buffer's current position is not smaller than its limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    public abstract $type$ get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * Relative <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * <p> Writes the given $fulltype$ into this buffer at the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * position, and then increments the position. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @param  $x$
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *         The $fulltype$ to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * @return  This buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * @throws  BufferOverflowException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *          If this buffer's current position is not smaller than its limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * @throws  ReadOnlyBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     *          If this buffer is read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    public abstract $Type$Buffer put($type$ $x$);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * Absolute <i>get</i> method.  Reads the $fulltype$ at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * index. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @param  index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *         The index from which the $fulltype$ will be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @return  The $fulltype$ at the given index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *          If <tt>index</tt> is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     *          or not smaller than the buffer's limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    public abstract $type$ get(int index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * Absolute <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * <p> Writes the given $fulltype$ into this buffer at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * index. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * @param  index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *         The index at which the $fulltype$ will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @param  $x$
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *         The $fulltype$ value to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @return  This buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *          If <tt>index</tt> is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     *          or not smaller than the buffer's limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * @throws  ReadOnlyBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     *          If this buffer is read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    public abstract $Type$Buffer put(int index, $type$ $x$);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    // -- Bulk get operations --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * Relative bulk <i>get</i> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * <p> This method transfers $fulltype$s from this buffer into the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * destination array.  If there are fewer $fulltype$s remaining in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * buffer than are required to satisfy the request, that is, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * <tt>length</tt>&nbsp;<tt>&gt;</tt>&nbsp;<tt>remaining()</tt>, then no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * $fulltype$s are transferred and a {@link BufferUnderflowException} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * <p> Otherwise, this method copies <tt>length</tt> $fulltype$s from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * buffer into the given array, starting at the current position of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * buffer and at the given offset in the array.  The position of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * buffer is then incremented by <tt>length</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * <p> In other words, an invocation of this method of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * <tt>src.get(dst,&nbsp;off,&nbsp;len)</tt> has exactly the same effect as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * the loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *     for (int i = off; i < off + len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     *         dst[i] = src.get(); </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * except that it first checks that there are sufficient $fulltype$s in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * this buffer and it is potentially much more efficient. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * @param  dst
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *         The array into which $fulltype$s are to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     *         The offset within the array of the first $fulltype$ to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     *         written; must be non-negative and no larger than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     *         <tt>dst.length</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * @param  length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *         The maximum number of $fulltype$s to be written to the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     *         array; must be non-negative and no larger than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     *         <tt>dst.length - offset</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * @return  This buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * @throws  BufferUnderflowException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     *          If there are fewer than <tt>length</tt> $fulltype$s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     *          remaining in this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     *          If the preconditions on the <tt>offset</tt> and <tt>length</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     *          parameters do not hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    public $Type$Buffer get($type$[] dst, int offset, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        checkBounds(offset, length, dst.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        if (length > remaining())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            throw new BufferUnderflowException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        int end = offset + length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        for (int i = offset; i < end; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            dst[i] = get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * Relative bulk <i>get</i> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * <p> This method transfers $fulltype$s from this buffer into the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * destination array.  An invocation of this method of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * <tt>src.get(a)</tt> behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     *     src.get(a, 0, a.length) </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * @return  This buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * @throws  BufferUnderflowException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     *          If there are fewer than <tt>length</tt> $fulltype$s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *          remaining in this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    public $Type$Buffer get($type$[] dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        return get(dst, 0, dst.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    // -- Bulk put operations --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * Relative bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * <p> This method transfers the $fulltype$s remaining in the given source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * buffer into this buffer.  If there are more $fulltype$s remaining in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * source buffer than in this buffer, that is, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * <tt>src.remaining()</tt>&nbsp;<tt>&gt;</tt>&nbsp;<tt>remaining()</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * then no $fulltype$s are transferred and a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * BufferOverflowException} is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * <p> Otherwise, this method copies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * <i>n</i>&nbsp;=&nbsp;<tt>src.remaining()</tt> $fulltype$s from the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * buffer into this buffer, starting at each buffer's current position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * The positions of both buffers are then incremented by <i>n</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * <p> In other words, an invocation of this method of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * <tt>dst.put(src)</tt> has exactly the same effect as the loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     *     while (src.hasRemaining())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     *         dst.put(src.get()); </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * except that it first checks that there is sufficient space in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * buffer and it is potentially much more efficient. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * @param  src
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     *         The source buffer from which $fulltype$s are to be read;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     *         must not be this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * @return  This buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * @throws  BufferOverflowException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     *          If there is insufficient space in this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     *          for the remaining $fulltype$s in the source buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     *          If the source buffer is this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * @throws  ReadOnlyBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     *          If this buffer is read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    public $Type$Buffer put($Type$Buffer src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        if (src == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        int n = src.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        if (n > remaining())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            throw new BufferOverflowException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        for (int i = 0; i < n; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            put(src.get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * Relative bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * <p> This method transfers $fulltype$s into this buffer from the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * source array.  If there are more $fulltype$s to be copied from the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * than remain in this buffer, that is, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * <tt>length</tt>&nbsp;<tt>&gt;</tt>&nbsp;<tt>remaining()</tt>, then no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * $fulltype$s are transferred and a {@link BufferOverflowException} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * <p> Otherwise, this method copies <tt>length</tt> $fulltype$s from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * given array into this buffer, starting at the given offset in the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * and at the current position of this buffer.  The position of this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * is then incremented by <tt>length</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * <p> In other words, an invocation of this method of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * <tt>dst.put(src,&nbsp;off,&nbsp;len)</tt> has exactly the same effect as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * the loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     *     for (int i = off; i < off + len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     *         dst.put(a[i]); </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * except that it first checks that there is sufficient space in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * buffer and it is potentially much more efficient. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * @param  src
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     *         The array from which $fulltype$s are to be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     *         The offset within the array of the first $fulltype$ to be read;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     *         must be non-negative and no larger than <tt>array.length</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * @param  length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     *         The number of $fulltype$s to be read from the given array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     *         must be non-negative and no larger than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     *         <tt>array.length - offset</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * @return  This buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * @throws  BufferOverflowException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     *          If there is insufficient space in this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     *          If the preconditions on the <tt>offset</tt> and <tt>length</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     *          parameters do not hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * @throws  ReadOnlyBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     *          If this buffer is read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    public $Type$Buffer put($type$[] src, int offset, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        checkBounds(offset, length, src.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        if (length > remaining())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            throw new BufferOverflowException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        int end = offset + length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        for (int i = offset; i < end; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            this.put(src[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * Relative bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * <p> This method transfers the entire content of the given source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * $fulltype$ array into this buffer.  An invocation of this method of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * form <tt>dst.put(a)</tt> behaves in exactly the same way as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     *     dst.put(a, 0, a.length) </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * @return  This buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * @throws  BufferOverflowException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     *          If there is insufficient space in this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * @throws  ReadOnlyBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     *          If this buffer is read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    public final $Type$Buffer put($type$[] src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        return put(src, 0, src.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
#if[char]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * Relative bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * <p> This method transfers $fulltype$s from the given string into this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * buffer.  If there are more $fulltype$s to be copied from the string than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * remain in this buffer, that is, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * <tt>end&nbsp;-&nbsp;start</tt>&nbsp;<tt>&gt;</tt>&nbsp;<tt>remaining()</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * then no $fulltype$s are transferred and a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * BufferOverflowException} is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * <p> Otherwise, this method copies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * <i>n</i>&nbsp;=&nbsp;<tt>end</tt>&nbsp;-&nbsp;<tt>start</tt> $fulltype$s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * from the given string into this buffer, starting at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * <tt>start</tt> index and at the current position of this buffer.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * position of this buffer is then incremented by <i>n</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * <p> In other words, an invocation of this method of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * <tt>dst.put(src,&nbsp;start,&nbsp;end)</tt> has exactly the same effect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * as the loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     *     for (int i = start; i < end; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     *         dst.put(src.charAt(i)); </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * except that it first checks that there is sufficient space in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * buffer and it is potentially much more efficient. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * @param  src
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     *         The string from which $fulltype$s are to be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * @param  start
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     *         The offset within the string of the first $fulltype$ to be read;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     *         must be non-negative and no larger than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     *         <tt>string.length()</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * @param  end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     *         The offset within the string of the last $fulltype$ to be read,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     *         plus one; must be non-negative and no larger than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     *         <tt>string.length()</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * @return  This buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * @throws  BufferOverflowException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     *          If there is insufficient space in this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     *          If the preconditions on the <tt>start</tt> and <tt>end</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     *          parameters do not hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * @throws  ReadOnlyBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     *          If this buffer is read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    public $Type$Buffer put(String src, int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        checkBounds(start, end - start, src.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        for (int i = start; i < end; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            this.put(src.charAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * Relative bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * <p> This method transfers the entire content of the given source string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * into this buffer.  An invocation of this method of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * <tt>dst.put(s)</tt> behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     *     dst.put(s, 0, s.length()) </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * @return  This buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * @throws  BufferOverflowException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     *          If there is insufficient space in this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * @throws  ReadOnlyBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     *          If this buffer is read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    public final $Type$Buffer put(String src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        return put(src, 0, src.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
#end[char]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    // -- Other stuff --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * Tells whether or not this buffer is backed by an accessible $fulltype$
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * <p> If this method returns <tt>true</tt> then the {@link #array() array}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * and {@link #arrayOffset() arrayOffset} methods may safely be invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * @return  <tt>true</tt> if, and only if, this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     *          is backed by an array and is not read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    public final boolean hasArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        return (hb != null) && !isReadOnly;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * Returns the $fulltype$ array that backs this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * buffer&nbsp;&nbsp;<i>(optional operation)</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * <p> Modifications to this buffer's content will cause the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * array's content to be modified, and vice versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * <p> Invoke the {@link #hasArray hasArray} method before invoking this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * method in order to ensure that this buffer has an accessible backing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * array.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * @return  The array that backs this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * @throws  ReadOnlyBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     *          If this buffer is backed by an array but is read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * @throws  UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     *          If this buffer is not backed by an accessible array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    public final $type$[] array() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        if (hb == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        if (isReadOnly)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            throw new ReadOnlyBufferException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        return hb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * Returns the offset within this buffer's backing array of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * element of the buffer&nbsp;&nbsp;<i>(optional operation)</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * <p> If this buffer is backed by an array then buffer position <i>p</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * corresponds to array index <i>p</i>&nbsp;+&nbsp;<tt>arrayOffset()</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * <p> Invoke the {@link #hasArray hasArray} method before invoking this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * method in order to ensure that this buffer has an accessible backing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * array.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * @return  The offset within this buffer's array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     *          of the first element of the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * @throws  ReadOnlyBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     *          If this buffer is backed by an array but is read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * @throws  UnsupportedOperationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     *          If this buffer is not backed by an accessible array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    public final int arrayOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        if (hb == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        if (isReadOnly)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            throw new ReadOnlyBufferException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        return offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * Compacts this buffer&nbsp;&nbsp;<i>(optional operation)</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * <p> The $fulltype$s between the buffer's current position and its limit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * if any, are copied to the beginning of the buffer.  That is, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * $fulltype$ at index <i>p</i>&nbsp;=&nbsp;<tt>position()</tt> is copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * to index zero, the $fulltype$ at index <i>p</i>&nbsp;+&nbsp;1 is copied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * to index one, and so forth until the $fulltype$ at index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * <tt>limit()</tt>&nbsp;-&nbsp;1 is copied to index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * <i>n</i>&nbsp;=&nbsp;<tt>limit()</tt>&nbsp;-&nbsp;<tt>1</tt>&nbsp;-&nbsp;<i>p</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * The buffer's position is then set to <i>n+1</i> and its limit is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * its capacity.  The mark, if defined, is discarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * <p> The buffer's position is set to the number of $fulltype$s copied,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * rather than to zero, so that an invocation of this method can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * followed immediately by an invocation of another relative <i>put</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * method. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
#if[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * <p> Invoke this method after writing data from a buffer in case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * write was incomplete.  The following loop, for example, copies bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * from one channel to another via the buffer <tt>buf</tt>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * buf.clear();          // Prepare buffer for use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * while (in.read(buf) >= 0 || buf.position != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     *     buf.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     *     out.write(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     *     buf.compact();    // In case of partial write
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * }</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
#end[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * @return  This buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * @throws  ReadOnlyBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     *          If this buffer is read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
    public abstract $Type$Buffer compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * Tells whether or not this $fulltype$ buffer is direct. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * @return  <tt>true</tt> if, and only if, this buffer is direct
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    public abstract boolean isDirect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
#if[!char]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * Returns a string summarizing the state of this buffer.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * @return  A summary string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        sb.append(getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        sb.append("[pos=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        sb.append(position());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        sb.append(" lim=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        sb.append(limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        sb.append(" cap=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        sb.append(capacity());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        sb.append("]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
#end[!char]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
    // ## Should really use unchecked accessors here for speed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * Returns the current hash code of this buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * <p> The hash code of a $type$ buffer depends only upon its remaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * elements; that is, upon the elements from <tt>position()</tt> up to, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * including, the element at <tt>limit()</tt>&nbsp;-&nbsp;<tt>1</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * <p> Because buffer hash codes are content-dependent, it is inadvisable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * to use buffers as keys in hash maps or similar data structures unless it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * is known that their contents will not change.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * @return  The current hash code of this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        int h = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        int p = position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        for (int i = limit() - 1; i >= p; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            h = 31 * h + (int)get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        return h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * Tells whether or not this buffer is equal to another object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * <p> Two $type$ buffers are equal if, and only if,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * <p><ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     *   <li><p> They have the same element type,  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     *   <li><p> They have the same number of remaining elements, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     *   </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     *   <li><p> The two sequences of remaining elements, considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     *   independently of their starting positions, are pointwise equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     *   </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * <p> A $type$ buffer is not equal to any other type of object.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * @param  ob  The object to which this buffer is to be compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * @return  <tt>true</tt> if, and only if, this buffer is equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     *           given object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    public boolean equals(Object ob) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        if (this == ob)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        if (!(ob instanceof $Type$Buffer))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        $Type$Buffer that = ($Type$Buffer)ob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        if (this.remaining() != that.remaining())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        int p = this.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        for (int i = this.limit() - 1, j = that.limit() - 1; i >= p; i--, j--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            $type$ v1 = this.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            $type$ v2 = that.get(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            if (v1 != v2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                if ((v1 != v1) && (v2 != v2))   // For float and double
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * Compares this buffer to another.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * <p> Two $type$ buffers are compared by comparing their sequences of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * remaining elements lexicographically, without regard to the starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * position of each sequence within its corresponding buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * <p> A $type$ buffer is not comparable to any other type of object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * @return  A negative integer, zero, or a positive integer as this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     *          is less than, equal to, or greater than the given buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
    public int compareTo($Type$Buffer that) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        int n = this.position() + Math.min(this.remaining(), that.remaining());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        for (int i = this.position(), j = that.position(); i < n; i++, j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            $type$ v1 = this.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            $type$ v2 = that.get(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            if (v1 == v2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
            if ((v1 != v1) && (v2 != v2))       // For float and double
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            if (v1 < v2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            return +1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        return this.remaining() - that.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    // -- Other char stuff --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
#if[char]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * Returns a string containing the characters in this buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * <p> The first character of the resulting string will be the character at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * this buffer's position, while the last character will be the character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * at index <tt>limit()</tt>&nbsp;-&nbsp;1.  Invoking this method does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * change the buffer's position. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     * @return  The specified string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        return toString(position(), limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    abstract String toString(int start, int end);       // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
    // --- Methods to support CharSequence ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * Returns the length of this character buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * <p> When viewed as a character sequence, the length of a character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * buffer is simply the number of characters between the position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * (inclusive) and the limit (exclusive); that is, it is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * <tt>remaining()</tt>. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     * @return  The length of this character buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
    public final int length() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        return remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * Reads the character at the given index relative to the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * position. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * @param  index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     *         The index of the character to be read, relative to the position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     *         must be non-negative and smaller than <tt>remaining()</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * @return  The character at index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     *          <tt>position()&nbsp;+&nbsp;index</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     *          If the preconditions on <tt>index</tt> do not hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
    public final char charAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        return get(position() + checkIndex(index, 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * Creates a new character buffer that represents the specified subsequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * of this buffer, relative to the current position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     * <p> The new buffer will share this buffer's content; that is, if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     * content of this buffer is mutable then modifications to one buffer will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     * cause the other to be modified.  The new buffer's capacity will be that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
     * of this buffer, its position will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
     * <tt>position()</tt>&nbsp;+&nbsp;<tt>start</tt>, and its limit will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
     * <tt>position()</tt>&nbsp;+&nbsp;<tt>end</tt>.  The new buffer will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
     * direct if, and only if, this buffer is direct, and it will be read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
     * if, and only if, this buffer is read-only.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * @param  start
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     *         The index, relative to the current position, of the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     *         character in the subsequence; must be non-negative and no larger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     *         than <tt>remaining()</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     * @param  end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     *         The index, relative to the current position, of the character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     *         following the last character in the subsequence; must be no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     *         smaller than <tt>start</tt> and no larger than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     *         <tt>remaining()</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * @return  The new character sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     *          If the preconditions on <tt>start</tt> and <tt>end</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     *          do not hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
    public abstract CharSequence subSequence(int start, int end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
    // --- Methods to support Appendable ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * Appends the specified character sequence  to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * buffer&nbsp;&nbsp;<i>(optional operation)</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     * <p> An invocation of this method of the form <tt>dst.append(csq)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     *     dst.put(csq.toString()) </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * <p> Depending on the specification of <tt>toString</tt> for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * character sequence <tt>csq</tt>, the entire sequence may not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * appended.  For instance, invoking the {@link $Type$Buffer#toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * toString} method of a character buffer will return a subsequence whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * content depends upon the buffer's position and limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * @param  csq
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     *         The character sequence to append.  If <tt>csq</tt> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     *         <tt>null</tt>, then the four characters <tt>"null"</tt> are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     *         appended to this character buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * @return  This buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * @throws  BufferOverflowException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     *          If there is insufficient space in this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     * @throws  ReadOnlyBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     *          If this buffer is read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
    public $Type$Buffer append(CharSequence csq) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        if (csq == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
            return put("null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
            return put(csq.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     * Appends a subsequence of the  specified character sequence  to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * buffer&nbsp;&nbsp;<i>(optional operation)</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     * <p> An invocation of this method of the form <tt>dst.append(csq, start,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     * end)</tt> when <tt>csq</tt> is not <tt>null</tt>, behaves in exactly the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     *     dst.put(csq.subSequence(start, end).toString()) </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     * @param  csq
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     *         The character sequence from which a subsequence will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     *         appended.  If <tt>csq</tt> is <tt>null</tt>, then characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     *         will be appended as if <tt>csq</tt> contained the four
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     *         characters <tt>"null"</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
     * @return  This buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     * @throws  BufferOverflowException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
     *          If there is insufficient space in this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     *          If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     *          is greater than <tt>end</tt>, or <tt>end</tt> is greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     *          <tt>csq.length()</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     * @throws  ReadOnlyBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     *          If this buffer is read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
    public $Type$Buffer append(CharSequence csq, int start, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        CharSequence cs = (csq == null ? "null" : csq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        return put(cs.subSequence(start, end).toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     * Appends the specified $fulltype$  to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     * buffer&nbsp;&nbsp;<i>(optional operation)</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     * <p> An invocation of this method of the form <tt>dst.append($x$)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     * behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     *     dst.put($x$) </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     * @param  $x$
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     *         The 16-bit $fulltype$ to append
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     * @return  This buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     * @throws  BufferOverflowException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     *          If there is insufficient space in this buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * @throws  ReadOnlyBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     *          If this buffer is read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
    public $Type$Buffer append($type$ $x$) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        return put($x$);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
#end[char]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
    // -- Other byte stuff: Access to binary data --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
#if[!byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     * Retrieves this buffer's byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     * <p> The byte order of $a$ $fulltype$ buffer created by allocation or by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     * wrapping an existing <tt>$type$</tt> array is the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * ByteOrder#nativeOrder </code>native order<code>} of the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * hardware.  The byte order of $a$ $fulltype$ buffer created as a <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     * href="ByteBuffer.html#views">view</a> of a byte buffer is that of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * byte buffer at the moment that the view is created.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * @return  This buffer's byte order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
    public abstract ByteOrder order();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
#end[!byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
#if[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    boolean bigEndian                                   // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
    boolean nativeByteOrder                             // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        = (Bits.byteOrder() == ByteOrder.BIG_ENDIAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     * Retrieves this buffer's byte order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * <p> The byte order is used when reading or writing multibyte values, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     * when creating buffers that are views of this byte buffer.  The order of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * a newly-created byte buffer is always {@link ByteOrder#BIG_ENDIAN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * BIG_ENDIAN}.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * @return  This buffer's byte order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
    public final ByteOrder order() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
        return bigEndian ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * Modifies this buffer's byte order.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     * @param  bo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     *         The new byte order,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     *         either {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     *         or {@link ByteOrder#LITTLE_ENDIAN LITTLE_ENDIAN}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     * @return  This buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
    public final $Type$Buffer order(ByteOrder bo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        bigEndian = (bo == ByteOrder.BIG_ENDIAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        nativeByteOrder =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
            (bigEndian == (Bits.byteOrder() == ByteOrder.BIG_ENDIAN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
    // Unchecked accessors, for use by ByteBufferAs-X-Buffer classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
    abstract byte _get(int i);                          // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
    abstract void _put(int i, byte b);                  // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
    // #BIN
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
    // Binary-data access methods  for short, char, int, long, float,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
    // and double will be inserted here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
#end[byte]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
}