Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/io/CharArrayReader.java
38829 views
/*1* Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.io;2627/**28* This class implements a character buffer that can be used as a29* character-input stream.30*31* @author Herb Jellinek32* @since JDK1.133*/34public class CharArrayReader extends Reader {35/** The character buffer. */36protected char buf[];3738/** The current buffer position. */39protected int pos;4041/** The position of mark in buffer. */42protected int markedPos = 0;4344/**45* The index of the end of this buffer. There is not valid46* data at or beyond this index.47*/48protected int count;4950/**51* Creates a CharArrayReader from the specified array of chars.52* @param buf Input buffer (not copied)53*/54public CharArrayReader(char buf[]) {55this.buf = buf;56this.pos = 0;57this.count = buf.length;58}5960/**61* Creates a CharArrayReader from the specified array of chars.62*63* <p> The resulting reader will start reading at the given64* <tt>offset</tt>. The total number of <tt>char</tt> values that can be65* read from this reader will be either <tt>length</tt> or66* <tt>buf.length-offset</tt>, whichever is smaller.67*68* @throws IllegalArgumentException69* If <tt>offset</tt> is negative or greater than70* <tt>buf.length</tt>, or if <tt>length</tt> is negative, or if71* the sum of these two values is negative.72*73* @param buf Input buffer (not copied)74* @param offset Offset of the first char to read75* @param length Number of chars to read76*/77public CharArrayReader(char buf[], int offset, int length) {78if ((offset < 0) || (offset > buf.length) || (length < 0) ||79((offset + length) < 0)) {80throw new IllegalArgumentException();81}82this.buf = buf;83this.pos = offset;84this.count = Math.min(offset + length, buf.length);85this.markedPos = offset;86}8788/** Checks to make sure that the stream has not been closed */89private void ensureOpen() throws IOException {90if (buf == null)91throw new IOException("Stream closed");92}9394/**95* Reads a single character.96*97* @exception IOException If an I/O error occurs98*/99public int read() throws IOException {100synchronized (lock) {101ensureOpen();102if (pos >= count)103return -1;104else105return buf[pos++];106}107}108109/**110* Reads characters into a portion of an array.111* @param b Destination buffer112* @param off Offset at which to start storing characters113* @param len Maximum number of characters to read114* @return The actual number of characters read, or -1 if115* the end of the stream has been reached116*117* @exception IOException If an I/O error occurs118*/119public int read(char b[], int off, int len) throws IOException {120synchronized (lock) {121ensureOpen();122if ((off < 0) || (off > b.length) || (len < 0) ||123((off + len) > b.length) || ((off + len) < 0)) {124throw new IndexOutOfBoundsException();125} else if (len == 0) {126return 0;127}128129if (pos >= count) {130return -1;131}132133int avail = count - pos;134if (len > avail) {135len = avail;136}137if (len <= 0) {138return 0;139}140System.arraycopy(buf, pos, b, off, len);141pos += len;142return len;143}144}145146/**147* Skips characters. Returns the number of characters that were skipped.148*149* <p>The <code>n</code> parameter may be negative, even though the150* <code>skip</code> method of the {@link Reader} superclass throws151* an exception in this case. If <code>n</code> is negative, then152* this method does nothing and returns <code>0</code>.153*154* @param n The number of characters to skip155* @return The number of characters actually skipped156* @exception IOException If the stream is closed, or an I/O error occurs157*/158public long skip(long n) throws IOException {159synchronized (lock) {160ensureOpen();161162long avail = count - pos;163if (n > avail) {164n = avail;165}166if (n < 0) {167return 0;168}169pos += n;170return n;171}172}173174/**175* Tells whether this stream is ready to be read. Character-array readers176* are always ready to be read.177*178* @exception IOException If an I/O error occurs179*/180public boolean ready() throws IOException {181synchronized (lock) {182ensureOpen();183return (count - pos) > 0;184}185}186187/**188* Tells whether this stream supports the mark() operation, which it does.189*/190public boolean markSupported() {191return true;192}193194/**195* Marks the present position in the stream. Subsequent calls to reset()196* will reposition the stream to this point.197*198* @param readAheadLimit Limit on the number of characters that may be199* read while still preserving the mark. Because200* the stream's input comes from a character array,201* there is no actual limit; hence this argument is202* ignored.203*204* @exception IOException If an I/O error occurs205*/206public void mark(int readAheadLimit) throws IOException {207synchronized (lock) {208ensureOpen();209markedPos = pos;210}211}212213/**214* Resets the stream to the most recent mark, or to the beginning if it has215* never been marked.216*217* @exception IOException If an I/O error occurs218*/219public void reset() throws IOException {220synchronized (lock) {221ensureOpen();222pos = markedPos;223}224}225226/**227* Closes the stream and releases any system resources associated with228* it. Once the stream has been closed, further read(), ready(),229* mark(), reset(), or skip() invocations will throw an IOException.230* Closing a previously closed stream has no effect.231*/232public void close() {233buf = null;234}235}236237238