emul/compact/src/main/java/java/io/Reader.java
branchjdk7-b147
changeset 557 5be31d9fa455
child 560 53fafe384803
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/emul/compact/src/main/java/java/io/Reader.java	Wed Jan 23 22:32:27 2013 +0100
     1.3 @@ -0,0 +1,262 @@
     1.4 +/*
     1.5 + * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.io;
    1.30 +
    1.31 +
    1.32 +/**
    1.33 + * Abstract class for reading character streams.  The only methods that a
    1.34 + * subclass must implement are read(char[], int, int) and close().  Most
    1.35 + * subclasses, however, will override some of the methods defined here in order
    1.36 + * to provide higher efficiency, additional functionality, or both.
    1.37 + *
    1.38 + *
    1.39 + * @see BufferedReader
    1.40 + * @see   LineNumberReader
    1.41 + * @see CharArrayReader
    1.42 + * @see InputStreamReader
    1.43 + * @see   FileReader
    1.44 + * @see FilterReader
    1.45 + * @see   PushbackReader
    1.46 + * @see PipedReader
    1.47 + * @see StringReader
    1.48 + * @see Writer
    1.49 + *
    1.50 + * @author      Mark Reinhold
    1.51 + * @since       JDK1.1
    1.52 + */
    1.53 +
    1.54 +public abstract class Reader implements Readable, Closeable {
    1.55 +
    1.56 +    /**
    1.57 +     * The object used to synchronize operations on this stream.  For
    1.58 +     * efficiency, a character-stream object may use an object other than
    1.59 +     * itself to protect critical sections.  A subclass should therefore use
    1.60 +     * the object in this field rather than <tt>this</tt> or a synchronized
    1.61 +     * method.
    1.62 +     */
    1.63 +    protected Object lock;
    1.64 +
    1.65 +    /**
    1.66 +     * Creates a new character-stream reader whose critical sections will
    1.67 +     * synchronize on the reader itself.
    1.68 +     */
    1.69 +    protected Reader() {
    1.70 +        this.lock = this;
    1.71 +    }
    1.72 +
    1.73 +    /**
    1.74 +     * Creates a new character-stream reader whose critical sections will
    1.75 +     * synchronize on the given object.
    1.76 +     *
    1.77 +     * @param lock  The Object to synchronize on.
    1.78 +     */
    1.79 +    protected Reader(Object lock) {
    1.80 +        if (lock == null) {
    1.81 +            throw new NullPointerException();
    1.82 +        }
    1.83 +        this.lock = lock;
    1.84 +    }
    1.85 +
    1.86 +    /**
    1.87 +     * Attempts to read characters into the specified character buffer.
    1.88 +     * The buffer is used as a repository of characters as-is: the only
    1.89 +     * changes made are the results of a put operation. No flipping or
    1.90 +     * rewinding of the buffer is performed.
    1.91 +     *
    1.92 +     * @param target the buffer to read characters into
    1.93 +     * @return The number of characters added to the buffer, or
    1.94 +     *         -1 if this source of characters is at its end
    1.95 +     * @throws IOException if an I/O error occurs
    1.96 +     * @throws NullPointerException if target is null
    1.97 +     * @throws ReadOnlyBufferException if target is a read only buffer
    1.98 +     * @since 1.5
    1.99 +     */
   1.100 +    public int read(java.nio.CharBuffer target) throws IOException {
   1.101 +        int len = target.remaining();
   1.102 +        char[] cbuf = new char[len];
   1.103 +        int n = read(cbuf, 0, len);
   1.104 +        if (n > 0)
   1.105 +            target.put(cbuf, 0, n);
   1.106 +        return n;
   1.107 +    }
   1.108 +
   1.109 +    /**
   1.110 +     * Reads a single character.  This method will block until a character is
   1.111 +     * available, an I/O error occurs, or the end of the stream is reached.
   1.112 +     *
   1.113 +     * <p> Subclasses that intend to support efficient single-character input
   1.114 +     * should override this method.
   1.115 +     *
   1.116 +     * @return     The character read, as an integer in the range 0 to 65535
   1.117 +     *             (<tt>0x00-0xffff</tt>), or -1 if the end of the stream has
   1.118 +     *             been reached
   1.119 +     *
   1.120 +     * @exception  IOException  If an I/O error occurs
   1.121 +     */
   1.122 +    public int read() throws IOException {
   1.123 +        char cb[] = new char[1];
   1.124 +        if (read(cb, 0, 1) == -1)
   1.125 +            return -1;
   1.126 +        else
   1.127 +            return cb[0];
   1.128 +    }
   1.129 +
   1.130 +    /**
   1.131 +     * Reads characters into an array.  This method will block until some input
   1.132 +     * is available, an I/O error occurs, or the end of the stream is reached.
   1.133 +     *
   1.134 +     * @param       cbuf  Destination buffer
   1.135 +     *
   1.136 +     * @return      The number of characters read, or -1
   1.137 +     *              if the end of the stream
   1.138 +     *              has been reached
   1.139 +     *
   1.140 +     * @exception   IOException  If an I/O error occurs
   1.141 +     */
   1.142 +    public int read(char cbuf[]) throws IOException {
   1.143 +        return read(cbuf, 0, cbuf.length);
   1.144 +    }
   1.145 +
   1.146 +    /**
   1.147 +     * Reads characters into a portion of an array.  This method will block
   1.148 +     * until some input is available, an I/O error occurs, or the end of the
   1.149 +     * stream is reached.
   1.150 +     *
   1.151 +     * @param      cbuf  Destination buffer
   1.152 +     * @param      off   Offset at which to start storing characters
   1.153 +     * @param      len   Maximum number of characters to read
   1.154 +     *
   1.155 +     * @return     The number of characters read, or -1 if the end of the
   1.156 +     *             stream has been reached
   1.157 +     *
   1.158 +     * @exception  IOException  If an I/O error occurs
   1.159 +     */
   1.160 +    abstract public int read(char cbuf[], int off, int len) throws IOException;
   1.161 +
   1.162 +    /** Maximum skip-buffer size */
   1.163 +    private static final int maxSkipBufferSize = 8192;
   1.164 +
   1.165 +    /** Skip buffer, null until allocated */
   1.166 +    private char skipBuffer[] = null;
   1.167 +
   1.168 +    /**
   1.169 +     * Skips characters.  This method will block until some characters are
   1.170 +     * available, an I/O error occurs, or the end of the stream is reached.
   1.171 +     *
   1.172 +     * @param  n  The number of characters to skip
   1.173 +     *
   1.174 +     * @return    The number of characters actually skipped
   1.175 +     *
   1.176 +     * @exception  IllegalArgumentException  If <code>n</code> is negative.
   1.177 +     * @exception  IOException  If an I/O error occurs
   1.178 +     */
   1.179 +    public long skip(long n) throws IOException {
   1.180 +        if (n < 0L)
   1.181 +            throw new IllegalArgumentException("skip value is negative");
   1.182 +        int nn = (int) Math.min(n, maxSkipBufferSize);
   1.183 +        synchronized (lock) {
   1.184 +            if ((skipBuffer == null) || (skipBuffer.length < nn))
   1.185 +                skipBuffer = new char[nn];
   1.186 +            long r = n;
   1.187 +            while (r > 0) {
   1.188 +                int nc = read(skipBuffer, 0, (int)Math.min(r, nn));
   1.189 +                if (nc == -1)
   1.190 +                    break;
   1.191 +                r -= nc;
   1.192 +            }
   1.193 +            return n - r;
   1.194 +        }
   1.195 +    }
   1.196 +
   1.197 +    /**
   1.198 +     * Tells whether this stream is ready to be read.
   1.199 +     *
   1.200 +     * @return True if the next read() is guaranteed not to block for input,
   1.201 +     * false otherwise.  Note that returning false does not guarantee that the
   1.202 +     * next read will block.
   1.203 +     *
   1.204 +     * @exception  IOException  If an I/O error occurs
   1.205 +     */
   1.206 +    public boolean ready() throws IOException {
   1.207 +        return false;
   1.208 +    }
   1.209 +
   1.210 +    /**
   1.211 +     * Tells whether this stream supports the mark() operation. The default
   1.212 +     * implementation always returns false. Subclasses should override this
   1.213 +     * method.
   1.214 +     *
   1.215 +     * @return true if and only if this stream supports the mark operation.
   1.216 +     */
   1.217 +    public boolean markSupported() {
   1.218 +        return false;
   1.219 +    }
   1.220 +
   1.221 +    /**
   1.222 +     * Marks the present position in the stream.  Subsequent calls to reset()
   1.223 +     * will attempt to reposition the stream to this point.  Not all
   1.224 +     * character-input streams support the mark() operation.
   1.225 +     *
   1.226 +     * @param  readAheadLimit  Limit on the number of characters that may be
   1.227 +     *                         read while still preserving the mark.  After
   1.228 +     *                         reading this many characters, attempting to
   1.229 +     *                         reset the stream may fail.
   1.230 +     *
   1.231 +     * @exception  IOException  If the stream does not support mark(),
   1.232 +     *                          or if some other I/O error occurs
   1.233 +     */
   1.234 +    public void mark(int readAheadLimit) throws IOException {
   1.235 +        throw new IOException("mark() not supported");
   1.236 +    }
   1.237 +
   1.238 +    /**
   1.239 +     * Resets the stream.  If the stream has been marked, then attempt to
   1.240 +     * reposition it at the mark.  If the stream has not been marked, then
   1.241 +     * attempt to reset it in some way appropriate to the particular stream,
   1.242 +     * for example by repositioning it to its starting point.  Not all
   1.243 +     * character-input streams support the reset() operation, and some support
   1.244 +     * reset() without supporting mark().
   1.245 +     *
   1.246 +     * @exception  IOException  If the stream has not been marked,
   1.247 +     *                          or if the mark has been invalidated,
   1.248 +     *                          or if the stream does not support reset(),
   1.249 +     *                          or if some other I/O error occurs
   1.250 +     */
   1.251 +    public void reset() throws IOException {
   1.252 +        throw new IOException("reset() not supported");
   1.253 +    }
   1.254 +
   1.255 +    /**
   1.256 +     * Closes the stream and releases any system resources associated with
   1.257 +     * it.  Once the stream has been closed, further read(), ready(),
   1.258 +     * mark(), reset(), or skip() invocations will throw an IOException.
   1.259 +     * Closing a previously closed stream has no effect.
   1.260 +     *
   1.261 +     * @exception  IOException  If an I/O error occurs
   1.262 +     */
   1.263 +     abstract public void close() throws IOException;
   1.264 +
   1.265 +}