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