/* * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ // -- This file was mechanically generated: Do not edit! -- // package java.nio; import java.io.IOException; /** * A char buffer. * * <p> This class defines four categories of operations upon * char buffers: * * <ul> * * <li><p> Absolute and relative {@link #get() </code><i>get</i><code>} and * {@link #put(char) </code><i>put</i><code>} methods that read and write * single chars; </p></li> * * <li><p> Relative {@link #get(char[]) </code><i>bulk get</i><code>} * methods that transfer contiguous sequences of chars from this buffer * into an array; and</p></li> * * <li><p> Relative {@link #put(char[]) </code><i>bulk put</i><code>} * methods that transfer contiguous sequences of chars from a * char array, a string, or some other char * buffer into this buffer; and </p></li> * * * <li><p> Methods for {@link #compact </code>compacting<code>}, {@link * #duplicate </code>duplicating<code>}, and {@link #slice * </code>slicing<code>} a char buffer. </p></li> * * </ul> * * <p> Char buffers can be created either by {@link #allocate * </code><i>allocation</i><code>}, which allocates space for the buffer's * * * content, by {@link #wrap(char[]) </code><i>wrapping</i><code>} an existing * char array or string into a buffer, or by creating a * <a href="ByteBuffer.html#views"><i>view</i></a> of an existing byte buffer. * * * * * <p> Like a byte buffer, a char buffer is either <a * href="ByteBuffer.html#direct"><i>direct</i> or <i>non-direct</i></a>. A * char buffer created via the <tt>wrap</tt> methods of this class will * be non-direct. A char buffer created as a view of a byte buffer will * be direct if, and only if, the byte buffer itself is direct. Whether or not * a char buffer is direct may be determined by invoking the {@link * #isDirect isDirect} method. </p> * * * * <p> This class implements the {@link CharSequence} interface so that * character buffers may be used wherever character sequences are accepted, for * example in the regular-expression package <tt>{@link java.util.regex}</tt>. * </p> * * * * <p> Methods in this class that do not otherwise have a value to return are * specified to return the buffer upon which they are invoked. This allows * method invocations to be chained. * * * The sequence of statements * * <blockquote><pre> * cb.put("text/"); * cb.put(subtype); * cb.put("; charset="); * cb.put(enc);</pre></blockquote> * * can, for example, be replaced by the single statement * * <blockquote><pre> * cb.put("text/").put(subtype).put("; charset=").put(enc);</pre></blockquote> * * * * @author Mark Reinhold * @author JSR-51 Expert Group * @since 1.4 */ public abstract class CharBuffer extends Buffer implements Comparable<CharBuffer>, Appendable, CharSequence, Readable{ // These fields are declared here rather than in Heap-X-Buffer in order to // reduce the number of virtual method invocations needed to access these // values, which is especially costly when coding small buffers. // final char[] hb; // Non-null only for heap buffers final int offset; boolean isReadOnly; // Valid only for heap buffers // Creates a new buffer with the given mark, position, limit, capacity, // backing array, and array offset // CharBuffer(int mark, int pos, int lim, int cap, // package-private char[] hb, int offset) { super(mark, pos, lim, cap); this.hb = hb; this.offset = offset; } // Creates a new buffer with the given mark, position, limit, and capacity // CharBuffer(int mark, int pos, int lim, int cap) { // package-private this(mark, pos, lim, cap, null, 0); } /** * Allocates a new char buffer. * * <p> The new buffer's position will be zero, its limit will be its * capacity, its mark will be undefined, and each of its elements will be * initialized to zero. It will have a {@link #array * </code>backing array<code>}, and its {@link #arrayOffset </code>array * offset<code>} will be zero. * * @param capacity * The new buffer's capacity, in chars * * @return The new char buffer * * @throws IllegalArgumentException * If the <tt>capacity</tt> is a negative integer */ public static CharBuffer allocate(int capacity) { if (capacity < 0) throw new IllegalArgumentException(); return new HeapCharBuffer(capacity, capacity); } /** * Wraps a char array into a buffer. * * <p> The new buffer will be backed by the given char array; * that is, modifications to the buffer will cause the array to be modified * and vice versa. The new buffer's capacity will be * <tt>array.length</tt>, its position will be <tt>offset</tt>, its limit * will be <tt>offset + length</tt>, and its mark will be undefined. Its * {@link #array </code>backing array<code>} will be the given array, and * its {@link #arrayOffset </code>array offset<code>} will be zero. </p> * * @param array * The array that will back the new buffer * * @param offset * The offset of the subarray to be used; must be non-negative and * no larger than <tt>array.length</tt>. The new buffer's position * will be set to this value. * * @param length * The length of the subarray to be used; * must be non-negative and no larger than * <tt>array.length - offset</tt>. * The new buffer's limit will be set to <tt>offset + length</tt>. * * @return The new char buffer * * @throws IndexOutOfBoundsException * If the preconditions on the <tt>offset</tt> and <tt>length</tt> * parameters do not hold */ public static CharBuffer wrap(char[] array, int offset, int length) { try { return new HeapCharBuffer(array, offset, length); } catch (IllegalArgumentException x) { throw new IndexOutOfBoundsException(); } } /** * Wraps a char array into a buffer. * * <p> The new buffer will be backed by the given char array; * that is, modifications to the buffer will cause the array to be modified * and vice versa. The new buffer's capacity and limit will be * <tt>array.length</tt>, its position will be zero, and its mark will be * undefined. Its {@link #array </code>backing array<code>} will be the * given array, and its {@link #arrayOffset </code>array offset<code>} will * be zero. </p> * * @param array * The array that will back this buffer * * @return The new char buffer */ public static CharBuffer wrap(char[] array) { return wrap(array, 0, array.length); } /** * Attempts to read characters into the specified character buffer. * The buffer is used as a repository of characters as-is: the only * changes made are the results of a put operation. No flipping or * rewinding of the buffer is performed. * * @param target the buffer to read characters into * @return The number of characters added to the buffer, or * -1 if this source of characters is at its end * @throws IOException if an I/O error occurs * @throws NullPointerException if target is null * @throws ReadOnlyBufferException if target is a read only buffer * @since 1.5 */ public int read(CharBuffer target) throws IOException { // Determine the number of bytes n that can be transferred int targetRemaining = target.remaining(); int remaining = remaining(); if (remaining == 0) return -1; int n = Math.min(remaining, targetRemaining); int limit = limit(); // Set source limit to prevent target overflow if (targetRemaining < remaining) limit(position() + n); try { if (n > 0) target.put(this); } finally { limit(limit); // restore real limit } return n; } /** * Wraps a character sequence into a buffer. * * <p> The content of the new, read-only buffer will be the content of the * given character sequence. The buffer's capacity will be * <tt>csq.length()</tt>, its position will be <tt>start</tt>, its limit * will be <tt>end</tt>, and its mark will be undefined. </p> * * @param csq * The character sequence from which the new character buffer is to * be created * * @param start * The index of the first character to be used; * must be non-negative and no larger than <tt>csq.length()</tt>. * The new buffer's position will be set to this value. * * @param end * The index of the character following the last character to be * used; must be no smaller than <tt>start</tt> and no larger * than <tt>csq.length()</tt>. * The new buffer's limit will be set to this value. * * @return The new character buffer * * @throws IndexOutOfBoundsException * If the preconditions on the <tt>start</tt> and <tt>end</tt> * parameters do not hold */ public static CharBuffer wrap(CharSequence csq, int start, int end) { try { return new StringCharBuffer(csq, start, end); } catch (IllegalArgumentException x) { throw new IndexOutOfBoundsException(); } } /** * Wraps a character sequence into a buffer. * * <p> The content of the new, read-only buffer will be the content of the * given character sequence. The new buffer's capacity and limit will be * <tt>csq.length()</tt>, its position will be zero, and its mark will be * undefined. </p> * * @param csq * The character sequence from which the new character buffer is to * be created * * @return The new character buffer */ public static CharBuffer wrap(CharSequence csq) { return wrap(csq, 0, csq.length()); } /** * Creates a new char buffer whose content is a shared subsequence of * this buffer's content. * * <p> The content of the new buffer will start at this buffer's current * position. Changes to this buffer's content will be visible in the new * buffer, and vice versa; the two buffers' position, limit, and mark * values will be independent. * * <p> The new buffer's position will be zero, its capacity and its limit * will be the number of chars remaining in this buffer, and its mark * will be undefined. The new buffer will be direct if, and only if, this * buffer is direct, and it will be read-only if, and only if, this bufferLines 1–500 of 1463. Load more
/* * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ // -- This file was mechanically generated: Do not edit! -- // package java.nio; import java.io.IOException; /** * A char buffer. * * <p> This class defines four categories of operations upon * char buffers: * * <ul> * * <li><p> Absolute and relative {@link #get() </code><i>get</i><code>} and * {@link #put(char) </code><i>put</i><code>} methods that read and write * single chars; </p></li> * * <li><p> Relative {@link #get(char[]) </code><i>bulk get</i><code>} * methods that transfer contiguous sequences of chars from this buffer * into an array; and</p></li> * * <li><p> Relative {@link #put(char[]) </code><i>bulk put</i><code>} * methods that transfer contiguous sequences of chars from a * char array, a string, or some other char * buffer into this buffer; and </p></li> * * * <li><p> Methods for {@link #compact </code>compacting<code>}, {@link * #duplicate </code>duplicating<code>}, and {@link #slice * </code>slicing<code>} a char buffer. </p></li> * * </ul> * * <p> Char buffers can be created either by {@link #allocate * </code><i>allocation</i><code>}, which allocates space for the buffer's * * * content, by {@link #wrap(char[]) </code><i>wrapping</i><code>} an existing * char array or string into a buffer, or by creating a * <a href="ByteBuffer.html#views"><i>view</i></a> of an existing byte buffer. * * * * * <p> Like a byte buffer, a char buffer is either <a * href="ByteBuffer.html#direct"><i>direct</i> or <i>non-direct</i></a>. A * char buffer created via the <tt>wrap</tt> methods of this class will * be non-direct. A char buffer created as a view of a byte buffer will * be direct if, and only if, the byte buffer itself is direct. Whether or not * a char buffer is direct may be determined by invoking the {@link * #isDirect isDirect} method. </p> * * * * <p> This class implements the {@link CharSequence} interface so that * character buffers may be used wherever character sequences are accepted, for * example in the regular-expression package <tt>{@link java.util.regex}</tt>. * </p> * * * * <p> Methods in this class that do not otherwise have a value to return are * specified to return the buffer upon which they are invoked. This allows * method invocations to be chained. * * * The sequence of statements * * <blockquote><pre> * cb.put("text/"); * cb.put(subtype); * cb.put("; charset="); * cb.put(enc);</pre></blockquote> * * can, for example, be replaced by the single statement * * <blockquote><pre> * cb.put("text/").put(subtype).put("; charset=").put(enc);</pre></blockquote> * * * * @author Mark Reinhold * @author JSR-51 Expert Group * @since 1.4 */ public abstract class CharBuffer extends Buffer implements Comparable<CharBuffer>, Appendable, CharSequence, Readable { // These fields are declared here rather than in Heap-X-Buffer in order to // reduce the number of virtual method invocations needed to access these // values, which is especially costly when coding small buffers. // final char[] hb; // Non-null only for heap buffers final int offset; boolean isReadOnly; // Valid only for heap buffers // Creates a new buffer with the given mark, position, limit, capacity, // backing array, and array offset // CharBuffer(int mark, int pos, int lim, int cap, // package-private char[] hb, int offset) { super(mark, pos, lim, cap); this.hb = hb; this.offset = offset; } // Creates a new buffer with the given mark, position, limit, and capacity // CharBuffer(int mark, int pos, int lim, int cap) { // package-private this(mark, pos, lim, cap, null, 0); } /** * Allocates a new char buffer. * * <p> The new buffer's position will be zero, its limit will be its * capacity, its mark will be undefined, and each of its elements will be * initialized to zero. It will have a {@link #array * </code>backing array<code>}, and its {@link #arrayOffset </code>array * offset<code>} will be zero. * * @param capacity * The new buffer's capacity, in chars * * @return The new char buffer * * @throws IllegalArgumentException * If the <tt>capacity</tt> is a negative integer */ public static CharBuffer allocate(int capacity) { if (capacity < 0) throw new IllegalArgumentException(); return new HeapCharBuffer(capacity, capacity); } /** * Wraps a char array into a buffer. * * <p> The new buffer will be backed by the given char array; * that is, modifications to the buffer will cause the array to be modified * and vice versa. The new buffer's capacity will be * <tt>array.length</tt>, its position will be <tt>offset</tt>, its limit * will be <tt>offset + length</tt>, and its mark will be undefined. Its * {@link #array </code>backing array<code>} will be the given array, and * its {@link #arrayOffset </code>array offset<code>} will be zero. </p> * * @param array * The array that will back the new buffer * * @param offset * The offset of the subarray to be used; must be non-negative and * no larger than <tt>array.length</tt>. The new buffer's position * will be set to this value. * * @param length * The length of the subarray to be used; * must be non-negative and no larger than * <tt>array.length - offset</tt>. * The new buffer's limit will be set to <tt>offset + length</tt>. * * @return The new char buffer * * @throws IndexOutOfBoundsException * If the preconditions on the <tt>offset</tt> and <tt>length</tt> * parameters do not hold */ public static CharBuffer wrap(char[] array, int offset, int length) { try { return new HeapCharBuffer(array, offset, length); } catch (IllegalArgumentException x) { throw new IndexOutOfBoundsException(); } } /** * Wraps a char array into a buffer. * * <p> The new buffer will be backed by the given char array; * that is, modifications to the buffer will cause the array to be modified * and vice versa. The new buffer's capacity and limit will be * <tt>array.length</tt>, its position will be zero, and its mark will be * undefined. Its {@link #array </code>backing array<code>} will be the * given array, and its {@link #arrayOffset </code>array offset<code>} will * be zero. </p> * * @param array * The array that will back this buffer * * @return The new char buffer */ public static CharBuffer wrap(char[] array) { return wrap(array, 0, array.length); } /** * Attempts to read characters into the specified character buffer. * The buffer is used as a repository of characters as-is: the only * changes made are the results of a put operation. No flipping or * rewinding of the buffer is performed. * * @param target the buffer to read characters into * @return The number of characters added to the buffer, or * -1 if this source of characters is at its end * @throws IOException if an I/O error occurs * @throws NullPointerException if target is null * @throws ReadOnlyBufferException if target is a read only buffer * @since 1.5 */ public int read(CharBuffer target) throws IOException { // Determine the number of bytes n that can be transferred int targetRemaining = target.remaining(); int remaining = remaining(); if (remaining == 0) return -1; int n = Math.min(remaining, targetRemaining); int limit = limit(); // Set source limit to prevent target overflow if (targetRemaining < remaining) limit(position() + n); try { if (n > 0) target.put(this); } finally { limit(limit); // restore real limit } return n; } /** * Wraps a character sequence into a buffer. * * <p> The content of the new, read-only buffer will be the content of the * given character sequence. The buffer's capacity will be * <tt>csq.length()</tt>, its position will be <tt>start</tt>, its limit * will be <tt>end</tt>, and its mark will be undefined. </p> * * @param csq * The character sequence from which the new character buffer is to * be created * * @param start * The index of the first character to be used; * must be non-negative and no larger than <tt>csq.length()</tt>. * The new buffer's position will be set to this value. * * @param end * The index of the character following the last character to be * used; must be no smaller than <tt>start</tt> and no larger * than <tt>csq.length()</tt>. * The new buffer's limit will be set to this value. * * @return The new character buffer * * @throws IndexOutOfBoundsException * If the preconditions on the <tt>start</tt> and <tt>end</tt> * parameters do not hold */ public static CharBuffer wrap(CharSequence csq, int start, int end) { try { return new StringCharBuffer(csq, start, end); } catch (IllegalArgumentException x) { throw new IndexOutOfBoundsException(); } } /** * Wraps a character sequence into a buffer. * * <p> The content of the new, read-only buffer will be the content of the * given character sequence. The new buffer's capacity and limit will be * <tt>csq.length()</tt>, its position will be zero, and its mark will be * undefined. </p> * * @param csq * The character sequence from which the new character buffer is to * be created * * @return The new character buffer */ public static CharBuffer wrap(CharSequence csq) { return wrap(csq, 0, csq.length()); } /** * Creates a new char buffer whose content is a shared subsequence of * this buffer's content. * * <p> The content of the new buffer will start at this buffer's current * position. Changes to this buffer's content will be visible in the new * buffer, and vice versa; the two buffers' position, limit, and mark * values will be independent. * * <p> The new buffer's position will be zero, its capacity and its limit * will be the number of chars remaining in this buffer, and its mark * will be undefined. The new buffer will be direct if, and only if, this * buffer is direct, and it will be read-only if, and only if, this buffer