emul/compact/src/main/java/java/io/Reader.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 23 Jan 2013 22:32:27 +0100
branchjdk7-b147
changeset 557 5be31d9fa455
child 560 53fafe384803
permissions -rw-r--r--
Basic classes needed to support ServiceLoader
jaroslav@557
     1
/*
jaroslav@557
     2
 * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
jaroslav@557
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@557
     4
 *
jaroslav@557
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@557
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@557
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@557
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@557
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@557
    10
 *
jaroslav@557
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@557
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@557
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@557
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@557
    15
 * accompanied this code).
jaroslav@557
    16
 *
jaroslav@557
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@557
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@557
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@557
    20
 *
jaroslav@557
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@557
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@557
    23
 * questions.
jaroslav@557
    24
 */
jaroslav@557
    25
jaroslav@557
    26
package java.io;
jaroslav@557
    27
jaroslav@557
    28
jaroslav@557
    29
/**
jaroslav@557
    30
 * Abstract class for reading character streams.  The only methods that a
jaroslav@557
    31
 * subclass must implement are read(char[], int, int) and close().  Most
jaroslav@557
    32
 * subclasses, however, will override some of the methods defined here in order
jaroslav@557
    33
 * to provide higher efficiency, additional functionality, or both.
jaroslav@557
    34
 *
jaroslav@557
    35
 *
jaroslav@557
    36
 * @see BufferedReader
jaroslav@557
    37
 * @see   LineNumberReader
jaroslav@557
    38
 * @see CharArrayReader
jaroslav@557
    39
 * @see InputStreamReader
jaroslav@557
    40
 * @see   FileReader
jaroslav@557
    41
 * @see FilterReader
jaroslav@557
    42
 * @see   PushbackReader
jaroslav@557
    43
 * @see PipedReader
jaroslav@557
    44
 * @see StringReader
jaroslav@557
    45
 * @see Writer
jaroslav@557
    46
 *
jaroslav@557
    47
 * @author      Mark Reinhold
jaroslav@557
    48
 * @since       JDK1.1
jaroslav@557
    49
 */
jaroslav@557
    50
jaroslav@557
    51
public abstract class Reader implements Readable, Closeable {
jaroslav@557
    52
jaroslav@557
    53
    /**
jaroslav@557
    54
     * The object used to synchronize operations on this stream.  For
jaroslav@557
    55
     * efficiency, a character-stream object may use an object other than
jaroslav@557
    56
     * itself to protect critical sections.  A subclass should therefore use
jaroslav@557
    57
     * the object in this field rather than <tt>this</tt> or a synchronized
jaroslav@557
    58
     * method.
jaroslav@557
    59
     */
jaroslav@557
    60
    protected Object lock;
jaroslav@557
    61
jaroslav@557
    62
    /**
jaroslav@557
    63
     * Creates a new character-stream reader whose critical sections will
jaroslav@557
    64
     * synchronize on the reader itself.
jaroslav@557
    65
     */
jaroslav@557
    66
    protected Reader() {
jaroslav@557
    67
        this.lock = this;
jaroslav@557
    68
    }
jaroslav@557
    69
jaroslav@557
    70
    /**
jaroslav@557
    71
     * Creates a new character-stream reader whose critical sections will
jaroslav@557
    72
     * synchronize on the given object.
jaroslav@557
    73
     *
jaroslav@557
    74
     * @param lock  The Object to synchronize on.
jaroslav@557
    75
     */
jaroslav@557
    76
    protected Reader(Object lock) {
jaroslav@557
    77
        if (lock == null) {
jaroslav@557
    78
            throw new NullPointerException();
jaroslav@557
    79
        }
jaroslav@557
    80
        this.lock = lock;
jaroslav@557
    81
    }
jaroslav@557
    82
jaroslav@557
    83
    /**
jaroslav@557
    84
     * Attempts to read characters into the specified character buffer.
jaroslav@557
    85
     * The buffer is used as a repository of characters as-is: the only
jaroslav@557
    86
     * changes made are the results of a put operation. No flipping or
jaroslav@557
    87
     * rewinding of the buffer is performed.
jaroslav@557
    88
     *
jaroslav@557
    89
     * @param target the buffer to read characters into
jaroslav@557
    90
     * @return The number of characters added to the buffer, or
jaroslav@557
    91
     *         -1 if this source of characters is at its end
jaroslav@557
    92
     * @throws IOException if an I/O error occurs
jaroslav@557
    93
     * @throws NullPointerException if target is null
jaroslav@557
    94
     * @throws ReadOnlyBufferException if target is a read only buffer
jaroslav@557
    95
     * @since 1.5
jaroslav@557
    96
     */
jaroslav@557
    97
    public int read(java.nio.CharBuffer target) throws IOException {
jaroslav@557
    98
        int len = target.remaining();
jaroslav@557
    99
        char[] cbuf = new char[len];
jaroslav@557
   100
        int n = read(cbuf, 0, len);
jaroslav@557
   101
        if (n > 0)
jaroslav@557
   102
            target.put(cbuf, 0, n);
jaroslav@557
   103
        return n;
jaroslav@557
   104
    }
jaroslav@557
   105
jaroslav@557
   106
    /**
jaroslav@557
   107
     * Reads a single character.  This method will block until a character is
jaroslav@557
   108
     * available, an I/O error occurs, or the end of the stream is reached.
jaroslav@557
   109
     *
jaroslav@557
   110
     * <p> Subclasses that intend to support efficient single-character input
jaroslav@557
   111
     * should override this method.
jaroslav@557
   112
     *
jaroslav@557
   113
     * @return     The character read, as an integer in the range 0 to 65535
jaroslav@557
   114
     *             (<tt>0x00-0xffff</tt>), or -1 if the end of the stream has
jaroslav@557
   115
     *             been reached
jaroslav@557
   116
     *
jaroslav@557
   117
     * @exception  IOException  If an I/O error occurs
jaroslav@557
   118
     */
jaroslav@557
   119
    public int read() throws IOException {
jaroslav@557
   120
        char cb[] = new char[1];
jaroslav@557
   121
        if (read(cb, 0, 1) == -1)
jaroslav@557
   122
            return -1;
jaroslav@557
   123
        else
jaroslav@557
   124
            return cb[0];
jaroslav@557
   125
    }
jaroslav@557
   126
jaroslav@557
   127
    /**
jaroslav@557
   128
     * Reads characters into an array.  This method will block until some input
jaroslav@557
   129
     * is available, an I/O error occurs, or the end of the stream is reached.
jaroslav@557
   130
     *
jaroslav@557
   131
     * @param       cbuf  Destination buffer
jaroslav@557
   132
     *
jaroslav@557
   133
     * @return      The number of characters read, or -1
jaroslav@557
   134
     *              if the end of the stream
jaroslav@557
   135
     *              has been reached
jaroslav@557
   136
     *
jaroslav@557
   137
     * @exception   IOException  If an I/O error occurs
jaroslav@557
   138
     */
jaroslav@557
   139
    public int read(char cbuf[]) throws IOException {
jaroslav@557
   140
        return read(cbuf, 0, cbuf.length);
jaroslav@557
   141
    }
jaroslav@557
   142
jaroslav@557
   143
    /**
jaroslav@557
   144
     * Reads characters into a portion of an array.  This method will block
jaroslav@557
   145
     * until some input is available, an I/O error occurs, or the end of the
jaroslav@557
   146
     * stream is reached.
jaroslav@557
   147
     *
jaroslav@557
   148
     * @param      cbuf  Destination buffer
jaroslav@557
   149
     * @param      off   Offset at which to start storing characters
jaroslav@557
   150
     * @param      len   Maximum number of characters to read
jaroslav@557
   151
     *
jaroslav@557
   152
     * @return     The number of characters read, or -1 if the end of the
jaroslav@557
   153
     *             stream has been reached
jaroslav@557
   154
     *
jaroslav@557
   155
     * @exception  IOException  If an I/O error occurs
jaroslav@557
   156
     */
jaroslav@557
   157
    abstract public int read(char cbuf[], int off, int len) throws IOException;
jaroslav@557
   158
jaroslav@557
   159
    /** Maximum skip-buffer size */
jaroslav@557
   160
    private static final int maxSkipBufferSize = 8192;
jaroslav@557
   161
jaroslav@557
   162
    /** Skip buffer, null until allocated */
jaroslav@557
   163
    private char skipBuffer[] = null;
jaroslav@557
   164
jaroslav@557
   165
    /**
jaroslav@557
   166
     * Skips characters.  This method will block until some characters are
jaroslav@557
   167
     * available, an I/O error occurs, or the end of the stream is reached.
jaroslav@557
   168
     *
jaroslav@557
   169
     * @param  n  The number of characters to skip
jaroslav@557
   170
     *
jaroslav@557
   171
     * @return    The number of characters actually skipped
jaroslav@557
   172
     *
jaroslav@557
   173
     * @exception  IllegalArgumentException  If <code>n</code> is negative.
jaroslav@557
   174
     * @exception  IOException  If an I/O error occurs
jaroslav@557
   175
     */
jaroslav@557
   176
    public long skip(long n) throws IOException {
jaroslav@557
   177
        if (n < 0L)
jaroslav@557
   178
            throw new IllegalArgumentException("skip value is negative");
jaroslav@557
   179
        int nn = (int) Math.min(n, maxSkipBufferSize);
jaroslav@557
   180
        synchronized (lock) {
jaroslav@557
   181
            if ((skipBuffer == null) || (skipBuffer.length < nn))
jaroslav@557
   182
                skipBuffer = new char[nn];
jaroslav@557
   183
            long r = n;
jaroslav@557
   184
            while (r > 0) {
jaroslav@557
   185
                int nc = read(skipBuffer, 0, (int)Math.min(r, nn));
jaroslav@557
   186
                if (nc == -1)
jaroslav@557
   187
                    break;
jaroslav@557
   188
                r -= nc;
jaroslav@557
   189
            }
jaroslav@557
   190
            return n - r;
jaroslav@557
   191
        }
jaroslav@557
   192
    }
jaroslav@557
   193
jaroslav@557
   194
    /**
jaroslav@557
   195
     * Tells whether this stream is ready to be read.
jaroslav@557
   196
     *
jaroslav@557
   197
     * @return True if the next read() is guaranteed not to block for input,
jaroslav@557
   198
     * false otherwise.  Note that returning false does not guarantee that the
jaroslav@557
   199
     * next read will block.
jaroslav@557
   200
     *
jaroslav@557
   201
     * @exception  IOException  If an I/O error occurs
jaroslav@557
   202
     */
jaroslav@557
   203
    public boolean ready() throws IOException {
jaroslav@557
   204
        return false;
jaroslav@557
   205
    }
jaroslav@557
   206
jaroslav@557
   207
    /**
jaroslav@557
   208
     * Tells whether this stream supports the mark() operation. The default
jaroslav@557
   209
     * implementation always returns false. Subclasses should override this
jaroslav@557
   210
     * method.
jaroslav@557
   211
     *
jaroslav@557
   212
     * @return true if and only if this stream supports the mark operation.
jaroslav@557
   213
     */
jaroslav@557
   214
    public boolean markSupported() {
jaroslav@557
   215
        return false;
jaroslav@557
   216
    }
jaroslav@557
   217
jaroslav@557
   218
    /**
jaroslav@557
   219
     * Marks the present position in the stream.  Subsequent calls to reset()
jaroslav@557
   220
     * will attempt to reposition the stream to this point.  Not all
jaroslav@557
   221
     * character-input streams support the mark() operation.
jaroslav@557
   222
     *
jaroslav@557
   223
     * @param  readAheadLimit  Limit on the number of characters that may be
jaroslav@557
   224
     *                         read while still preserving the mark.  After
jaroslav@557
   225
     *                         reading this many characters, attempting to
jaroslav@557
   226
     *                         reset the stream may fail.
jaroslav@557
   227
     *
jaroslav@557
   228
     * @exception  IOException  If the stream does not support mark(),
jaroslav@557
   229
     *                          or if some other I/O error occurs
jaroslav@557
   230
     */
jaroslav@557
   231
    public void mark(int readAheadLimit) throws IOException {
jaroslav@557
   232
        throw new IOException("mark() not supported");
jaroslav@557
   233
    }
jaroslav@557
   234
jaroslav@557
   235
    /**
jaroslav@557
   236
     * Resets the stream.  If the stream has been marked, then attempt to
jaroslav@557
   237
     * reposition it at the mark.  If the stream has not been marked, then
jaroslav@557
   238
     * attempt to reset it in some way appropriate to the particular stream,
jaroslav@557
   239
     * for example by repositioning it to its starting point.  Not all
jaroslav@557
   240
     * character-input streams support the reset() operation, and some support
jaroslav@557
   241
     * reset() without supporting mark().
jaroslav@557
   242
     *
jaroslav@557
   243
     * @exception  IOException  If the stream has not been marked,
jaroslav@557
   244
     *                          or if the mark has been invalidated,
jaroslav@557
   245
     *                          or if the stream does not support reset(),
jaroslav@557
   246
     *                          or if some other I/O error occurs
jaroslav@557
   247
     */
jaroslav@557
   248
    public void reset() throws IOException {
jaroslav@557
   249
        throw new IOException("reset() not supported");
jaroslav@557
   250
    }
jaroslav@557
   251
jaroslav@557
   252
    /**
jaroslav@557
   253
     * Closes the stream and releases any system resources associated with
jaroslav@557
   254
     * it.  Once the stream has been closed, further read(), ready(),
jaroslav@557
   255
     * mark(), reset(), or skip() invocations will throw an IOException.
jaroslav@557
   256
     * Closing a previously closed stream has no effect.
jaroslav@557
   257
     *
jaroslav@557
   258
     * @exception  IOException  If an I/O error occurs
jaroslav@557
   259
     */
jaroslav@557
   260
     abstract public void close() throws IOException;
jaroslav@557
   261
jaroslav@557
   262
}