< prev index next >

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

Print this page




  87             if (pos < buf.length)
  88                 return buf[pos++];
  89             else
  90                 return super.read();
  91         }
  92     }
  93 
  94     /**
  95      * Reads characters into a portion of an array.
  96      *
  97      * @param      cbuf  Destination buffer
  98      * @param      off   Offset at which to start writing characters
  99      * @param      len   Maximum number of characters to read
 100      *
 101      * @return     The number of characters read, or -1 if the end of the
 102      *             stream has been reached
 103      *
 104      * @exception  IOException  If an I/O error occurs
 105      * @exception  IndexOutOfBoundsException {@inheritDoc}
 106      */
 107     public int read(char cbuf[], int off, int len) throws IOException {
 108         synchronized (lock) {
 109             ensureOpen();
 110             try {
 111                 if (len <= 0) {
 112                     if (len < 0) {
 113                         throw new IndexOutOfBoundsException();
 114                     } else if ((off < 0) || (off > cbuf.length)) {
 115                         throw new IndexOutOfBoundsException();
 116                     }
 117                     return 0;
 118                 }
 119                 int avail = buf.length - pos;
 120                 if (avail > 0) {
 121                     if (len < avail)
 122                         avail = len;
 123                     System.arraycopy(buf, pos, cbuf, off, avail);
 124                     pos += avail;
 125                     off += avail;
 126                     len -= avail;
 127                 }


 155             if (pos == 0)
 156                 throw new IOException("Pushback buffer overflow");
 157             buf[--pos] = (char) c;
 158         }
 159     }
 160 
 161     /**
 162      * Pushes back a portion of an array of characters by copying it to the
 163      * front of the pushback buffer.  After this method returns, the next
 164      * character to be read will have the value <code>cbuf[off]</code>, the
 165      * character after that will have the value <code>cbuf[off+1]</code>, and
 166      * so forth.
 167      *
 168      * @param  cbuf  Character array
 169      * @param  off   Offset of first character to push back
 170      * @param  len   Number of characters to push back
 171      *
 172      * @exception  IOException  If there is insufficient room in the pushback
 173      *                          buffer, or if some other I/O error occurs
 174      */
 175     public void unread(char cbuf[], int off, int len) throws IOException {
 176         synchronized (lock) {
 177             ensureOpen();
 178             if (len > pos)
 179                 throw new IOException("Pushback buffer overflow");
 180             pos -= len;
 181             System.arraycopy(cbuf, off, buf, pos, len);
 182         }
 183     }
 184 
 185     /**
 186      * Pushes back an array of characters by copying it to the front of the
 187      * pushback buffer.  After this method returns, the next character to be
 188      * read will have the value <code>cbuf[0]</code>, the character after that
 189      * will have the value <code>cbuf[1]</code>, and so forth.
 190      *
 191      * @param  cbuf  Character array to push back
 192      *
 193      * @exception  IOException  If there is insufficient room in the pushback
 194      *                          buffer, or if some other I/O error occurs
 195      */
 196     public void unread(char cbuf[]) throws IOException {
 197         unread(cbuf, 0, cbuf.length);
 198     }
 199 
 200     /**
 201      * Tells whether this stream is ready to be read.
 202      *
 203      * @exception  IOException  If an I/O error occurs
 204      */
 205     public boolean ready() throws IOException {
 206         synchronized (lock) {
 207             ensureOpen();
 208             return (pos < buf.length) || super.ready();
 209         }
 210     }
 211 
 212     /**
 213      * Marks the present position in the stream. The <code>mark</code>
 214      * for class <code>PushbackReader</code> always throws an exception.
 215      *
 216      * @exception  IOException  Always, since mark is not supported




  87             if (pos < buf.length)
  88                 return buf[pos++];
  89             else
  90                 return super.read();
  91         }
  92     }
  93 
  94     /**
  95      * Reads characters into a portion of an array.
  96      *
  97      * @param      cbuf  Destination buffer
  98      * @param      off   Offset at which to start writing characters
  99      * @param      len   Maximum number of characters to read
 100      *
 101      * @return     The number of characters read, or -1 if the end of the
 102      *             stream has been reached
 103      *
 104      * @exception  IOException  If an I/O error occurs
 105      * @exception  IndexOutOfBoundsException {@inheritDoc}
 106      */
 107     public int read(char[] cbuf, int off, int len) throws IOException {
 108         synchronized (lock) {
 109             ensureOpen();
 110             try {
 111                 if (len <= 0) {
 112                     if (len < 0) {
 113                         throw new IndexOutOfBoundsException();
 114                     } else if ((off < 0) || (off > cbuf.length)) {
 115                         throw new IndexOutOfBoundsException();
 116                     }
 117                     return 0;
 118                 }
 119                 int avail = buf.length - pos;
 120                 if (avail > 0) {
 121                     if (len < avail)
 122                         avail = len;
 123                     System.arraycopy(buf, pos, cbuf, off, avail);
 124                     pos += avail;
 125                     off += avail;
 126                     len -= avail;
 127                 }


 155             if (pos == 0)
 156                 throw new IOException("Pushback buffer overflow");
 157             buf[--pos] = (char) c;
 158         }
 159     }
 160 
 161     /**
 162      * Pushes back a portion of an array of characters by copying it to the
 163      * front of the pushback buffer.  After this method returns, the next
 164      * character to be read will have the value <code>cbuf[off]</code>, the
 165      * character after that will have the value <code>cbuf[off+1]</code>, and
 166      * so forth.
 167      *
 168      * @param  cbuf  Character array
 169      * @param  off   Offset of first character to push back
 170      * @param  len   Number of characters to push back
 171      *
 172      * @exception  IOException  If there is insufficient room in the pushback
 173      *                          buffer, or if some other I/O error occurs
 174      */
 175     public void unread(char[] cbuf, int off, int len) throws IOException {
 176         synchronized (lock) {
 177             ensureOpen();
 178             if (len > pos)
 179                 throw new IOException("Pushback buffer overflow");
 180             pos -= len;
 181             System.arraycopy(cbuf, off, buf, pos, len);
 182         }
 183     }
 184 
 185     /**
 186      * Pushes back an array of characters by copying it to the front of the
 187      * pushback buffer.  After this method returns, the next character to be
 188      * read will have the value <code>cbuf[0]</code>, the character after that
 189      * will have the value <code>cbuf[1]</code>, and so forth.
 190      *
 191      * @param  cbuf  Character array to push back
 192      *
 193      * @exception  IOException  If there is insufficient room in the pushback
 194      *                          buffer, or if some other I/O error occurs
 195      */
 196     public void unread(char[] cbuf) throws IOException {
 197         unread(cbuf, 0, cbuf.length);
 198     }
 199 
 200     /**
 201      * Tells whether this stream is ready to be read.
 202      *
 203      * @exception  IOException  If an I/O error occurs
 204      */
 205     public boolean ready() throws IOException {
 206         synchronized (lock) {
 207             ensureOpen();
 208             return (pos < buf.length) || super.ready();
 209         }
 210     }
 211 
 212     /**
 213      * Marks the present position in the stream. The <code>mark</code>
 214      * for class <code>PushbackReader</code> always throws an exception.
 215      *
 216      * @exception  IOException  Always, since mark is not supported


< prev index next >