jdk/src/share/classes/java/io/CharArrayReader.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 24865 09b1d992ca72
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * This class implements a character buffer that can be used as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * character-input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @author      Herb Jellinek
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @since       JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class CharArrayReader extends Reader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    /** The character buffer. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    protected char buf[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    /** The current buffer position. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    protected int pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    /** The position of mark in buffer. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    protected int markedPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     *  The index of the end of this buffer.  There is not valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     *  data at or beyond this index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    protected int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Creates a CharArrayReader from the specified array of chars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * @param buf       Input buffer (not copied)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public CharArrayReader(char buf[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        this.buf = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        this.pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this.count = buf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Creates a CharArrayReader from the specified array of chars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * <p> The resulting reader will start reading at the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * <tt>offset</tt>.  The total number of <tt>char</tt> values that can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * read from this reader will be either <tt>length</tt> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * <tt>buf.length-offset</tt>, whichever is smaller.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *         If <tt>offset</tt> is negative or greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *         <tt>buf.length</tt>, or if <tt>length</tt> is negative, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *         the sum of these two values is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @param buf       Input buffer (not copied)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param offset    Offset of the first char to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param length    Number of chars to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public CharArrayReader(char buf[], int offset, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if ((offset < 0) || (offset > buf.length) || (length < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            ((offset + length) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        this.buf = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        this.pos = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        this.count = Math.min(offset + length, buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        this.markedPos = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /** Checks to make sure that the stream has not been closed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (buf == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            throw new IOException("Stream closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Reads a single character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @exception   IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            if (pos >= count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                return buf[pos++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Reads characters into a portion of an array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param b  Destination buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param off  Offset at which to start storing characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param len   Maximum number of characters to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @return  The actual number of characters read, or -1 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *          the end of the stream has been reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @exception   IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public int read(char b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if ((off < 0) || (off > b.length) || (len < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                ((off + len) > b.length) || ((off + len) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            if (pos >= count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (pos + len > count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                len = count - pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if (len <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            System.arraycopy(buf, pos, b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            pos += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Skips characters.  Returns the number of characters that were skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <p>The <code>n</code> parameter may be negative, even though the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <code>skip</code> method of the {@link Reader} superclass throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * an exception in this case. If <code>n</code> is negative, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * this method does nothing and returns <code>0</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param n The number of characters to skip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @return       The number of characters actually skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @exception  IOException If the stream is closed, or an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            if (pos + n > count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                n = count - pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            pos += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Tells whether this stream is ready to be read.  Character-array readers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * are always ready to be read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public boolean ready() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            return (count - pos) > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Tells whether this stream supports the mark() operation, which it does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public boolean markSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Marks the present position in the stream.  Subsequent calls to reset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * will reposition the stream to this point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @param  readAheadLimit  Limit on the number of characters that may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *                         read while still preserving the mark.  Because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *                         the stream's input comes from a character array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *                         there is no actual limit; hence this argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *                         ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public void mark(int readAheadLimit) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            markedPos = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Resets the stream to the most recent mark, or to the beginning if it has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * never been marked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            pos = markedPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Closes the stream and releases any system resources associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * it.  Once the stream has been closed, further read(), ready(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * mark(), reset(), or skip() invocations will throw an IOException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * Closing a previously closed stream has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        buf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
}