0N/A/*
6092N/A * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.io;
0N/A
0N/Aimport java.nio.channels.FileChannel;
0N/Aimport sun.nio.ch.FileChannelImpl;
5503N/Aimport sun.misc.IoTrace;
0N/A
0N/A
0N/A/**
0N/A * Instances of this class support both reading and writing to a
0N/A * random access file. A random access file behaves like a large
0N/A * array of bytes stored in the file system. There is a kind of cursor,
0N/A * or index into the implied array, called the <em>file pointer</em>;
0N/A * input operations read bytes starting at the file pointer and advance
0N/A * the file pointer past the bytes read. If the random access file is
0N/A * created in read/write mode, then output operations are also available;
0N/A * output operations write bytes starting at the file pointer and advance
0N/A * the file pointer past the bytes written. Output operations that write
0N/A * past the current end of the implied array cause the array to be
0N/A * extended. The file pointer can be read by the
0N/A * <code>getFilePointer</code> method and set by the <code>seek</code>
0N/A * method.
0N/A * <p>
0N/A * It is generally true of all the reading routines in this class that
0N/A * if end-of-file is reached before the desired number of bytes has been
0N/A * read, an <code>EOFException</code> (which is a kind of
0N/A * <code>IOException</code>) is thrown. If any byte cannot be read for
0N/A * any reason other than end-of-file, an <code>IOException</code> other
0N/A * than <code>EOFException</code> is thrown. In particular, an
0N/A * <code>IOException</code> may be thrown if the stream has been closed.
0N/A *
0N/A * @author unascribed
0N/A * @since JDK1.0
0N/A */
0N/A
0N/Apublic class RandomAccessFile implements DataOutput, DataInput, Closeable {
0N/A
0N/A private FileDescriptor fd;
0N/A private FileChannel channel = null;
0N/A private boolean rw;
0N/A
5503N/A /* The path of the referenced file */
5503N/A private final String path;
5503N/A
0N/A private Object closeLock = new Object();
0N/A private volatile boolean closed = false;
0N/A
0N/A private static final int O_RDONLY = 1;
0N/A private static final int O_RDWR = 2;
0N/A private static final int O_SYNC = 4;
0N/A private static final int O_DSYNC = 8;
0N/A
0N/A /**
0N/A * Creates a random access file stream to read from, and optionally
0N/A * to write to, a file with the specified name. A new
0N/A * {@link FileDescriptor} object is created to represent the
0N/A * connection to the file.
0N/A *
0N/A * <p> The <tt>mode</tt> argument specifies the access mode with which the
0N/A * file is to be opened. The permitted values and their meanings are as
0N/A * specified for the <a
0N/A * href="#mode"><tt>RandomAccessFile(File,String)</tt></a> constructor.
0N/A *
0N/A * <p>
0N/A * If there is a security manager, its <code>checkRead</code> method
0N/A * is called with the <code>name</code> argument
0N/A * as its argument to see if read access to the file is allowed.
0N/A * If the mode allows writing, the security manager's
0N/A * <code>checkWrite</code> method
0N/A * is also called with the <code>name</code> argument
0N/A * as its argument to see if write access to the file is allowed.
0N/A *
0N/A * @param name the system-dependent filename
0N/A * @param mode the access <a href="#mode">mode</a>
0N/A * @exception IllegalArgumentException if the mode argument is not equal
0N/A * to one of <tt>"r"</tt>, <tt>"rw"</tt>, <tt>"rws"</tt>, or
0N/A * <tt>"rwd"</tt>
0N/A * @exception FileNotFoundException
0N/A * if the mode is <tt>"r"</tt> but the given string does not
0N/A * denote an existing regular file, or if the mode begins with
0N/A * <tt>"rw"</tt> but the given string does not denote an
0N/A * existing, writable regular file and a new regular file of
0N/A * that name cannot be created, or if some other error occurs
0N/A * while opening or creating the file
0N/A * @exception SecurityException if a security manager exists and its
0N/A * <code>checkRead</code> method denies read access to the file
0N/A * or the mode is "rw" and the security manager's
0N/A * <code>checkWrite</code> method denies write access to the file
0N/A * @see java.lang.SecurityException
0N/A * @see java.lang.SecurityManager#checkRead(java.lang.String)
0N/A * @see java.lang.SecurityManager#checkWrite(java.lang.String)
0N/A * @revised 1.4
0N/A * @spec JSR-51
0N/A */
0N/A public RandomAccessFile(String name, String mode)
0N/A throws FileNotFoundException
0N/A {
0N/A this(name != null ? new File(name) : null, mode);
0N/A }
0N/A
0N/A /**
0N/A * Creates a random access file stream to read from, and optionally to
0N/A * write to, the file specified by the {@link File} argument. A new {@link
0N/A * FileDescriptor} object is created to represent this file connection.
0N/A *
0N/A * <a name="mode"><p> The <tt>mode</tt> argument specifies the access mode
0N/A * in which the file is to be opened. The permitted values and their
0N/A * meanings are:
0N/A *
0N/A * <blockquote><table summary="Access mode permitted values and meanings">
0N/A * <tr><th><p align="left">Value</p></th><th><p align="left">Meaning</p></th></tr>
0N/A * <tr><td valign="top"><tt>"r"</tt></td>
0N/A * <td> Open for reading only. Invoking any of the <tt>write</tt>
0N/A * methods of the resulting object will cause an {@link
0N/A * java.io.IOException} to be thrown. </td></tr>
0N/A * <tr><td valign="top"><tt>"rw"</tt></td>
0N/A * <td> Open for reading and writing. If the file does not already
0N/A * exist then an attempt will be made to create it. </td></tr>
0N/A * <tr><td valign="top"><tt>"rws"</tt></td>
0N/A * <td> Open for reading and writing, as with <tt>"rw"</tt>, and also
0N/A * require that every update to the file's content or metadata be
0N/A * written synchronously to the underlying storage device. </td></tr>
0N/A * <tr><td valign="top"><tt>"rwd"&nbsp;&nbsp;</tt></td>
0N/A * <td> Open for reading and writing, as with <tt>"rw"</tt>, and also
0N/A * require that every update to the file's content be written
0N/A * synchronously to the underlying storage device. </td></tr>
0N/A * </table></blockquote>
0N/A *
0N/A * The <tt>"rws"</tt> and <tt>"rwd"</tt> modes work much like the {@link
0N/A * java.nio.channels.FileChannel#force(boolean) force(boolean)} method of
0N/A * the {@link java.nio.channels.FileChannel} class, passing arguments of
0N/A * <tt>true</tt> and <tt>false</tt>, respectively, except that they always
0N/A * apply to every I/O operation and are therefore often more efficient. If
0N/A * the file resides on a local storage device then when an invocation of a
0N/A * method of this class returns it is guaranteed that all changes made to
0N/A * the file by that invocation will have been written to that device. This
0N/A * is useful for ensuring that critical information is not lost in the
0N/A * event of a system crash. If the file does not reside on a local device
0N/A * then no such guarantee is made.
0N/A *
0N/A * <p> The <tt>"rwd"</tt> mode can be used to reduce the number of I/O
0N/A * operations performed. Using <tt>"rwd"</tt> only requires updates to the
0N/A * file's content to be written to storage; using <tt>"rws"</tt> requires
0N/A * updates to both the file's content and its metadata to be written, which
0N/A * generally requires at least one more low-level I/O operation.
0N/A *
0N/A * <p> If there is a security manager, its <code>checkRead</code> method is
0N/A * called with the pathname of the <code>file</code> argument as its
0N/A * argument to see if read access to the file is allowed. If the mode
0N/A * allows writing, the security manager's <code>checkWrite</code> method is
0N/A * also called with the path argument to see if write access to the file is
0N/A * allowed.
0N/A *
0N/A * @param file the file object
0N/A * @param mode the access mode, as described
0N/A * <a href="#mode">above</a>
0N/A * @exception IllegalArgumentException if the mode argument is not equal
0N/A * to one of <tt>"r"</tt>, <tt>"rw"</tt>, <tt>"rws"</tt>, or
0N/A * <tt>"rwd"</tt>
0N/A * @exception FileNotFoundException
0N/A * if the mode is <tt>"r"</tt> but the given file object does
0N/A * not denote an existing regular file, or if the mode begins
0N/A * with <tt>"rw"</tt> but the given file object does not denote
0N/A * an existing, writable regular file and a new regular file of
0N/A * that name cannot be created, or if some other error occurs
0N/A * while opening or creating the file
0N/A * @exception SecurityException if a security manager exists and its
0N/A * <code>checkRead</code> method denies read access to the file
0N/A * or the mode is "rw" and the security manager's
0N/A * <code>checkWrite</code> method denies write access to the file
0N/A * @see java.lang.SecurityManager#checkRead(java.lang.String)
0N/A * @see java.lang.SecurityManager#checkWrite(java.lang.String)
0N/A * @see java.nio.channels.FileChannel#force(boolean)
0N/A * @revised 1.4
0N/A * @spec JSR-51
0N/A */
0N/A public RandomAccessFile(File file, String mode)
0N/A throws FileNotFoundException
0N/A {
0N/A String name = (file != null ? file.getPath() : null);
0N/A int imode = -1;
0N/A if (mode.equals("r"))
0N/A imode = O_RDONLY;
0N/A else if (mode.startsWith("rw")) {
0N/A imode = O_RDWR;
0N/A rw = true;
0N/A if (mode.length() > 2) {
0N/A if (mode.equals("rws"))
0N/A imode |= O_SYNC;
0N/A else if (mode.equals("rwd"))
0N/A imode |= O_DSYNC;
0N/A else
0N/A imode = -1;
0N/A }
0N/A }
0N/A if (imode < 0)
0N/A throw new IllegalArgumentException("Illegal mode \"" + mode
0N/A + "\" must be one of "
0N/A + "\"r\", \"rw\", \"rws\","
0N/A + " or \"rwd\"");
0N/A SecurityManager security = System.getSecurityManager();
0N/A if (security != null) {
0N/A security.checkRead(name);
0N/A if (rw) {
0N/A security.checkWrite(name);
0N/A }
0N/A }
0N/A if (name == null) {
0N/A throw new NullPointerException();
0N/A }
6092N/A if (file.isInvalid()) {
6092N/A throw new FileNotFoundException("Invalid file path");
6092N/A }
0N/A fd = new FileDescriptor();
5182N/A fd.incrementAndGetUseCount();
5503N/A this.path = name;
0N/A open(name, imode);
0N/A }
0N/A
0N/A /**
0N/A * Returns the opaque file descriptor object associated with this
0N/A * stream. </p>
0N/A *
0N/A * @return the file descriptor object associated with this stream.
0N/A * @exception IOException if an I/O error occurs.
0N/A * @see java.io.FileDescriptor
0N/A */
0N/A public final FileDescriptor getFD() throws IOException {
5182N/A if (fd != null) return fd;
0N/A throw new IOException();
0N/A }
0N/A
0N/A /**
0N/A * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
0N/A * object associated with this file.
0N/A *
0N/A * <p> The {@link java.nio.channels.FileChannel#position()
0N/A * </code>position<code>} of the returned channel will always be equal to
0N/A * this object's file-pointer offset as returned by the {@link
0N/A * #getFilePointer getFilePointer} method. Changing this object's
0N/A * file-pointer offset, whether explicitly or by reading or writing bytes,
0N/A * will change the position of the channel, and vice versa. Changing the
0N/A * file's length via this object will change the length seen via the file
0N/A * channel, and vice versa.
0N/A *
0N/A * @return the file channel associated with this file
0N/A *
0N/A * @since 1.4
0N/A * @spec JSR-51
0N/A */
0N/A public final FileChannel getChannel() {
0N/A synchronized (this) {
0N/A if (channel == null) {
5503N/A channel = FileChannelImpl.open(fd, path, true, rw, this);
5182N/A
5182N/A /*
5182N/A * FileDescriptor could be shared by FileInputStream or
5182N/A * FileOutputStream.
5182N/A * Ensure that FD is GC'ed only when all the streams/channels
5182N/A * are done using it.
5182N/A * Increment fd's use count. Invoking the channel's close()
5182N/A * method will result in decrementing the use count set for
5182N/A * the channel.
5182N/A */
5182N/A fd.incrementAndGetUseCount();
0N/A }
0N/A return channel;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Opens a file and returns the file descriptor. The file is
0N/A * opened in read-write mode if the O_RDWR bit in <code>mode</code>
0N/A * is true, else the file is opened as read-only.
0N/A * If the <code>name</code> refers to a directory, an IOException
0N/A * is thrown.
0N/A *
0N/A * @param name the name of the file
0N/A * @param mode the mode flags, a combination of the O_ constants
0N/A * defined above
0N/A */
0N/A private native void open(String name, int mode)
0N/A throws FileNotFoundException;
0N/A
0N/A // 'Read' primitives
0N/A
0N/A /**
0N/A * Reads a byte of data from this file. The byte is returned as an
0N/A * integer in the range 0 to 255 (<code>0x00-0x0ff</code>). This
0N/A * method blocks if no input is yet available.
0N/A * <p>
0N/A * Although <code>RandomAccessFile</code> is not a subclass of
0N/A * <code>InputStream</code>, this method behaves in exactly the same
0N/A * way as the {@link InputStream#read()} method of
0N/A * <code>InputStream</code>.
0N/A *
0N/A * @return the next byte of data, or <code>-1</code> if the end of the
0N/A * file has been reached.
0N/A * @exception IOException if an I/O error occurs. Not thrown if
0N/A * end-of-file has been reached.
0N/A */
5503N/A public int read() throws IOException {
5503N/A Object traceContext = IoTrace.fileReadBegin(path);
5503N/A int b = 0;
5503N/A try {
5503N/A b = read0();
5503N/A } finally {
5503N/A IoTrace.fileReadEnd(traceContext, b == -1 ? 0 : 1);
5503N/A }
5503N/A return b;
5503N/A }
5503N/A
5503N/A private native int read0() throws IOException;
0N/A
0N/A /**
0N/A * Reads a sub array as a sequence of bytes.
0N/A * @param b the buffer into which the data is read.
0N/A * @param off the start offset of the data.
0N/A * @param len the number of bytes to read.
0N/A * @exception IOException If an I/O error has occurred.
0N/A */
5503N/A private int readBytes(byte b[], int off, int len) throws IOException {
5503N/A Object traceContext = IoTrace.fileReadBegin(path);
5503N/A int bytesRead = 0;
5503N/A try {
5503N/A bytesRead = readBytes0(b, off, len);
5503N/A } finally {
5503N/A IoTrace.fileReadEnd(traceContext, bytesRead == -1 ? 0 : bytesRead);
5503N/A }
5503N/A return bytesRead;
5503N/A }
5503N/A
5503N/A private native int readBytes0(byte b[], int off, int len) throws IOException;
0N/A
0N/A /**
0N/A * Reads up to <code>len</code> bytes of data from this file into an
0N/A * array of bytes. This method blocks until at least one byte of input
0N/A * is available.
0N/A * <p>
0N/A * Although <code>RandomAccessFile</code> is not a subclass of
0N/A * <code>InputStream</code>, this method behaves in exactly the
0N/A * same way as the {@link InputStream#read(byte[], int, int)} method of
0N/A * <code>InputStream</code>.
0N/A *
0N/A * @param b the buffer into which the data is read.
0N/A * @param off the start offset in array <code>b</code>
0N/A * at which the data is written.
0N/A * @param len the maximum number of bytes read.
0N/A * @return the total number of bytes read into the buffer, or
0N/A * <code>-1</code> if there is no more data because the end of
0N/A * the file has been reached.
0N/A * @exception IOException If the first byte cannot be read for any reason
0N/A * other than end of file, or if the random access file has been closed, or if
0N/A * some other I/O error occurs.
0N/A * @exception NullPointerException If <code>b</code> is <code>null</code>.
0N/A * @exception IndexOutOfBoundsException If <code>off</code> is negative,
0N/A * <code>len</code> is negative, or <code>len</code> is greater than
0N/A * <code>b.length - off</code>
0N/A */
0N/A public int read(byte b[], int off, int len) throws IOException {
0N/A return readBytes(b, off, len);
0N/A }
0N/A
0N/A /**
0N/A * Reads up to <code>b.length</code> bytes of data from this file
0N/A * into an array of bytes. This method blocks until at least one byte
0N/A * of input is available.
0N/A * <p>
0N/A * Although <code>RandomAccessFile</code> is not a subclass of
0N/A * <code>InputStream</code>, this method behaves in exactly the
0N/A * same way as the {@link InputStream#read(byte[])} method of
0N/A * <code>InputStream</code>.
0N/A *
0N/A * @param b the buffer into which the data is read.
0N/A * @return the total number of bytes read into the buffer, or
0N/A * <code>-1</code> if there is no more data because the end of
0N/A * this file has been reached.
0N/A * @exception IOException If the first byte cannot be read for any reason
0N/A * other than end of file, or if the random access file has been closed, or if
0N/A * some other I/O error occurs.
0N/A * @exception NullPointerException If <code>b</code> is <code>null</code>.
0N/A */
0N/A public int read(byte b[]) throws IOException {
0N/A return readBytes(b, 0, b.length);
0N/A }
0N/A
0N/A /**
0N/A * Reads <code>b.length</code> bytes from this file into the byte
0N/A * array, starting at the current file pointer. This method reads
0N/A * repeatedly from the file until the requested number of bytes are
0N/A * read. This method blocks until the requested number of bytes are
0N/A * read, the end of the stream is detected, or an exception is thrown.
0N/A *
0N/A * @param b the buffer into which the data is read.
0N/A * @exception EOFException if this file reaches the end before reading
0N/A * all the bytes.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public final void readFully(byte b[]) throws IOException {
0N/A readFully(b, 0, b.length);
0N/A }
0N/A
0N/A /**
0N/A * Reads exactly <code>len</code> bytes from this file into the byte
0N/A * array, starting at the current file pointer. This method reads
0N/A * repeatedly from the file until the requested number of bytes are
0N/A * read. This method blocks until the requested number of bytes are
0N/A * read, the end of the stream is detected, or an exception is thrown.
0N/A *
0N/A * @param b the buffer into which the data is read.
0N/A * @param off the start offset of the data.
0N/A * @param len the number of bytes to read.
0N/A * @exception EOFException if this file reaches the end before reading
0N/A * all the bytes.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public final void readFully(byte b[], int off, int len) throws IOException {
0N/A int n = 0;
0N/A do {
0N/A int count = this.read(b, off + n, len - n);
0N/A if (count < 0)
0N/A throw new EOFException();
0N/A n += count;
0N/A } while (n < len);
0N/A }
0N/A
0N/A /**
0N/A * Attempts to skip over <code>n</code> bytes of input discarding the
0N/A * skipped bytes.
0N/A * <p>
0N/A *
0N/A * This method may skip over some smaller number of bytes, possibly zero.
0N/A * This may result from any of a number of conditions; reaching end of
0N/A * file before <code>n</code> bytes have been skipped is only one
0N/A * possibility. This method never throws an <code>EOFException</code>.
0N/A * The actual number of bytes skipped is returned. If <code>n</code>
0N/A * is negative, no bytes are skipped.
0N/A *
0N/A * @param n the number of bytes to be skipped.
0N/A * @return the actual number of bytes skipped.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public int skipBytes(int n) throws IOException {
0N/A long pos;
0N/A long len;
0N/A long newpos;
0N/A
0N/A if (n <= 0) {
0N/A return 0;
0N/A }
0N/A pos = getFilePointer();
0N/A len = length();
0N/A newpos = pos + n;
0N/A if (newpos > len) {
0N/A newpos = len;
0N/A }
0N/A seek(newpos);
0N/A
0N/A /* return the actual number of bytes skipped */
0N/A return (int) (newpos - pos);
0N/A }
0N/A
0N/A // 'Write' primitives
0N/A
0N/A /**
0N/A * Writes the specified byte to this file. The write starts at
0N/A * the current file pointer.
0N/A *
0N/A * @param b the <code>byte</code> to be written.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
5503N/A public void write(int b) throws IOException {
5503N/A Object traceContext = IoTrace.fileWriteBegin(path);
5503N/A int bytesWritten = 0;
5503N/A try {
5503N/A write0(b);
5503N/A bytesWritten = 1;
5503N/A } finally {
5503N/A IoTrace.fileWriteEnd(traceContext, bytesWritten);
5503N/A }
5503N/A }
5503N/A
5503N/A private native void write0(int b) throws IOException;
0N/A
0N/A /**
0N/A * Writes a sub array as a sequence of bytes.
0N/A * @param b the data to be written
0N/A * @param off the start offset in the data
0N/A * @param len the number of bytes that are written
0N/A * @exception IOException If an I/O error has occurred.
0N/A */
5503N/A private void writeBytes(byte b[], int off, int len) throws IOException {
5503N/A Object traceContext = IoTrace.fileWriteBegin(path);
5503N/A int bytesWritten = 0;
5503N/A try {
5503N/A writeBytes0(b, off, len);
5503N/A bytesWritten = len;
5503N/A } finally {
5503N/A IoTrace.fileWriteEnd(traceContext, bytesWritten);
5503N/A }
5503N/A }
5503N/A
5503N/A private native void writeBytes0(byte b[], int off, int len) throws IOException;
0N/A
0N/A /**
0N/A * Writes <code>b.length</code> bytes from the specified byte array
0N/A * to this file, starting at the current file pointer.
0N/A *
0N/A * @param b the data.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public void write(byte b[]) throws IOException {
0N/A writeBytes(b, 0, b.length);
0N/A }
0N/A
0N/A /**
0N/A * Writes <code>len</code> bytes from the specified byte array
0N/A * starting at offset <code>off</code> to this file.
0N/A *
0N/A * @param b the data.
0N/A * @param off the start offset in the data.
0N/A * @param len the number of bytes to write.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public void write(byte b[], int off, int len) throws IOException {
0N/A writeBytes(b, off, len);
0N/A }
0N/A
0N/A // 'Random access' stuff
0N/A
0N/A /**
0N/A * Returns the current offset in this file.
0N/A *
0N/A * @return the offset from the beginning of the file, in bytes,
0N/A * at which the next read or write occurs.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public native long getFilePointer() throws IOException;
0N/A
0N/A /**
0N/A * Sets the file-pointer offset, measured from the beginning of this
0N/A * file, at which the next read or write occurs. The offset may be
0N/A * set beyond the end of the file. Setting the offset beyond the end
0N/A * of the file does not change the file length. The file length will
0N/A * change only by writing after the offset has been set beyond the end
0N/A * of the file.
0N/A *
0N/A * @param pos the offset position, measured in bytes from the
0N/A * beginning of the file, at which to set the file
0N/A * pointer.
0N/A * @exception IOException if <code>pos</code> is less than
0N/A * <code>0</code> or if an I/O error occurs.
0N/A */
0N/A public native void seek(long pos) throws IOException;
0N/A
0N/A /**
0N/A * Returns the length of this file.
0N/A *
0N/A * @return the length of this file, measured in bytes.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public native long length() throws IOException;
0N/A
0N/A /**
0N/A * Sets the length of this file.
0N/A *
0N/A * <p> If the present length of the file as returned by the
0N/A * <code>length</code> method is greater than the <code>newLength</code>
0N/A * argument then the file will be truncated. In this case, if the file
0N/A * offset as returned by the <code>getFilePointer</code> method is greater
0N/A * than <code>newLength</code> then after this method returns the offset
0N/A * will be equal to <code>newLength</code>.
0N/A *
0N/A * <p> If the present length of the file as returned by the
0N/A * <code>length</code> method is smaller than the <code>newLength</code>
0N/A * argument then the file will be extended. In this case, the contents of
0N/A * the extended portion of the file are not defined.
0N/A *
0N/A * @param newLength The desired length of the file
0N/A * @exception IOException If an I/O error occurs
0N/A * @since 1.2
0N/A */
0N/A public native void setLength(long newLength) throws IOException;
0N/A
0N/A /**
0N/A * Closes this random access file stream and releases any system
0N/A * resources associated with the stream. A closed random access
0N/A * file cannot perform input or output operations and cannot be
0N/A * reopened.
0N/A *
0N/A * <p> If this file has an associated channel then the channel is closed
0N/A * as well.
0N/A *
0N/A * @exception IOException if an I/O error occurs.
0N/A *
0N/A * @revised 1.4
0N/A * @spec JSR-51
0N/A */
0N/A public void close() throws IOException {
0N/A synchronized (closeLock) {
0N/A if (closed) {
0N/A return;
0N/A }
0N/A closed = true;
0N/A }
0N/A if (channel != null) {
5182N/A /*
5182N/A * Decrement FD use count associated with the channel. The FD use
5182N/A * count is incremented whenever a new channel is obtained from
5182N/A * this stream.
5182N/A */
5182N/A fd.decrementAndGetUseCount();
0N/A channel.close();
0N/A }
5182N/A
5182N/A /*
5182N/A * Decrement FD use count associated with this stream.
5182N/A * The count got incremented by FileDescriptor during its construction.
5182N/A */
5182N/A fd.decrementAndGetUseCount();
5182N/A close0();
0N/A }
0N/A
0N/A //
0N/A // Some "reading/writing Java data types" methods stolen from
0N/A // DataInputStream and DataOutputStream.
0N/A //
0N/A
0N/A /**
0N/A * Reads a <code>boolean</code> from this file. This method reads a
0N/A * single byte from the file, starting at the current file pointer.
0N/A * A value of <code>0</code> represents
0N/A * <code>false</code>. Any other value represents <code>true</code>.
0N/A * This method blocks until the byte is read, the end of the stream
0N/A * is detected, or an exception is thrown.
0N/A *
0N/A * @return the <code>boolean</code> value read.
0N/A * @exception EOFException if this file has reached the end.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public final boolean readBoolean() throws IOException {
0N/A int ch = this.read();
0N/A if (ch < 0)
0N/A throw new EOFException();
0N/A return (ch != 0);
0N/A }
0N/A
0N/A /**
0N/A * Reads a signed eight-bit value from this file. This method reads a
0N/A * byte from the file, starting from the current file pointer.
0N/A * If the byte read is <code>b</code>, where
0N/A * <code>0&nbsp;&lt;=&nbsp;b&nbsp;&lt;=&nbsp;255</code>,
0N/A * then the result is:
0N/A * <blockquote><pre>
0N/A * (byte)(b)
0N/A * </pre></blockquote>
0N/A * <p>
0N/A * This method blocks until the byte is read, the end of the stream
0N/A * is detected, or an exception is thrown.
0N/A *
0N/A * @return the next byte of this file as a signed eight-bit
0N/A * <code>byte</code>.
0N/A * @exception EOFException if this file has reached the end.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public final byte readByte() throws IOException {
0N/A int ch = this.read();
0N/A if (ch < 0)
0N/A throw new EOFException();
0N/A return (byte)(ch);
0N/A }
0N/A
0N/A /**
0N/A * Reads an unsigned eight-bit number from this file. This method reads
0N/A * a byte from this file, starting at the current file pointer,
0N/A * and returns that byte.
0N/A * <p>
0N/A * This method blocks until the byte is read, the end of the stream
0N/A * is detected, or an exception is thrown.
0N/A *
0N/A * @return the next byte of this file, interpreted as an unsigned
0N/A * eight-bit number.
0N/A * @exception EOFException if this file has reached the end.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public final int readUnsignedByte() throws IOException {
0N/A int ch = this.read();
0N/A if (ch < 0)
0N/A throw new EOFException();
0N/A return ch;
0N/A }
0N/A
0N/A /**
0N/A * Reads a signed 16-bit number from this file. The method reads two
0N/A * bytes from this file, starting at the current file pointer.
0N/A * If the two bytes read, in order, are
0N/A * <code>b1</code> and <code>b2</code>, where each of the two values is
0N/A * between <code>0</code> and <code>255</code>, inclusive, then the
0N/A * result is equal to:
0N/A * <blockquote><pre>
0N/A * (short)((b1 &lt;&lt; 8) | b2)
0N/A * </pre></blockquote>
0N/A * <p>
0N/A * This method blocks until the two bytes are read, the end of the
0N/A * stream is detected, or an exception is thrown.
0N/A *
0N/A * @return the next two bytes of this file, interpreted as a signed
0N/A * 16-bit number.
0N/A * @exception EOFException if this file reaches the end before reading
0N/A * two bytes.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public final short readShort() throws IOException {
0N/A int ch1 = this.read();
0N/A int ch2 = this.read();
0N/A if ((ch1 | ch2) < 0)
0N/A throw new EOFException();
0N/A return (short)((ch1 << 8) + (ch2 << 0));
0N/A }
0N/A
0N/A /**
0N/A * Reads an unsigned 16-bit number from this file. This method reads
0N/A * two bytes from the file, starting at the current file pointer.
0N/A * If the bytes read, in order, are
0N/A * <code>b1</code> and <code>b2</code>, where
0N/A * <code>0&nbsp;&lt;=&nbsp;b1, b2&nbsp;&lt;=&nbsp;255</code>,
0N/A * then the result is equal to:
0N/A * <blockquote><pre>
0N/A * (b1 &lt;&lt; 8) | b2
0N/A * </pre></blockquote>
0N/A * <p>
0N/A * This method blocks until the two bytes are read, the end of the
0N/A * stream is detected, or an exception is thrown.
0N/A *
0N/A * @return the next two bytes of this file, interpreted as an unsigned
0N/A * 16-bit integer.
0N/A * @exception EOFException if this file reaches the end before reading
0N/A * two bytes.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public final int readUnsignedShort() throws IOException {
0N/A int ch1 = this.read();
0N/A int ch2 = this.read();
0N/A if ((ch1 | ch2) < 0)
0N/A throw new EOFException();
0N/A return (ch1 << 8) + (ch2 << 0);
0N/A }
0N/A
0N/A /**
0N/A * Reads a character from this file. This method reads two
0N/A * bytes from the file, starting at the current file pointer.
0N/A * If the bytes read, in order, are
0N/A * <code>b1</code> and <code>b2</code>, where
0N/A * <code>0&nbsp;&lt;=&nbsp;b1,&nbsp;b2&nbsp;&lt;=&nbsp;255</code>,
0N/A * then the result is equal to:
0N/A * <blockquote><pre>
0N/A * (char)((b1 &lt;&lt; 8) | b2)
0N/A * </pre></blockquote>
0N/A * <p>
0N/A * This method blocks until the two bytes are read, the end of the
0N/A * stream is detected, or an exception is thrown.
0N/A *
0N/A * @return the next two bytes of this file, interpreted as a
0N/A * <code>char</code>.
0N/A * @exception EOFException if this file reaches the end before reading
0N/A * two bytes.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public final char readChar() throws IOException {
0N/A int ch1 = this.read();
0N/A int ch2 = this.read();
0N/A if ((ch1 | ch2) < 0)
0N/A throw new EOFException();
0N/A return (char)((ch1 << 8) + (ch2 << 0));
0N/A }
0N/A
0N/A /**
0N/A * Reads a signed 32-bit integer from this file. This method reads 4
0N/A * bytes from the file, starting at the current file pointer.
0N/A * If the bytes read, in order, are <code>b1</code>,
0N/A * <code>b2</code>, <code>b3</code>, and <code>b4</code>, where
0N/A * <code>0&nbsp;&lt;=&nbsp;b1, b2, b3, b4&nbsp;&lt;=&nbsp;255</code>,
0N/A * then the result is equal to:
0N/A * <blockquote><pre>
0N/A * (b1 &lt;&lt; 24) | (b2 &lt;&lt; 16) + (b3 &lt;&lt; 8) + b4
0N/A * </pre></blockquote>
0N/A * <p>
0N/A * This method blocks until the four bytes are read, the end of the
0N/A * stream is detected, or an exception is thrown.
0N/A *
0N/A * @return the next four bytes of this file, interpreted as an
0N/A * <code>int</code>.
0N/A * @exception EOFException if this file reaches the end before reading
0N/A * four bytes.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public final int readInt() throws IOException {
0N/A int ch1 = this.read();
0N/A int ch2 = this.read();
0N/A int ch3 = this.read();
0N/A int ch4 = this.read();
0N/A if ((ch1 | ch2 | ch3 | ch4) < 0)
0N/A throw new EOFException();
0N/A return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
0N/A }
0N/A
0N/A /**
0N/A * Reads a signed 64-bit integer from this file. This method reads eight
0N/A * bytes from the file, starting at the current file pointer.
0N/A * If the bytes read, in order, are
0N/A * <code>b1</code>, <code>b2</code>, <code>b3</code>,
0N/A * <code>b4</code>, <code>b5</code>, <code>b6</code>,
0N/A * <code>b7</code>, and <code>b8,</code> where:
0N/A * <blockquote><pre>
0N/A * 0 &lt;= b1, b2, b3, b4, b5, b6, b7, b8 &lt;=255,
0N/A * </pre></blockquote>
0N/A * <p>
0N/A * then the result is equal to:
0N/A * <p><blockquote><pre>
0N/A * ((long)b1 &lt;&lt; 56) + ((long)b2 &lt;&lt; 48)
0N/A * + ((long)b3 &lt;&lt; 40) + ((long)b4 &lt;&lt; 32)
0N/A * + ((long)b5 &lt;&lt; 24) + ((long)b6 &lt;&lt; 16)
0N/A * + ((long)b7 &lt;&lt; 8) + b8
0N/A * </pre></blockquote>
0N/A * <p>
0N/A * This method blocks until the eight bytes are read, the end of the
0N/A * stream is detected, or an exception is thrown.
0N/A *
0N/A * @return the next eight bytes of this file, interpreted as a
0N/A * <code>long</code>.
0N/A * @exception EOFException if this file reaches the end before reading
0N/A * eight bytes.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public final long readLong() throws IOException {
0N/A return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL);
0N/A }
0N/A
0N/A /**
0N/A * Reads a <code>float</code> from this file. This method reads an
0N/A * <code>int</code> value, starting at the current file pointer,
0N/A * as if by the <code>readInt</code> method
0N/A * and then converts that <code>int</code> to a <code>float</code>
0N/A * using the <code>intBitsToFloat</code> method in class
0N/A * <code>Float</code>.
0N/A * <p>
0N/A * This method blocks until the four bytes are read, the end of the
0N/A * stream is detected, or an exception is thrown.
0N/A *
0N/A * @return the next four bytes of this file, interpreted as a
0N/A * <code>float</code>.
0N/A * @exception EOFException if this file reaches the end before reading
0N/A * four bytes.
0N/A * @exception IOException if an I/O error occurs.
0N/A * @see java.io.RandomAccessFile#readInt()
0N/A * @see java.lang.Float#intBitsToFloat(int)
0N/A */
0N/A public final float readFloat() throws IOException {
0N/A return Float.intBitsToFloat(readInt());
0N/A }
0N/A
0N/A /**
0N/A * Reads a <code>double</code> from this file. This method reads a
0N/A * <code>long</code> value, starting at the current file pointer,
0N/A * as if by the <code>readLong</code> method
0N/A * and then converts that <code>long</code> to a <code>double</code>
0N/A * using the <code>longBitsToDouble</code> method in
0N/A * class <code>Double</code>.
0N/A * <p>
0N/A * This method blocks until the eight bytes are read, the end of the
0N/A * stream is detected, or an exception is thrown.
0N/A *
0N/A * @return the next eight bytes of this file, interpreted as a
0N/A * <code>double</code>.
0N/A * @exception EOFException if this file reaches the end before reading
0N/A * eight bytes.
0N/A * @exception IOException if an I/O error occurs.
0N/A * @see java.io.RandomAccessFile#readLong()
0N/A * @see java.lang.Double#longBitsToDouble(long)
0N/A */
0N/A public final double readDouble() throws IOException {
0N/A return Double.longBitsToDouble(readLong());
0N/A }
0N/A
0N/A /**
0N/A * Reads the next line of text from this file. This method successively
0N/A * reads bytes from the file, starting at the current file pointer,
0N/A * until it reaches a line terminator or the end
0N/A * of the file. Each byte is converted into a character by taking the
0N/A * byte's value for the lower eight bits of the character and setting the
0N/A * high eight bits of the character to zero. This method does not,
0N/A * therefore, support the full Unicode character set.
0N/A *
0N/A * <p> A line of text is terminated by a carriage-return character
0N/A * (<code>'&#92;r'</code>), a newline character (<code>'&#92;n'</code>), a
0N/A * carriage-return character immediately followed by a newline character,
0N/A * or the end of the file. Line-terminating characters are discarded and
0N/A * are not included as part of the string returned.
0N/A *
0N/A * <p> This method blocks until a newline character is read, a carriage
0N/A * return and the byte following it are read (to see if it is a newline),
0N/A * the end of the file is reached, or an exception is thrown.
0N/A *
0N/A * @return the next line of text from this file, or null if end
0N/A * of file is encountered before even one byte is read.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A
0N/A public final String readLine() throws IOException {
0N/A StringBuffer input = new StringBuffer();
0N/A int c = -1;
0N/A boolean eol = false;
0N/A
0N/A while (!eol) {
0N/A switch (c = read()) {
0N/A case -1:
0N/A case '\n':
0N/A eol = true;
0N/A break;
0N/A case '\r':
0N/A eol = true;
0N/A long cur = getFilePointer();
0N/A if ((read()) != '\n') {
0N/A seek(cur);
0N/A }
0N/A break;
0N/A default:
0N/A input.append((char)c);
0N/A break;
0N/A }
0N/A }
0N/A
0N/A if ((c == -1) && (input.length() == 0)) {
0N/A return null;
0N/A }
0N/A return input.toString();
0N/A }
0N/A
0N/A /**
0N/A * Reads in a string from this file. The string has been encoded
0N/A * using a
0N/A * <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
0N/A * format.
0N/A * <p>
0N/A * The first two bytes are read, starting from the current file
0N/A * pointer, as if by
0N/A * <code>readUnsignedShort</code>. This value gives the number of
0N/A * following bytes that are in the encoded string, not
0N/A * the length of the resulting string. The following bytes are then
0N/A * interpreted as bytes encoding characters in the modified UTF-8 format
0N/A * and are converted into characters.
0N/A * <p>
0N/A * This method blocks until all the bytes are read, the end of the
0N/A * stream is detected, or an exception is thrown.
0N/A *
0N/A * @return a Unicode string.
0N/A * @exception EOFException if this file reaches the end before
0N/A * reading all the bytes.
0N/A * @exception IOException if an I/O error occurs.
0N/A * @exception UTFDataFormatException if the bytes do not represent
0N/A * valid modified UTF-8 encoding of a Unicode string.
0N/A * @see java.io.RandomAccessFile#readUnsignedShort()
0N/A */
0N/A public final String readUTF() throws IOException {
0N/A return DataInputStream.readUTF(this);
0N/A }
0N/A
0N/A /**
0N/A * Writes a <code>boolean</code> to the file as a one-byte value. The
0N/A * value <code>true</code> is written out as the value
0N/A * <code>(byte)1</code>; the value <code>false</code> is written out
0N/A * as the value <code>(byte)0</code>. The write starts at
0N/A * the current position of the file pointer.
0N/A *
0N/A * @param v a <code>boolean</code> value to be written.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public final void writeBoolean(boolean v) throws IOException {
0N/A write(v ? 1 : 0);
0N/A //written++;
0N/A }
0N/A
0N/A /**
0N/A * Writes a <code>byte</code> to the file as a one-byte value. The
0N/A * write starts at the current position of the file pointer.
0N/A *
0N/A * @param v a <code>byte</code> value to be written.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public final void writeByte(int v) throws IOException {
0N/A write(v);
0N/A //written++;
0N/A }
0N/A
0N/A /**
0N/A * Writes a <code>short</code> to the file as two bytes, high byte first.
0N/A * The write starts at the current position of the file pointer.
0N/A *
0N/A * @param v a <code>short</code> to be written.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public final void writeShort(int v) throws IOException {
0N/A write((v >>> 8) & 0xFF);
0N/A write((v >>> 0) & 0xFF);
0N/A //written += 2;
0N/A }
0N/A
0N/A /**
0N/A * Writes a <code>char</code> to the file as a two-byte value, high
0N/A * byte first. The write starts at the current position of the
0N/A * file pointer.
0N/A *
0N/A * @param v a <code>char</code> value to be written.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public final void writeChar(int v) throws IOException {
0N/A write((v >>> 8) & 0xFF);
0N/A write((v >>> 0) & 0xFF);
0N/A //written += 2;
0N/A }
0N/A
0N/A /**
0N/A * Writes an <code>int</code> to the file as four bytes, high byte first.
0N/A * The write starts at the current position of the file pointer.
0N/A *
0N/A * @param v an <code>int</code> to be written.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public final void writeInt(int v) throws IOException {
0N/A write((v >>> 24) & 0xFF);
0N/A write((v >>> 16) & 0xFF);
0N/A write((v >>> 8) & 0xFF);
0N/A write((v >>> 0) & 0xFF);
0N/A //written += 4;
0N/A }
0N/A
0N/A /**
0N/A * Writes a <code>long</code> to the file as eight bytes, high byte first.
0N/A * The write starts at the current position of the file pointer.
0N/A *
0N/A * @param v a <code>long</code> to be written.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public final void writeLong(long v) throws IOException {
0N/A write((int)(v >>> 56) & 0xFF);
0N/A write((int)(v >>> 48) & 0xFF);
0N/A write((int)(v >>> 40) & 0xFF);
0N/A write((int)(v >>> 32) & 0xFF);
0N/A write((int)(v >>> 24) & 0xFF);
0N/A write((int)(v >>> 16) & 0xFF);
0N/A write((int)(v >>> 8) & 0xFF);
0N/A write((int)(v >>> 0) & 0xFF);
0N/A //written += 8;
0N/A }
0N/A
0N/A /**
0N/A * Converts the float argument to an <code>int</code> using the
0N/A * <code>floatToIntBits</code> method in class <code>Float</code>,
0N/A * and then writes that <code>int</code> value to the file as a
0N/A * four-byte quantity, high byte first. The write starts at the
0N/A * current position of the file pointer.
0N/A *
0N/A * @param v a <code>float</code> value to be written.
0N/A * @exception IOException if an I/O error occurs.
0N/A * @see java.lang.Float#floatToIntBits(float)
0N/A */
0N/A public final void writeFloat(float v) throws IOException {
0N/A writeInt(Float.floatToIntBits(v));
0N/A }
0N/A
0N/A /**
0N/A * Converts the double argument to a <code>long</code> using the
0N/A * <code>doubleToLongBits</code> method in class <code>Double</code>,
0N/A * and then writes that <code>long</code> value to the file as an
0N/A * eight-byte quantity, high byte first. The write starts at the current
0N/A * position of the file pointer.
0N/A *
0N/A * @param v a <code>double</code> value to be written.
0N/A * @exception IOException if an I/O error occurs.
0N/A * @see java.lang.Double#doubleToLongBits(double)
0N/A */
0N/A public final void writeDouble(double v) throws IOException {
0N/A writeLong(Double.doubleToLongBits(v));
0N/A }
0N/A
0N/A /**
0N/A * Writes the string to the file as a sequence of bytes. Each
0N/A * character in the string is written out, in sequence, by discarding
0N/A * its high eight bits. The write starts at the current position of
0N/A * the file pointer.
0N/A *
0N/A * @param s a string of bytes to be written.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public final void writeBytes(String s) throws IOException {
0N/A int len = s.length();
0N/A byte[] b = new byte[len];
0N/A s.getBytes(0, len, b, 0);
0N/A writeBytes(b, 0, len);
0N/A }
0N/A
0N/A /**
0N/A * Writes a string to the file as a sequence of characters. Each
0N/A * character is written to the data output stream as if by the
0N/A * <code>writeChar</code> method. The write starts at the current
0N/A * position of the file pointer.
0N/A *
0N/A * @param s a <code>String</code> value to be written.
0N/A * @exception IOException if an I/O error occurs.
0N/A * @see java.io.RandomAccessFile#writeChar(int)
0N/A */
0N/A public final void writeChars(String s) throws IOException {
0N/A int clen = s.length();
0N/A int blen = 2*clen;
0N/A byte[] b = new byte[blen];
0N/A char[] c = new char[clen];
0N/A s.getChars(0, clen, c, 0);
0N/A for (int i = 0, j = 0; i < clen; i++) {
0N/A b[j++] = (byte)(c[i] >>> 8);
0N/A b[j++] = (byte)(c[i] >>> 0);
0N/A }
0N/A writeBytes(b, 0, blen);
0N/A }
0N/A
0N/A /**
0N/A * Writes a string to the file using
0N/A * <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
0N/A * encoding in a machine-independent manner.
0N/A * <p>
0N/A * First, two bytes are written to the file, starting at the
0N/A * current file pointer, as if by the
0N/A * <code>writeShort</code> method giving the number of bytes to
0N/A * follow. This value is the number of bytes actually written out,
0N/A * not the length of the string. Following the length, each character
0N/A * of the string is output, in sequence, using the modified UTF-8 encoding
0N/A * for each character.
0N/A *
0N/A * @param str a string to be written.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public final void writeUTF(String str) throws IOException {
0N/A DataOutputStream.writeUTF(str, this);
0N/A }
0N/A
0N/A private static native void initIDs();
0N/A
0N/A private native void close0() throws IOException;
0N/A
0N/A static {
0N/A initIDs();
0N/A }
0N/A}