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

Print this page




  67     public int read() throws IOException {
  68         synchronized (lock) {
  69             ensureOpen();
  70             if (next >= length)
  71                 return -1;
  72             return str.charAt(next++);
  73         }
  74     }
  75 
  76     /**
  77      * Reads characters into a portion of an array.
  78      *
  79      * @param      cbuf  Destination buffer
  80      * @param      off   Offset at which to start writing characters
  81      * @param      len   Maximum number of characters to read
  82      *
  83      * @return     The number of characters read, or -1 if the end of the
  84      *             stream has been reached
  85      *
  86      * @exception  IOException  If an I/O error occurs

  87      */
  88     public int read(char cbuf[], int off, int len) throws IOException {
  89         synchronized (lock) {
  90             ensureOpen();
  91             if ((off < 0) || (off > cbuf.length) || (len < 0) ||
  92                 ((off + len) > cbuf.length) || ((off + len) < 0)) {
  93                 throw new IndexOutOfBoundsException();
  94             } else if (len == 0) {
  95                 return 0;
  96             }
  97             if (next >= length)
  98                 return -1;
  99             int n = Math.min(length - next, len);
 100             str.getChars(next, next + n, cbuf, off);
 101             next += n;
 102             return n;
 103         }
 104     }
 105 
 106     /**




  67     public int read() throws IOException {
  68         synchronized (lock) {
  69             ensureOpen();
  70             if (next >= length)
  71                 return -1;
  72             return str.charAt(next++);
  73         }
  74     }
  75 
  76     /**
  77      * Reads characters into a portion of an array.
  78      *
  79      * @param      cbuf  Destination buffer
  80      * @param      off   Offset at which to start writing characters
  81      * @param      len   Maximum number of characters to read
  82      *
  83      * @return     The number of characters read, or -1 if the end of the
  84      *             stream has been reached
  85      *
  86      * @exception  IOException  If an I/O error occurs
  87      * @exception  IndexOutOfBoundsException {@inheritDoc}
  88      */
  89     public int read(char cbuf[], int off, int len) throws IOException {
  90         synchronized (lock) {
  91             ensureOpen();
  92             if ((off < 0) || (off > cbuf.length) || (len < 0) ||
  93                 ((off + len) > cbuf.length) || ((off + len) < 0)) {
  94                 throw new IndexOutOfBoundsException();
  95             } else if (len == 0) {
  96                 return 0;
  97             }
  98             if (next >= length)
  99                 return -1;
 100             int n = Math.min(length - next, len);
 101             str.getChars(next, next + n, cbuf, off);
 102             next += n;
 103             return n;
 104         }
 105     }
 106 
 107     /**