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

Print this page




 212                 else
 213                     imode = -1;
 214             }
 215         }
 216         if (imode < 0)
 217             throw new IllegalArgumentException("Illegal mode \"" + mode
 218                                                + "\" must be one of "
 219                                                + "\"r\", \"rw\", \"rws\","
 220                                                + " or \"rwd\"");
 221         SecurityManager security = System.getSecurityManager();
 222         if (security != null) {
 223             security.checkRead(name);
 224             if (rw) {
 225                 security.checkWrite(name);
 226             }
 227         }
 228         if (name == null) {
 229             throw new NullPointerException();
 230         }
 231         fd = new FileDescriptor();
 232         fd.attach(this);
 233         open(name, imode);
 234     }
 235 
 236     /**
 237      * Returns the opaque file descriptor object associated with this
 238      * stream. </p>
 239      *
 240      * @return     the file descriptor object associated with this stream.
 241      * @exception  IOException  if an I/O error occurs.
 242      * @see        java.io.FileDescriptor
 243      */
 244     public final FileDescriptor getFD() throws IOException {
 245         if (fd != null) {
 246             return fd;
 247         }
 248         throw new IOException();
 249     }
 250 
 251     /**
 252      * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
 253      * object associated with this file.
 254      *
 255      * <p> The {@link java.nio.channels.FileChannel#position()
 256      * </code>position<code>} of the returned channel will always be equal to
 257      * this object's file-pointer offset as returned by the {@link
 258      * #getFilePointer getFilePointer} method.  Changing this object's
 259      * file-pointer offset, whether explicitly or by reading or writing bytes,
 260      * will change the position of the channel, and vice versa.  Changing the
 261      * file's length via this object will change the length seen via the file
 262      * channel, and vice versa.
 263      *
 264      * @return  the file channel associated with this file
 265      *
 266      * @since 1.4
 267      * @spec JSR-51
 268      */
 269     public final FileChannel getChannel() {
 270         synchronized (this) {
 271             if (channel == null) {
 272                 channel = FileChannelImpl.open(fd, true, rw, this);











 273             }
 274             return channel;
 275         }
 276     }
 277 
 278     /**
 279      * Opens a file and returns the file descriptor.  The file is
 280      * opened in read-write mode if the O_RDWR bit in <code>mode</code>
 281      * is true, else the file is opened as read-only.
 282      * If the <code>name</code> refers to a directory, an IOException
 283      * is thrown.
 284      *
 285      * @param name the name of the file
 286      * @param mode the mode flags, a combination of the O_ constants
 287      *             defined above
 288      */
 289     private native void open(String name, int mode)
 290         throws FileNotFoundException;
 291 
 292     // 'Read' primitives


 551      * resources associated with the stream. A closed random access
 552      * file cannot perform input or output operations and cannot be
 553      * reopened.
 554      *
 555      * <p> If this file has an associated channel then the channel is closed
 556      * as well.
 557      *
 558      * @exception  IOException  if an I/O error occurs.
 559      *
 560      * @revised 1.4
 561      * @spec JSR-51
 562      */
 563     public void close() throws IOException {
 564         synchronized (closeLock) {
 565             if (closed) {
 566                 return;
 567             }
 568             closed = true;
 569         }
 570         if (channel != null) {






 571             channel.close();
 572         }
 573         fd.closeAll(new Closeable() {
 574             public void close() throws IOException {




 575                close0();
 576            }
 577         });
 578     }
 579 
 580     //
 581     //  Some "reading/writing Java data types" methods stolen from
 582     //  DataInputStream and DataOutputStream.
 583     //
 584 
 585     /**
 586      * Reads a <code>boolean</code> from this file. This method reads a
 587      * single byte from the file, starting at the current file pointer.
 588      * A value of <code>0</code> represents
 589      * <code>false</code>. Any other value represents <code>true</code>.
 590      * This method blocks until the byte is read, the end of the stream
 591      * is detected, or an exception is thrown.
 592      *
 593      * @return     the <code>boolean</code> value read.
 594      * @exception  EOFException  if this file has reached the end.
 595      * @exception  IOException   if an I/O error occurs.
 596      */
 597     public final boolean readBoolean() throws IOException {
 598         int ch = this.read();




 212                 else
 213                     imode = -1;
 214             }
 215         }
 216         if (imode < 0)
 217             throw new IllegalArgumentException("Illegal mode \"" + mode
 218                                                + "\" must be one of "
 219                                                + "\"r\", \"rw\", \"rws\","
 220                                                + " or \"rwd\"");
 221         SecurityManager security = System.getSecurityManager();
 222         if (security != null) {
 223             security.checkRead(name);
 224             if (rw) {
 225                 security.checkWrite(name);
 226             }
 227         }
 228         if (name == null) {
 229             throw new NullPointerException();
 230         }
 231         fd = new FileDescriptor();
 232         fd.incrementAndGetUseCount();
 233         open(name, imode);
 234     }
 235 
 236     /**
 237      * Returns the opaque file descriptor object associated with this
 238      * stream. </p>
 239      *
 240      * @return     the file descriptor object associated with this stream.
 241      * @exception  IOException  if an I/O error occurs.
 242      * @see        java.io.FileDescriptor
 243      */
 244     public final FileDescriptor getFD() throws IOException {
 245         if (fd != null) return fd;


 246         throw new IOException();
 247     }
 248 
 249     /**
 250      * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
 251      * object associated with this file.
 252      *
 253      * <p> The {@link java.nio.channels.FileChannel#position()
 254      * </code>position<code>} of the returned channel will always be equal to
 255      * this object's file-pointer offset as returned by the {@link
 256      * #getFilePointer getFilePointer} method.  Changing this object's
 257      * file-pointer offset, whether explicitly or by reading or writing bytes,
 258      * will change the position of the channel, and vice versa.  Changing the
 259      * file's length via this object will change the length seen via the file
 260      * channel, and vice versa.
 261      *
 262      * @return  the file channel associated with this file
 263      *
 264      * @since 1.4
 265      * @spec JSR-51
 266      */
 267     public final FileChannel getChannel() {
 268         synchronized (this) {
 269             if (channel == null) {
 270                 channel = FileChannelImpl.open(fd, true, rw, this);
 271 
 272                 /*
 273                  * FileDescriptor could be shared by FileInputStream or
 274                  * FileOutputStream.
 275                  * Ensure that FD is GC'ed only when all the streams/channels
 276                  * are done using it.
 277                  * Increment fd's use count. Invoking the channel's close()
 278                  * method will result in decrementing the use count set for
 279                  * the channel.
 280                  */
 281                 fd.incrementAndGetUseCount();
 282             }
 283             return channel;
 284         }
 285     }
 286 
 287     /**
 288      * Opens a file and returns the file descriptor.  The file is
 289      * opened in read-write mode if the O_RDWR bit in <code>mode</code>
 290      * is true, else the file is opened as read-only.
 291      * If the <code>name</code> refers to a directory, an IOException
 292      * is thrown.
 293      *
 294      * @param name the name of the file
 295      * @param mode the mode flags, a combination of the O_ constants
 296      *             defined above
 297      */
 298     private native void open(String name, int mode)
 299         throws FileNotFoundException;
 300 
 301     // 'Read' primitives


 560      * resources associated with the stream. A closed random access
 561      * file cannot perform input or output operations and cannot be
 562      * reopened.
 563      *
 564      * <p> If this file has an associated channel then the channel is closed
 565      * as well.
 566      *
 567      * @exception  IOException  if an I/O error occurs.
 568      *
 569      * @revised 1.4
 570      * @spec JSR-51
 571      */
 572     public void close() throws IOException {
 573         synchronized (closeLock) {
 574             if (closed) {
 575                 return;
 576             }
 577             closed = true;
 578         }
 579         if (channel != null) {
 580             /*
 581              * Decrement FD use count associated with the channel. The FD use
 582              * count is incremented whenever a new channel is obtained from
 583              * this stream.
 584              */
 585             fd.decrementAndGetUseCount();
 586             channel.close();
 587         }
 588 
 589         /*
 590          * Decrement FD use count associated with this stream.
 591          * The count got incremented by FileDescriptor during its construction.
 592          */
 593         fd.decrementAndGetUseCount();
 594         close0();
 595     }


 596 
 597     //
 598     //  Some "reading/writing Java data types" methods stolen from
 599     //  DataInputStream and DataOutputStream.
 600     //
 601 
 602     /**
 603      * Reads a <code>boolean</code> from this file. This method reads a
 604      * single byte from the file, starting at the current file pointer.
 605      * A value of <code>0</code> represents
 606      * <code>false</code>. Any other value represents <code>true</code>.
 607      * This method blocks until the byte is read, the end of the stream
 608      * is detected, or an exception is thrown.
 609      *
 610      * @return     the <code>boolean</code> value read.
 611      * @exception  EOFException  if this file has reached the end.
 612      * @exception  IOException   if an I/O error occurs.
 613      */
 614     public final boolean readBoolean() throws IOException {
 615         int ch = this.read();