< prev index next >

src/java.base/share/classes/java/io/Reader.java

Print this page




  37  * @see   LineNumberReader
  38  * @see CharArrayReader
  39  * @see InputStreamReader
  40  * @see   FileReader
  41  * @see FilterReader
  42  * @see   PushbackReader
  43  * @see PipedReader
  44  * @see StringReader
  45  * @see Writer
  46  *
  47  * @author      Mark Reinhold
  48  * @since       1.1
  49  */
  50 
  51 public abstract class Reader implements Readable, Closeable {
  52 
  53     /**
  54      * The object used to synchronize operations on this stream.  For
  55      * efficiency, a character-stream object may use an object other than
  56      * itself to protect critical sections.  A subclass should therefore use
  57      * the object in this field rather than <tt>this</tt> or a synchronized
  58      * method.
  59      */
  60     protected Object lock;
  61 
  62     /**
  63      * Creates a new character-stream reader whose critical sections will
  64      * synchronize on the reader itself.
  65      */
  66     protected Reader() {
  67         this.lock = this;
  68     }
  69 
  70     /**
  71      * Creates a new character-stream reader whose critical sections will
  72      * synchronize on the given object.
  73      *
  74      * @param lock  The Object to synchronize on.
  75      */
  76     protected Reader(Object lock) {
  77         if (lock == null) {


  94      * @throws java.nio.ReadOnlyBufferException if target is a read only buffer
  95      * @since 1.5
  96      */
  97     public int read(java.nio.CharBuffer target) throws IOException {
  98         int len = target.remaining();
  99         char[] cbuf = new char[len];
 100         int n = read(cbuf, 0, len);
 101         if (n > 0)
 102             target.put(cbuf, 0, n);
 103         return n;
 104     }
 105 
 106     /**
 107      * Reads a single character.  This method will block until a character is
 108      * available, an I/O error occurs, or the end of the stream is reached.
 109      *
 110      * <p> Subclasses that intend to support efficient single-character input
 111      * should override this method.
 112      *
 113      * @return     The character read, as an integer in the range 0 to 65535
 114      *             (<tt>0x00-0xffff</tt>), or -1 if the end of the stream has
 115      *             been reached
 116      *
 117      * @exception  IOException  If an I/O error occurs
 118      */
 119     public int read() throws IOException {
 120         char cb[] = new char[1];
 121         if (read(cb, 0, 1) == -1)
 122             return -1;
 123         else
 124             return cb[0];
 125     }
 126 
 127     /**
 128      * Reads characters into an array.  This method will block until some input
 129      * is available, an I/O error occurs, or the end of the stream is reached.
 130      *
 131      * @param       cbuf  Destination buffer
 132      *
 133      * @return      The number of characters read, or -1
 134      *              if the end of the stream




  37  * @see   LineNumberReader
  38  * @see CharArrayReader
  39  * @see InputStreamReader
  40  * @see   FileReader
  41  * @see FilterReader
  42  * @see   PushbackReader
  43  * @see PipedReader
  44  * @see StringReader
  45  * @see Writer
  46  *
  47  * @author      Mark Reinhold
  48  * @since       1.1
  49  */
  50 
  51 public abstract class Reader implements Readable, Closeable {
  52 
  53     /**
  54      * The object used to synchronize operations on this stream.  For
  55      * efficiency, a character-stream object may use an object other than
  56      * itself to protect critical sections.  A subclass should therefore use
  57      * the object in this field rather than {@code this} or a synchronized
  58      * method.
  59      */
  60     protected Object lock;
  61 
  62     /**
  63      * Creates a new character-stream reader whose critical sections will
  64      * synchronize on the reader itself.
  65      */
  66     protected Reader() {
  67         this.lock = this;
  68     }
  69 
  70     /**
  71      * Creates a new character-stream reader whose critical sections will
  72      * synchronize on the given object.
  73      *
  74      * @param lock  The Object to synchronize on.
  75      */
  76     protected Reader(Object lock) {
  77         if (lock == null) {


  94      * @throws java.nio.ReadOnlyBufferException if target is a read only buffer
  95      * @since 1.5
  96      */
  97     public int read(java.nio.CharBuffer target) throws IOException {
  98         int len = target.remaining();
  99         char[] cbuf = new char[len];
 100         int n = read(cbuf, 0, len);
 101         if (n > 0)
 102             target.put(cbuf, 0, n);
 103         return n;
 104     }
 105 
 106     /**
 107      * Reads a single character.  This method will block until a character is
 108      * available, an I/O error occurs, or the end of the stream is reached.
 109      *
 110      * <p> Subclasses that intend to support efficient single-character input
 111      * should override this method.
 112      *
 113      * @return     The character read, as an integer in the range 0 to 65535
 114      *             ({@code 0x00-0xffff}), or -1 if the end of the stream has
 115      *             been reached
 116      *
 117      * @exception  IOException  If an I/O error occurs
 118      */
 119     public int read() throws IOException {
 120         char cb[] = new char[1];
 121         if (read(cb, 0, 1) == -1)
 122             return -1;
 123         else
 124             return cb[0];
 125     }
 126 
 127     /**
 128      * Reads characters into an array.  This method will block until some input
 129      * is available, an I/O error occurs, or the end of the stream is reached.
 130      *
 131      * @param       cbuf  Destination buffer
 132      *
 133      * @return      The number of characters read, or -1
 134      *              if the end of the stream


< prev index next >