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