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

Print this page
rev 14187 : 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
Summary: Clarify and expand specification of ObjectInputStream.read(byte[],int,int) and both variants of {DataInput,DataInputStream,ObjectInputStream,RandomAccessfile}.readFully().
Reviewed-by: XXX
   1 /*
   2  * Copyright (c) 1994, 2013, 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


 402      * @return     the total number of bytes read into the buffer, or
 403      *             {@code -1} if there is no more data because the end of
 404      *             this file has been reached.
 405      * @exception  IOException If the first byte cannot be read for any reason
 406      * other than end of file, or if the random access file has been closed, or if
 407      * some other I/O error occurs.
 408      * @exception  NullPointerException If {@code b} is {@code null}.
 409      */
 410     public int read(byte b[]) throws IOException {
 411         return readBytes(b, 0, b.length);
 412     }
 413 
 414     /**
 415      * Reads {@code b.length} bytes from this file into the byte
 416      * array, starting at the current file pointer. This method reads
 417      * repeatedly from the file until the requested number of bytes are
 418      * read. This method blocks until the requested number of bytes are
 419      * read, the end of the stream is detected, or an exception is thrown.
 420      *
 421      * @param      b   the buffer into which the data is read.
 422      * @exception  EOFException  if this file reaches the end before reading

 423      *               all the bytes.
 424      * @exception  IOException   if an I/O error occurs.
 425      */
 426     public final void readFully(byte b[]) throws IOException {
 427         readFully(b, 0, b.length);
 428     }
 429 
 430     /**
 431      * Reads exactly {@code len} bytes from this file into the byte
 432      * array, starting at the current file pointer. This method reads
 433      * repeatedly from the file until the requested number of bytes are
 434      * read. This method blocks until the requested number of bytes are
 435      * read, the end of the stream is detected, or an exception is thrown.
 436      *
 437      * @param      b     the buffer into which the data is read.
 438      * @param      off   the start offset of the data.
 439      * @param      len   the number of bytes to read.
 440      * @exception  EOFException  if this file reaches the end before reading




 441      *               all the bytes.
 442      * @exception  IOException   if an I/O error occurs.
 443      */
 444     public final void readFully(byte b[], int off, int len) throws IOException {
 445         int n = 0;
 446         do {
 447             int count = this.read(b, off + n, len - n);
 448             if (count < 0)
 449                 throw new EOFException();
 450             n += count;
 451         } while (n < len);
 452     }
 453 
 454     /**
 455      * Attempts to skip over {@code n} bytes of input discarding the
 456      * skipped bytes.
 457      * <p>
 458      *
 459      * This method may skip over some smaller number of bytes, possibly zero.
 460      * This may result from any of a number of conditions; reaching end of
 461      * file before {@code n} bytes have been skipped is only one
 462      * possibility. This method never throws an {@code EOFException}.


   1 /*
   2  * Copyright (c) 1994, 2016, 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


 402      * @return     the total number of bytes read into the buffer, or
 403      *             {@code -1} if there is no more data because the end of
 404      *             this file has been reached.
 405      * @exception  IOException If the first byte cannot be read for any reason
 406      * other than end of file, or if the random access file has been closed, or if
 407      * some other I/O error occurs.
 408      * @exception  NullPointerException If {@code b} is {@code null}.
 409      */
 410     public int read(byte b[]) throws IOException {
 411         return readBytes(b, 0, b.length);
 412     }
 413 
 414     /**
 415      * Reads {@code b.length} bytes from this file into the byte
 416      * array, starting at the current file pointer. This method reads
 417      * repeatedly from the file until the requested number of bytes are
 418      * read. This method blocks until the requested number of bytes are
 419      * read, the end of the stream is detected, or an exception is thrown.
 420      *
 421      * @param   b   the buffer into which the data is read.
 422      * @throws  NullPointerException If {@code b} is {@code null}
 423      * @throws  EOFException  if this file reaches the end before reading
 424      *          all the bytes.
 425      * @throws  IOException   if an I/O error occurs.
 426      */
 427     public final void readFully(byte b[]) throws IOException {
 428         readFully(b, 0, b.length);
 429     }
 430 
 431     /**
 432      * Reads exactly {@code len} bytes from this file into the byte
 433      * array, starting at the current file pointer. This method reads
 434      * repeatedly from the file until the requested number of bytes are
 435      * read. This method blocks until the requested number of bytes are
 436      * read, the end of the stream is detected, or an exception is thrown.
 437      *
 438      * @param   b     the buffer into which the data is read.
 439      * @param   off   the start offset into the data array {@code b}.
 440      * @param   len   the number of bytes to read.
 441      * @throws  NullPointerException If {@code b} is {@code null}.
 442      * @throws  IndexOutOfBoundsException If {@code off} is negative,
 443      *          {@code len} is negative, or {@code len} is greater than
 444      *          {@code b.length - off}
 445      * @throws  EOFException  if this file reaches the end before reading
 446      *          all the bytes.
 447      * @throws  IOException   if an I/O error occurs.
 448      */
 449     public final void readFully(byte b[], int off, int len) throws IOException {
 450         int n = 0;
 451         do {
 452             int count = this.read(b, off + n, len - n);
 453             if (count < 0)
 454                 throw new EOFException();
 455             n += count;
 456         } while (n < len);
 457     }
 458 
 459     /**
 460      * Attempts to skip over {@code n} bytes of input discarding the
 461      * skipped bytes.
 462      * <p>
 463      *
 464      * This method may skip over some smaller number of bytes, possibly zero.
 465      * This may result from any of a number of conditions; reaching end of
 466      * file before {@code n} bytes have been skipped is only one
 467      * possibility. This method never throws an {@code EOFException}.