0N/A/*
2362N/A * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#warn This file is preprocessed before being compiled
0N/A
0N/Apackage java.nio;
0N/A
0N/A#if[char]
0N/Aimport java.io.IOException;
0N/A#end[char]
0N/A
0N/A/**
2005N/A * $A$ $type$ buffer.
0N/A *
0N/A * <p> This class defines {#if[byte]?six:four} categories of operations upon
2005N/A * $type$ buffers:
0N/A *
0N/A * <ul>
0N/A *
0N/A * <li><p> Absolute and relative {@link #get() </code><i>get</i><code>} and
0N/A * {@link #put($type$) </code><i>put</i><code>} methods that read and write
2005N/A * single $type$s; </p></li>
0N/A *
0N/A * <li><p> Relative {@link #get($type$[]) </code><i>bulk get</i><code>}
2005N/A * methods that transfer contiguous sequences of $type$s from this buffer
0N/A * into an array; {#if[!byte]?and}</p></li>
0N/A *
0N/A * <li><p> Relative {@link #put($type$[]) </code><i>bulk put</i><code>}
2005N/A * methods that transfer contiguous sequences of $type$s from $a$
2005N/A * $type$ array{#if[char]?,&#32;a&#32;string,} or some other $type$
0N/A * buffer into this buffer;{#if[!byte]?&#32;and} </p></li>
0N/A *
0N/A#if[byte]
0N/A *
0N/A * <li><p> Absolute and relative {@link #getChar() </code><i>get</i><code>}
0N/A * and {@link #putChar(char) </code><i>put</i><code>} methods that read and
0N/A * write values of other primitive types, translating them to and from
0N/A * sequences of bytes in a particular byte order; </p></li>
0N/A *
0N/A * <li><p> Methods for creating <i><a href="#views">view buffers</a></i>,
0N/A * which allow a byte buffer to be viewed as a buffer containing values of
0N/A * some other primitive type; and </p></li>
0N/A *
0N/A#end[byte]
0N/A *
0N/A * <li><p> Methods for {@link #compact </code>compacting<code>}, {@link
0N/A * #duplicate </code>duplicating<code>}, and {@link #slice
2005N/A * </code>slicing<code>} $a$ $type$ buffer. </p></li>
0N/A *
0N/A * </ul>
0N/A *
2005N/A * <p> $Type$ buffers can be created either by {@link #allocate
0N/A * </code><i>allocation</i><code>}, which allocates space for the buffer's
0N/A *
0N/A#if[byte]
0N/A *
0N/A * content, or by {@link #wrap($type$[]) </code><i>wrapping</i><code>} an
2005N/A * existing $type$ array {#if[char]?or&#32;string} into a buffer.
0N/A *
0N/A#else[byte]
0N/A *
0N/A * content, by {@link #wrap($type$[]) </code><i>wrapping</i><code>} an existing
2005N/A * $type$ array {#if[char]?or&#32;string} into a buffer, or by creating a
0N/A * <a href="ByteBuffer.html#views"><i>view</i></a> of an existing byte buffer.
0N/A *
0N/A#end[byte]
0N/A *
0N/A#if[byte]
0N/A *
0N/A * <a name="direct">
0N/A * <h4> Direct <i>vs.</i> non-direct buffers </h4>
0N/A *
0N/A * <p> A byte buffer is either <i>direct</i> or <i>non-direct</i>. Given a
0N/A * direct byte buffer, the Java virtual machine will make a best effort to
0N/A * perform native I/O operations directly upon it. That is, it will attempt to
0N/A * avoid copying the buffer's content to (or from) an intermediate buffer
0N/A * before (or after) each invocation of one of the underlying operating
0N/A * system's native I/O operations.
0N/A *
0N/A * <p> A direct byte buffer may be created by invoking the {@link
0N/A * #allocateDirect(int) allocateDirect} factory method of this class. The
0N/A * buffers returned by this method typically have somewhat higher allocation
0N/A * and deallocation costs than non-direct buffers. The contents of direct
0N/A * buffers may reside outside of the normal garbage-collected heap, and so
0N/A * their impact upon the memory footprint of an application might not be
0N/A * obvious. It is therefore recommended that direct buffers be allocated
0N/A * primarily for large, long-lived buffers that are subject to the underlying
0N/A * system's native I/O operations. In general it is best to allocate direct
0N/A * buffers only when they yield a measureable gain in program performance.
0N/A *
0N/A * <p> A direct byte buffer may also be created by {@link
0N/A * java.nio.channels.FileChannel#map </code>mapping<code>} a region of a file
0N/A * directly into memory. An implementation of the Java platform may optionally
0N/A * support the creation of direct byte buffers from native code via JNI. If an
0N/A * instance of one of these kinds of buffers refers to an inaccessible region
0N/A * of memory then an attempt to access that region will not change the buffer's
0N/A * content and will cause an unspecified exception to be thrown either at the
0N/A * time of the access or at some later time.
0N/A *
0N/A * <p> Whether a byte buffer is direct or non-direct may be determined by
0N/A * invoking its {@link #isDirect isDirect} method. This method is provided so
0N/A * that explicit buffer management can be done in performance-critical code.
0N/A *
0N/A *
0N/A * <a name="bin">
0N/A * <h4> Access to binary data </h4>
0N/A *
0N/A * <p> This class defines methods for reading and writing values of all other
0N/A * primitive types, except <tt>boolean</tt>. Primitive values are translated
0N/A * to (or from) sequences of bytes according to the buffer's current byte
0N/A * order, which may be retrieved and modified via the {@link #order order}
0N/A * methods. Specific byte orders are represented by instances of the {@link
0N/A * ByteOrder} class. The initial order of a byte buffer is always {@link
0N/A * ByteOrder#BIG_ENDIAN BIG_ENDIAN}.
0N/A *
0N/A * <p> For access to heterogeneous binary data, that is, sequences of values of
0N/A * different types, this class defines a family of absolute and relative
0N/A * <i>get</i> and <i>put</i> methods for each type. For 32-bit floating-point
0N/A * values, for example, this class defines:
0N/A *
0N/A * <blockquote><pre>
0N/A * float {@link #getFloat()}
0N/A * float {@link #getFloat(int) getFloat(int index)}
0N/A * void {@link #putFloat(float) putFloat(float f)}
0N/A * void {@link #putFloat(int,float) putFloat(int index, float f)}</pre></blockquote>
0N/A *
0N/A * <p> Corresponding methods are defined for the types <tt>char</tt>,
0N/A * <tt>short</tt>, <tt>int</tt>, <tt>long</tt>, and <tt>double</tt>. The index
0N/A * parameters of the absolute <i>get</i> and <i>put</i> methods are in terms of
0N/A * bytes rather than of the type being read or written.
0N/A *
0N/A * <a name="views">
0N/A *
0N/A * <p> For access to homogeneous binary data, that is, sequences of values of
0N/A * the same type, this class defines methods that can create <i>views</i> of a
0N/A * given byte buffer. A <i>view buffer</i> is simply another buffer whose
0N/A * content is backed by the byte buffer. Changes to the byte buffer's content
0N/A * will be visible in the view buffer, and vice versa; the two buffers'
0N/A * position, limit, and mark values are independent. The {@link
0N/A * #asFloatBuffer() asFloatBuffer} method, for example, creates an instance of
0N/A * the {@link FloatBuffer} class that is backed by the byte buffer upon which
0N/A * the method is invoked. Corresponding view-creation methods are defined for
0N/A * the types <tt>char</tt>, <tt>short</tt>, <tt>int</tt>, <tt>long</tt>, and
0N/A * <tt>double</tt>.
0N/A *
0N/A * <p> View buffers have three important advantages over the families of
0N/A * type-specific <i>get</i> and <i>put</i> methods described above:
0N/A *
0N/A * <ul>
0N/A *
0N/A * <li><p> A view buffer is indexed not in terms of bytes but rather in terms
0N/A * of the type-specific size of its values; </p></li>
0N/A *
0N/A * <li><p> A view buffer provides relative bulk <i>get</i> and <i>put</i>
0N/A * methods that can transfer contiguous sequences of values between a buffer
0N/A * and an array or some other buffer of the same type; and </p></li>
0N/A *
0N/A * <li><p> A view buffer is potentially much more efficient because it will
0N/A * be direct if, and only if, its backing byte buffer is direct. </p></li>
0N/A *
0N/A * </ul>
0N/A *
0N/A * <p> The byte order of a view buffer is fixed to be that of its byte buffer
0N/A * at the time that the view is created. </p>
0N/A *
0N/A#end[byte]
0N/A*
0N/A#if[!byte]
0N/A *
2005N/A * <p> Like a byte buffer, $a$ $type$ buffer is either <a
0N/A * href="ByteBuffer.html#direct"><i>direct</i> or <i>non-direct</i></a>. A
2005N/A * $type$ buffer created via the <tt>wrap</tt> methods of this class will
2005N/A * be non-direct. $A$ $type$ buffer created as a view of a byte buffer will
0N/A * be direct if, and only if, the byte buffer itself is direct. Whether or not
2005N/A * $a$ $type$ buffer is direct may be determined by invoking the {@link
0N/A * #isDirect isDirect} method. </p>
0N/A *
0N/A#end[!byte]
0N/A*
0N/A#if[char]
0N/A *
0N/A * <p> This class implements the {@link CharSequence} interface so that
0N/A * character buffers may be used wherever character sequences are accepted, for
0N/A * example in the regular-expression package <tt>{@link java.util.regex}</tt>.
0N/A * </p>
0N/A *
0N/A#end[char]
0N/A *
0N/A#if[byte]
0N/A * <h4> Invocation chaining </h4>
0N/A#end[byte]
0N/A *
0N/A * <p> Methods in this class that do not otherwise have a value to return are
0N/A * specified to return the buffer upon which they are invoked. This allows
0N/A * method invocations to be chained.
0N/A *
0N/A#if[byte]
0N/A *
0N/A * The sequence of statements
0N/A *
0N/A * <blockquote><pre>
0N/A * bb.putInt(0xCAFEBABE);
0N/A * bb.putShort(3);
0N/A * bb.putShort(45);</pre></blockquote>
0N/A *
0N/A * can, for example, be replaced by the single statement
0N/A *
0N/A * <blockquote><pre>
0N/A * bb.putInt(0xCAFEBABE).putShort(3).putShort(45);</pre></blockquote>
0N/A *
0N/A#end[byte]
0N/A#if[char]
0N/A *
0N/A * The sequence of statements
0N/A *
0N/A * <blockquote><pre>
0N/A * cb.put("text/");
0N/A * cb.put(subtype);
0N/A * cb.put("; charset=");
0N/A * cb.put(enc);</pre></blockquote>
0N/A *
0N/A * can, for example, be replaced by the single statement
0N/A *
0N/A * <blockquote><pre>
0N/A * cb.put("text/").put(subtype).put("; charset=").put(enc);</pre></blockquote>
0N/A *
0N/A#end[char]
0N/A *
0N/A *
0N/A * @author Mark Reinhold
0N/A * @author JSR-51 Expert Group
0N/A * @since 1.4
0N/A */
0N/A
0N/Apublic abstract class $Type$Buffer
0N/A extends Buffer
0N/A implements Comparable<$Type$Buffer>{#if[char]?, Appendable, CharSequence, Readable}
0N/A{
0N/A
0N/A // These fields are declared here rather than in Heap-X-Buffer in order to
0N/A // reduce the number of virtual method invocations needed to access these
0N/A // values, which is especially costly when coding small buffers.
0N/A //
0N/A final $type$[] hb; // Non-null only for heap buffers
0N/A final int offset;
0N/A boolean isReadOnly; // Valid only for heap buffers
0N/A
0N/A // Creates a new buffer with the given mark, position, limit, capacity,
0N/A // backing array, and array offset
0N/A //
0N/A $Type$Buffer(int mark, int pos, int lim, int cap, // package-private
0N/A $type$[] hb, int offset)
0N/A {
0N/A super(mark, pos, lim, cap);
0N/A this.hb = hb;
0N/A this.offset = offset;
0N/A }
0N/A
0N/A // Creates a new buffer with the given mark, position, limit, and capacity
0N/A //
0N/A $Type$Buffer(int mark, int pos, int lim, int cap) { // package-private
0N/A this(mark, pos, lim, cap, null, 0);
0N/A }
0N/A
0N/A#if[byte]
0N/A
0N/A /**
2005N/A * Allocates a new direct $type$ buffer.
0N/A *
0N/A * <p> The new buffer's position will be zero, its limit will be its
0N/A * capacity, its mark will be undefined, and each of its elements will be
0N/A * initialized to zero. Whether or not it has a
0N/A * {@link #hasArray </code>backing array<code>} is unspecified.
0N/A *
0N/A * @param capacity
2005N/A * The new buffer's capacity, in $type$s
0N/A *
2005N/A * @return The new $type$ buffer
0N/A *
0N/A * @throws IllegalArgumentException
0N/A * If the <tt>capacity</tt> is a negative integer
0N/A */
0N/A public static $Type$Buffer allocateDirect(int capacity) {
0N/A return new Direct$Type$Buffer(capacity);
0N/A }
0N/A
0N/A#end[byte]
0N/A
0N/A /**
2005N/A * Allocates a new $type$ buffer.
0N/A *
0N/A * <p> The new buffer's position will be zero, its limit will be its
0N/A * capacity, its mark will be undefined, and each of its elements will be
0N/A * initialized to zero. It will have a {@link #array
0N/A * </code>backing array<code>}, and its {@link #arrayOffset </code>array
0N/A * offset<code>} will be zero.
0N/A *
0N/A * @param capacity
2005N/A * The new buffer's capacity, in $type$s
0N/A *
2005N/A * @return The new $type$ buffer
0N/A *
0N/A * @throws IllegalArgumentException
0N/A * If the <tt>capacity</tt> is a negative integer
0N/A */
0N/A public static $Type$Buffer allocate(int capacity) {
0N/A if (capacity < 0)
0N/A throw new IllegalArgumentException();
0N/A return new Heap$Type$Buffer(capacity, capacity);
0N/A }
0N/A
0N/A /**
2005N/A * Wraps $a$ $type$ array into a buffer.
0N/A *
2005N/A * <p> The new buffer will be backed by the given $type$ array;
0N/A * that is, modifications to the buffer will cause the array to be modified
0N/A * and vice versa. The new buffer's capacity will be
0N/A * <tt>array.length</tt>, its position will be <tt>offset</tt>, its limit
0N/A * will be <tt>offset + length</tt>, and its mark will be undefined. Its
0N/A * {@link #array </code>backing array<code>} will be the given array, and
0N/A * its {@link #arrayOffset </code>array offset<code>} will be zero. </p>
0N/A *
0N/A * @param array
0N/A * The array that will back the new buffer
0N/A *
0N/A * @param offset
0N/A * The offset of the subarray to be used; must be non-negative and
0N/A * no larger than <tt>array.length</tt>. The new buffer's position
0N/A * will be set to this value.
0N/A *
0N/A * @param length
0N/A * The length of the subarray to be used;
0N/A * must be non-negative and no larger than
0N/A * <tt>array.length - offset</tt>.
0N/A * The new buffer's limit will be set to <tt>offset + length</tt>.
0N/A *
2005N/A * @return The new $type$ buffer
0N/A *
0N/A * @throws IndexOutOfBoundsException
0N/A * If the preconditions on the <tt>offset</tt> and <tt>length</tt>
0N/A * parameters do not hold
0N/A */
0N/A public static $Type$Buffer wrap($type$[] array,
0N/A int offset, int length)
0N/A {
0N/A try {
0N/A return new Heap$Type$Buffer(array, offset, length);
0N/A } catch (IllegalArgumentException x) {
0N/A throw new IndexOutOfBoundsException();
0N/A }
0N/A }
0N/A
0N/A /**
2005N/A * Wraps $a$ $type$ array into a buffer.
0N/A *
2005N/A * <p> The new buffer will be backed by the given $type$ array;
0N/A * that is, modifications to the buffer will cause the array to be modified
0N/A * and vice versa. The new buffer's capacity and limit will be
0N/A * <tt>array.length</tt>, its position will be zero, and its mark will be
0N/A * undefined. Its {@link #array </code>backing array<code>} will be the
0N/A * given array, and its {@link #arrayOffset </code>array offset<code>} will
0N/A * be zero. </p>
0N/A *
0N/A * @param array
0N/A * The array that will back this buffer
0N/A *
2005N/A * @return The new $type$ buffer
0N/A */
0N/A public static $Type$Buffer wrap($type$[] array) {
0N/A return wrap(array, 0, array.length);
0N/A }
0N/A
0N/A#if[char]
0N/A
0N/A /**
0N/A * Attempts to read characters into the specified character buffer.
0N/A * The buffer is used as a repository of characters as-is: the only
0N/A * changes made are the results of a put operation. No flipping or
0N/A * rewinding of the buffer is performed.
0N/A *
0N/A * @param target the buffer to read characters into
0N/A * @return The number of characters added to the buffer, or
0N/A * -1 if this source of characters is at its end
0N/A * @throws IOException if an I/O error occurs
0N/A * @throws NullPointerException if target is null
0N/A * @throws ReadOnlyBufferException if target is a read only buffer
0N/A * @since 1.5
0N/A */
0N/A public int read(CharBuffer target) throws IOException {
0N/A // Determine the number of bytes n that can be transferred
0N/A int targetRemaining = target.remaining();
0N/A int remaining = remaining();
0N/A if (remaining == 0)
0N/A return -1;
0N/A int n = Math.min(remaining, targetRemaining);
0N/A int limit = limit();
0N/A // Set source limit to prevent target overflow
0N/A if (targetRemaining < remaining)
0N/A limit(position() + n);
0N/A try {
0N/A if (n > 0)
0N/A target.put(this);
0N/A } finally {
0N/A limit(limit); // restore real limit
0N/A }
0N/A return n;
0N/A }
0N/A
0N/A /**
0N/A * Wraps a character sequence into a buffer.
0N/A *
0N/A * <p> The content of the new, read-only buffer will be the content of the
0N/A * given character sequence. The buffer's capacity will be
0N/A * <tt>csq.length()</tt>, its position will be <tt>start</tt>, its limit
0N/A * will be <tt>end</tt>, and its mark will be undefined. </p>
0N/A *
0N/A * @param csq
0N/A * The character sequence from which the new character buffer is to
0N/A * be created
0N/A *
0N/A * @param start
0N/A * The index of the first character to be used;
0N/A * must be non-negative and no larger than <tt>csq.length()</tt>.
0N/A * The new buffer's position will be set to this value.
0N/A *
0N/A * @param end
0N/A * The index of the character following the last character to be
0N/A * used; must be no smaller than <tt>start</tt> and no larger
0N/A * than <tt>csq.length()</tt>.
0N/A * The new buffer's limit will be set to this value.
0N/A *
0N/A * @return The new character buffer
0N/A *
0N/A * @throws IndexOutOfBoundsException
0N/A * If the preconditions on the <tt>start</tt> and <tt>end</tt>
0N/A * parameters do not hold
0N/A */
0N/A public static CharBuffer wrap(CharSequence csq, int start, int end) {
0N/A try {
0N/A return new StringCharBuffer(csq, start, end);
0N/A } catch (IllegalArgumentException x) {
0N/A throw new IndexOutOfBoundsException();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Wraps a character sequence into a buffer.
0N/A *
0N/A * <p> The content of the new, read-only buffer will be the content of the
0N/A * given character sequence. The new buffer's capacity and limit will be
0N/A * <tt>csq.length()</tt>, its position will be zero, and its mark will be
0N/A * undefined. </p>
0N/A *
0N/A * @param csq
0N/A * The character sequence from which the new character buffer is to
0N/A * be created
0N/A *
0N/A * @return The new character buffer
0N/A */
0N/A public static CharBuffer wrap(CharSequence csq) {
0N/A return wrap(csq, 0, csq.length());
0N/A }
0N/A
0N/A#end[char]
0N/A
0N/A /**
2005N/A * Creates a new $type$ buffer whose content is a shared subsequence of
0N/A * this buffer's content.
0N/A *
0N/A * <p> The content of the new buffer will start at this buffer's current
0N/A * position. Changes to this buffer's content will be visible in the new
0N/A * buffer, and vice versa; the two buffers' position, limit, and mark
0N/A * values will be independent.
0N/A *
0N/A * <p> The new buffer's position will be zero, its capacity and its limit
2005N/A * will be the number of $type$s remaining in this buffer, and its mark
0N/A * will be undefined. The new buffer will be direct if, and only if, this
0N/A * buffer is direct, and it will be read-only if, and only if, this buffer
0N/A * is read-only. </p>
0N/A *
2005N/A * @return The new $type$ buffer
0N/A */
0N/A public abstract $Type$Buffer slice();
0N/A
0N/A /**
2005N/A * Creates a new $type$ buffer that shares this buffer's content.
0N/A *
0N/A * <p> The content of the new buffer will be that of this buffer. Changes
0N/A * to this buffer's content will be visible in the new buffer, and vice
0N/A * versa; the two buffers' position, limit, and mark values will be
0N/A * independent.
0N/A *
0N/A * <p> The new buffer's capacity, limit, position, and mark values will be
0N/A * identical to those of this buffer. The new buffer will be direct if,
0N/A * and only if, this buffer is direct, and it will be read-only if, and
0N/A * only if, this buffer is read-only. </p>
0N/A *
2005N/A * @return The new $type$ buffer
0N/A */
0N/A public abstract $Type$Buffer duplicate();
0N/A
0N/A /**
2005N/A * Creates a new, read-only $type$ buffer that shares this buffer's
0N/A * content.
0N/A *
0N/A * <p> The content of the new buffer will be that of this buffer. Changes
0N/A * to this buffer's content will be visible in the new buffer; the new
0N/A * buffer itself, however, will be read-only and will not allow the shared
0N/A * content to be modified. The two buffers' position, limit, and mark
0N/A * values will be independent.
0N/A *
0N/A * <p> The new buffer's capacity, limit, position, and mark values will be
0N/A * identical to those of this buffer.
0N/A *
0N/A * <p> If this buffer is itself read-only then this method behaves in
0N/A * exactly the same way as the {@link #duplicate duplicate} method. </p>
0N/A *
2005N/A * @return The new, read-only $type$ buffer
0N/A */
0N/A public abstract $Type$Buffer asReadOnlyBuffer();
0N/A
0N/A
0N/A // -- Singleton get/put methods --
0N/A
0N/A /**
2005N/A * Relative <i>get</i> method. Reads the $type$ at this buffer's
0N/A * current position, and then increments the position. </p>
0N/A *
2005N/A * @return The $type$ at the buffer's current position
0N/A *
0N/A * @throws BufferUnderflowException
0N/A * If the buffer's current position is not smaller than its limit
0N/A */
0N/A public abstract $type$ get();
0N/A
0N/A /**
0N/A * Relative <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
0N/A *
2005N/A * <p> Writes the given $type$ into this buffer at the current
0N/A * position, and then increments the position. </p>
0N/A *
0N/A * @param $x$
2005N/A * The $type$ to be written
0N/A *
0N/A * @return This buffer
0N/A *
0N/A * @throws BufferOverflowException
0N/A * If this buffer's current position is not smaller than its limit
0N/A *
0N/A * @throws ReadOnlyBufferException
0N/A * If this buffer is read-only
0N/A */
0N/A public abstract $Type$Buffer put($type$ $x$);
0N/A
0N/A /**
2005N/A * Absolute <i>get</i> method. Reads the $type$ at the given
0N/A * index. </p>
0N/A *
0N/A * @param index
2005N/A * The index from which the $type$ will be read
0N/A *
2005N/A * @return The $type$ at the given index
0N/A *
0N/A * @throws IndexOutOfBoundsException
0N/A * If <tt>index</tt> is negative
0N/A * or not smaller than the buffer's limit
0N/A */
0N/A public abstract $type$ get(int index);
0N/A
0N/A /**
0N/A * Absolute <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
0N/A *
2005N/A * <p> Writes the given $type$ into this buffer at the given
0N/A * index. </p>
0N/A *
0N/A * @param index
2005N/A * The index at which the $type$ will be written
0N/A *
0N/A * @param $x$
2005N/A * The $type$ value to be written
0N/A *
0N/A * @return This buffer
0N/A *
0N/A * @throws IndexOutOfBoundsException
0N/A * If <tt>index</tt> is negative
0N/A * or not smaller than the buffer's limit
0N/A *
0N/A * @throws ReadOnlyBufferException
0N/A * If this buffer is read-only
0N/A */
0N/A public abstract $Type$Buffer put(int index, $type$ $x$);
0N/A
0N/A
0N/A // -- Bulk get operations --
0N/A
0N/A /**
0N/A * Relative bulk <i>get</i> method.
0N/A *
2005N/A * <p> This method transfers $type$s from this buffer into the given
2005N/A * destination array. If there are fewer $type$s remaining in the
0N/A * buffer than are required to satisfy the request, that is, if
0N/A * <tt>length</tt>&nbsp;<tt>&gt;</tt>&nbsp;<tt>remaining()</tt>, then no
2005N/A * $type$s are transferred and a {@link BufferUnderflowException} is
0N/A * thrown.
0N/A *
2005N/A * <p> Otherwise, this method copies <tt>length</tt> $type$s from this
0N/A * buffer into the given array, starting at the current position of this
0N/A * buffer and at the given offset in the array. The position of this
0N/A * buffer is then incremented by <tt>length</tt>.
0N/A *
0N/A * <p> In other words, an invocation of this method of the form
0N/A * <tt>src.get(dst,&nbsp;off,&nbsp;len)</tt> has exactly the same effect as
0N/A * the loop
0N/A *
0N/A * <pre>
0N/A * for (int i = off; i < off + len; i++)
0N/A * dst[i] = src.get(); </pre>
0N/A *
2005N/A * except that it first checks that there are sufficient $type$s in
0N/A * this buffer and it is potentially much more efficient. </p>
0N/A *
0N/A * @param dst
2005N/A * The array into which $type$s are to be written
0N/A *
0N/A * @param offset
2005N/A * The offset within the array of the first $type$ to be
0N/A * written; must be non-negative and no larger than
0N/A * <tt>dst.length</tt>
0N/A *
0N/A * @param length
2005N/A * The maximum number of $type$s to be written to the given
0N/A * array; must be non-negative and no larger than
0N/A * <tt>dst.length - offset</tt>
0N/A *
0N/A * @return This buffer
0N/A *
0N/A * @throws BufferUnderflowException
2005N/A * If there are fewer than <tt>length</tt> $type$s
0N/A * remaining in this buffer
0N/A *
0N/A * @throws IndexOutOfBoundsException
0N/A * If the preconditions on the <tt>offset</tt> and <tt>length</tt>
0N/A * parameters do not hold
0N/A */
0N/A public $Type$Buffer get($type$[] dst, int offset, int length) {
0N/A checkBounds(offset, length, dst.length);
0N/A if (length > remaining())
0N/A throw new BufferUnderflowException();
0N/A int end = offset + length;
0N/A for (int i = offset; i < end; i++)
0N/A dst[i] = get();
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Relative bulk <i>get</i> method.
0N/A *
2005N/A * <p> This method transfers $type$s from this buffer into the given
0N/A * destination array. An invocation of this method of the form
0N/A * <tt>src.get(a)</tt> behaves in exactly the same way as the invocation
0N/A *
0N/A * <pre>
0N/A * src.get(a, 0, a.length) </pre>
0N/A *
0N/A * @return This buffer
0N/A *
0N/A * @throws BufferUnderflowException
2005N/A * If there are fewer than <tt>length</tt> $type$s
0N/A * remaining in this buffer
0N/A */
0N/A public $Type$Buffer get($type$[] dst) {
0N/A return get(dst, 0, dst.length);
0N/A }
0N/A
0N/A
0N/A // -- Bulk put operations --
0N/A
0N/A /**
0N/A * Relative bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
0N/A *
2005N/A * <p> This method transfers the $type$s remaining in the given source
2005N/A * buffer into this buffer. If there are more $type$s remaining in the
0N/A * source buffer than in this buffer, that is, if
0N/A * <tt>src.remaining()</tt>&nbsp;<tt>&gt;</tt>&nbsp;<tt>remaining()</tt>,
2005N/A * then no $type$s are transferred and a {@link
0N/A * BufferOverflowException} is thrown.
0N/A *
0N/A * <p> Otherwise, this method copies
2005N/A * <i>n</i>&nbsp;=&nbsp;<tt>src.remaining()</tt> $type$s from the given
0N/A * buffer into this buffer, starting at each buffer's current position.
0N/A * The positions of both buffers are then incremented by <i>n</i>.
0N/A *
0N/A * <p> In other words, an invocation of this method of the form
0N/A * <tt>dst.put(src)</tt> has exactly the same effect as the loop
0N/A *
0N/A * <pre>
0N/A * while (src.hasRemaining())
0N/A * dst.put(src.get()); </pre>
0N/A *
0N/A * except that it first checks that there is sufficient space in this
0N/A * buffer and it is potentially much more efficient. </p>
0N/A *
0N/A * @param src
2005N/A * The source buffer from which $type$s are to be read;
0N/A * must not be this buffer
0N/A *
0N/A * @return This buffer
0N/A *
0N/A * @throws BufferOverflowException
0N/A * If there is insufficient space in this buffer
2005N/A * for the remaining $type$s in the source buffer
0N/A *
0N/A * @throws IllegalArgumentException
0N/A * If the source buffer is this buffer
0N/A *
0N/A * @throws ReadOnlyBufferException
0N/A * If this buffer is read-only
0N/A */
0N/A public $Type$Buffer put($Type$Buffer src) {
0N/A if (src == this)
0N/A throw new IllegalArgumentException();
0N/A int n = src.remaining();
0N/A if (n > remaining())
0N/A throw new BufferOverflowException();
0N/A for (int i = 0; i < n; i++)
0N/A put(src.get());
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Relative bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
0N/A *
2005N/A * <p> This method transfers $type$s into this buffer from the given
2005N/A * source array. If there are more $type$s to be copied from the array
0N/A * than remain in this buffer, that is, if
0N/A * <tt>length</tt>&nbsp;<tt>&gt;</tt>&nbsp;<tt>remaining()</tt>, then no
2005N/A * $type$s are transferred and a {@link BufferOverflowException} is
0N/A * thrown.
0N/A *
2005N/A * <p> Otherwise, this method copies <tt>length</tt> $type$s from the
0N/A * given array into this buffer, starting at the given offset in the array
0N/A * and at the current position of this buffer. The position of this buffer
0N/A * is then incremented by <tt>length</tt>.
0N/A *
0N/A * <p> In other words, an invocation of this method of the form
0N/A * <tt>dst.put(src,&nbsp;off,&nbsp;len)</tt> has exactly the same effect as
0N/A * the loop
0N/A *
0N/A * <pre>
0N/A * for (int i = off; i < off + len; i++)
0N/A * dst.put(a[i]); </pre>
0N/A *
0N/A * except that it first checks that there is sufficient space in this
0N/A * buffer and it is potentially much more efficient. </p>
0N/A *
0N/A * @param src
2005N/A * The array from which $type$s are to be read
0N/A *
0N/A * @param offset
2005N/A * The offset within the array of the first $type$ to be read;
0N/A * must be non-negative and no larger than <tt>array.length</tt>
0N/A *
0N/A * @param length
2005N/A * The number of $type$s to be read from the given array;
0N/A * must be non-negative and no larger than
0N/A * <tt>array.length - offset</tt>
0N/A *
0N/A * @return This buffer
0N/A *
0N/A * @throws BufferOverflowException
0N/A * If there is insufficient space in this buffer
0N/A *
0N/A * @throws IndexOutOfBoundsException
0N/A * If the preconditions on the <tt>offset</tt> and <tt>length</tt>
0N/A * parameters do not hold
0N/A *
0N/A * @throws ReadOnlyBufferException
0N/A * If this buffer is read-only
0N/A */
0N/A public $Type$Buffer put($type$[] src, int offset, int length) {
0N/A checkBounds(offset, length, src.length);
0N/A if (length > remaining())
0N/A throw new BufferOverflowException();
0N/A int end = offset + length;
0N/A for (int i = offset; i < end; i++)
0N/A this.put(src[i]);
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Relative bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
0N/A *
0N/A * <p> This method transfers the entire content of the given source
2005N/A * $type$ array into this buffer. An invocation of this method of the
0N/A * form <tt>dst.put(a)</tt> behaves in exactly the same way as the
0N/A * invocation
0N/A *
0N/A * <pre>
0N/A * dst.put(a, 0, a.length) </pre>
0N/A *
0N/A * @return This buffer
0N/A *
0N/A * @throws BufferOverflowException
0N/A * If there is insufficient space in this buffer
0N/A *
0N/A * @throws ReadOnlyBufferException
0N/A * If this buffer is read-only
0N/A */
0N/A public final $Type$Buffer put($type$[] src) {
0N/A return put(src, 0, src.length);
0N/A }
0N/A
0N/A#if[char]
0N/A
0N/A /**
0N/A * Relative bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
0N/A *
2005N/A * <p> This method transfers $type$s from the given string into this
2005N/A * buffer. If there are more $type$s to be copied from the string than
0N/A * remain in this buffer, that is, if
0N/A * <tt>end&nbsp;-&nbsp;start</tt>&nbsp;<tt>&gt;</tt>&nbsp;<tt>remaining()</tt>,
2005N/A * then no $type$s are transferred and a {@link
0N/A * BufferOverflowException} is thrown.
0N/A *
0N/A * <p> Otherwise, this method copies
2005N/A * <i>n</i>&nbsp;=&nbsp;<tt>end</tt>&nbsp;-&nbsp;<tt>start</tt> $type$s
0N/A * from the given string into this buffer, starting at the given
0N/A * <tt>start</tt> index and at the current position of this buffer. The
0N/A * position of this buffer is then incremented by <i>n</i>.
0N/A *
0N/A * <p> In other words, an invocation of this method of the form
0N/A * <tt>dst.put(src,&nbsp;start,&nbsp;end)</tt> has exactly the same effect
0N/A * as the loop
0N/A *
0N/A * <pre>
0N/A * for (int i = start; i < end; i++)
0N/A * dst.put(src.charAt(i)); </pre>
0N/A *
0N/A * except that it first checks that there is sufficient space in this
0N/A * buffer and it is potentially much more efficient. </p>
0N/A *
0N/A * @param src
2005N/A * The string from which $type$s are to be read
0N/A *
0N/A * @param start
2005N/A * The offset within the string of the first $type$ to be read;
0N/A * must be non-negative and no larger than
0N/A * <tt>string.length()</tt>
0N/A *
0N/A * @param end
2005N/A * The offset within the string of the last $type$ to be read,
0N/A * plus one; must be non-negative and no larger than
0N/A * <tt>string.length()</tt>
0N/A *
0N/A * @return This buffer
0N/A *
0N/A * @throws BufferOverflowException
0N/A * If there is insufficient space in this buffer
0N/A *
0N/A * @throws IndexOutOfBoundsException
0N/A * If the preconditions on the <tt>start</tt> and <tt>end</tt>
0N/A * parameters do not hold
0N/A *
0N/A * @throws ReadOnlyBufferException
0N/A * If this buffer is read-only
0N/A */
0N/A public $Type$Buffer put(String src, int start, int end) {
0N/A checkBounds(start, end - start, src.length());
0N/A for (int i = start; i < end; i++)
0N/A this.put(src.charAt(i));
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Relative bulk <i>put</i> method&nbsp;&nbsp;<i>(optional operation)</i>.
0N/A *
0N/A * <p> This method transfers the entire content of the given source string
0N/A * into this buffer. An invocation of this method of the form
0N/A * <tt>dst.put(s)</tt> behaves in exactly the same way as the invocation
0N/A *
0N/A * <pre>
0N/A * dst.put(s, 0, s.length()) </pre>
0N/A *
0N/A * @return This buffer
0N/A *
0N/A * @throws BufferOverflowException
0N/A * If there is insufficient space in this buffer
0N/A *
0N/A * @throws ReadOnlyBufferException
0N/A * If this buffer is read-only
0N/A */
0N/A public final $Type$Buffer put(String src) {
0N/A return put(src, 0, src.length());
0N/A }
0N/A
0N/A#end[char]
0N/A
0N/A
0N/A // -- Other stuff --
0N/A
0N/A /**
2005N/A * Tells whether or not this buffer is backed by an accessible $type$
0N/A * array.
0N/A *
0N/A * <p> If this method returns <tt>true</tt> then the {@link #array() array}
0N/A * and {@link #arrayOffset() arrayOffset} methods may safely be invoked.
0N/A * </p>
0N/A *
0N/A * @return <tt>true</tt> if, and only if, this buffer
0N/A * is backed by an array and is not read-only
0N/A */
0N/A public final boolean hasArray() {
0N/A return (hb != null) && !isReadOnly;
0N/A }
0N/A
0N/A /**
2005N/A * Returns the $type$ array that backs this
0N/A * buffer&nbsp;&nbsp;<i>(optional operation)</i>.
0N/A *
0N/A * <p> Modifications to this buffer's content will cause the returned
0N/A * array's content to be modified, and vice versa.
0N/A *
0N/A * <p> Invoke the {@link #hasArray hasArray} method before invoking this
0N/A * method in order to ensure that this buffer has an accessible backing
0N/A * array. </p>
0N/A *
0N/A * @return The array that backs this buffer
0N/A *
0N/A * @throws ReadOnlyBufferException
0N/A * If this buffer is backed by an array but is read-only
0N/A *
0N/A * @throws UnsupportedOperationException
0N/A * If this buffer is not backed by an accessible array
0N/A */
0N/A public final $type$[] array() {
0N/A if (hb == null)
0N/A throw new UnsupportedOperationException();
0N/A if (isReadOnly)
0N/A throw new ReadOnlyBufferException();
0N/A return hb;
0N/A }
0N/A
0N/A /**
0N/A * Returns the offset within this buffer's backing array of the first
0N/A * element of the buffer&nbsp;&nbsp;<i>(optional operation)</i>.
0N/A *
0N/A * <p> If this buffer is backed by an array then buffer position <i>p</i>
0N/A * corresponds to array index <i>p</i>&nbsp;+&nbsp;<tt>arrayOffset()</tt>.
0N/A *
0N/A * <p> Invoke the {@link #hasArray hasArray} method before invoking this
0N/A * method in order to ensure that this buffer has an accessible backing
0N/A * array. </p>
0N/A *
0N/A * @return The offset within this buffer's array
0N/A * of the first element of the buffer
0N/A *
0N/A * @throws ReadOnlyBufferException
0N/A * If this buffer is backed by an array but is read-only
0N/A *
0N/A * @throws UnsupportedOperationException
0N/A * If this buffer is not backed by an accessible array
0N/A */
0N/A public final int arrayOffset() {
0N/A if (hb == null)
0N/A throw new UnsupportedOperationException();
0N/A if (isReadOnly)
0N/A throw new ReadOnlyBufferException();
0N/A return offset;
0N/A }
0N/A
0N/A /**
0N/A * Compacts this buffer&nbsp;&nbsp;<i>(optional operation)</i>.
0N/A *
2005N/A * <p> The $type$s between the buffer's current position and its limit,
0N/A * if any, are copied to the beginning of the buffer. That is, the
2005N/A * $type$ at index <i>p</i>&nbsp;=&nbsp;<tt>position()</tt> is copied
2005N/A * to index zero, the $type$ at index <i>p</i>&nbsp;+&nbsp;1 is copied
2005N/A * to index one, and so forth until the $type$ at index
0N/A * <tt>limit()</tt>&nbsp;-&nbsp;1 is copied to index
0N/A * <i>n</i>&nbsp;=&nbsp;<tt>limit()</tt>&nbsp;-&nbsp;<tt>1</tt>&nbsp;-&nbsp;<i>p</i>.
0N/A * The buffer's position is then set to <i>n+1</i> and its limit is set to
0N/A * its capacity. The mark, if defined, is discarded.
0N/A *
2005N/A * <p> The buffer's position is set to the number of $type$s copied,
0N/A * rather than to zero, so that an invocation of this method can be
0N/A * followed immediately by an invocation of another relative <i>put</i>
0N/A * method. </p>
0N/A *
0N/A#if[byte]
0N/A *
0N/A * <p> Invoke this method after writing data from a buffer in case the
0N/A * write was incomplete. The following loop, for example, copies bytes
0N/A * from one channel to another via the buffer <tt>buf</tt>:
0N/A *
0N/A * <blockquote><pre>
0N/A * buf.clear(); // Prepare buffer for use
0N/A * while (in.read(buf) >= 0 || buf.position != 0) {
0N/A * buf.flip();
0N/A * out.write(buf);
0N/A * buf.compact(); // In case of partial write
0N/A * }</pre></blockquote>
0N/A *
0N/A#end[byte]
0N/A *
0N/A * @return This buffer
0N/A *
0N/A * @throws ReadOnlyBufferException
0N/A * If this buffer is read-only
0N/A */
0N/A public abstract $Type$Buffer compact();
0N/A
0N/A /**
2005N/A * Tells whether or not this $type$ buffer is direct. </p>
0N/A *
0N/A * @return <tt>true</tt> if, and only if, this buffer is direct
0N/A */
0N/A public abstract boolean isDirect();
0N/A
0N/A#if[!char]
0N/A
0N/A /**
0N/A * Returns a string summarizing the state of this buffer. </p>
0N/A *
0N/A * @return A summary string
0N/A */
0N/A public String toString() {
0N/A StringBuffer sb = new StringBuffer();
0N/A sb.append(getClass().getName());
0N/A sb.append("[pos=");
0N/A sb.append(position());
0N/A sb.append(" lim=");
0N/A sb.append(limit());
0N/A sb.append(" cap=");
0N/A sb.append(capacity());
0N/A sb.append("]");
0N/A return sb.toString();
0N/A }
0N/A
0N/A#end[!char]
0N/A
0N/A
0N/A // ## Should really use unchecked accessors here for speed
0N/A
0N/A /**
0N/A * Returns the current hash code of this buffer.
0N/A *
0N/A * <p> The hash code of a $type$ buffer depends only upon its remaining
0N/A * elements; that is, upon the elements from <tt>position()</tt> up to, and
0N/A * including, the element at <tt>limit()</tt>&nbsp;-&nbsp;<tt>1</tt>.
0N/A *
0N/A * <p> Because buffer hash codes are content-dependent, it is inadvisable
0N/A * to use buffers as keys in hash maps or similar data structures unless it
0N/A * is known that their contents will not change. </p>
0N/A *
0N/A * @return The current hash code of this buffer
0N/A */
0N/A public int hashCode() {
0N/A int h = 1;
0N/A int p = position();
0N/A for (int i = limit() - 1; i >= p; i--)
0N/A h = 31 * h + (int)get(i);
0N/A return h;
0N/A }
0N/A
0N/A /**
0N/A * Tells whether or not this buffer is equal to another object.
0N/A *
0N/A * <p> Two $type$ buffers are equal if, and only if,
0N/A *
0N/A * <p><ol>
0N/A *
0N/A * <li><p> They have the same element type, </p></li>
0N/A *
0N/A * <li><p> They have the same number of remaining elements, and
0N/A * </p></li>
0N/A *
0N/A * <li><p> The two sequences of remaining elements, considered
0N/A * independently of their starting positions, are pointwise equal.
2005N/A#if[floatingPointType]
2005N/A * This method considers two $type$ elements {@code a} and {@code b}
2005N/A * to be equal if
2005N/A * {@code (a == b) || ($Fulltype$.isNaN(a) && $Fulltype$.isNaN(b))}.
2005N/A * The values {@code -0.0} and {@code +0.0} are considered to be
2005N/A * equal, unlike {@link $Fulltype$#equals(Object)}.
2005N/A#end[floatingPointType]
0N/A * </p></li>
0N/A *
0N/A * </ol>
0N/A *
0N/A * <p> A $type$ buffer is not equal to any other type of object. </p>
0N/A *
0N/A * @param ob The object to which this buffer is to be compared
0N/A *
0N/A * @return <tt>true</tt> if, and only if, this buffer is equal to the
0N/A * given object
0N/A */
0N/A public boolean equals(Object ob) {
0N/A if (this == ob)
0N/A return true;
0N/A if (!(ob instanceof $Type$Buffer))
0N/A return false;
0N/A $Type$Buffer that = ($Type$Buffer)ob;
0N/A if (this.remaining() != that.remaining())
0N/A return false;
0N/A int p = this.position();
2005N/A for (int i = this.limit() - 1, j = that.limit() - 1; i >= p; i--, j--)
2005N/A if (!equals(this.get(i), that.get(j)))
0N/A return false;
0N/A return true;
0N/A }
0N/A
2005N/A private static boolean equals($type$ x, $type$ y) {
2005N/A#if[floatingPointType]
2005N/A return (x == y) || ($Fulltype$.isNaN(x) && $Fulltype$.isNaN(y));
2005N/A#else[floatingPointType]
2005N/A return x == y;
2005N/A#end[floatingPointType]
2005N/A }
2005N/A
0N/A /**
0N/A * Compares this buffer to another.
0N/A *
0N/A * <p> Two $type$ buffers are compared by comparing their sequences of
0N/A * remaining elements lexicographically, without regard to the starting
0N/A * position of each sequence within its corresponding buffer.
2005N/A#if[floatingPointType]
2005N/A * Pairs of {@code $type$} elements are compared as if by invoking
2005N/A * {@link $Fulltype$#compare($type$,$type$)}, except that
2005N/A * {@code -0.0} and {@code 0.0} are considered to be equal.
2005N/A * {@code $Fulltype$.NaN} is considered by this method to be equal
2005N/A * to itself and greater than all other {@code $type$} values
2005N/A * (including {@code $Fulltype$.POSITIVE_INFINITY}).
2005N/A#else[floatingPointType]
2005N/A * Pairs of {@code $type$} elements are compared as if by invoking
2005N/A * {@link $Fulltype$#compare($type$,$type$)}.
2005N/A#end[floatingPointType]
0N/A *
0N/A * <p> A $type$ buffer is not comparable to any other type of object.
0N/A *
0N/A * @return A negative integer, zero, or a positive integer as this buffer
0N/A * is less than, equal to, or greater than the given buffer
0N/A */
0N/A public int compareTo($Type$Buffer that) {
0N/A int n = this.position() + Math.min(this.remaining(), that.remaining());
0N/A for (int i = this.position(), j = that.position(); i < n; i++, j++) {
2005N/A int cmp = compare(this.get(i), that.get(j));
2005N/A if (cmp != 0)
2005N/A return cmp;
0N/A }
0N/A return this.remaining() - that.remaining();
0N/A }
0N/A
2005N/A private static int compare($type$ x, $type$ y) {
2005N/A#if[floatingPointType]
2005N/A return ((x < y) ? -1 :
2005N/A (x > y) ? +1 :
2005N/A (x == y) ? 0 :
2005N/A $Fulltype$.isNaN(x) ? ($Fulltype$.isNaN(y) ? 0 : +1) : -1);
2005N/A#else[floatingPointType]
2005N/A return $Fulltype$.compare(x, y);
2005N/A#end[floatingPointType]
2005N/A }
0N/A
0N/A // -- Other char stuff --
0N/A
0N/A#if[char]
0N/A
0N/A /**
0N/A * Returns a string containing the characters in this buffer.
0N/A *
0N/A * <p> The first character of the resulting string will be the character at
0N/A * this buffer's position, while the last character will be the character
0N/A * at index <tt>limit()</tt>&nbsp;-&nbsp;1. Invoking this method does not
0N/A * change the buffer's position. </p>
0N/A *
0N/A * @return The specified string
0N/A */
0N/A public String toString() {
0N/A return toString(position(), limit());
0N/A }
0N/A
0N/A abstract String toString(int start, int end); // package-private
0N/A
0N/A
0N/A // --- Methods to support CharSequence ---
0N/A
0N/A /**
0N/A * Returns the length of this character buffer.
0N/A *
0N/A * <p> When viewed as a character sequence, the length of a character
0N/A * buffer is simply the number of characters between the position
0N/A * (inclusive) and the limit (exclusive); that is, it is equivalent to
0N/A * <tt>remaining()</tt>. </p>
0N/A *
0N/A * @return The length of this character buffer
0N/A */
0N/A public final int length() {
0N/A return remaining();
0N/A }
0N/A
0N/A /**
0N/A * Reads the character at the given index relative to the current
0N/A * position. </p>
0N/A *
0N/A * @param index
0N/A * The index of the character to be read, relative to the position;
0N/A * must be non-negative and smaller than <tt>remaining()</tt>
0N/A *
0N/A * @return The character at index
0N/A * <tt>position()&nbsp;+&nbsp;index</tt>
0N/A *
0N/A * @throws IndexOutOfBoundsException
0N/A * If the preconditions on <tt>index</tt> do not hold
0N/A */
0N/A public final char charAt(int index) {
0N/A return get(position() + checkIndex(index, 1));
0N/A }
0N/A
0N/A /**
0N/A * Creates a new character buffer that represents the specified subsequence
0N/A * of this buffer, relative to the current position.
0N/A *
0N/A * <p> The new buffer will share this buffer's content; that is, if the
0N/A * content of this buffer is mutable then modifications to one buffer will
0N/A * cause the other to be modified. The new buffer's capacity will be that
0N/A * of this buffer, its position will be
0N/A * <tt>position()</tt>&nbsp;+&nbsp;<tt>start</tt>, and its limit will be
0N/A * <tt>position()</tt>&nbsp;+&nbsp;<tt>end</tt>. The new buffer will be
0N/A * direct if, and only if, this buffer is direct, and it will be read-only
0N/A * if, and only if, this buffer is read-only. </p>
0N/A *
0N/A * @param start
0N/A * The index, relative to the current position, of the first
0N/A * character in the subsequence; must be non-negative and no larger
0N/A * than <tt>remaining()</tt>
0N/A *
0N/A * @param end
0N/A * The index, relative to the current position, of the character
0N/A * following the last character in the subsequence; must be no
0N/A * smaller than <tt>start</tt> and no larger than
0N/A * <tt>remaining()</tt>
0N/A *
571N/A * @return The new character buffer
0N/A *
0N/A * @throws IndexOutOfBoundsException
0N/A * If the preconditions on <tt>start</tt> and <tt>end</tt>
0N/A * do not hold
0N/A */
571N/A public abstract CharBuffer subSequence(int start, int end);
0N/A
0N/A
0N/A // --- Methods to support Appendable ---
0N/A
0N/A /**
0N/A * Appends the specified character sequence to this
0N/A * buffer&nbsp;&nbsp;<i>(optional operation)</i>.
0N/A *
0N/A * <p> An invocation of this method of the form <tt>dst.append(csq)</tt>
0N/A * behaves in exactly the same way as the invocation
0N/A *
0N/A * <pre>
0N/A * dst.put(csq.toString()) </pre>
0N/A *
0N/A * <p> Depending on the specification of <tt>toString</tt> for the
0N/A * character sequence <tt>csq</tt>, the entire sequence may not be
0N/A * appended. For instance, invoking the {@link $Type$Buffer#toString()
0N/A * toString} method of a character buffer will return a subsequence whose
0N/A * content depends upon the buffer's position and limit.
0N/A *
0N/A * @param csq
0N/A * The character sequence to append. If <tt>csq</tt> is
0N/A * <tt>null</tt>, then the four characters <tt>"null"</tt> are
0N/A * appended to this character buffer.
0N/A *
0N/A * @return This buffer
0N/A *
0N/A * @throws BufferOverflowException
0N/A * If there is insufficient space in this buffer
0N/A *
0N/A * @throws ReadOnlyBufferException
0N/A * If this buffer is read-only
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public $Type$Buffer append(CharSequence csq) {
0N/A if (csq == null)
0N/A return put("null");
0N/A else
0N/A return put(csq.toString());
0N/A }
0N/A
0N/A /**
0N/A * Appends a subsequence of the specified character sequence to this
0N/A * buffer&nbsp;&nbsp;<i>(optional operation)</i>.
0N/A *
0N/A * <p> An invocation of this method of the form <tt>dst.append(csq, start,
0N/A * end)</tt> when <tt>csq</tt> is not <tt>null</tt>, behaves in exactly the
0N/A * same way as the invocation
0N/A *
0N/A * <pre>
0N/A * dst.put(csq.subSequence(start, end).toString()) </pre>
0N/A *
0N/A * @param csq
0N/A * The character sequence from which a subsequence will be
0N/A * appended. If <tt>csq</tt> is <tt>null</tt>, then characters
0N/A * will be appended as if <tt>csq</tt> contained the four
0N/A * characters <tt>"null"</tt>.
0N/A *
0N/A * @return This buffer
0N/A *
0N/A * @throws BufferOverflowException
0N/A * If there is insufficient space in this buffer
0N/A *
0N/A * @throws IndexOutOfBoundsException
0N/A * If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>
0N/A * is greater than <tt>end</tt>, or <tt>end</tt> is greater than
0N/A * <tt>csq.length()</tt>
0N/A *
0N/A * @throws ReadOnlyBufferException
0N/A * If this buffer is read-only
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public $Type$Buffer append(CharSequence csq, int start, int end) {
0N/A CharSequence cs = (csq == null ? "null" : csq);
0N/A return put(cs.subSequence(start, end).toString());
0N/A }
0N/A
0N/A /**
2005N/A * Appends the specified $type$ to this
0N/A * buffer&nbsp;&nbsp;<i>(optional operation)</i>.
0N/A *
0N/A * <p> An invocation of this method of the form <tt>dst.append($x$)</tt>
0N/A * behaves in exactly the same way as the invocation
0N/A *
0N/A * <pre>
0N/A * dst.put($x$) </pre>
0N/A *
0N/A * @param $x$
2005N/A * The 16-bit $type$ to append
0N/A *
0N/A * @return This buffer
0N/A *
0N/A * @throws BufferOverflowException
0N/A * If there is insufficient space in this buffer
0N/A *
0N/A * @throws ReadOnlyBufferException
0N/A * If this buffer is read-only
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public $Type$Buffer append($type$ $x$) {
0N/A return put($x$);
0N/A }
0N/A
0N/A#end[char]
0N/A
0N/A
0N/A // -- Other byte stuff: Access to binary data --
0N/A
0N/A#if[!byte]
0N/A
0N/A /**
0N/A * Retrieves this buffer's byte order.
0N/A *
2005N/A * <p> The byte order of $a$ $type$ buffer created by allocation or by
0N/A * wrapping an existing <tt>$type$</tt> array is the {@link
0N/A * ByteOrder#nativeOrder </code>native order<code>} of the underlying
2005N/A * hardware. The byte order of $a$ $type$ buffer created as a <a
0N/A * href="ByteBuffer.html#views">view</a> of a byte buffer is that of the
0N/A * byte buffer at the moment that the view is created. </p>
0N/A *
0N/A * @return This buffer's byte order
0N/A */
0N/A public abstract ByteOrder order();
0N/A
0N/A#end[!byte]
0N/A
0N/A#if[byte]
0N/A
0N/A boolean bigEndian // package-private
0N/A = true;
0N/A boolean nativeByteOrder // package-private
0N/A = (Bits.byteOrder() == ByteOrder.BIG_ENDIAN);
0N/A
0N/A /**
0N/A * Retrieves this buffer's byte order.
0N/A *
0N/A * <p> The byte order is used when reading or writing multibyte values, and
0N/A * when creating buffers that are views of this byte buffer. The order of
0N/A * a newly-created byte buffer is always {@link ByteOrder#BIG_ENDIAN
0N/A * BIG_ENDIAN}. </p>
0N/A *
0N/A * @return This buffer's byte order
0N/A */
0N/A public final ByteOrder order() {
0N/A return bigEndian ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
0N/A }
0N/A
0N/A /**
0N/A * Modifies this buffer's byte order. </p>
0N/A *
0N/A * @param bo
0N/A * The new byte order,
0N/A * either {@link ByteOrder#BIG_ENDIAN BIG_ENDIAN}
0N/A * or {@link ByteOrder#LITTLE_ENDIAN LITTLE_ENDIAN}
0N/A *
0N/A * @return This buffer
0N/A */
0N/A public final $Type$Buffer order(ByteOrder bo) {
0N/A bigEndian = (bo == ByteOrder.BIG_ENDIAN);
0N/A nativeByteOrder =
0N/A (bigEndian == (Bits.byteOrder() == ByteOrder.BIG_ENDIAN));
0N/A return this;
0N/A }
0N/A
0N/A // Unchecked accessors, for use by ByteBufferAs-X-Buffer classes
0N/A //
0N/A abstract byte _get(int i); // package-private
0N/A abstract void _put(int i, byte b); // package-private
0N/A
0N/A // #BIN
0N/A //
0N/A // Binary-data access methods for short, char, int, long, float,
0N/A // and double will be inserted here
0N/A
0N/A#end[byte]
0N/A
0N/A}