jdk/src/share/classes/java/io/FileInputStream.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8158 77d9c0f1c19f
child 10586 6e20ecfec8ed
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8158
diff changeset
     2
 * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.nio.channels.FileChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.nio.ch.FileChannelImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A <code>FileInputStream</code> obtains input bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * from a file in a file system. What files
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * are  available depends on the host environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p><code>FileInputStream</code> is meant for reading streams of raw bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * such as image data. For reading streams of characters, consider using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <code>FileReader</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author  Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @see     java.io.File
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @see     java.io.FileDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @see     java.io.FileOutputStream
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
    45
 * @see     java.nio.file.Files#newInputStream
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
class FileInputStream extends InputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /* File Descriptor - handle to the open file */
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
    52
    private final FileDescriptor fd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private FileChannel channel = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
    56
    private final Object closeLock = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private volatile boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
    59
    private static final ThreadLocal<Boolean> runningFinalize =
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
    60
        new ThreadLocal<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static boolean isRunningFinalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        Boolean val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if ((val = runningFinalize.get()) != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            return val.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Creates a <code>FileInputStream</code> by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * opening a connection to an actual file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * the file named by the path name <code>name</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * in the file system.  A new <code>FileDescriptor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * object is created to represent this file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * First, if there is a security
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * manager, its <code>checkRead</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * is called with the <code>name</code> argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * If the named file does not exist, is a directory rather than a regular
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * file, or for some other reason cannot be opened for reading then a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * <code>FileNotFoundException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param      name   the system-dependent file name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @exception  FileNotFoundException  if the file does not exist,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *                   is a directory rather than a regular file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *                   or for some other reason cannot be opened for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *                   reading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @exception  SecurityException      if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *               <code>checkRead</code> method denies read access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *               to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @see        java.lang.SecurityManager#checkRead(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public FileInputStream(String name) throws FileNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        this(name != null ? new File(name) : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Creates a <code>FileInputStream</code> by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * opening a connection to an actual file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * the file named by the <code>File</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * object <code>file</code> in the file system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * A new <code>FileDescriptor</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * is created to represent this file connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * First, if there is a security manager,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * its <code>checkRead</code> method  is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * with the path represented by the <code>file</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * argument as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * If the named file does not exist, is a directory rather than a regular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * file, or for some other reason cannot be opened for reading then a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * <code>FileNotFoundException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param      file   the file to be opened for reading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @exception  FileNotFoundException  if the file does not exist,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *                   is a directory rather than a regular file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *                   or for some other reason cannot be opened for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *                   reading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @exception  SecurityException      if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *               <code>checkRead</code> method denies read access to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @see        java.io.File#getPath()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @see        java.lang.SecurityManager#checkRead(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public FileInputStream(File file) throws FileNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        String name = (file != null ? file.getPath() : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            security.checkRead(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        fd = new FileDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        fd.incrementAndGetUseCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        open(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Creates a <code>FileInputStream</code> by using the file descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * <code>fdObj</code>, which represents an existing connection to an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * actual file in the file system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * If there is a security manager, its <code>checkRead</code> method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * called with the file descriptor <code>fdObj</code> as its argument to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * see if it's ok to read the file descriptor. If read access is denied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * to the file descriptor a <code>SecurityException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * If <code>fdObj</code> is null then a <code>NullPointerException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * This constructor does not throw an exception if <code>fdObj</code>
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
   155
     * is {@link java.io.FileDescriptor#valid() invalid}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * However, if the methods are invoked on the resulting stream to attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * I/O on the stream, an <code>IOException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param      fdObj   the file descriptor to be opened for reading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @throws     SecurityException      if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *                 <code>checkRead</code> method denies read access to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *                 file descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @see        SecurityManager#checkRead(java.io.FileDescriptor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public FileInputStream(FileDescriptor fdObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (fdObj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            security.checkRead(fdObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        fd = fdObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
         * FileDescriptor is being shared by streams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
         * Ensure that it's GC'ed only when all the streams/channels are done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
         * using it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        fd.incrementAndGetUseCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Opens the specified file for reading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @param name the name of the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    private native void open(String name) throws FileNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Reads a byte of data from this input stream. This method blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * if no input is yet available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @return     the next byte of data, or <code>-1</code> if the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *             file is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public native int read() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Reads a subarray as a sequence of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @param off the start offset in the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @param len the number of bytes that are written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @exception IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    private native int readBytes(byte b[], int off, int len) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Reads up to <code>b.length</code> bytes of data from this input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * stream into an array of bytes. This method blocks until some input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @param      b   the buffer into which the data is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @return     the total number of bytes read into the buffer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *             <code>-1</code> if there is no more data because the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *             the file has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public int read(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return readBytes(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Reads up to <code>len</code> bytes of data from this input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * into an array of bytes. If <code>len</code> is not zero, the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * blocks until some input is available; otherwise, no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * bytes are read and <code>0</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @param      b     the buffer into which the data is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @param      off   the start offset in the destination array <code>b</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @param      len   the maximum number of bytes read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @return     the total number of bytes read into the buffer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *             <code>-1</code> if there is no more data because the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *             the file has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @exception  NullPointerException If <code>b</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @exception  IndexOutOfBoundsException If <code>off</code> is negative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * <code>len</code> is negative, or <code>len</code> is greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * <code>b.length - off</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public int read(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        return readBytes(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Skips over and discards <code>n</code> bytes of data from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * <p>The <code>skip</code> method may, for a variety of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * reasons, end up skipping over some smaller number of bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * possibly <code>0</code>. If <code>n</code> is negative, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * <code>IOException</code> is thrown, even though the <code>skip</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * method of the {@link InputStream} superclass does nothing in this case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * The actual number of bytes skipped is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * <p>This method may skip more bytes than are remaining in the backing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * file. This produces no exception and the number of bytes skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * may include some number of bytes that were beyond the EOF of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * backing file. Attempting to read from the stream after skipping past
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * the end will result in -1 indicating the end of the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param      n   the number of bytes to be skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @return     the actual number of bytes skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @exception  IOException  if n is negative, if the stream does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *             support seek, or if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public native long skip(long n) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Returns an estimate of the number of remaining bytes that can be read (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * skipped over) from this input stream without blocking by the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * invocation of a method for this input stream. The next invocation might be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * the same thread or another thread.  A single read or skip of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * many bytes will not block, but may read or skip fewer bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * <p> In some cases, a non-blocking read (or skip) may appear to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * blocked when it is merely slow, for example when reading large
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * files over slow networks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @return     an estimate of the number of remaining bytes that can be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *             (or skipped over) from this input stream without blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @exception  IOException  if this file input stream has been closed by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *             {@code close} or an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    public native int available() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Closes this file input stream and releases any system resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * associated with the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * <p> If this stream has an associated channel then the channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        synchronized (closeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (channel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
             * Decrement the FD use count associated with the channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
             * The use count is incremented whenever a new channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
             * is obtained from this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
           fd.decrementAndGetUseCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
           channel.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
         * Decrement the FD use count associated with this stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        int useCount = fd.decrementAndGetUseCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
         * If FileDescriptor is still in use by another stream, the finalizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
         * will not close it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if ((useCount <= 0) || !isRunningFinalize()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            close0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Returns the <code>FileDescriptor</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * object  that represents the connection to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * the actual file in the file system being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * used by this <code>FileInputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @return     the file descriptor object associated with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @see        java.io.FileDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public final FileDescriptor getFD() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        if (fd != null) return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        throw new IOException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * object associated with this file input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * <p> The initial {@link java.nio.channels.FileChannel#position()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * </code>position<code>} of the returned channel will be equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * number of bytes read from the file so far.  Reading bytes from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * stream will increment the channel's position.  Changing the channel's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * position, either explicitly or by reading, will change this stream's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * file position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @return  the file channel associated with this file input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    public FileChannel getChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            if (channel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                channel = FileChannelImpl.open(fd, true, false, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                 * Increment fd's use count. Invoking the channel's close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                 * method will result in decrementing the use count set for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                 * the channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                fd.incrementAndGetUseCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            return channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    private native void close0() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Ensures that the <code>close</code> method of this file input stream is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * called when there are no more references to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @see        java.io.FileInputStream#close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    protected void finalize() throws IOException {
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
   393
        if ((fd != null) &&  (fd != FileDescriptor.in)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
             * Finalizer should not release the FileDescriptor if another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
             * stream is still using it. If the user directly invokes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
             * close() then the FileDescriptor is also released.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            runningFinalize.set(Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                runningFinalize.set(Boolean.FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
}