0001/*
0002 * Copyright 2000-2007 Sun Microsystems, Inc. All Rights Reserved.
0003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0004 *
0005 * This code is free software; you can redistribute it and/or modify it
0006 * under the terms of the GNU General Public License version 2 only, as
0007 * published by the Free Software Foundation. Sun designates this
0008 * particular file as subject to the "Classpath" exception as provided
0009 * by Sun in the LICENSE file that accompanied this code.
0010 *
0011 * This code is distributed in the hope that it will be useful, but WITHOUT
0012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0014 * version 2 for more details (a copy is included in the LICENSE file that
0015 * accompanied this code).
0016 *
0017 * You should have received a copy of the GNU General Public License version
0018 * 2 along with this work; if not, write to the Free Software Foundation,
0019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0020 *
0021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0022 * CA 95054 USA or visit www.sun.com if you need additional information or
0023 * have any questions.
0024 */
0025
0026#warn This file is preprocessed before being compiled
0027
0028package java.nio;
0029
0030#if[char]
0031import java.io.IOException;
0032#end[char]
0033
0034/**
0035 * $A$ $fulltype$ buffer.
0036 *
0037 * <p> This class defines {#if[byte]?six:four} categories of operations upon
0038 * $fulltype$ buffers:
0039 *
0040 * <ul>
0041 *
0042 * <li><p> Absolute and relative {@link #get() </code><i>get</i><code>} and
0043 * {@link #put($type$) </code><i>put</i><code>} methods that read and write
0044 * single $fulltype$s; </p></li>
0045 *
0046 * <li><p> Relative {@link #get($type$[]) </code><i>bulk get</i><code>}
0047 * methods that transfer contiguous sequences of $fulltype$s from this buffer
0048 * into an array; {#if[!byte]?and}</p></li>
0049 *
0050 * <li><p> Relative {@link #put($type$[]) </code><i>bulk put</i><code>}
0051 * methods that transfer contiguous sequences of $fulltype$s from $a$
0052 * $fulltype$ array{#if[char]?, a string,} or some other $fulltype$
0053 * buffer into this buffer;{#if[!byte]? and} </p></li>
0054 *
0055#if[byte]
0056 *
0057 * <li><p> Absolute and relative {@link #getChar() </code><i>get</i><code>}
0058 * and {@link #putChar(char) </code><i>put</i><code>} methods that read and
0059 * write values of other primitive types, translating them to and from
0060 * sequences of bytes in a particular byte order; </p></li>
0061 *
0062 * <li><p> Methods for creating <i><a href="#views">view buffers</a></i>,
0063 * which allow a byte buffer to be viewed as a buffer containing values of
0064 * some other primitive type; and </p></li>
0065 *
0066#end[byte]
0067 *
0068 * <li><p> Methods for {@link #compact </code>compacting<code>}, {@link
0069 * #duplicate </code>duplicating<code>}, and {@link #slice
0070 * </code>slicing<code>} $a$ $fulltype$ buffer. </p></li>
0071 *
0072 * </ul>
0073 *
0074 * <p> $Fulltype$ buffers can be created either by {@link #allocate
0075 * </code><i>allocation</i><code>}, which allocates space for the buffer's
0076 *
0077#if[byte]
0078 *
0079 * content, or by {@link #wrap($type$[]) </code><i>wrapping</i><code>} an
0080 * existing $fulltype$ array {#if[char]?or string} into a buffer.
0081 *
0082#else[byte]
0083 *
0084 * content, by {@link #wrap($type$[]) </code><i>wrapping</i><code>} an existing
0085 * $fulltype$ array {#if[char]?or string} into a buffer, or by creating a
0086 * <a href="ByteBuffer.html#views"><i>view</i></a> of an existing byte buffer.
0087 *
0088#end[byte]
0089 *
0090#if[byte]
0091 *
0092 * <a name="direct">
0093 * <h4> Direct <i>vs.</i> non-direct buffers </h4>
0094 *
0095 * <p> A byte buffer is either <i>direct</i> or <i>non-direct</i>. Given a
0096 * direct byte buffer, the Java virtual machine will make a best effort to
0097 * perform native I/O operations directly upon it. That is, it will attempt to
0098 * avoid copying the buffer's content to (or from) an intermediate buffer
0099 * before (or after) each invocation of one of the underlying operating
0100 * system's native I/O operations.
0101 *
0102 * <p> A direct byte buffer may be created by invoking the {@link
0103 * #allocateDirect(int) allocateDirect} factory method of this class. The
0104 * buffers returned by this method typically have somewhat higher allocation
0105 * and deallocation costs than non-direct buffers. The contents of direct
0106 * buffers may reside outside of the normal garbage-collected heap, and so
0107 * their impact upon the memory footprint of an application might not be
0108 * obvious. It is therefore recommended that direct buffers be allocated
0109 * primarily for large, long-lived buffers that are subject to the underlying
0110 * system's native I/O operations. In general it is best to allocate direct
0111 * buffers only when they yield a measureable gain in program performance.
0112 *
0113 * <p> A direct byte buffer may also be created by {@link
0114 * java.nio.channels.FileChannel#map </code>mapping<code>} a region of a file
0115 * directly into memory. An implementation of the Java platform may optionally
0116 * support the creation of direct byte buffers from native code via JNI. If an
0117 * instance of one of these kinds of buffers refers to an inaccessible region
0118 * of memory then an attempt to access that region will not change the buffer's
0119 * content and will cause an unspecified exception to be thrown either at the
0120 * time of the access or at some later time.
0121 *
0122 * <p> Whether a byte buffer is direct or non-direct may be determined by
0123 * invoking its {@link #isDirect isDirect} method. This method is provided so
0124 * that explicit buffer management can be done in performance-critical code.
0125 *
0126 *
0127 * <a name="bin">
0128 * <h4> Access to binary data </h4>
0129 *
0130 * <p> This class defines methods for reading and writing values of all other
0131 * primitive types, except <tt>boolean</tt>. Primitive values are translated
0132 * to (or from) sequences of bytes according to the buffer's current byte
0133 * order, which may be retrieved and modified via the {@link #order order}
0134 * methods. Specific byte orders are represented by instances of the {@link
0135 * ByteOrder} class. The initial order of a byte buffer is always {@link
0136 * ByteOrder#BIG_ENDIAN BIG_ENDIAN}.
0137 *
0138 * <p> For access to heterogeneous binary data, that is, sequences of values of
0139 * different types, this class defines a family of absolute and relative
0140 * <i>get</i> and <i>put</i> methods for each type. For 32-bit floating-point
0141 * values, for example, this class defines:
0142 *
0143 * <blockquote><pre>
0144 * float {@link #getFloat()}
0145 * float {@link #getFloat(int) getFloat(int index)}
0146 * void {@link #putFloat(float) putFloat(float f)}
0147 * void {@link #putFloat(int,float) putFloat(int index, float f)}</pre></blockquote>
0148 *
0149 * <p> Corresponding methods are defined for the types <tt>char</tt>,
0150 * <tt>short</tt>, <tt>int</tt>, <tt>long</tt>, and <tt>double</tt>. The index
0151 * parameters of the absolute <i>get</i> and <i>put</i> methods are in terms of
0152 * bytes rather than of the type being read or written.
0153 *
0154 * <a name="views">
0155 *
0156 * <p> For access to homogeneous binary data, that is, sequences of values of
0157 * the same type, this class defines methods that can create <i>views</i> of a
0158 * given byte buffer. A <i>view buffer</i> is simply another buffer whose
0159 * content is backed by the byte buffer. Changes to the byte buffer's content
0160 * will be visible in the view buffer, and vice versa; the two buffers'
0161 * position, limit, and mark values are independent. The {@link
0162 * #asFloatBuffer() asFloatBuffer} method, for example, creates an instance of
0163 * the {@link FloatBuffer} class that is backed by the byte buffer upon which
0164 * the method is invoked. Corresponding view-creation methods are defined for
0165 * the types <tt>char</tt>, <tt>short</tt>, <tt>int</tt>, <tt>long</tt>, and
0166 * <tt>double</tt>.
0167 *
0168 * <p> View buffers have three important advantages over the families of
0169 * type-specific <i>get</i> and <i>put</i> methods described above:
0170 *
0171 * <ul>
0172 *
0173 * <li><p> A view buffer is indexed not in terms of bytes but rather in terms
0174 * of the type-specific size of its values; </p></li>
0175 *
0176 * <li><p> A view buffer provides relative bulk <i>get</i> and <i>put</i>
0177 * methods that can transfer contiguous sequences of values between a buffer
0178 * and an array or some other buffer of the same type; and </p></li>
0179 *
0180 * <li><p> A view buffer is potentially much more efficient because it will
0181 * be direct if, and only if, its backing byte buffer is direct. </p></li>
0182 *
0183 * </ul>
0184 *
0185 * <p> The byte order of a view buffer is fixed to be that of its byte buffer
0186 * at the time that the view is created. </p>
0187 *
0188#end[byte]
0189*
0190#if[!byte]
0191 *
0192 * <p> Like a byte buffer, $a$ $fulltype$ buffer is either <a
0193 * href="ByteBuffer.html#direct"><i>direct</i> or <i>non-direct</i></a>. A
0194 * $fulltype$ buffer created via the <tt>wrap</tt> methods of this class will
0195 * be non-direct. $A$ $fulltype$ buffer created as a view of a byte buffer will
0196 * be direct if, and only if, the byte buffer itself is direct. Whether or not
0197 * $a$ $fulltype$ buffer is direct may be determined by invoking the {@link
0198 * #isDirect isDirect} method. </p>
0199 *
0200#end[!byte]
0201*
0202#if[char]
0203 *
0204 * <p> This class implements the {@link CharSequence} interface so that
0205 * character buffers may be used wherever character sequences are accepted, for
0206 * example in the regular-expression package <tt>{@link java.util.regex}</tt>.
0207 * </p>
0208 *
0209#end[char]
0210 *
0211#if[byte]
0212 * <h4> Invocation chaining </h4>
0213#end[byte]
0214 *
0215 * <p> Methods in this class that do not otherwise have a value to return are
0216 * specified to return the buffer upon which they are invoked. This allows
0217 * method invocations to be chained.
0218 *
0219#if[byte]
0220 *
0221 * The sequence of statements
0222 *
0223 * <blockquote><pre>
0224 * bb.putInt(0xCAFEBABE);
0225 * bb.putShort(3);
0226 * bb.putShort(45);</pre></blockquote>
0227 *
0228 * can, for example, be replaced by the single statement
0229 *
0230 * <blockquote><pre>
0231 * bb.putInt(0xCAFEBABE).putShort(3).putShort(45);</pre></blockquote>
0232 *
0233#end[byte]
0234#if[char]
0235 *
0236 * The sequence of statements
0237 *
0238 * <blockquote><pre>
0239 * cb.put("text/");
0240 * cb.put(subtype);
0241 * cb.put("; charset=");
0242 * cb.put(enc);</pre></blockquote>
0243 *
0244 * can, for example, be replaced by the single statement
0245 *
0246 * <blockquote><pre>
0247 * cb.put("text/").put(subtype).put("; charset=").put(enc);</pre></blockquote>
0248 *
0249#end[char]
0250 *
0251 *
0252 * @author Mark Reinhold
0253 * @author JSR-51 Expert Group
0254 * @since 1.4
0255 */
0256
0257public abstract class $Type$Buffer
0258 extends Buffer
0259 implements Comparable<$Type$Buffer>{#if[char]?, Appendable, CharSequence, Readable}
0260{
0261
0262 // These fields are declared here rather than in Heap-X-Buffer in order to
0263 // reduce the number of virtual method invocations needed to access these
0264 // values, which is especially costly when coding small buffers.
0265 //
0266 final $type$[] hb; // Non-null only for heap buffers
0267 final int offset;
0268 boolean isReadOnly; // Valid only for heap buffers
0269
0270 // Creates a new buffer with the given mark, position, limit, capacity,
0271 // backing array, and array offset
0272 //
0273 $Type$Buffer(int mark, int pos, int lim, int cap, // package-private
0274 $type$[] hb, int offset)
0275 {
0276 super (mark, pos, lim, cap);
0277 this .hb = hb;
0278 this .offset = offset;
0279 }
0280
0281 // Creates a new buffer with the given mark, position, limit, and capacity
0282 //
0283 $Type$Buffer(int mark, int pos, int lim, int cap) { // package-private
0284 this (mark, pos, lim, cap, null, 0);
0285 }
0286
0287#if[byte]
0288
0289 /**
0290 * Allocates a new direct $fulltype$ buffer.
0291 *
0292 * <p> The new buffer's position will be zero, its limit will be its
0293 * capacity, its mark will be undefined, and each of its elements will be
0294 * initialized to zero. Whether or not it has a
0295 * {@link #hasArray </code>backing array<code>} is unspecified.
0296 *
0297 * @param capacity
0298 * The new buffer's capacity, in $fulltype$s
0299 *
0300 * @return The new $fulltype$ buffer
0301 *
0302 * @throws IllegalArgumentException
0303 * If the <tt>capacity</tt> is a negative integer
0304 */
0305 public static $Type$Buffer allocateDirect(int capacity) {
0306 return new Direct$Type$Buffer(capacity);
0307 }
0308
0309#end[byte]
0310
0311 /**
0312 * Allocates a new $fulltype$ buffer.
0313 *
0314 * <p> The new buffer's position will be zero, its limit will be its
0315 * capacity, its mark will be undefined, and each of its elements will be
0316 * initialized to zero. It will have a {@link #array
0317 * </code>backing array<code>}, and its {@link #arrayOffset </code>array
0318 * offset<code>} will be zero.
0319 *
0320 * @param capacity
0321 * The new buffer's capacity, in $fulltype$s
0322 *
0323 * @return The new $fulltype$ buffer
0324 *
0325 * @throws IllegalArgumentException
0326 * If the <tt>capacity</tt> is a negative integer
0327 */
0328 public static $Type$Buffer allocate(int capacity) {
0329 if (capacity < 0)
0330 throw new IllegalArgumentException();
0331 return new Heap$Type$Buffer(capacity, capacity);
0332 }
0333
0334 /**
0335 * Wraps $a$ $fulltype$ array into a buffer.
0336 *
0337 * <p> The new buffer will be backed by the given $fulltype$ array;
0338 * that is, modifications to the buffer will cause the array to be modified
0339 * and vice versa. The new buffer's capacity will be
0340 * <tt>array.length</tt>, its position will be <tt>offset</tt>, its limit
0341 * will be <tt>offset + length</tt>, and its mark will be undefined. Its
0342 * {@link #array </code>backing array<code>} will be the given array, and
0343 * its {@link #arrayOffset </code>array offset<code>} will be zero. </p>
0344 *
0345 * @param array
0346 * The array that will back the new buffer
0347 *
0348 * @param offset
0349 * The offset of the subarray to be used; must be non-negative and
0350 * no larger than <tt>array.length</tt>. The new buffer's position
0351 * will be set to this value.
0352 *
0353 * @param length
0354 * The length of the subarray to be used;
0355 * must be non-negative and no larger than
0356 * <tt>array.length - offset</tt>.
0357 * The new buffer's limit will be set to <tt>offset + length</tt>.
0358 *
0359 * @return The new $fulltype$ buffer
0360 *
0361 * @throws IndexOutOfBoundsException
0362 * If the preconditions on the <tt>offset</tt> and <tt>length</tt>
0363 * parameters do not hold
0364 */
0365 public static $Type$Buffer wrap($type$[] array,
0366 int offset, int length)
0367 {
0368 try {
0369 return new Heap$Type$Buffer(array, offset, length);
0370 } catch (IllegalArgumentException x) {
0371 throw new IndexOutOfBoundsException();
0372 }
0373 }
0374
0375 /**
0376 * Wraps $a$ $fulltype$ array into a buffer.
0377 *
0378 * <p> The new buffer will be backed by the given $fulltype$ array;
0379 * that is, modifications to the buffer will cause the array to be modified
0380 * and vice versa. The new buffer's capacity and limit will be
0381 * <tt>array.length</tt>, its position will be zero, and its mark will be
0382 * undefined. Its {@link #array </code>backing array<code>} will be the
0383 * given array, and its {@link #arrayOffset </code>array offset<code>} will
0384 * be zero. </p>
0385 *
0386 * @param array
0387 * The array that will back this buffer
0388 *
0389 * @return The new $fulltype$ buffer
0390 */
0391 public static $Type$Buffer wrap($type$[] array) {
0392 return wrap(array, 0, array.length);
0393 }
0394
0395#if[char]
0396
0397 /**
0398 * Attempts to read characters into the specified character buffer.
0399 * The buffer is used as a repository of characters as-is: the only
0400 * changes made are the results of a put operation. No flipping or
0401 * rewinding of the buffer is performed.
0402 *
0403 * @param target the buffer to read characters into
0404 * @return The number of characters added to the buffer, or
0405 * -1 if this source of characters is at its end
0406 * @throws IOException if an I/O error occurs
0407 * @throws NullPointerException if target is null
0408 * @throws ReadOnlyBufferException if target is a read only buffer
0409 * @since 1.5
0410 */
0411 public int read(CharBuffer target) throws IOException {
0412 // Determine the number of bytes n that can be transferred
0413 int targetRemaining = target.remaining();
0414 int remaining = remaining();
0415 if (remaining == 0)
0416 return -1;
0417 int n = Math.min(remaining, targetRemaining);
0418 int limit = limit();
0419 // Set source limit to prevent target overflow
0420 if (targetRemaining < remaining)
0421 limit(position() + n);
0422 try {
0423 if (n > 0)
0424 target.put(this );
0425 } finally {
0426 limit(limit); // restore real limit
0427 }
0428 return n;
0429 }
0430
0431 /**
0432 * Wraps a character sequence into a buffer.
0433 *
0434 * <p> The content of the new, read-only buffer will be the content of the
0435 * given character sequence. The buffer's capacity will be
0436 * <tt>csq.length()</tt>, its position will be <tt>start</tt>, its limit
0437 * will be <tt>end</tt>, and its mark will be undefined. </p>
0438 *
0439 * @param csq
0440 * The character sequence from which the new character buffer is to
0441 * be created
0442 *
0443 * @param start
0444 * The index of the first character to be used;
0445 * must be non-negative and no larger than <tt>csq.length()</tt>.
0446 * The new buffer's position will be set to this value.
0447 *
0448 * @param end
0449 * The index of the character following the last character to be
0450 * used; must be no smaller than <tt>start</tt> and no larger
0451 * than <tt>csq.length()</tt>.
0452 * The new buffer's limit will be set to this value.
0453 *
0454 * @return The new character buffer
0455 *
0456 * @throws IndexOutOfBoundsException
0457 * If the preconditions on the <tt>start</tt> and <tt>end</tt>
0458 * parameters do not hold
0459 */
0460 public static CharBuffer wrap(CharSequence csq, int start, int end) {
0461 try {
0462 return new StringCharBuffer(csq, start, end);
0463 } catch (IllegalArgumentException x) {
0464 throw new IndexOutOfBoundsException();
0465 }
0466 }
0467
0468 /**
0469 * Wraps a character sequence into a buffer.
0470 *
0471 * <p> The content of the new, read-only buffer will be the content of the
0472 * given character sequence. The new buffer's capacity and limit will be
0473 * <tt>csq.length()</tt>, its position will be zero, and its mark will be
0474 * undefined. </p>
0475 *
0476 * @param csq
0477 * The character sequence from which the new character buffer is to
0478 * be created
0479 *
0480 * @return The new character buffer
0481 */
0482 public static CharBuffer wrap(CharSequence csq) {
0483 return wrap(csq, 0, csq.length());
0484 }
0485
0486#end[char]
0487
0488 /**
0489 * Creates a new $fulltype$ buffer whose content is a shared subsequence of
0490 * this buffer's content.
0491 *
0492 * <p> The content of the new buffer will start at this buffer's current
0493 * position. Changes to this buffer's content will be visible in the new
0494 * buffer, and vice versa; the two buffers' position, limit, and mark
0495 * values will be independent.
0496 *
0497 * <p> The new buffer's position will be zero, its capacity and its limit
0498 * will be the number of $fulltype$s remaining in this buffer, and its mark
0499 * will be undefined. The new buffer will be direct if, and only if, this
0500 * buffer is direct, and it will be read-only if, and only if, this buffer
0501 * is read-only. </p>
0502 *
0503 * @return The new $fulltype$ buffer
0504 */
0505 public abstract $Type$Buffer slice();
0506
0507 /**
0508 * Creates a new $fulltype$ buffer that shares this buffer's content.
0509 *
0510 * <p> The content of the new buffer will be that of this buffer. Changes
0511 * to this buffer's content will be visible in the new buffer, and vice
0512 * versa; the two buffers' position, limit, and mark values will be
0513 * independent.
0514 *
0515 * <p> The new buffer's capacity, limit, position, and mark values will be
0516 * identical to those of this buffer. The new buffer will be direct if,
0517 * and only if, this buffer is direct, and it will be read-only if, and
0518 * only if, this buffer is read-only. </p>
0519 *
0520 * @return The new $fulltype$ buffer
0521 */
0522 public abstract $Type$Buffer duplicate();
0523
0524 /**
0525 * Creates a new, read-only $fulltype$ buffer that shares this buffer's
0526 * content.
0527 *
0528 * <p> The content of the new buffer will be that of this buffer. Changes
0529 * to this buffer's content will be visible in the new buffer; the new
0530 * buffer itself, however, will be read-only and will not allow the shared
0531 * content to be modified. The two buffers' position, limit, and mark
0532 * values will be independent.
0533 *
0534 * <p> The new buffer's capacity, limit, position, and mark values will be
0535 * identical to those of this buffer.
0536 *
0537 * <p> If this buffer is itself read-only then this method behaves in
0538 * exactly the same way as the {@link #duplicate duplicate} method. </p>
0539 *
0540 * @return The new, read-only $fulltype$ buffer
0541 */
0542 public abstract $Type$Buffer asReadOnlyBuffer();
0543
0544
0545 // -- Singleton get/put methods --
0546
0547 /**
0548 * Relative <i>get</i> method. Reads the $fulltype$ at this buffer's
0549 * current position, and then increments the position. </p>
0550 *
0551 * @return The $fulltype$ at the buffer's current position
0552 *
0553 * @throws BufferUnderflowException
0554 * If the buffer's current position is not smaller than its limit
0555 */
0556 public abstract $type$ get();
0557
0558 /**
0559 * Relative <i>put</i> method <i>(optional operation)</i>.
0560 *
0561 * <p> Writes the given $fulltype$ into this buffer at the current
0562 * position, and then increments the position. </p>
0563 *
0564 * @param $x$
0565 * The $fulltype$ to be written
0566 *
0567 * @return This buffer
0568 *
0569 * @throws BufferOverflowException
0570 * If this buffer's current position is not smaller than its limit
0571 *
0572 * @throws ReadOnlyBufferException
0573 * If this buffer is read-only
0574 */
0575 public abstract $Type$Buffer put($type$ $x$);
0576
0577 /**
0578 * Absolute <i>get</i> method. Reads the $fulltype$ at the given
0579 * index. </p>
0580 *
0581 * @param index
0582 * The index from which the $fulltype$ will be read
0583 *
0584 * @return The $fulltype$ at the given index
0585 *
0586 * @throws IndexOutOfBoundsException
0587 * If <tt>index</tt> is negative
0588 * or not smaller than the buffer's limit
0589 */
0590 public abstract $type$ get(int index);
0591
0592 /**
0593 * Absolute <i>put</i> method <i>(optional operation)</i>.
0594 *
0595 * <p> Writes the given $fulltype$ into this buffer at the given
0596 * index. </p>
0597 *
0598 * @param index
0599 * The index at which the $fulltype$ will be written
0600 *
0601 * @param $x$
0602 * The $fulltype$ value to be written
0603 *
0604 * @return This buffer
0605 *
0606 * @throws IndexOutOfBoundsException
0607 * If <tt>index</tt> is negative
0608 * or not smaller than the buffer's limit
0609 *
0610 * @throws ReadOnlyBufferException
0611 * If this buffer is read-only
0612 */
0613 public abstract $Type$Buffer put(int index, $type$ $x$);
0614
0615
0616 // -- Bulk get operations --
0617
0618 /**
0619 * Relative bulk <i>get</i> method.
0620 *
0621 * <p> This method transfers $fulltype$s from this buffer into the given
0622 * destination array. If there are fewer $fulltype$s remaining in the
0623 * buffer than are required to satisfy the request, that is, if
0624 * <tt>length</tt> <tt>></tt> <tt>remaining()</tt>, then no
0625 * $fulltype$s are transferred and a {@link BufferUnderflowException} is
0626 * thrown.
0627 *
0628 * <p> Otherwise, this method copies <tt>length</tt> $fulltype$s from this
0629 * buffer into the given array, starting at the current position of this
0630 * buffer and at the given offset in the array. The position of this
0631 * buffer is then incremented by <tt>length</tt>.
0632 *
0633 * <p> In other words, an invocation of this method of the form
0634 * <tt>src.get(dst, off, len)</tt> has exactly the same effect as
0635 * the loop
0636 *
0637 * <pre>
0638 * for (int i = off; i < off + len; i++)
0639 * dst[i] = src.get(); </pre>
0640 *
0641 * except that it first checks that there are sufficient $fulltype$s in
0642 * this buffer and it is potentially much more efficient. </p>
0643 *
0644 * @param dst
0645 * The array into which $fulltype$s are to be written
0646 *
0647 * @param offset
0648 * The offset within the array of the first $fulltype$ to be
0649 * written; must be non-negative and no larger than
0650 * <tt>dst.length</tt>
0651 *
0652 * @param length
0653 * The maximum number of $fulltype$s to be written to the given
0654 * array; must be non-negative and no larger than
0655 * <tt>dst.length - offset</tt>
0656 *
0657 * @return This buffer
0658 *
0659 * @throws BufferUnderflowException
0660 * If there are fewer than <tt>length</tt> $fulltype$s
0661 * remaining in this buffer
0662 *
0663 * @throws IndexOutOfBoundsException
0664 * If the preconditions on the <tt>offset</tt> and <tt>length</tt>
0665 * parameters do not hold
0666 */
0667 public $Type$Buffer get($type$[] dst, int offset, int length) {
0668 checkBounds(offset, length, dst.length);
0669 if (length > remaining())
0670 throw new BufferUnderflowException();
0671 int end = offset + length;
0672 for (int i = offset; i < end; i++)
0673 dst[i] = get();
0674 return this ;
0675 }
0676
0677 /**
0678 * Relative bulk <i>get</i> method.
0679 *
0680 * <p> This method transfers $fulltype$s from this buffer into the given
0681 * destination array. An invocation of this method of the form
0682 * <tt>src.get(a)</tt> behaves in exactly the same way as the invocation
0683 *
0684 * <pre>
0685 * src.get(a, 0, a.length) </pre>
0686 *
0687 * @return This buffer
0688 *
0689 * @throws BufferUnderflowException
0690 * If there are fewer than <tt>length</tt> $fulltype$s
0691 * remaining in this buffer
0692 */
0693 public $Type$Buffer get($type$[] dst) {
0694 return get(dst, 0, dst.length);
0695 }
0696
0697
0698 // -- Bulk put operations --
0699
0700 /**
0701 * Relative bulk <i>put</i> method <i>(optional operation)</i>.
0702 *
0703 * <p> This method transfers the $fulltype$s remaining in the given source
0704 * buffer into this buffer. If there are more $fulltype$s remaining in the
0705 * source buffer than in this buffer, that is, if
0706 * <tt>src.remaining()</tt> <tt>></tt> <tt>remaining()</tt>,
0707 * then no $fulltype$s are transferred and a {@link
0708 * BufferOverflowException} is thrown.
0709 *
0710 * <p> Otherwise, this method copies
0711 * <i>n</i> = <tt>src.remaining()</tt> $fulltype$s from the given
0712 * buffer into this buffer, starting at each buffer's current position.
0713 * The positions of both buffers are then incremented by <i>n</i>.
0714 *
0715 * <p> In other words, an invocation of this method of the form
0716 * <tt>dst.put(src)</tt> has exactly the same effect as the loop
0717 *
0718 * <pre>
0719 * while (src.hasRemaining())
0720 * dst.put(src.get()); </pre>
0721 *
0722 * except that it first checks that there is sufficient space in this
0723 * buffer and it is potentially much more efficient. </p>
0724 *
0725 * @param src
0726 * The source buffer from which $fulltype$s are to be read;
0727 * must not be this buffer
0728 *
0729 * @return This buffer
0730 *
0731 * @throws BufferOverflowException
0732 * If there is insufficient space in this buffer
0733 * for the remaining $fulltype$s in the source buffer
0734 *
0735 * @throws IllegalArgumentException
0736 * If the source buffer is this buffer
0737 *
0738 * @throws ReadOnlyBufferException
0739 * If this buffer is read-only
0740 */
0741 public $Type$Buffer put($Type$Buffer src) {
0742 if (src == this )
0743 throw new IllegalArgumentException();
0744 int n = src.remaining();
0745 if (n > remaining())
0746 throw new BufferOverflowException();
0747 for (int i = 0; i < n; i++)
0748 put(src.get());
0749 return this ;
0750 }
0751
0752 /**
0753 * Relative bulk <i>put</i> method <i>(optional operation)</i>.
0754 *
0755 * <p> This method transfers $fulltype$s into this buffer from the given
0756 * source array. If there are more $fulltype$s to be copied from the array
0757 * than remain in this buffer, that is, if
0758 * <tt>length</tt> <tt>></tt> <tt>remaining()</tt>, then no
0759 * $fulltype$s are transferred and a {@link BufferOverflowException} is
0760 * thrown.
0761 *
0762 * <p> Otherwise, this method copies <tt>length</tt> $fulltype$s from the
0763 * given array into this buffer, starting at the given offset in the array
0764 * and at the current position of this buffer. The position of this buffer
0765 * is then incremented by <tt>length</tt>.
0766 *
0767 * <p> In other words, an invocation of this method of the form
0768 * <tt>dst.put(src, off, len)</tt> has exactly the same effect as
0769 * the loop
0770 *
0771 * <pre>
0772 * for (int i = off; i < off + len; i++)
0773 * dst.put(a[i]); </pre>
0774 *
0775 * except that it first checks that there is sufficient space in this
0776 * buffer and it is potentially much more efficient. </p>
0777 *
0778 * @param src
0779 * The array from which $fulltype$s are to be read
0780 *
0781 * @param offset
0782 * The offset within the array of the first $fulltype$ to be read;
0783 * must be non-negative and no larger than <tt>array.length</tt>
0784 *
0785 * @param length
0786 * The number of $fulltype$s to be read from the given array;
0787 * must be non-negative and no larger than
0788 * <tt>array.length - offset</tt>
0789 *
0790 * @return This buffer
0791 *
0792 * @throws BufferOverflowException
0793 * If there is insufficient space in this buffer
0794 *
0795 * @throws IndexOutOfBoundsException
0796 * If the preconditions on the <tt>offset</tt> and <tt>length</tt>
0797 * parameters do not hold
0798 *
0799 * @throws ReadOnlyBufferException
0800 * If this buffer is read-only
0801 */
0802 public $Type$Buffer put($type$[] src, int offset, int length) {
0803 checkBounds(offset, length, src.length);
0804 if (length > remaining())
0805 throw new BufferOverflowException();
0806 int end = offset + length;
0807 for (int i = offset; i < end; i++)
0808 this .put(src[i]);
0809 return this ;
0810 }
0811
0812 /**
0813 * Relative bulk <i>put</i> method <i>(optional operation)</i>.
0814 *
0815 * <p> This method transfers the entire content of the given source
0816 * $fulltype$ array into this buffer. An invocation of this method of the
0817 * form <tt>dst.put(a)</tt> behaves in exactly the same way as the
0818 * invocation
0819 *
0820 * <pre>
0821 * dst.put(a, 0, a.length) </pre>
0822 *
0823 * @return This buffer
0824 *
0825 * @throws BufferOverflowException
0826 * If there is insufficient space in this buffer
0827 *
0828 * @throws ReadOnlyBufferException
0829 * If this buffer is read-only
0830 */
0831 public final $Type$Buffer put($type$[] src) {
0832 return put(src, 0, src.length);
0833 }
0834
0835#if[char]
0836
0837 /**
0838 * Relative bulk <i>put</i> method <i>(optional operation)</i>.
0839 *
0840 * <p> This method transfers $fulltype$s from the given string into this
0841 * buffer. If there are more $fulltype$s to be copied from the string than
0842 * remain in this buffer, that is, if
0843 * <tt>end - start</tt> <tt>></tt> <tt>remaining()</tt>,
0844 * then no $fulltype$s are transferred and a {@link
0845 * BufferOverflowException} is thrown.
0846 *
0847 * <p> Otherwise, this method copies
0848 * <i>n</i> = <tt>end</tt> - <tt>start</tt> $fulltype$s
0849 * from the given string into this buffer, starting at the given
0850 * <tt>start</tt> index and at the current position of this buffer. The
0851 * position of this buffer is then incremented by <i>n</i>.
0852 *
0853 * <p> In other words, an invocation of this method of the form
0854 * <tt>dst.put(src, start, end)</tt> has exactly the same effect
0855 * as the loop
0856 *
0857 * <pre>
0858 * for (int i = start; i < end; i++)
0859 * dst.put(src.charAt(i)); </pre>
0860 *
0861 * except that it first checks that there is sufficient space in this
0862 * buffer and it is potentially much more efficient. </p>
0863 *
0864 * @param src
0865 * The string from which $fulltype$s are to be read
0866 *
0867 * @param start
0868 * The offset within the string of the first $fulltype$ to be read;
0869 * must be non-negative and no larger than
0870 * <tt>string.length()</tt>
0871 *
0872 * @param end
0873 * The offset within the string of the last $fulltype$ to be read,
0874 * plus one; must be non-negative and no larger than
0875 * <tt>string.length()</tt>
0876 *
0877 * @return This buffer
0878 *
0879 * @throws BufferOverflowException
0880 * If there is insufficient space in this buffer
0881 *
0882 * @throws IndexOutOfBoundsException
0883 * If the preconditions on the <tt>start</tt> and <tt>end</tt>
0884 * parameters do not hold
0885 *
0886 * @throws ReadOnlyBufferException
0887 * If this buffer is read-only
0888 */
0889 public $Type$Buffer put(String src, int start, int end) {
0890 checkBounds(start, end - start, src.length());
0891 for (int i = start; i < end; i++)
0892 this .put(src.charAt(i));
0893 return this ;
0894 }
0895
0896 /**
0897 * Relative bulk <i>put</i> method <i>(optional operation)</i>.
0898 *
0899 * <p> This method transfers the entire content of the given source string
0900 * into this buffer. An invocation of this method of the form
0901 * <tt>dst.put(s)</tt> behaves in exactly the same way as the invocation
0902 *
0903 * <pre>
0904 * dst.put(s, 0, s.length()) </pre>
0905 *
0906 * @return This buffer
0907 *
0908 * @throws BufferOverflowException
0909 * If there is insufficient space in this buffer
0910 *
0911 * @throws ReadOnlyBufferException
0912 * If this buffer is read-only
0913 */
0914 public final $Type$Buffer put(String src) {
0915 return put(src, 0, src.length());
0916 }
0917
0918#end[char]
0919
0920
0921 // -- Other stuff --
0922
0923 /**
0924 * Tells whether or not this buffer is backed by an accessible $fulltype$
0925 * array.
0926 *
0927 * <p> If this method returns <tt>true</tt> then the {@link #array() array}
0928 * and {@link #arrayOffset() arrayOffset} methods may safely be invoked.
0929 * </p>
0930 *
0931 * @return <tt>true</tt> if, and only if, this buffer
0932 * is backed by an array and is not read-only
0933 */
0934 public final boolean hasArray() {
0935 return (hb != null) && !isReadOnly;
0936 }
0937
0938 /**
0939 * Returns the $fulltype$ array that backs this
0940 * buffer <i>(optional operation)</i>.
0941 *
0942 * <p> Modifications to this buffer's content will cause the returned
0943 * array's content to be modified, and vice versa.
0944 *
0945 * <p> Invoke the {@link #hasArray hasArray} method before invoking this
0946 * method in order to ensure that this buffer has an accessible backing
0947 * array. </p>
0948 *
0949 * @return The array that backs this buffer
0950 *
0951 * @throws ReadOnlyBufferException
0952 * If this buffer is backed by an array but is read-only
0953 *
0954 * @throws UnsupportedOperationException
0955 * If this buffer is not backed by an accessible array
0956 */
0957 public final $type$[] array() {
0958 if (hb == null)
0959 throw new UnsupportedOperationException();
0960 if (isReadOnly)
0961 throw new ReadOnlyBufferException();
0962 return hb;
0963 }
0964
0965 /**
0966 * Returns the offset within this buffer's backing array of the first
0967 * element of the buffer <i>(optional operation)</i>.
0968 *
0969 * <p> If this buffer is backed by an array then buffer position <i>p</i>
0970 * corresponds to array index <i>p</i> + <tt>arrayOffset()</tt>.
0971 *
0972 * <p> Invoke the {@link #hasArray hasArray} method before invoking this
0973 * method in order to ensure that this buffer has an accessible backing
0974 * array. </p>
0975 *
0976 * @return The offset within this buffer's array
0977 * of the first element of the buffer
0978 *
0979 * @throws ReadOnlyBufferException
0980 * If this buffer is backed by an array but is read-only
0981 *
0982 * @throws UnsupportedOperationException
0983 * If this buffer is not backed by an accessible array
0984 */
0985 public final int arrayOffset() {
0986 if (hb == null)
0987 throw new UnsupportedOperationException();
0988 if (isReadOnly)
0989 throw new ReadOnlyBufferException();
0990 return offset;
0991 }
0992
0993 /**
0994 * Compacts this buffer <i>(optional operation)</i>.
0995 *
0996 * <p> The $fulltype$s between the buffer's current position and its limit,
0997 * if any, are copied to the beginning of the buffer. That is, the
0998 * $fulltype$ at index <i>p</i> = <tt>position()</tt> is copied
0999 * to index zero, the $fulltype$ at index <i>p</i> + 1 is copied
1000 * to index one, and so forth until the $fulltype$ at index
1001 * <tt>limit()</tt> - 1 is copied to index
1002 * <i>n</i> = <tt>limit()</tt> - <tt>1</tt> - <i>p</i>.
1003 * The buffer's position is then set to <i>n+1</i> and its limit is set to
1004 * its capacity. The mark, if defined, is discarded.
1005 *
1006 * <p> The buffer's position is set to the number of $fulltype$s copied,
1007 * rather than to zero, so that an invocation of this method can be
1008 * followed immediately by an invocation of another relative <i>put</i>
1009 * method. </p>
1010 *
1011#if[byte]
1012 *
1013 * <p> Invoke this method after writing data from a buffer in case the
1014 * write was incomplete. The following loop, for example, copies bytes
1015 * from one channel to another via the buffer <tt>buf</tt>:
1016 *
1017 * <blockquote><pre>
1018 * buf.clear(); // Prepare buffer for use
1019 * while (in.read(buf) >= 0 || buf.position != 0) {
1020 * buf.flip();
1021 * out.write(buf);
1022 * buf.compact(); // In case of partial write
1023 * }</pre></blockquote>
1024 *
1025#end[byte]
1026 *
1027 * @return This buffer
1028 *
1029 * @throws ReadOnlyBufferException
1030 * If this buffer is read-only
1031 */
1032 public abstract $Type$Buffer compact();
1033
1034 /**
1035 * Tells whether or not this $fulltype$ buffer is direct. </p>
1036 *
1037 * @return <tt>true</tt> if, and only if, this buffer is direct
1038 */
1039 public abstract boolean isDirect();
1040
1041#if[!char]
1042
1043 /**
1044 * Returns a string summarizing the state of this buffer. </p>
1045 *
1046 * @return A summary string
1047 */
1048 public String toString() {
1049 StringBuffer sb = new StringBuffer();
1050 sb.append(getClass().getName());
1051 sb.append("[pos=");
1052 sb.append(position());
1053 sb.append(" lim=");
1054 sb.append(limit());
1055 sb.append(" cap=");
1056 sb.append(capacity());
1057 sb.append("]");
1058 return sb.toString();
1059 }
1060
1061#end[!char]
1062
1063
1064 // ## Should really use unchecked accessors here for speed
1065
1066 /**
1067 * Returns the current hash code of this buffer.
1068 *
1069 * <p> The hash code of a $type$ buffer depends only upon its remaining
1070 * elements; that is, upon the elements from <tt>position()</tt> up to, and
1071 * including, the element at <tt>limit()</tt> - <tt>1</tt>.
1072 *
1073 * <p> Because buffer hash codes are content-dependent, it is inadvisable
1074 * to use buffers as keys in hash maps or similar data structures unless it
1075 * is known that their contents will not change. </p>
1076 *
1077 * @return The current hash code of this buffer
1078 */
1079 public int hashCode() {
1080 int h = 1;
1081 int p = position();
1082 for (int i = limit() - 1; i >= p; i--)
1083 h = 31 * h + (int)get(i);
1084 return h;
1085 }
1086
1087 /**
1088 * Tells whether or not this buffer is equal to another object.
1089 *
1090 * <p> Two $type$ buffers are equal if, and only if,
1091 *
1092 * <p><ol>
1093 *
1094 * <li><p> They have the same element type, </p></li>
1095 *
1096 * <li><p> They have the same number of remaining elements, and
1097 * </p></li>
1098 *
1099 * <li><p> The two sequences of remaining elements, considered
1100 * independently of their starting positions, are pointwise equal.
1101 * </p></li>
1102 *
1103 * </ol>
1104 *
1105 * <p> A $type$ buffer is not equal to any other type of object. </p>
1106 *
1107 * @param ob The object to which this buffer is to be compared
1108 *
1109 * @return <tt>true</tt> if, and only if, this buffer is equal to the
1110 * given object
1111 */
1112 public boolean equals(Object ob) {
1113 if (this == ob)
1114 return true;
1115 if (!(ob instanceof $Type$Buffer))
1116 return false;
1117 $Type$Buffer that = ($Type$Buffer)ob;
1118 if (this .remaining() != that.remaining())
1119 return false;
1120 int p = this .position();
1121 for (int i = this .limit() - 1, j = that.limit() - 1; i >= p; i--, j--) {
1122 $type$ v1 = this .get(i);
1123 $type$ v2 = that.get(j);
1124 if (v1 != v2) {
1125 if ((v1 != v1) && (v2 != v2)) // For float and double
1126 continue;
1127 return false;
1128 }
1129 }
1130 return true;
1131 }
1132
1133 /**
1134 * Compares this buffer to another.
1135 *
1136 * <p> Two $type$ buffers are compared by comparing their sequences of
1137 * remaining elements lexicographically, without regard to the starting
1138 * position of each sequence within its corresponding buffer.
1139 *
1140 * <p> A $type$ buffer is not comparable to any other type of object.
1141 *
1142 * @return A negative integer, zero, or a positive integer as this buffer
1143 * is less than, equal to, or greater than the given buffer
1144 */
1145 public int compareTo($Type$Buffer that) {
1146 int n = this .position() + Math.min(this .remaining(), that.remaining());
1147 for (int i = this .position(), j = that.position(); i < n; i++, j++) {
1148 $type$ v1 = this .get(i);
1149 $type$ v2 = that.get(j);
1150 if (v1 == v2)
1151 continue;
1152 if ((v1 != v1) && (v2 != v2)) // For float and double
1153 continue;
1154 if (v1 < v2)
1155 return -1;
1156 return +1;
1157 }
1158 return this .remaining() - that.remaining();
1159 }
1160
1161
1162
1163 // -- Other char stuff --
1164
1165#if[char]
1166
1167 /**
1168 * Returns a string containing the characters in this buffer.
1169 *
1170 * <p> The first character of the resulting string will be the character at
1171 * this buffer's position, while the last character will be the character
1172 * at index <tt>limit()</tt> - 1. Invoking this method does not
1173 * change the buffer's position. </p>
1174 *
1175 * @return The specified string
1176 */
1177 public String toString() {
1178 return toString(position(), limit());
1179 }
1180
1181 abstract String toString(int start, int end); // package-private
1182
1183
1184 // --- Methods to support CharSequence ---
1185
1186 /**
1187 * Returns the length of this character buffer.
1188 *
1189 * <p> When viewed as a character sequence, the length of a character
1190 * buffer is simply the number of characters between the position
1191 * (inclusive) and the limit (exclusive); that is, it is equivalent to
1192 * <tt>remaining()</tt>. </p>
1193 *
1194 * @return The length of this character buffer
1195 */
1196 public final int length() {
1197 return remaining();
1198 }
1199
1200 /**
1201 * Reads the character at the given index relative to the current
1202 * position. </p>
1203 *
1204 * @param index
1205 * The index of the character to be read, relative to the position;
1206 * must be non-negative and smaller than <tt>remaining()</tt>
1207 *
1208 * @return The character at index
1209 * <tt>position() + index</tt>
1210 *
1211 * @throws IndexOutOfBoundsException
1212 * If the preconditions on <tt>index</tt> do not hold
1213 */
1214 public final char charAt(int index) {
1215 return get(position() + checkIndex(index, 1));
1216 }
1217
1218 /**
1219 * Creates a new character buffer that represents the specified subsequence
1220 * of this buffer, relative to the current position.
1221 *
1222 * <p> The new buffer will share this buffer's content; that is, if the
1223 * content of this buffer is mutable then modifications to one buffer will
1224 * cause the other to be modified. The new buffer's capacity will be that
1225 * of this buffer, its position will be
1226 * <tt>position()</tt> + <tt>start</tt>, and its limit will be
1227 * <tt>position()</tt> + <tt>end</tt>. The new buffer will be
1228 * direct if, and only if, this buffer is direct, and it will be read-only
1229 * if, and only if, this buffer is read-only. </p>
1230 *
1231 * @param start
1232 * The index, relative to the current position, of the first
1233 * character in the subsequence; must be non-negative and no larger
1234 * than <tt>remaining()</tt>
1235 *
1236 * @param end
1237 * The index, relative to the current position, of the character
1238 * following the last character in the subsequence; must be no
1239 * smaller than <tt>start</tt> and no larger than
1240 * <tt>remaining()</tt>
1241 *
1242 * @return The new character sequence
1243 *
1244 * @throws IndexOutOfBoundsException
1245 * If the preconditions on <tt>start</tt> and <tt>end</tt>
1246 * do not hold
1247 */
1248 public abstract CharSequence subSequence(int start, int end);
1249
1250
1251 // --- Methods to support Appendable ---
1252
1253 /**
1254 * Appends the specified character sequence to this
1255 * buffer <i>(optional operation)</i>.
1256 *
1257 * <p> An invocation of this method of the form <tt>dst.append(csq)</tt>
1258 * behaves in exactly the same way as the invocation
1259 *
1260 * <pre>
1261 * dst.put(csq.toString()) </pre>
1262 *
1263 * <p> Depending on the specification of <tt>toString</tt> for the
1264 * character sequence <tt>csq</tt>, the entire sequence may not be
1265 * appended. For instance, invoking the {@link $Type$Buffer#toString()
1266 * toString} method of a character buffer will return a subsequence whose
1267 * content depends upon the buffer's position and limit.
1268 *
1269 * @param csq
1270 * The character sequence to append. If <tt>csq</tt> is
1271 * <tt>null</tt>, then the four characters <tt>"null"</tt> are
1272 * appended to this character buffer.
1273 *
1274 * @return This buffer
1275 *
1276 * @throws BufferOverflowException
1277 * If there is insufficient space in this buffer
1278 *
1279 * @throws ReadOnlyBufferException
1280 * If this buffer is read-only
1281 *
1282 * @since 1.5
1283 */
1284 public $Type$Buffer append(CharSequence csq) {
1285 if (csq == null)
1286 return put("null");
1287 else
1288 return put(csq.toString());
1289 }
1290
1291 /**
1292 * Appends a subsequence of the specified character sequence to this
1293 * buffer <i>(optional operation)</i>.
1294 *
1295 * <p> An invocation of this method of the form <tt>dst.append(csq, start,
1296 * end)</tt> when <tt>csq</tt> is not <tt>null</tt>, behaves in exactly the
1297 * same way as the invocation
1298 *
1299 * <pre>
1300 * dst.put(csq.subSequence(start, end).toString()) </pre>
1301 *
1302 * @param csq
1303 * The character sequence from which a subsequence will be
1304 * appended. If <tt>csq</tt> is <tt>null</tt>, then characters
1305 * will be appended as if <tt>csq</tt> contained the four
1306 * characters <tt>"null"</tt>.
1307 *
1308 * @return This buffer
1309 *
1310 * @throws BufferOverflowException
1311 * If there is insufficient space in this buffer
1312 *
1313 * @throws IndexOutOfBoundsException
1314 * If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>
1315 * is greater than <tt>end</tt>, or <tt>end</tt> is greater than
1316 * <tt>csq.length()</tt>
1317 *
1318 * @throws ReadOnlyBufferException
1319 * If this buffer is read-only
1320 *
1321 * @since 1.5
1322 */
1323 public $Type$Buffer append(CharSequence csq, int start, int end) {
1324 CharSequence cs = (csq == null ? "null" : csq);
1325 return put(cs.subSequence(start, end).toString());
1326 }
1327
1328 /**
1329 * Appends the specified $fulltype$ to this
1330 * buffer <i>(optional operation)</i>.
1331 *
1332 * <p> An invocation of this method of the form <tt>dst.append($x$)</tt>
1333 * behaves in exactly the same way as the invocation
1334 *
1335 * <pre>
1336 * dst.put($x$) </pre>
1337 *
1338 * @param $x$
1339 * The 16-bit $fulltype$ to append
1340 *
1341 * @return This buffer
1342 *
1343 * @throws BufferOverflowException
1344 * If there is insufficient space in this buffer
1345 *
1346 * @throws ReadOnlyBufferException
1347 * If this buffer is read-only
1348 *
1349 * @since 1.5
1350 */
1351 public $Type$Buffer append($type$ $x$) {
1352 return put($x$);
1353 }
1354
1355#end[char]
1356
1357
1358 // -- Other byte stuff: Access to binary data --
1359
1360#if[!byte]
1361
1362 /**
1363 * Retrieves this buffer's byte order.
1364 *
1365 * <p> The byte order of $a$ $fulltype$ buffer created by allocation or by
1366 * wrapping an existing <tt>$type$</tt> array is the {@link
1367 * ByteOrder#nativeOrder </code>native order<code>} of the underlying
1368 * hardware. The byte order of $a$ $fulltype$ buffer created as a <a
1369 * href="ByteBuffer.html#views">view</a> of a byte buffer is that of the
1370 * byte buffer at the moment that the view is created. </p>
1371 *
1372 * @return This buffer's byte order
1373 */
1374 public abstract ByteOrder order();
1375
1376#end[!byte]
1377
1378#if[byte]
1379
1380 boolean bigEndian // package-private
1381 = true;
1382 boolean nativeByteOrder // package-private
1383 = (Bits.byteOrder() == ByteOrder.BIG_ENDIAN);
1384
1385 /**
1386 * Retrieves this buffer's byte order.
1387 *
1388 * <p> The byte order is used when reading or writing multibyte values, and
1389 * when creating buffers that are views of this byte buffer. The order of
1390 * a newly-created byte buffer is always {@link ByteOrder#BIG_ENDIAN
1391 * BIG_ENDIAN}. </p>
1392 *
1393 * @return This buffer's byte order
1394 */
1395 public final ByteOrder order() {
1396 return bigEndian ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
1397 }
1398
1399 /**
1400 * Modifies this buffer's byte order. </p>
1401 *
1402 * @param bo
1403 * The new byte order,
1404 * either {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}
1405 * or {@link ByteOrder#LITTLE_ENDIAN LITTLE_ENDIAN}
1406 *
1407 * @return This buffer
1408 */
1409 public final $Type$Buffer order(ByteOrder bo) {
1410 bigEndian = (bo == ByteOrder.BIG_ENDIAN);
1411 nativeByteOrder =
1412 (bigEndian == (Bits.byteOrder() == ByteOrder.BIG_ENDIAN));
1413 return this ;
1414 }
1415
1416 // Unchecked accessors, for use by ByteBufferAs-X-Buffer classes
1417 //
1418 abstract byte _get(int i); // package-private
1419 abstract void _put(int i, byte b); // package-private
1420
1421 // #BIN
1422 //
1423 // Binary-data access methods for short, char, int, long, float,
1424 // and double will be inserted here
1425
1426#end[byte]
1427
1428}
|