rev 10457 : 8054720: Modifications of I/O methods for instrumentation purposes
Summary: Wrap some native methods in Java methods and change native method names to a consistent form.
Reviewed-by: TBD

   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
  23  * questions.
  24  */
  25 
  26 package java.io;
  27 
  28 import java.nio.channels.FileChannel;
  29 import sun.nio.ch.FileChannelImpl;
  30 
  31 
  32 /**
  33  * Instances of this class support both reading and writing to a
  34  * random access file. A random access file behaves like a large
  35  * array of bytes stored in the file system. There is a kind of cursor,
  36  * or index into the implied array, called the <em>file pointer</em>;
  37  * input operations read bytes starting at the file pointer and advance
  38  * the file pointer past the bytes read. If the random access file is
  39  * created in read/write mode, then output operations are also available;
  40  * output operations write bytes starting at the file pointer and advance
  41  * the file pointer past the bytes written. Output operations that write
  42  * past the current end of the implied array cause the array to be
  43  * extended. The file pointer can be read by the
  44  * {@code getFilePointer} method and set by the {@code seek}
  45  * method.
  46  * <p>
  47  * It is generally true of all the reading routines in this class that
  48  * if end-of-file is reached before the desired number of bytes has been
  49  * read, an {@code EOFException} (which is a kind of
  50  * {@code IOException}) is thrown. If any byte cannot be read for
  51  * any reason other than end-of-file, an {@code IOException} other
  52  * than {@code EOFException} is thrown. In particular, an
  53  * {@code IOException} may be thrown if the stream has been closed.
  54  *
  55  * @author  unascribed
  56  * @since   1.0
  57  */
  58 
  59 public class RandomAccessFile implements DataOutput, DataInput, Closeable {
  60 
  61     private FileDescriptor fd;
  62     private FileChannel channel = null;
  63     private boolean rw;
  64 
  65     /**
  66      * The path of the referenced file
  67      * (null if the stream is created with a file descriptor)
  68      */
  69     private final String path;
  70 
  71     private Object closeLock = new Object();
  72     private volatile boolean closed = false;
  73 
  74     private static final int O_RDONLY = 1;
  75     private static final int O_RDWR =   2;
  76     private static final int O_SYNC =   4;
  77     private static final int O_DSYNC =  8;
  78 
  79     /**
  80      * Creates a random access file stream to read from, and optionally
  81      * to write to, a file with the specified name. A new
  82      * {@link FileDescriptor} object is created to represent the
  83      * connection to the file.
  84      *
  85      * <p> The <tt>mode</tt> argument specifies the access mode with which the
  86      * file is to be opened.  The permitted values and their meanings are as
  87      * specified for the <a
  88      * href="#mode"><tt>RandomAccessFile(File,String)</tt></a> constructor.
  89      *
  90      * <p>
  91      * If there is a security manager, its {@code checkRead} method
  92      * is called with the {@code name} argument
  93      * as its argument to see if read access to the file is allowed.
  94      * If the mode allows writing, the security manager's
  95      * {@code checkWrite} method
  96      * is also called with the {@code name} argument
  97      * as its argument to see if write access to the file is allowed.
  98      *
  99      * @param      name   the system-dependent filename
 100      * @param      mode   the access <a href="#mode">mode</a>
 101      * @exception  IllegalArgumentException  if the mode argument is not equal
 102      *               to one of <tt>"r"</tt>, <tt>"rw"</tt>, <tt>"rws"</tt>, or
 103      *               <tt>"rwd"</tt>
 104      * @exception FileNotFoundException
 105      *            if the mode is <tt>"r"</tt> but the given string does not
 106      *            denote an existing regular file, or if the mode begins with
 107      *            <tt>"rw"</tt> but the given string does not denote an
 108      *            existing, writable regular file and a new regular file of
 109      *            that name cannot be created, or if some other error occurs
 110      *            while opening or creating the file
 111      * @exception  SecurityException         if a security manager exists and its
 112      *               {@code checkRead} method denies read access to the file
 113      *               or the mode is "rw" and the security manager's
 114      *               {@code checkWrite} method denies write access to the file
 115      * @see        java.lang.SecurityException
 116      * @see        java.lang.SecurityManager#checkRead(java.lang.String)
 117      * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
 118      * @revised 1.4
 119      * @spec JSR-51
 120      */
 121     public RandomAccessFile(String name, String mode)
 122         throws FileNotFoundException
 123     {
 124         this(name != null ? new File(name) : null, mode);
 125     }
 126 
 127     /**
 128      * Creates a random access file stream to read from, and optionally to
 129      * write to, the file specified by the {@link File} argument.  A new {@link
 130      * FileDescriptor} object is created to represent this file connection.
 131      *
 132      * <p>The <a name="mode"><tt>mode</tt></a> argument specifies the access mode
 133      * in which the file is to be opened.  The permitted values and their
 134      * meanings are:
 135      *
 136      * <table summary="Access mode permitted values and meanings">
 137      * <tr><th align="left">Value</th><th align="left">Meaning</th></tr>
 138      * <tr><td valign="top"><tt>"r"</tt></td>
 139      *     <td> Open for reading only.  Invoking any of the <tt>write</tt>
 140      *     methods of the resulting object will cause an {@link
 141      *     java.io.IOException} to be thrown. </td></tr>
 142      * <tr><td valign="top"><tt>"rw"</tt></td>
 143      *     <td> Open for reading and writing.  If the file does not already
 144      *     exist then an attempt will be made to create it. </td></tr>
 145      * <tr><td valign="top"><tt>"rws"</tt></td>
 146      *     <td> Open for reading and writing, as with <tt>"rw"</tt>, and also
 147      *     require that every update to the file's content or metadata be
 148      *     written synchronously to the underlying storage device.  </td></tr>
 149      * <tr><td valign="top"><tt>"rwd"&nbsp;&nbsp;</tt></td>
 150      *     <td> Open for reading and writing, as with <tt>"rw"</tt>, and also
 151      *     require that every update to the file's content be written
 152      *     synchronously to the underlying storage device. </td></tr>
 153      * </table>
 154      *
 155      * The <tt>"rws"</tt> and <tt>"rwd"</tt> modes work much like the {@link
 156      * java.nio.channels.FileChannel#force(boolean) force(boolean)} method of
 157      * the {@link java.nio.channels.FileChannel} class, passing arguments of
 158      * <tt>true</tt> and <tt>false</tt>, respectively, except that they always
 159      * apply to every I/O operation and are therefore often more efficient.  If
 160      * the file resides on a local storage device then when an invocation of a
 161      * method of this class returns it is guaranteed that all changes made to
 162      * the file by that invocation will have been written to that device.  This
 163      * is useful for ensuring that critical information is not lost in the
 164      * event of a system crash.  If the file does not reside on a local device
 165      * then no such guarantee is made.
 166      *
 167      * <p>The <tt>"rwd"</tt> mode can be used to reduce the number of I/O
 168      * operations performed.  Using <tt>"rwd"</tt> only requires updates to the
 169      * file's content to be written to storage; using <tt>"rws"</tt> requires
 170      * updates to both the file's content and its metadata to be written, which
 171      * generally requires at least one more low-level I/O operation.
 172      *
 173      * <p>If there is a security manager, its {@code checkRead} method is
 174      * called with the pathname of the {@code file} argument as its
 175      * argument to see if read access to the file is allowed.  If the mode
 176      * allows writing, the security manager's {@code checkWrite} method is
 177      * also called with the path argument to see if write access to the file is
 178      * allowed.
 179      *
 180      * @param      file   the file object
 181      * @param      mode   the access mode, as described
 182      *                    <a href="#mode">above</a>
 183      * @exception  IllegalArgumentException  if the mode argument is not equal
 184      *               to one of <tt>"r"</tt>, <tt>"rw"</tt>, <tt>"rws"</tt>, or
 185      *               <tt>"rwd"</tt>
 186      * @exception FileNotFoundException
 187      *            if the mode is <tt>"r"</tt> but the given file object does
 188      *            not denote an existing regular file, or if the mode begins
 189      *            with <tt>"rw"</tt> but the given file object does not denote
 190      *            an existing, writable regular file and a new regular file of
 191      *            that name cannot be created, or if some other error occurs
 192      *            while opening or creating the file
 193      * @exception  SecurityException         if a security manager exists and its
 194      *               {@code checkRead} method denies read access to the file
 195      *               or the mode is "rw" and the security manager's
 196      *               {@code checkWrite} method denies write access to the file
 197      * @see        java.lang.SecurityManager#checkRead(java.lang.String)
 198      * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
 199      * @see        java.nio.channels.FileChannel#force(boolean)
 200      * @revised 1.4
 201      * @spec JSR-51
 202      */
 203     public RandomAccessFile(File file, String mode)
 204         throws FileNotFoundException
 205     {
 206         String name = (file != null ? file.getPath() : null);
 207         int imode = -1;
 208         if (mode.equals("r"))
 209             imode = O_RDONLY;
 210         else if (mode.startsWith("rw")) {
 211             imode = O_RDWR;
 212             rw = true;
 213             if (mode.length() > 2) {
 214                 if (mode.equals("rws"))
 215                     imode |= O_SYNC;
 216                 else if (mode.equals("rwd"))
 217                     imode |= O_DSYNC;
 218                 else
 219                     imode = -1;
 220             }
 221         }
 222         if (imode < 0)
 223             throw new IllegalArgumentException("Illegal mode \"" + mode
 224                                                + "\" must be one of "
 225                                                + "\"r\", \"rw\", \"rws\","
 226                                                + " or \"rwd\"");
 227         SecurityManager security = System.getSecurityManager();
 228         if (security != null) {
 229             security.checkRead(name);
 230             if (rw) {
 231                 security.checkWrite(name);
 232             }
 233         }
 234         if (name == null) {
 235             throw new NullPointerException();
 236         }
 237         if (file.isInvalid()) {
 238             throw new FileNotFoundException("Invalid file path");
 239         }
 240         fd = new FileDescriptor();
 241         fd.attach(this);
 242         path = name;
 243         open(name, imode);
 244     }
 245 
 246     /**
 247      * Returns the opaque file descriptor object associated with this
 248      * stream.
 249      *
 250      * @return     the file descriptor object associated with this stream.
 251      * @exception  IOException  if an I/O error occurs.
 252      * @see        java.io.FileDescriptor
 253      */
 254     public final FileDescriptor getFD() throws IOException {
 255         if (fd != null) {
 256             return fd;
 257         }
 258         throw new IOException();
 259     }
 260 
 261     /**
 262      * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
 263      * object associated with this file.
 264      *
 265      * <p> The {@link java.nio.channels.FileChannel#position()
 266      * position} of the returned channel will always be equal to
 267      * this object's file-pointer offset as returned by the {@link
 268      * #getFilePointer getFilePointer} method.  Changing this object's
 269      * file-pointer offset, whether explicitly or by reading or writing bytes,
 270      * will change the position of the channel, and vice versa.  Changing the
 271      * file's length via this object will change the length seen via the file
 272      * channel, and vice versa.
 273      *
 274      * @return  the file channel associated with this file
 275      *
 276      * @since 1.4
 277      * @spec JSR-51
 278      */
 279     public final FileChannel getChannel() {
 280         synchronized (this) {
 281             if (channel == null) {
 282                 channel = FileChannelImpl.open(fd, path, true, rw, this);
 283             }
 284             return channel;
 285         }
 286     }
 287 
 288     /**
 289      * Opens a file and returns the file descriptor.  The file is
 290      * opened in read-write mode if the O_RDWR bit in {@code mode}
 291      * is true, else the file is opened as read-only.
 292      * If the {@code name} refers to a directory, an IOException
 293      * is thrown.
 294      *
 295      * @param name the name of the file
 296      * @param mode the mode flags, a combination of the O_ constants
 297      *             defined above
 298      */
 299     private native void open(String name, int mode)















 300         throws FileNotFoundException;
 301 
 302     // 'Read' primitives
 303 
 304     /**
 305      * Reads a byte of data from this file. The byte is returned as an
 306      * integer in the range 0 to 255 ({@code 0x00-0x0ff}). This
 307      * method blocks if no input is yet available.
 308      * <p>
 309      * Although {@code RandomAccessFile} is not a subclass of
 310      * {@code InputStream}, this method behaves in exactly the same
 311      * way as the {@link InputStream#read()} method of
 312      * {@code InputStream}.
 313      *
 314      * @return     the next byte of data, or {@code -1} if the end of the
 315      *             file has been reached.
 316      * @exception  IOException  if an I/O error occurs. Not thrown if
 317      *                          end-of-file has been reached.
 318      */
 319     public int read() throws IOException {
 320         return read0();
 321     }
 322 
 323     private native int read0() throws IOException;
 324 
 325     /**
 326      * Reads a sub array as a sequence of bytes.
 327      * @param b the buffer into which the data is read.
 328      * @param off the start offset of the data.
 329      * @param len the number of bytes to read.
 330      * @exception IOException If an I/O error has occurred.
 331      */
 332     private native int readBytes(byte b[], int off, int len) throws IOException;
 333 
 334     /**
 335      * Reads up to {@code len} bytes of data from this file into an
 336      * array of bytes. This method blocks until at least one byte of input
 337      * is available.
 338      * <p>
 339      * Although {@code RandomAccessFile} is not a subclass of
 340      * {@code InputStream}, this method behaves in exactly the
 341      * same way as the {@link InputStream#read(byte[], int, int)} method of
 342      * {@code InputStream}.
 343      *
 344      * @param      b     the buffer into which the data is read.
 345      * @param      off   the start offset in array {@code b}
 346      *                   at which the data is written.
 347      * @param      len   the maximum number of bytes read.
 348      * @return     the total number of bytes read into the buffer, or
 349      *             {@code -1} if there is no more data because the end of
 350      *             the file has been reached.
 351      * @exception  IOException If the first byte cannot be read for any reason
 352      * other than end of file, or if the random access file has been closed, or if
 353      * some other I/O error occurs.
 354      * @exception  NullPointerException If {@code b} is {@code null}.
 355      * @exception  IndexOutOfBoundsException If {@code off} is negative,
 356      * {@code len} is negative, or {@code len} is greater than
 357      * {@code b.length - off}
 358      */
 359     public int read(byte b[], int off, int len) throws IOException {
 360         return readBytes(b, off, len);
 361     }
 362 
 363     /**
 364      * Reads up to {@code b.length} bytes of data from this file
 365      * into an array of bytes. This method blocks until at least one byte
 366      * of input is available.
 367      * <p>
 368      * Although {@code RandomAccessFile} is not a subclass of
 369      * {@code InputStream}, this method behaves in exactly the
 370      * same way as the {@link InputStream#read(byte[])} method of
 371      * {@code InputStream}.
 372      *
 373      * @param      b   the buffer into which the data is read.
 374      * @return     the total number of bytes read into the buffer, or
 375      *             {@code -1} if there is no more data because the end of
 376      *             this file has been reached.
 377      * @exception  IOException If the first byte cannot be read for any reason
 378      * other than end of file, or if the random access file has been closed, or if
 379      * some other I/O error occurs.
 380      * @exception  NullPointerException If {@code b} is {@code null}.
 381      */
 382     public int read(byte b[]) throws IOException {
 383         return readBytes(b, 0, b.length);
 384     }
 385 
 386     /**
 387      * Reads {@code b.length} bytes from this file into the byte
 388      * array, starting at the current file pointer. This method reads
 389      * repeatedly from the file until the requested number of bytes are
 390      * read. This method blocks until the requested number of bytes are
 391      * read, the end of the stream is detected, or an exception is thrown.
 392      *
 393      * @param      b   the buffer into which the data is read.
 394      * @exception  EOFException  if this file reaches the end before reading
 395      *               all the bytes.
 396      * @exception  IOException   if an I/O error occurs.
 397      */
 398     public final void readFully(byte b[]) throws IOException {
 399         readFully(b, 0, b.length);
 400     }
 401 
 402     /**
 403      * Reads exactly {@code len} bytes from this file into the byte
 404      * array, starting at the current file pointer. This method reads
 405      * repeatedly from the file until the requested number of bytes are
 406      * read. This method blocks until the requested number of bytes are
 407      * read, the end of the stream is detected, or an exception is thrown.
 408      *
 409      * @param      b     the buffer into which the data is read.
 410      * @param      off   the start offset of the data.
 411      * @param      len   the number of bytes to read.
 412      * @exception  EOFException  if this file reaches the end before reading
 413      *               all the bytes.
 414      * @exception  IOException   if an I/O error occurs.
 415      */
 416     public final void readFully(byte b[], int off, int len) throws IOException {
 417         int n = 0;
 418         do {
 419             int count = this.read(b, off + n, len - n);
 420             if (count < 0)
 421                 throw new EOFException();
 422             n += count;
 423         } while (n < len);
 424     }
 425 
 426     /**
 427      * Attempts to skip over {@code n} bytes of input discarding the
 428      * skipped bytes.
 429      * <p>
 430      *
 431      * This method may skip over some smaller number of bytes, possibly zero.
 432      * This may result from any of a number of conditions; reaching end of
 433      * file before {@code n} bytes have been skipped is only one
 434      * possibility. This method never throws an {@code EOFException}.
 435      * The actual number of bytes skipped is returned.  If {@code n}
 436      * is negative, no bytes are skipped.
 437      *
 438      * @param      n   the number of bytes to be skipped.
 439      * @return     the actual number of bytes skipped.
 440      * @exception  IOException  if an I/O error occurs.
 441      */
 442     public int skipBytes(int n) throws IOException {
 443         long pos;
 444         long len;
 445         long newpos;
 446 
 447         if (n <= 0) {
 448             return 0;
 449         }
 450         pos = getFilePointer();
 451         len = length();
 452         newpos = pos + n;
 453         if (newpos > len) {
 454             newpos = len;
 455         }
 456         seek(newpos);
 457 
 458         /* return the actual number of bytes skipped */
 459         return (int) (newpos - pos);
 460     }
 461 
 462     // 'Write' primitives
 463 
 464     /**
 465      * Writes the specified byte to this file. The write starts at
 466      * the current file pointer.
 467      *
 468      * @param      b   the {@code byte} to be written.
 469      * @exception  IOException  if an I/O error occurs.
 470      */
 471     public void write(int b) throws IOException {
 472         write0(b);
 473     }
 474 
 475     private native void write0(int b) throws IOException;
 476 
 477     /**
 478      * Writes a sub array as a sequence of bytes.
 479      * @param b the data to be written
 480 
 481      * @param off the start offset in the data
 482      * @param len the number of bytes that are written
 483      * @exception IOException If an I/O error has occurred.
 484      */
 485     private native void writeBytes(byte b[], int off, int len) throws IOException;
 486 
 487     /**
 488      * Writes {@code b.length} bytes from the specified byte array
 489      * to this file, starting at the current file pointer.
 490      *
 491      * @param      b   the data.
 492      * @exception  IOException  if an I/O error occurs.
 493      */
 494     public void write(byte b[]) throws IOException {
 495         writeBytes(b, 0, b.length);
 496     }
 497 
 498     /**
 499      * Writes {@code len} bytes from the specified byte array
 500      * starting at offset {@code off} to this file.
 501      *
 502      * @param      b     the data.
 503      * @param      off   the start offset in the data.
 504      * @param      len   the number of bytes to write.
 505      * @exception  IOException  if an I/O error occurs.
 506      */
 507     public void write(byte b[], int off, int len) throws IOException {
 508         writeBytes(b, off, len);
 509     }
 510 
 511     // 'Random access' stuff
 512 
 513     /**
 514      * Returns the current offset in this file.
 515      *
 516      * @return     the offset from the beginning of the file, in bytes,
 517      *             at which the next read or write occurs.
 518      * @exception  IOException  if an I/O error occurs.
 519      */
 520     public native long getFilePointer() throws IOException;
 521 
 522     /**
 523      * Sets the file-pointer offset, measured from the beginning of this
 524      * file, at which the next read or write occurs.  The offset may be
 525      * set beyond the end of the file. Setting the offset beyond the end
 526      * of the file does not change the file length.  The file length will
 527      * change only by writing after the offset has been set beyond the end
 528      * of the file.
 529      *
 530      * @param      pos   the offset position, measured in bytes from the
 531      *                   beginning of the file, at which to set the file
 532      *                   pointer.
 533      * @exception  IOException  if {@code pos} is less than
 534      *                          {@code 0} or if an I/O error occurs.
 535      */
 536     public void seek(long pos) throws IOException {
 537         if (pos < 0) {
 538             throw new IOException("Negative seek offset");
 539         } else {
 540             seek0(pos);
 541         }
 542     }
 543 
 544     private native void seek0(long pos) throws IOException;
 545 
 546     /**
 547      * Returns the length of this file.
 548      *
 549      * @return     the length of this file, measured in bytes.
 550      * @exception  IOException  if an I/O error occurs.
 551      */
 552     public native long length() throws IOException;
 553 
 554     /**
 555      * Sets the length of this file.
 556      *
 557      * <p> If the present length of the file as returned by the
 558      * {@code length} method is greater than the {@code newLength}
 559      * argument then the file will be truncated.  In this case, if the file
 560      * offset as returned by the {@code getFilePointer} method is greater
 561      * than {@code newLength} then after this method returns the offset
 562      * will be equal to {@code newLength}.
 563      *
 564      * <p> If the present length of the file as returned by the
 565      * {@code length} method is smaller than the {@code newLength}
 566      * argument then the file will be extended.  In this case, the contents of
 567      * the extended portion of the file are not defined.
 568      *
 569      * @param      newLength    The desired length of the file
 570      * @exception  IOException  If an I/O error occurs
 571      * @since      1.2
 572      */
 573     public native void setLength(long newLength) throws IOException;
 574 
 575     /**
 576      * Closes this random access file stream and releases any system
 577      * resources associated with the stream. A closed random access
 578      * file cannot perform input or output operations and cannot be
 579      * reopened.
 580      *
 581      * <p> If this file has an associated channel then the channel is closed
 582      * as well.
 583      *
 584      * @exception  IOException  if an I/O error occurs.
 585      *
 586      * @revised 1.4
 587      * @spec JSR-51
 588      */
 589     public void close() throws IOException {
 590         synchronized (closeLock) {
 591             if (closed) {
 592                 return;
 593             }
 594             closed = true;
 595         }
 596         if (channel != null) {
 597             channel.close();
 598         }
 599 
 600         fd.closeAll(new Closeable() {
 601             public void close() throws IOException {
 602                close0();
 603            }
 604         });
 605     }
 606 
 607     //
 608     //  Some "reading/writing Java data types" methods stolen from
 609     //  DataInputStream and DataOutputStream.
 610     //
 611 
 612     /**
 613      * Reads a {@code boolean} from this file. This method reads a
 614      * single byte from the file, starting at the current file pointer.
 615      * A value of {@code 0} represents
 616      * {@code false}. Any other value represents {@code true}.
 617      * This method blocks until the byte is read, the end of the stream
 618      * is detected, or an exception is thrown.
 619      *
 620      * @return     the {@code boolean} value read.
 621      * @exception  EOFException  if this file has reached the end.
 622      * @exception  IOException   if an I/O error occurs.
 623      */
 624     public final boolean readBoolean() throws IOException {
 625         int ch = this.read();
 626         if (ch < 0)
 627             throw new EOFException();
 628         return (ch != 0);
 629     }
 630 
 631     /**
 632      * Reads a signed eight-bit value from this file. This method reads a
 633      * byte from the file, starting from the current file pointer.
 634      * If the byte read is {@code b}, where
 635      * <code>0&nbsp;&lt;=&nbsp;b&nbsp;&lt;=&nbsp;255</code>,
 636      * then the result is:
 637      * <blockquote><pre>
 638      *     (byte)(b)
 639      * </pre></blockquote>
 640      * <p>
 641      * This method blocks until the byte is read, the end of the stream
 642      * is detected, or an exception is thrown.
 643      *
 644      * @return     the next byte of this file as a signed eight-bit
 645      *             {@code byte}.
 646      * @exception  EOFException  if this file has reached the end.
 647      * @exception  IOException   if an I/O error occurs.
 648      */
 649     public final byte readByte() throws IOException {
 650         int ch = this.read();
 651         if (ch < 0)
 652             throw new EOFException();
 653         return (byte)(ch);
 654     }
 655 
 656     /**
 657      * Reads an unsigned eight-bit number from this file. This method reads
 658      * a byte from this file, starting at the current file pointer,
 659      * and returns that byte.
 660      * <p>
 661      * This method blocks until the byte is read, the end of the stream
 662      * is detected, or an exception is thrown.
 663      *
 664      * @return     the next byte of this file, interpreted as an unsigned
 665      *             eight-bit number.
 666      * @exception  EOFException  if this file has reached the end.
 667      * @exception  IOException   if an I/O error occurs.
 668      */
 669     public final int readUnsignedByte() throws IOException {
 670         int ch = this.read();
 671         if (ch < 0)
 672             throw new EOFException();
 673         return ch;
 674     }
 675 
 676     /**
 677      * Reads a signed 16-bit number from this file. The method reads two
 678      * bytes from this file, starting at the current file pointer.
 679      * If the two bytes read, in order, are
 680      * {@code b1} and {@code b2}, where each of the two values is
 681      * between {@code 0} and {@code 255}, inclusive, then the
 682      * result is equal to:
 683      * <blockquote><pre>
 684      *     (short)((b1 &lt;&lt; 8) | b2)
 685      * </pre></blockquote>
 686      * <p>
 687      * This method blocks until the two bytes are read, the end of the
 688      * stream is detected, or an exception is thrown.
 689      *
 690      * @return     the next two bytes of this file, interpreted as a signed
 691      *             16-bit number.
 692      * @exception  EOFException  if this file reaches the end before reading
 693      *               two bytes.
 694      * @exception  IOException   if an I/O error occurs.
 695      */
 696     public final short readShort() throws IOException {
 697         int ch1 = this.read();
 698         int ch2 = this.read();
 699         if ((ch1 | ch2) < 0)
 700             throw new EOFException();
 701         return (short)((ch1 << 8) + (ch2 << 0));
 702     }
 703 
 704     /**
 705      * Reads an unsigned 16-bit number from this file. This method reads
 706      * two bytes from the file, starting at the current file pointer.
 707      * If the bytes read, in order, are
 708      * {@code b1} and {@code b2}, where
 709      * <code>0&nbsp;&lt;=&nbsp;b1, b2&nbsp;&lt;=&nbsp;255</code>,
 710      * then the result is equal to:
 711      * <blockquote><pre>
 712      *     (b1 &lt;&lt; 8) | b2
 713      * </pre></blockquote>
 714      * <p>
 715      * This method blocks until the two bytes are read, the end of the
 716      * stream is detected, or an exception is thrown.
 717      *
 718      * @return     the next two bytes of this file, interpreted as an unsigned
 719      *             16-bit integer.
 720      * @exception  EOFException  if this file reaches the end before reading
 721      *               two bytes.
 722      * @exception  IOException   if an I/O error occurs.
 723      */
 724     public final int readUnsignedShort() throws IOException {
 725         int ch1 = this.read();
 726         int ch2 = this.read();
 727         if ((ch1 | ch2) < 0)
 728             throw new EOFException();
 729         return (ch1 << 8) + (ch2 << 0);
 730     }
 731 
 732     /**
 733      * Reads a character from this file. This method reads two
 734      * bytes from the file, starting at the current file pointer.
 735      * If the bytes read, in order, are
 736      * {@code b1} and {@code b2}, where
 737      * <code>0&nbsp;&lt;=&nbsp;b1,&nbsp;b2&nbsp;&lt;=&nbsp;255</code>,
 738      * then the result is equal to:
 739      * <blockquote><pre>
 740      *     (char)((b1 &lt;&lt; 8) | b2)
 741      * </pre></blockquote>
 742      * <p>
 743      * This method blocks until the two bytes are read, the end of the
 744      * stream is detected, or an exception is thrown.
 745      *
 746      * @return     the next two bytes of this file, interpreted as a
 747      *                  {@code char}.
 748      * @exception  EOFException  if this file reaches the end before reading
 749      *               two bytes.
 750      * @exception  IOException   if an I/O error occurs.
 751      */
 752     public final char readChar() throws IOException {
 753         int ch1 = this.read();
 754         int ch2 = this.read();
 755         if ((ch1 | ch2) < 0)
 756             throw new EOFException();
 757         return (char)((ch1 << 8) + (ch2 << 0));
 758     }
 759 
 760     /**
 761      * Reads a signed 32-bit integer from this file. This method reads 4
 762      * bytes from the file, starting at the current file pointer.
 763      * If the bytes read, in order, are {@code b1},
 764      * {@code b2}, {@code b3}, and {@code b4}, where
 765      * <code>0&nbsp;&lt;=&nbsp;b1, b2, b3, b4&nbsp;&lt;=&nbsp;255</code>,
 766      * then the result is equal to:
 767      * <blockquote><pre>
 768      *     (b1 &lt;&lt; 24) | (b2 &lt;&lt; 16) + (b3 &lt;&lt; 8) + b4
 769      * </pre></blockquote>
 770      * <p>
 771      * This method blocks until the four bytes are read, the end of the
 772      * stream is detected, or an exception is thrown.
 773      *
 774      * @return     the next four bytes of this file, interpreted as an
 775      *             {@code int}.
 776      * @exception  EOFException  if this file reaches the end before reading
 777      *               four bytes.
 778      * @exception  IOException   if an I/O error occurs.
 779      */
 780     public final int readInt() throws IOException {
 781         int ch1 = this.read();
 782         int ch2 = this.read();
 783         int ch3 = this.read();
 784         int ch4 = this.read();
 785         if ((ch1 | ch2 | ch3 | ch4) < 0)
 786             throw new EOFException();
 787         return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
 788     }
 789 
 790     /**
 791      * Reads a signed 64-bit integer from this file. This method reads eight
 792      * bytes from the file, starting at the current file pointer.
 793      * If the bytes read, in order, are
 794      * {@code b1}, {@code b2}, {@code b3},
 795      * {@code b4}, {@code b5}, {@code b6},
 796      * {@code b7}, and {@code b8,} where:
 797      * <blockquote><pre>
 798      *     0 &lt;= b1, b2, b3, b4, b5, b6, b7, b8 &lt;=255,
 799      * </pre></blockquote>
 800      * <p>
 801      * then the result is equal to:
 802      * <blockquote><pre>
 803      *     ((long)b1 &lt;&lt; 56) + ((long)b2 &lt;&lt; 48)
 804      *     + ((long)b3 &lt;&lt; 40) + ((long)b4 &lt;&lt; 32)
 805      *     + ((long)b5 &lt;&lt; 24) + ((long)b6 &lt;&lt; 16)
 806      *     + ((long)b7 &lt;&lt; 8) + b8
 807      * </pre></blockquote>
 808      * <p>
 809      * This method blocks until the eight bytes are read, the end of the
 810      * stream is detected, or an exception is thrown.
 811      *
 812      * @return     the next eight bytes of this file, interpreted as a
 813      *             {@code long}.
 814      * @exception  EOFException  if this file reaches the end before reading
 815      *               eight bytes.
 816      * @exception  IOException   if an I/O error occurs.
 817      */
 818     public final long readLong() throws IOException {
 819         return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL);
 820     }
 821 
 822     /**
 823      * Reads a {@code float} from this file. This method reads an
 824      * {@code int} value, starting at the current file pointer,
 825      * as if by the {@code readInt} method
 826      * and then converts that {@code int} to a {@code float}
 827      * using the {@code intBitsToFloat} method in class
 828      * {@code Float}.
 829      * <p>
 830      * This method blocks until the four bytes are read, the end of the
 831      * stream is detected, or an exception is thrown.
 832      *
 833      * @return     the next four bytes of this file, interpreted as a
 834      *             {@code float}.
 835      * @exception  EOFException  if this file reaches the end before reading
 836      *             four bytes.
 837      * @exception  IOException   if an I/O error occurs.
 838      * @see        java.io.RandomAccessFile#readInt()
 839      * @see        java.lang.Float#intBitsToFloat(int)
 840      */
 841     public final float readFloat() throws IOException {
 842         return Float.intBitsToFloat(readInt());
 843     }
 844 
 845     /**
 846      * Reads a {@code double} from this file. This method reads a
 847      * {@code long} value, starting at the current file pointer,
 848      * as if by the {@code readLong} method
 849      * and then converts that {@code long} to a {@code double}
 850      * using the {@code longBitsToDouble} method in
 851      * class {@code Double}.
 852      * <p>
 853      * This method blocks until the eight bytes are read, the end of the
 854      * stream is detected, or an exception is thrown.
 855      *
 856      * @return     the next eight bytes of this file, interpreted as a
 857      *             {@code double}.
 858      * @exception  EOFException  if this file reaches the end before reading
 859      *             eight bytes.
 860      * @exception  IOException   if an I/O error occurs.
 861      * @see        java.io.RandomAccessFile#readLong()
 862      * @see        java.lang.Double#longBitsToDouble(long)
 863      */
 864     public final double readDouble() throws IOException {
 865         return Double.longBitsToDouble(readLong());
 866     }
 867 
 868     /**
 869      * Reads the next line of text from this file.  This method successively
 870      * reads bytes from the file, starting at the current file pointer,
 871      * until it reaches a line terminator or the end
 872      * of the file.  Each byte is converted into a character by taking the
 873      * byte's value for the lower eight bits of the character and setting the
 874      * high eight bits of the character to zero.  This method does not,
 875      * therefore, support the full Unicode character set.
 876      *
 877      * <p> A line of text is terminated by a carriage-return character
 878      * ({@code '\u005Cr'}), a newline character ({@code '\u005Cn'}), a
 879      * carriage-return character immediately followed by a newline character,
 880      * or the end of the file.  Line-terminating characters are discarded and
 881      * are not included as part of the string returned.
 882      *
 883      * <p> This method blocks until a newline character is read, a carriage
 884      * return and the byte following it are read (to see if it is a newline),
 885      * the end of the file is reached, or an exception is thrown.
 886      *
 887      * @return     the next line of text from this file, or null if end
 888      *             of file is encountered before even one byte is read.
 889      * @exception  IOException  if an I/O error occurs.
 890      */
 891 
 892     public final String readLine() throws IOException {
 893         StringBuilder input = new StringBuilder();
 894         int c = -1;
 895         boolean eol = false;
 896 
 897         while (!eol) {
 898             switch (c = read()) {
 899             case -1:
 900             case '\n':
 901                 eol = true;
 902                 break;
 903             case '\r':
 904                 eol = true;
 905                 long cur = getFilePointer();
 906                 if ((read()) != '\n') {
 907                     seek(cur);
 908                 }
 909                 break;
 910             default:
 911                 input.append((char)c);
 912                 break;
 913             }
 914         }
 915 
 916         if ((c == -1) && (input.length() == 0)) {
 917             return null;
 918         }
 919         return input.toString();
 920     }
 921 
 922     /**
 923      * Reads in a string from this file. The string has been encoded
 924      * using a
 925      * <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
 926      * format.
 927      * <p>
 928      * The first two bytes are read, starting from the current file
 929      * pointer, as if by
 930      * {@code readUnsignedShort}. This value gives the number of
 931      * following bytes that are in the encoded string, not
 932      * the length of the resulting string. The following bytes are then
 933      * interpreted as bytes encoding characters in the modified UTF-8 format
 934      * and are converted into characters.
 935      * <p>
 936      * This method blocks until all the bytes are read, the end of the
 937      * stream is detected, or an exception is thrown.
 938      *
 939      * @return     a Unicode string.
 940      * @exception  EOFException            if this file reaches the end before
 941      *               reading all the bytes.
 942      * @exception  IOException             if an I/O error occurs.
 943      * @exception  UTFDataFormatException  if the bytes do not represent
 944      *               valid modified UTF-8 encoding of a Unicode string.
 945      * @see        java.io.RandomAccessFile#readUnsignedShort()
 946      */
 947     public final String readUTF() throws IOException {
 948         return DataInputStream.readUTF(this);
 949     }
 950 
 951     /**
 952      * Writes a {@code boolean} to the file as a one-byte value. The
 953      * value {@code true} is written out as the value
 954      * {@code (byte)1}; the value {@code false} is written out
 955      * as the value {@code (byte)0}. The write starts at
 956      * the current position of the file pointer.
 957      *
 958      * @param      v   a {@code boolean} value to be written.
 959      * @exception  IOException  if an I/O error occurs.
 960      */
 961     public final void writeBoolean(boolean v) throws IOException {
 962         write(v ? 1 : 0);
 963         //written++;
 964     }
 965 
 966     /**
 967      * Writes a {@code byte} to the file as a one-byte value. The
 968      * write starts at the current position of the file pointer.
 969      *
 970      * @param      v   a {@code byte} value to be written.
 971      * @exception  IOException  if an I/O error occurs.
 972      */
 973     public final void writeByte(int v) throws IOException {
 974         write(v);
 975         //written++;
 976     }
 977 
 978     /**
 979      * Writes a {@code short} to the file as two bytes, high byte first.
 980      * The write starts at the current position of the file pointer.
 981      *
 982      * @param      v   a {@code short} to be written.
 983      * @exception  IOException  if an I/O error occurs.
 984      */
 985     public final void writeShort(int v) throws IOException {
 986         write((v >>> 8) & 0xFF);
 987         write((v >>> 0) & 0xFF);
 988         //written += 2;
 989     }
 990 
 991     /**
 992      * Writes a {@code char} to the file as a two-byte value, high
 993      * byte first. The write starts at the current position of the
 994      * file pointer.
 995      *
 996      * @param      v   a {@code char} value to be written.
 997      * @exception  IOException  if an I/O error occurs.
 998      */
 999     public final void writeChar(int v) throws IOException {
1000         write((v >>> 8) & 0xFF);
1001         write((v >>> 0) & 0xFF);
1002         //written += 2;
1003     }
1004 
1005     /**
1006      * Writes an {@code int} to the file as four bytes, high byte first.
1007      * The write starts at the current position of the file pointer.
1008      *
1009      * @param      v   an {@code int} to be written.
1010      * @exception  IOException  if an I/O error occurs.
1011      */
1012     public final void writeInt(int v) throws IOException {
1013         write((v >>> 24) & 0xFF);
1014         write((v >>> 16) & 0xFF);
1015         write((v >>>  8) & 0xFF);
1016         write((v >>>  0) & 0xFF);
1017         //written += 4;
1018     }
1019 
1020     /**
1021      * Writes a {@code long} to the file as eight bytes, high byte first.
1022      * The write starts at the current position of the file pointer.
1023      *
1024      * @param      v   a {@code long} to be written.
1025      * @exception  IOException  if an I/O error occurs.
1026      */
1027     public final void writeLong(long v) throws IOException {
1028         write((int)(v >>> 56) & 0xFF);
1029         write((int)(v >>> 48) & 0xFF);
1030         write((int)(v >>> 40) & 0xFF);
1031         write((int)(v >>> 32) & 0xFF);
1032         write((int)(v >>> 24) & 0xFF);
1033         write((int)(v >>> 16) & 0xFF);
1034         write((int)(v >>>  8) & 0xFF);
1035         write((int)(v >>>  0) & 0xFF);
1036         //written += 8;
1037     }
1038 
1039     /**
1040      * Converts the float argument to an {@code int} using the
1041      * {@code floatToIntBits} method in class {@code Float},
1042      * and then writes that {@code int} value to the file as a
1043      * four-byte quantity, high byte first. The write starts at the
1044      * current position of the file pointer.
1045      *
1046      * @param      v   a {@code float} value to be written.
1047      * @exception  IOException  if an I/O error occurs.
1048      * @see        java.lang.Float#floatToIntBits(float)
1049      */
1050     public final void writeFloat(float v) throws IOException {
1051         writeInt(Float.floatToIntBits(v));
1052     }
1053 
1054     /**
1055      * Converts the double argument to a {@code long} using the
1056      * {@code doubleToLongBits} method in class {@code Double},
1057      * and then writes that {@code long} value to the file as an
1058      * eight-byte quantity, high byte first. The write starts at the current
1059      * position of the file pointer.
1060      *
1061      * @param      v   a {@code double} value to be written.
1062      * @exception  IOException  if an I/O error occurs.
1063      * @see        java.lang.Double#doubleToLongBits(double)
1064      */
1065     public final void writeDouble(double v) throws IOException {
1066         writeLong(Double.doubleToLongBits(v));
1067     }
1068 
1069     /**
1070      * Writes the string to the file as a sequence of bytes. Each
1071      * character in the string is written out, in sequence, by discarding
1072      * its high eight bits. The write starts at the current position of
1073      * the file pointer.
1074      *
1075      * @param      s   a string of bytes to be written.
1076      * @exception  IOException  if an I/O error occurs.
1077      */
1078     @SuppressWarnings("deprecation")
1079     public final void writeBytes(String s) throws IOException {
1080         int len = s.length();
1081         byte[] b = new byte[len];
1082         s.getBytes(0, len, b, 0);
1083         writeBytes(b, 0, len);
1084     }
1085 
1086     /**
1087      * Writes a string to the file as a sequence of characters. Each
1088      * character is written to the data output stream as if by the
1089      * {@code writeChar} method. The write starts at the current
1090      * position of the file pointer.
1091      *
1092      * @param      s   a {@code String} value to be written.
1093      * @exception  IOException  if an I/O error occurs.
1094      * @see        java.io.RandomAccessFile#writeChar(int)
1095      */
1096     public final void writeChars(String s) throws IOException {
1097         int clen = s.length();
1098         int blen = 2*clen;
1099         byte[] b = new byte[blen];
1100         char[] c = new char[clen];
1101         s.getChars(0, clen, c, 0);
1102         for (int i = 0, j = 0; i < clen; i++) {
1103             b[j++] = (byte)(c[i] >>> 8);
1104             b[j++] = (byte)(c[i] >>> 0);
1105         }
1106         writeBytes(b, 0, blen);
1107     }
1108 
1109     /**
1110      * Writes a string to the file using
1111      * <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
1112      * encoding in a machine-independent manner.
1113      * <p>
1114      * First, two bytes are written to the file, starting at the
1115      * current file pointer, as if by the
1116      * {@code writeShort} method giving the number of bytes to
1117      * follow. This value is the number of bytes actually written out,
1118      * not the length of the string. Following the length, each character
1119      * of the string is output, in sequence, using the modified UTF-8 encoding
1120      * for each character.
1121      *
1122      * @param      str   a string to be written.
1123      * @exception  IOException  if an I/O error occurs.
1124      */
1125     public final void writeUTF(String str) throws IOException {
1126         DataOutputStream.writeUTF(str, this);
1127     }
1128 
1129     private static native void initIDs();
1130 
1131     private native void close0() throws IOException;
1132 
1133     static {
1134         initIDs();
1135     }
1136 }
--- EOF ---