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

Print this page
rev 11891 : 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
Reviewed-by: Stephen Hawking, Hedonismbot


 255      *
 256      * <p> Subclasses of this class are encouraged, but not required, to
 257      * attempt to read as many characters as possible in the same fashion.
 258      *
 259      * <p> Ordinarily this method takes characters from this stream's character
 260      * buffer, filling it from the underlying stream as necessary.  If,
 261      * however, the buffer is empty, the mark is not valid, and the requested
 262      * length is at least as large as the buffer, then this method will read
 263      * characters directly from the underlying stream into the given array.
 264      * Thus redundant <code>BufferedReader</code>s will not copy data
 265      * unnecessarily.
 266      *
 267      * @param      cbuf  Destination buffer
 268      * @param      off   Offset at which to start storing characters
 269      * @param      len   Maximum number of characters to read
 270      *
 271      * @return     The number of characters read, or -1 if the end of the
 272      *             stream has been reached
 273      *
 274      * @exception  IOException  If an I/O error occurs

 275      */
 276     public int read(char cbuf[], int off, int len) throws IOException {
 277         synchronized (lock) {
 278             ensureOpen();
 279             if ((off < 0) || (off > cbuf.length) || (len < 0) ||
 280                 ((off + len) > cbuf.length) || ((off + len) < 0)) {
 281                 throw new IndexOutOfBoundsException();
 282             } else if (len == 0) {
 283                 return 0;
 284             }
 285 
 286             int n = read1(cbuf, off, len);
 287             if (n <= 0) return n;
 288             while ((n < len) && in.ready()) {
 289                 int n1 = read1(cbuf, off + n, len - n);
 290                 if (n1 <= 0) break;
 291                 n += n1;
 292             }
 293             return n;
 294         }




 255      *
 256      * <p> Subclasses of this class are encouraged, but not required, to
 257      * attempt to read as many characters as possible in the same fashion.
 258      *
 259      * <p> Ordinarily this method takes characters from this stream's character
 260      * buffer, filling it from the underlying stream as necessary.  If,
 261      * however, the buffer is empty, the mark is not valid, and the requested
 262      * length is at least as large as the buffer, then this method will read
 263      * characters directly from the underlying stream into the given array.
 264      * Thus redundant <code>BufferedReader</code>s will not copy data
 265      * unnecessarily.
 266      *
 267      * @param      cbuf  Destination buffer
 268      * @param      off   Offset at which to start storing characters
 269      * @param      len   Maximum number of characters to read
 270      *
 271      * @return     The number of characters read, or -1 if the end of the
 272      *             stream has been reached
 273      *
 274      * @exception  IOException  If an I/O error occurs
 275      * @exception  IndexOutOfBoundsException {@inheritDoc}
 276      */
 277     public int read(char cbuf[], int off, int len) throws IOException {
 278         synchronized (lock) {
 279             ensureOpen();
 280             if ((off < 0) || (off > cbuf.length) || (len < 0) ||
 281                 ((off + len) > cbuf.length) || ((off + len) < 0)) {
 282                 throw new IndexOutOfBoundsException();
 283             } else if (len == 0) {
 284                 return 0;
 285             }
 286 
 287             int n = read1(cbuf, off, len);
 288             if (n <= 0) return n;
 289             while ((n < len) && in.ready()) {
 290                 int n1 = read1(cbuf, off + n, len - n);
 291                 if (n1 <= 0) break;
 292                 n += n1;
 293             }
 294             return n;
 295         }