jdk/src/share/classes/java/io/RandomAccessFile.java
author coffeys
Tue, 13 Sep 2011 11:21:51 +0100
changeset 10586 6e20ecfec8ed
parent 5506 202f599c92aa
child 11017 353f81426721
permissions -rw-r--r--
7082769: FileInputStream/FileOutputStream/RandomAccessFile allow file descriptor be closed when still in use Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10586
6e20ecfec8ed 7082769: FileInputStream/FileOutputStream/RandomAccessFile allow file descriptor be closed when still in use
coffeys
parents: 5506
diff changeset
     2
 * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.nio.channels.FileChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.nio.ch.FileChannelImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Instances of this class support both reading and writing to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * random access file. A random access file behaves like a large
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * array of bytes stored in the file system. There is a kind of cursor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * or index into the implied array, called the <em>file pointer</em>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * input operations read bytes starting at the file pointer and advance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * the file pointer past the bytes read. If the random access file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * created in read/write mode, then output operations are also available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * output operations write bytes starting at the file pointer and advance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * the file pointer past the bytes written. Output operations that write
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * past the current end of the implied array cause the array to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * extended. The file pointer can be read by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <code>getFilePointer</code> method and set by the <code>seek</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * It is generally true of all the reading routines in this class that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * if end-of-file is reached before the desired number of bytes has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * read, an <code>EOFException</code> (which is a kind of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <code>IOException</code>) is thrown. If any byte cannot be read for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * any reason other than end-of-file, an <code>IOException</code> other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * than <code>EOFException</code> is thrown. In particular, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <code>IOException</code> may be thrown if the stream has been closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author  unascribed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public class RandomAccessFile implements DataOutput, DataInput, Closeable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private FileDescriptor fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private FileChannel channel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private boolean rw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private Object closeLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private volatile boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static final int O_RDONLY = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static final int O_RDWR =   2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static final int O_SYNC =   4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static final int O_DSYNC =  8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Creates a random access file stream to read from, and optionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * to write to, a file with the specified name. A new
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * {@link FileDescriptor} object is created to represent the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * connection to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * <p> The <tt>mode</tt> argument specifies the access mode with which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * file is to be opened.  The permitted values and their meanings are as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * specified for the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * href="#mode"><tt>RandomAccessFile(File,String)</tt></a> constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * If there is a security manager, its <code>checkRead</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * is called with the <code>name</code> argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * as its argument to see if read access to the file is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * If the mode allows writing, the security manager's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * <code>checkWrite</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * is also called with the <code>name</code> argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * as its argument to see if write access to the file is allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param      name   the system-dependent filename
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param      mode   the access <a href="#mode">mode</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @exception  IllegalArgumentException  if the mode argument is not equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *               to one of <tt>"r"</tt>, <tt>"rw"</tt>, <tt>"rws"</tt>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *               <tt>"rwd"</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @exception FileNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *            if the mode is <tt>"r"</tt> but the given string does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *            denote an existing regular file, or if the mode begins with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *            <tt>"rw"</tt> but the given string does not denote an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *            existing, writable regular file and a new regular file of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *            that name cannot be created, or if some other error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *            while opening or creating the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @exception  SecurityException         if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *               <code>checkRead</code> method denies read access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *               or the mode is "rw" and the security manager's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *               <code>checkWrite</code> method denies write access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @see        java.lang.SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @see        java.lang.SecurityManager#checkRead(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public RandomAccessFile(String name, String mode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        throws FileNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        this(name != null ? new File(name) : null, mode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Creates a random access file stream to read from, and optionally to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * write to, the file specified by the {@link File} argument.  A new {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * FileDescriptor} object is created to represent this file connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * <a name="mode"><p> The <tt>mode</tt> argument specifies the access mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * in which the file is to be opened.  The permitted values and their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * meanings are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <blockquote><table summary="Access mode permitted values and meanings">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * <tr><th><p align="left">Value</p></th><th><p align="left">Meaning</p></th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * <tr><td valign="top"><tt>"r"</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *     <td> Open for reading only.  Invoking any of the <tt>write</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *     methods of the resulting object will cause an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *     java.io.IOException} to be thrown. </td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * <tr><td valign="top"><tt>"rw"</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *     <td> Open for reading and writing.  If the file does not already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *     exist then an attempt will be made to create it. </td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * <tr><td valign="top"><tt>"rws"</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *     <td> Open for reading and writing, as with <tt>"rw"</tt>, and also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *     require that every update to the file's content or metadata be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *     written synchronously to the underlying storage device.  </td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * <tr><td valign="top"><tt>"rwd"&nbsp;&nbsp;</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *     <td> Open for reading and writing, as with <tt>"rw"</tt>, and also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *     require that every update to the file's content be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *     synchronously to the underlying storage device. </td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * </table></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * The <tt>"rws"</tt> and <tt>"rwd"</tt> modes work much like the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * java.nio.channels.FileChannel#force(boolean) force(boolean)} method of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * the {@link java.nio.channels.FileChannel} class, passing arguments of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * <tt>true</tt> and <tt>false</tt>, respectively, except that they always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * apply to every I/O operation and are therefore often more efficient.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * the file resides on a local storage device then when an invocation of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * method of this class returns it is guaranteed that all changes made to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * the file by that invocation will have been written to that device.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * is useful for ensuring that critical information is not lost in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * event of a system crash.  If the file does not reside on a local device
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * then no such guarantee is made.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * <p> The <tt>"rwd"</tt> mode can be used to reduce the number of I/O
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * operations performed.  Using <tt>"rwd"</tt> only requires updates to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * file's content to be written to storage; using <tt>"rws"</tt> requires
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * updates to both the file's content and its metadata to be written, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * generally requires at least one more low-level I/O operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * <p> If there is a security manager, its <code>checkRead</code> method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * called with the pathname of the <code>file</code> argument as its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * argument to see if read access to the file is allowed.  If the mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * allows writing, the security manager's <code>checkWrite</code> method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * also called with the path argument to see if write access to the file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param      file   the file object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @param      mode   the access mode, as described
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *                    <a href="#mode">above</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @exception  IllegalArgumentException  if the mode argument is not equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *               to one of <tt>"r"</tt>, <tt>"rw"</tt>, <tt>"rws"</tt>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *               <tt>"rwd"</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @exception FileNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *            if the mode is <tt>"r"</tt> but the given file object does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *            not denote an existing regular file, or if the mode begins
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *            with <tt>"rw"</tt> but the given file object does not denote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *            an existing, writable regular file and a new regular file of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *            that name cannot be created, or if some other error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *            while opening or creating the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @exception  SecurityException         if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *               <code>checkRead</code> method denies read access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *               or the mode is "rw" and the security manager's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *               <code>checkWrite</code> method denies write access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @see        java.lang.SecurityManager#checkRead(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @see        java.nio.channels.FileChannel#force(boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public RandomAccessFile(File file, String mode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        throws FileNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        String name = (file != null ? file.getPath() : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        int imode = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (mode.equals("r"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            imode = O_RDONLY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        else if (mode.startsWith("rw")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            imode = O_RDWR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            rw = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            if (mode.length() > 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                if (mode.equals("rws"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    imode |= O_SYNC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                else if (mode.equals("rwd"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    imode |= O_DSYNC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    imode = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (imode < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            throw new IllegalArgumentException("Illegal mode \"" + mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                               + "\" must be one of "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                               + "\"r\", \"rw\", \"rws\","
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                                               + " or \"rwd\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            security.checkRead(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            if (rw) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                security.checkWrite(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        fd = new FileDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        fd.incrementAndGetUseCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        open(name, imode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Returns the opaque file descriptor object associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * stream. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @return     the file descriptor object associated with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @see        java.io.FileDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public final FileDescriptor getFD() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (fd != null) return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        throw new IOException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * object associated with this file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * <p> The {@link java.nio.channels.FileChannel#position()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * </code>position<code>} of the returned channel will always be equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * this object's file-pointer offset as returned by the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * #getFilePointer getFilePointer} method.  Changing this object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * file-pointer offset, whether explicitly or by reading or writing bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * will change the position of the channel, and vice versa.  Changing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * file's length via this object will change the length seen via the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * channel, and vice versa.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @return  the file channel associated with this file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public final FileChannel getChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            if (channel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                channel = FileChannelImpl.open(fd, true, rw, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                 * FileDescriptor could be shared by FileInputStream or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                 * FileOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                 * Ensure that FD is GC'ed only when all the streams/channels
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                 * are done using it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                 * Increment fd's use count. Invoking the channel's close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                 * method will result in decrementing the use count set for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                 * the channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                fd.incrementAndGetUseCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            return channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Opens a file and returns the file descriptor.  The file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * opened in read-write mode if the O_RDWR bit in <code>mode</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * is true, else the file is opened as read-only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * If the <code>name</code> refers to a directory, an IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @param name the name of the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @param mode the mode flags, a combination of the O_ constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *             defined above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    private native void open(String name, int mode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        throws FileNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    // 'Read' primitives
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Reads a byte of data from this file. The byte is returned as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * integer in the range 0 to 255 (<code>0x00-0x0ff</code>). This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * method blocks if no input is yet available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * Although <code>RandomAccessFile</code> is not a subclass of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * <code>InputStream</code>, this method behaves in exactly the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * way as the {@link InputStream#read()} method of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * <code>InputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @return     the next byte of data, or <code>-1</code> if the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *             file has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @exception  IOException  if an I/O error occurs. Not thrown if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *                          end-of-file has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    public native int read() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * Reads a sub array as a sequence of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @param b the buffer into which the data is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @param off the start offset of the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @param len the number of bytes to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @exception IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    private native int readBytes(byte b[], int off, int len) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * Reads up to <code>len</code> bytes of data from this file into an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * array of bytes. This method blocks until at least one byte of input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Although <code>RandomAccessFile</code> is not a subclass of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * <code>InputStream</code>, this method behaves in exactly the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * same way as the {@link InputStream#read(byte[], int, int)} method of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * <code>InputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @param      b     the buffer into which the data is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @param      off   the start offset in array <code>b</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *                   at which the data is written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @param      len   the maximum number of bytes read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @return     the total number of bytes read into the buffer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *             <code>-1</code> if there is no more data because the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *             the file has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @exception  IOException If the first byte cannot be read for any reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * other than end of file, or if the random access file has been closed, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * some other I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @exception  NullPointerException If <code>b</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @exception  IndexOutOfBoundsException If <code>off</code> is negative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * <code>len</code> is negative, or <code>len</code> is greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * <code>b.length - off</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public int read(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        return readBytes(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Reads up to <code>b.length</code> bytes of data from this file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * into an array of bytes. This method blocks until at least one byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * of input is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * Although <code>RandomAccessFile</code> is not a subclass of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * <code>InputStream</code>, this method behaves in exactly the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * same way as the {@link InputStream#read(byte[])} method of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * <code>InputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @param      b   the buffer into which the data is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @return     the total number of bytes read into the buffer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *             <code>-1</code> if there is no more data because the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *             this file has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @exception  IOException If the first byte cannot be read for any reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * other than end of file, or if the random access file has been closed, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * some other I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @exception  NullPointerException If <code>b</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    public int read(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        return readBytes(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * Reads <code>b.length</code> bytes from this file into the byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * array, starting at the current file pointer. This method reads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * repeatedly from the file until the requested number of bytes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * read. This method blocks until the requested number of bytes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * read, the end of the stream is detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * @param      b   the buffer into which the data is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @exception  EOFException  if this file reaches the end before reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *               all the bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @exception  IOException   if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    public final void readFully(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        readFully(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * Reads exactly <code>len</code> bytes from this file into the byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * array, starting at the current file pointer. This method reads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * repeatedly from the file until the requested number of bytes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * read. This method blocks until the requested number of bytes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * read, the end of the stream is detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @param      b     the buffer into which the data is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @param      off   the start offset of the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @param      len   the number of bytes to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @exception  EOFException  if this file reaches the end before reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *               all the bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @exception  IOException   if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public final void readFully(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            int count = this.read(b, off + n, len - n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            if (count < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            n += count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        } while (n < len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * Attempts to skip over <code>n</code> bytes of input discarding the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * skipped bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * This method may skip over some smaller number of bytes, possibly zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * This may result from any of a number of conditions; reaching end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * file before <code>n</code> bytes have been skipped is only one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * possibility. This method never throws an <code>EOFException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * The actual number of bytes skipped is returned.  If <code>n</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * is negative, no bytes are skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @param      n   the number of bytes to be skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @return     the actual number of bytes skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    public int skipBytes(int n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        long pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        long len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        long newpos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        if (n <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        pos = getFilePointer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        len = length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        newpos = pos + n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        if (newpos > len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            newpos = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        seek(newpos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        /* return the actual number of bytes skipped */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        return (int) (newpos - pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    // 'Write' primitives
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * Writes the specified byte to this file. The write starts at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * the current file pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * @param      b   the <code>byte</code> to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    public native void write(int b) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * Writes a sub array as a sequence of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * @param off the start offset in the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * @param len the number of bytes that are written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @exception IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    private native void writeBytes(byte b[], int off, int len) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * Writes <code>b.length</code> bytes from the specified byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * to this file, starting at the current file pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @param      b   the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    public void write(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        writeBytes(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * Writes <code>len</code> bytes from the specified byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * starting at offset <code>off</code> to this file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @param      b     the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @param      off   the start offset in the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @param      len   the number of bytes to write.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    public void write(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        writeBytes(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    // 'Random access' stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * Returns the current offset in this file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @return     the offset from the beginning of the file, in bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *             at which the next read or write occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    public native long getFilePointer() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * Sets the file-pointer offset, measured from the beginning of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * file, at which the next read or write occurs.  The offset may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * set beyond the end of the file. Setting the offset beyond the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * of the file does not change the file length.  The file length will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * change only by writing after the offset has been set beyond the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * of the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @param      pos   the offset position, measured in bytes from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     *                   beginning of the file, at which to set the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     *                   pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * @exception  IOException  if <code>pos</code> is less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *                          <code>0</code> or if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    public native void seek(long pos) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * Returns the length of this file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * @return     the length of this file, measured in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    public native long length() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * Sets the length of this file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * <p> If the present length of the file as returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * <code>length</code> method is greater than the <code>newLength</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * argument then the file will be truncated.  In this case, if the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * offset as returned by the <code>getFilePointer</code> method is greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * than <code>newLength</code> then after this method returns the offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * will be equal to <code>newLength</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * <p> If the present length of the file as returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * <code>length</code> method is smaller than the <code>newLength</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * argument then the file will be extended.  In this case, the contents of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * the extended portion of the file are not defined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * @param      newLength    The desired length of the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @since      1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    public native void setLength(long newLength) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * Closes this random access file stream and releases any system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * resources associated with the stream. A closed random access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * file cannot perform input or output operations and cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * reopened.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * <p> If this file has an associated channel then the channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        synchronized (closeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        if (channel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
             * Decrement FD use count associated with the channel. The FD use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
             * count is incremented whenever a new channel is obtained from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
             * this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            fd.decrementAndGetUseCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            channel.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
         * Decrement FD use count associated with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
         * The count got incremented by FileDescriptor during its construction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
         */
10586
6e20ecfec8ed 7082769: FileInputStream/FileOutputStream/RandomAccessFile allow file descriptor be closed when still in use
coffeys
parents: 5506
diff changeset
   593
        int useCount = fd.decrementAndGetUseCount();
6e20ecfec8ed 7082769: FileInputStream/FileOutputStream/RandomAccessFile allow file descriptor be closed when still in use
coffeys
parents: 5506
diff changeset
   594
6e20ecfec8ed 7082769: FileInputStream/FileOutputStream/RandomAccessFile allow file descriptor be closed when still in use
coffeys
parents: 5506
diff changeset
   595
        /*
6e20ecfec8ed 7082769: FileInputStream/FileOutputStream/RandomAccessFile allow file descriptor be closed when still in use
coffeys
parents: 5506
diff changeset
   596
         * If FileDescriptor is still in use by another stream, we
6e20ecfec8ed 7082769: FileInputStream/FileOutputStream/RandomAccessFile allow file descriptor be closed when still in use
coffeys
parents: 5506
diff changeset
   597
         * will not close it.
6e20ecfec8ed 7082769: FileInputStream/FileOutputStream/RandomAccessFile allow file descriptor be closed when still in use
coffeys
parents: 5506
diff changeset
   598
         */
6e20ecfec8ed 7082769: FileInputStream/FileOutputStream/RandomAccessFile allow file descriptor be closed when still in use
coffeys
parents: 5506
diff changeset
   599
        if (useCount <= 0) {
6e20ecfec8ed 7082769: FileInputStream/FileOutputStream/RandomAccessFile allow file descriptor be closed when still in use
coffeys
parents: 5506
diff changeset
   600
            close0();
6e20ecfec8ed 7082769: FileInputStream/FileOutputStream/RandomAccessFile allow file descriptor be closed when still in use
coffeys
parents: 5506
diff changeset
   601
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    //  Some "reading/writing Java data types" methods stolen from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    //  DataInputStream and DataOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * Reads a <code>boolean</code> from this file. This method reads a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * single byte from the file, starting at the current file pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * A value of <code>0</code> represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * <code>false</code>. Any other value represents <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * This method blocks until the byte is read, the end of the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * is detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * @return     the <code>boolean</code> value read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * @exception  EOFException  if this file has reached the end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * @exception  IOException   if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    public final boolean readBoolean() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        int ch = this.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        if (ch < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        return (ch != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * Reads a signed eight-bit value from this file. This method reads a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * byte from the file, starting from the current file pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * If the byte read is <code>b</code>, where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * <code>0&nbsp;&lt;=&nbsp;b&nbsp;&lt;=&nbsp;255</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * then the result is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     *     (byte)(b)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * This method blocks until the byte is read, the end of the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * is detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * @return     the next byte of this file as a signed eight-bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *             <code>byte</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * @exception  EOFException  if this file has reached the end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * @exception  IOException   if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    public final byte readByte() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        int ch = this.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        if (ch < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        return (byte)(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * Reads an unsigned eight-bit number from this file. This method reads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * a byte from this file, starting at the current file pointer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * and returns that byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * This method blocks until the byte is read, the end of the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * is detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * @return     the next byte of this file, interpreted as an unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     *             eight-bit number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * @exception  EOFException  if this file has reached the end.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * @exception  IOException   if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    public final int readUnsignedByte() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        int ch = this.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        if (ch < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        return ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * Reads a signed 16-bit number from this file. The method reads two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * bytes from this file, starting at the current file pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * If the two bytes read, in order, are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * <code>b1</code> and <code>b2</code>, where each of the two values is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * between <code>0</code> and <code>255</code>, inclusive, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * result is equal to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     *     (short)((b1 &lt;&lt; 8) | b2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * This method blocks until the two bytes are read, the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * stream is detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * @return     the next two bytes of this file, interpreted as a signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *             16-bit number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * @exception  EOFException  if this file reaches the end before reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     *               two bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * @exception  IOException   if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    public final short readShort() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        int ch1 = this.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        int ch2 = this.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        if ((ch1 | ch2) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        return (short)((ch1 << 8) + (ch2 << 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * Reads an unsigned 16-bit number from this file. This method reads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * two bytes from the file, starting at the current file pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * If the bytes read, in order, are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * <code>b1</code> and <code>b2</code>, where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * <code>0&nbsp;&lt;=&nbsp;b1, b2&nbsp;&lt;=&nbsp;255</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * then the result is equal to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     *     (b1 &lt;&lt; 8) | b2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * This method blocks until the two bytes are read, the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * stream is detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * @return     the next two bytes of this file, interpreted as an unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     *             16-bit integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * @exception  EOFException  if this file reaches the end before reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     *               two bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * @exception  IOException   if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    public final int readUnsignedShort() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        int ch1 = this.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        int ch2 = this.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        if ((ch1 | ch2) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        return (ch1 << 8) + (ch2 << 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * Reads a character from this file. This method reads two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * bytes from the file, starting at the current file pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * If the bytes read, in order, are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * <code>b1</code> and <code>b2</code>, where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * <code>0&nbsp;&lt;=&nbsp;b1,&nbsp;b2&nbsp;&lt;=&nbsp;255</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * then the result is equal to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     *     (char)((b1 &lt;&lt; 8) | b2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * This method blocks until the two bytes are read, the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * stream is detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * @return     the next two bytes of this file, interpreted as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     *                  <code>char</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * @exception  EOFException  if this file reaches the end before reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     *               two bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * @exception  IOException   if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    public final char readChar() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        int ch1 = this.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        int ch2 = this.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        if ((ch1 | ch2) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        return (char)((ch1 << 8) + (ch2 << 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * Reads a signed 32-bit integer from this file. This method reads 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * bytes from the file, starting at the current file pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * If the bytes read, in order, are <code>b1</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * <code>b2</code>, <code>b3</code>, and <code>b4</code>, where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * <code>0&nbsp;&lt;=&nbsp;b1, b2, b3, b4&nbsp;&lt;=&nbsp;255</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * then the result is equal to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     *     (b1 &lt;&lt; 24) | (b2 &lt;&lt; 16) + (b3 &lt;&lt; 8) + b4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * This method blocks until the four bytes are read, the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * stream is detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * @return     the next four bytes of this file, interpreted as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     *             <code>int</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * @exception  EOFException  if this file reaches the end before reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     *               four bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * @exception  IOException   if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    public final int readInt() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        int ch1 = this.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        int ch2 = this.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        int ch3 = this.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        int ch4 = this.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        if ((ch1 | ch2 | ch3 | ch4) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * Reads a signed 64-bit integer from this file. This method reads eight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * bytes from the file, starting at the current file pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * If the bytes read, in order, are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * <code>b1</code>, <code>b2</code>, <code>b3</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * <code>b4</code>, <code>b5</code>, <code>b6</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * <code>b7</code>, and <code>b8,</code> where:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     *     0 &lt;= b1, b2, b3, b4, b5, b6, b7, b8 &lt;=255,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * then the result is equal to:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * <p><blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     *     ((long)b1 &lt;&lt; 56) + ((long)b2 &lt;&lt; 48)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     *     + ((long)b3 &lt;&lt; 40) + ((long)b4 &lt;&lt; 32)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     *     + ((long)b5 &lt;&lt; 24) + ((long)b6 &lt;&lt; 16)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     *     + ((long)b7 &lt;&lt; 8) + b8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * This method blocks until the eight bytes are read, the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * stream is detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * @return     the next eight bytes of this file, interpreted as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     *             <code>long</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * @exception  EOFException  if this file reaches the end before reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     *               eight bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * @exception  IOException   if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    public final long readLong() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * Reads a <code>float</code> from this file. This method reads an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * <code>int</code> value, starting at the current file pointer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * as if by the <code>readInt</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * and then converts that <code>int</code> to a <code>float</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * using the <code>intBitsToFloat</code> method in class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * <code>Float</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * This method blocks until the four bytes are read, the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * stream is detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * @return     the next four bytes of this file, interpreted as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     *             <code>float</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * @exception  EOFException  if this file reaches the end before reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     *             four bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * @exception  IOException   if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * @see        java.io.RandomAccessFile#readInt()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * @see        java.lang.Float#intBitsToFloat(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    public final float readFloat() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        return Float.intBitsToFloat(readInt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * Reads a <code>double</code> from this file. This method reads a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * <code>long</code> value, starting at the current file pointer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * as if by the <code>readLong</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * and then converts that <code>long</code> to a <code>double</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * using the <code>longBitsToDouble</code> method in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * class <code>Double</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * This method blocks until the eight bytes are read, the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * stream is detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * @return     the next eight bytes of this file, interpreted as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     *             <code>double</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * @exception  EOFException  if this file reaches the end before reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     *             eight bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * @exception  IOException   if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * @see        java.io.RandomAccessFile#readLong()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * @see        java.lang.Double#longBitsToDouble(long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    public final double readDouble() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        return Double.longBitsToDouble(readLong());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * Reads the next line of text from this file.  This method successively
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * reads bytes from the file, starting at the current file pointer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * until it reaches a line terminator or the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * of the file.  Each byte is converted into a character by taking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * byte's value for the lower eight bits of the character and setting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * high eight bits of the character to zero.  This method does not,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * therefore, support the full Unicode character set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * <p> A line of text is terminated by a carriage-return character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * (<code>'&#92;r'</code>), a newline character (<code>'&#92;n'</code>), a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * carriage-return character immediately followed by a newline character,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * or the end of the file.  Line-terminating characters are discarded and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * are not included as part of the string returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * <p> This method blocks until a newline character is read, a carriage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * return and the byte following it are read (to see if it is a newline),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * the end of the file is reached, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * @return     the next line of text from this file, or null if end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     *             of file is encountered before even one byte is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    public final String readLine() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        StringBuffer input = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        int c = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        boolean eol = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        while (!eol) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            switch (c = read()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            case -1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            case '\n':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                eol = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            case '\r':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                eol = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                long cur = getFilePointer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                if ((read()) != '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                    seek(cur);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                input.append((char)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        if ((c == -1) && (input.length() == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        return input.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * Reads in a string from this file. The string has been encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * using a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * The first two bytes are read, starting from the current file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * pointer, as if by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * <code>readUnsignedShort</code>. This value gives the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * following bytes that are in the encoded string, not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * the length of the resulting string. The following bytes are then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * interpreted as bytes encoding characters in the modified UTF-8 format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * and are converted into characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * This method blocks until all the bytes are read, the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * stream is detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * @return     a Unicode string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * @exception  EOFException            if this file reaches the end before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     *               reading all the bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * @exception  IOException             if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * @exception  UTFDataFormatException  if the bytes do not represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     *               valid modified UTF-8 encoding of a Unicode string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * @see        java.io.RandomAccessFile#readUnsignedShort()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    public final String readUTF() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        return DataInputStream.readUTF(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * Writes a <code>boolean</code> to the file as a one-byte value. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * value <code>true</code> is written out as the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * <code>(byte)1</code>; the value <code>false</code> is written out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * as the value <code>(byte)0</code>. The write starts at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * the current position of the file pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * @param      v   a <code>boolean</code> value to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    public final void writeBoolean(boolean v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        write(v ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        //written++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * Writes a <code>byte</code> to the file as a one-byte value. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * write starts at the current position of the file pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * @param      v   a <code>byte</code> value to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
    public final void writeByte(int v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        write(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        //written++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * Writes a <code>short</code> to the file as two bytes, high byte first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * The write starts at the current position of the file pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * @param      v   a <code>short</code> to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    public final void writeShort(int v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        write((v >>> 8) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        write((v >>> 0) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        //written += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * Writes a <code>char</code> to the file as a two-byte value, high
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * byte first. The write starts at the current position of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * file pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * @param      v   a <code>char</code> value to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    public final void writeChar(int v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        write((v >>> 8) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        write((v >>> 0) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        //written += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * Writes an <code>int</code> to the file as four bytes, high byte first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * The write starts at the current position of the file pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * @param      v   an <code>int</code> to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    public final void writeInt(int v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        write((v >>> 24) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        write((v >>> 16) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        write((v >>>  8) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        write((v >>>  0) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        //written += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * Writes a <code>long</code> to the file as eight bytes, high byte first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * The write starts at the current position of the file pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * @param      v   a <code>long</code> to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    public final void writeLong(long v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        write((int)(v >>> 56) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        write((int)(v >>> 48) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        write((int)(v >>> 40) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        write((int)(v >>> 32) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        write((int)(v >>> 24) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        write((int)(v >>> 16) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        write((int)(v >>>  8) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        write((int)(v >>>  0) & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        //written += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * Converts the float argument to an <code>int</code> using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * <code>floatToIntBits</code> method in class <code>Float</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * and then writes that <code>int</code> value to the file as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * four-byte quantity, high byte first. The write starts at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * current position of the file pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * @param      v   a <code>float</code> value to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * @see        java.lang.Float#floatToIntBits(float)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
    public final void writeFloat(float v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        writeInt(Float.floatToIntBits(v));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * Converts the double argument to a <code>long</code> using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * <code>doubleToLongBits</code> method in class <code>Double</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * and then writes that <code>long</code> value to the file as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * eight-byte quantity, high byte first. The write starts at the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * position of the file pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * @param      v   a <code>double</code> value to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * @see        java.lang.Double#doubleToLongBits(double)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
    public final void writeDouble(double v) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        writeLong(Double.doubleToLongBits(v));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * Writes the string to the file as a sequence of bytes. Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * character in the string is written out, in sequence, by discarding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * its high eight bits. The write starts at the current position of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * the file pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * @param      s   a string of bytes to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    public final void writeBytes(String s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        int len = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        byte[] b = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        s.getBytes(0, len, b, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        writeBytes(b, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * Writes a string to the file as a sequence of characters. Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * character is written to the data output stream as if by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * <code>writeChar</code> method. The write starts at the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * position of the file pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * @param      s   a <code>String</code> value to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * @see        java.io.RandomAccessFile#writeChar(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
    public final void writeChars(String s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        int clen = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        int blen = 2*clen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        byte[] b = new byte[blen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        char[] c = new char[clen];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        s.getChars(0, clen, c, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        for (int i = 0, j = 0; i < clen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            b[j++] = (byte)(c[i] >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            b[j++] = (byte)(c[i] >>> 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        writeBytes(b, 0, blen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * Writes a string to the file using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * encoding in a machine-independent manner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * First, two bytes are written to the file, starting at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     * current file pointer, as if by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * <code>writeShort</code> method giving the number of bytes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * follow. This value is the number of bytes actually written out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * not the length of the string. Following the length, each character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * of the string is output, in sequence, using the modified UTF-8 encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * for each character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * @param      str   a string to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    public final void writeUTF(String str) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        DataOutputStream.writeUTF(str, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    private native void close0() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
}