src/java.base/share/classes/java/io/FileInputStream.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58288 48e480e56aad
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52149
diff changeset
     2
 * Copyright (c) 1994, 2019, 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
/**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    33
 * A {@code FileInputStream} obtains input bytes
2
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
 *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    37
 * <p>{@code FileInputStream} is meant for reading streams of raw bytes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * such as image data. For reading streams of characters, consider using
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    39
 * {@code FileReader}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
48224
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    41
 * @apiNote
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    42
 * To release resources used by this stream {@link #close} should be called
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    43
 * directly or by try-with-resources. Subclasses are responsible for the cleanup
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    44
 * of resources acquired by the subclass.
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    45
 * Subclasses that override {@link #finalize} in order to perform cleanup
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    46
 * should be modified to use alternative cleanup mechanisms such as
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    47
 * {@link java.lang.ref.Cleaner} and remove the overriding {@code finalize} method.
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    48
 *
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    49
 * @implSpec
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    50
 * If this FileInputStream has been subclassed and the {@link #close}
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    51
 * method has been overridden, the {@link #close} method will be
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    52
 * called when the FileInputStream is unreachable.
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    53
 * Otherwise, it is implementation specific how the resource cleanup described in
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    54
 * {@link #close} is performed.
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    55
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    56
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author  Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see     java.io.File
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see     java.io.FileDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @see     java.io.FileOutputStream
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
    61
 * @see     java.nio.file.Files#newInputStream
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 22946
diff changeset
    62
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
class FileInputStream extends InputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /* 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
    68
    private final FileDescriptor fd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
22945
89dd803515d8 8033917: Keep track of file paths in file streams and channels for instrumentation purposes
sla
parents: 18786
diff changeset
    70
    /**
89dd803515d8 8033917: Keep track of file paths in file streams and channels for instrumentation purposes
sla
parents: 18786
diff changeset
    71
     * The path of the referenced file
89dd803515d8 8033917: Keep track of file paths in file streams and channels for instrumentation purposes
sla
parents: 18786
diff changeset
    72
     * (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: 18786
diff changeset
    73
     */
89dd803515d8 8033917: Keep track of file paths in file streams and channels for instrumentation purposes
sla
parents: 18786
diff changeset
    74
    private final String path;
89dd803515d8 8033917: Keep track of file paths in file streams and channels for instrumentation purposes
sla
parents: 18786
diff changeset
    75
28062
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 26190
diff changeset
    76
    private volatile FileChannel channel;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
41769
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 28062
diff changeset
    78
    private final Object closeLock = new Object();
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 28062
diff changeset
    79
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 28062
diff changeset
    80
    private volatile boolean closed;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    83
     * Creates a {@code FileInputStream} by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * opening a connection to an actual file,
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    85
     * the file named by the path name {@code name}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    86
     * in the file system.  A new {@code FileDescriptor}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * object is created to represent this file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * First, if there is a security
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    91
     * manager, its {@code checkRead} method
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    92
     * is called with the {@code name} argument
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * If the named file does not exist, is a directory rather than a regular
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * file, or for some other reason cannot be opened for reading then a
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    97
     * {@code FileNotFoundException} is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param      name   the system-dependent file name.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52149
diff changeset
   100
     * @throws     FileNotFoundException  if the file does not exist,
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52149
diff changeset
   101
     *             is a directory rather than a regular file,
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52149
diff changeset
   102
     *             or for some other reason cannot be opened for
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52149
diff changeset
   103
     *             reading.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52149
diff changeset
   104
     * @throws     SecurityException      if a security manager exists and its
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   105
     *             {@code checkRead} method denies read access
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52149
diff changeset
   106
     *             to the file.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @see        java.lang.SecurityManager#checkRead(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public FileInputStream(String name) throws FileNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        this(name != null ? new File(name) : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   114
     * Creates a {@code FileInputStream} by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * opening a connection to an actual file,
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   116
     * the file named by the {@code File}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   117
     * object {@code file} in the file system.
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   118
     * A new {@code FileDescriptor} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * is created to represent this file connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * First, if there is a security manager,
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   122
     * its {@code checkRead} method  is called
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   123
     * with the path represented by the {@code file}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * argument as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * If the named file does not exist, is a directory rather than a regular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * file, or for some other reason cannot be opened for reading then a
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   128
     * {@code FileNotFoundException} is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param      file   the file to be opened for reading.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52149
diff changeset
   131
     * @throws     FileNotFoundException  if the file does not exist,
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52149
diff changeset
   132
     *             is a directory rather than a regular file,
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52149
diff changeset
   133
     *             or for some other reason cannot be opened for
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52149
diff changeset
   134
     *             reading.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52149
diff changeset
   135
     * @throws     SecurityException      if a security manager exists and its
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   136
     *             {@code checkRead} method denies read access to the file.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @see        java.io.File#getPath()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @see        java.lang.SecurityManager#checkRead(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public FileInputStream(File file) throws FileNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        String name = (file != null ? file.getPath() : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            security.checkRead(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 11017
diff changeset
   149
        if (file.isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 11017
diff changeset
   150
            throw new FileNotFoundException("Invalid file path");
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 11017
diff changeset
   151
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        fd = new FileDescriptor();
11017
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   153
        fd.attach(this);
22945
89dd803515d8 8033917: Keep track of file paths in file streams and channels for instrumentation purposes
sla
parents: 18786
diff changeset
   154
        path = name;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        open(name);
52149
0edbbc64393c 8192939: Remove Finalize methods from FileInputStream and FileOutputStream
rriggs
parents: 49900
diff changeset
   156
        FileCleanable.register(fd);       // open set the fd, register the cleanup
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   160
     * Creates a {@code FileInputStream} by using the file descriptor
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   161
     * {@code fdObj}, which represents an existing connection to an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * actual file in the file system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   164
     * If there is a security manager, its {@code checkRead} method is
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   165
     * called with the file descriptor {@code fdObj} as its argument to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * see if it's ok to read the file descriptor. If read access is denied
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   167
     * to the file descriptor a {@code SecurityException} is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   169
     * If {@code fdObj} is null then a {@code NullPointerException}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   172
     * This constructor does not throw an exception if {@code fdObj}
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
   173
     * is {@link java.io.FileDescriptor#valid() invalid}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * However, if the methods are invoked on the resulting stream to attempt
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   175
     * I/O on the stream, an {@code IOException} is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param      fdObj   the file descriptor to be opened for reading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @throws     SecurityException      if a security manager exists and its
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   179
     *             {@code checkRead} method denies read access to the
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52149
diff changeset
   180
     *             file descriptor.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @see        SecurityManager#checkRead(java.io.FileDescriptor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public FileInputStream(FileDescriptor fdObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (fdObj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            security.checkRead(fdObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        fd = fdObj;
22945
89dd803515d8 8033917: Keep track of file paths in file streams and channels for instrumentation purposes
sla
parents: 18786
diff changeset
   192
        path = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
         * FileDescriptor is being shared by streams.
11017
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   196
         * Register this stream with FileDescriptor tracker.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
         */
11017
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   198
        fd.attach(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Opens the specified file for reading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @param name the name of the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
26190
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   205
    private native void open0(String name) throws FileNotFoundException;
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   206
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   207
    // wrap native call to allow instrumentation
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   208
    /**
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   209
     * Opens the specified file for reading.
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   210
     * @param name the name of the file
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   211
     */
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   212
    private void open(String name) throws FileNotFoundException {
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   213
        open0(name);
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   214
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Reads a byte of data from this input stream. This method blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * if no input is yet available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   220
     * @return     the next byte of data, or {@code -1} if the end of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *             file is reached.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52149
diff changeset
   222
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
22946
0a1379d15976 8033911: Simplify instrumentation of FileInputStream and RandomAccessFile
sla
parents: 22945
diff changeset
   224
    public int read() throws IOException {
0a1379d15976 8033911: Simplify instrumentation of FileInputStream and RandomAccessFile
sla
parents: 22945
diff changeset
   225
        return read0();
0a1379d15976 8033911: Simplify instrumentation of FileInputStream and RandomAccessFile
sla
parents: 22945
diff changeset
   226
    }
0a1379d15976 8033911: Simplify instrumentation of FileInputStream and RandomAccessFile
sla
parents: 22945
diff changeset
   227
0a1379d15976 8033911: Simplify instrumentation of FileInputStream and RandomAccessFile
sla
parents: 22945
diff changeset
   228
    private native int read0() throws IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Reads a subarray as a sequence of bytes.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52149
diff changeset
   232
     * @param     b the data to be written
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52149
diff changeset
   233
     * @param     off the start offset in the data
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52149
diff changeset
   234
     * @param     len the number of bytes that are written
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52149
diff changeset
   235
     * @throws    IOException If an I/O error has occurred.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    private native int readBytes(byte b[], int off, int len) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   240
     * Reads up to {@code b.length} bytes of data from this input
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * stream into an array of bytes. This method blocks until some input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @param      b   the buffer into which the data is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @return     the total number of bytes read into the buffer, or
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   246
     *             {@code -1} if there is no more data because the end of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *             the file has been reached.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52149
diff changeset
   248
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public int read(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return readBytes(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   255
     * Reads up to {@code len} bytes of data from this input stream
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   256
     * into an array of bytes. If {@code len} is not zero, the method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * blocks until some input is available; otherwise, no
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   258
     * bytes are read and {@code 0} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @param      b     the buffer into which the data is read.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   261
     * @param      off   the start offset in the destination array {@code b}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param      len   the maximum number of bytes read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @return     the total number of bytes read into the buffer, or
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   264
     *             {@code -1} if there is no more data because the end of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *             the file has been reached.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   266
     * @throws     NullPointerException If {@code b} is {@code null}.
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   267
     * @throws     IndexOutOfBoundsException If {@code off} is negative,
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   268
     *             {@code len} is negative, or {@code len} is greater than
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   269
     *             {@code b.length - off}
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52149
diff changeset
   270
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public int read(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return readBytes(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   277
     * Skips over and discards {@code n} bytes of data from the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   280
     * <p>The {@code skip} method may, for a variety of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * reasons, end up skipping over some smaller number of bytes,
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   282
     * possibly {@code 0}. If {@code n} is negative, the method
17691
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 17430
diff changeset
   283
     * will try to skip backwards. In case the backing file does not support
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   284
     * backward skip at its current position, an {@code IOException} is
17691
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 17430
diff changeset
   285
     * thrown. The actual number of bytes skipped is returned. If it skips
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 17430
diff changeset
   286
     * forwards, it returns a positive value. If it skips backwards, it
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 17430
diff changeset
   287
     * returns a negative value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
17691
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 17430
diff changeset
   289
     * <p>This method may skip more bytes than what are remaining in the
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 17430
diff changeset
   290
     * backing file. This produces no exception and the number of bytes skipped
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * may include some number of bytes that were beyond the EOF of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * backing file. Attempting to read from the stream after skipping past
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * the end will result in -1 indicating the end of the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @param      n   the number of bytes to be skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @return     the actual number of bytes skipped.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52149
diff changeset
   297
     * @throws     IOException  if n is negative, if the stream does not
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *             support seek, or if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
41971
d94dec08aecc 8169556: Wrapping of FileInputStream's native skip and available methods
rriggs
parents: 41769
diff changeset
   300
    public long skip(long n) throws IOException {
d94dec08aecc 8169556: Wrapping of FileInputStream's native skip and available methods
rriggs
parents: 41769
diff changeset
   301
        return skip0(n);
d94dec08aecc 8169556: Wrapping of FileInputStream's native skip and available methods
rriggs
parents: 41769
diff changeset
   302
    }
d94dec08aecc 8169556: Wrapping of FileInputStream's native skip and available methods
rriggs
parents: 41769
diff changeset
   303
d94dec08aecc 8169556: Wrapping of FileInputStream's native skip and available methods
rriggs
parents: 41769
diff changeset
   304
    private native long skip0(long n) throws IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Returns an estimate of the number of remaining bytes that can be read (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * skipped over) from this input stream without blocking by the next
17691
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 17430
diff changeset
   309
     * invocation of a method for this input stream. Returns 0 when the file
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 17430
diff changeset
   310
     * position is beyond EOF. The next invocation might be the same thread
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 17430
diff changeset
   311
     * or another thread. A single read or skip of this many bytes will not
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 17430
diff changeset
   312
     * block, but may read or skip fewer bytes.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * <p> In some cases, a non-blocking read (or skip) may appear to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * blocked when it is merely slow, for example when reading large
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * files over slow networks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @return     an estimate of the number of remaining bytes that can be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *             (or skipped over) from this input stream without blocking.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52149
diff changeset
   320
     * @throws     IOException  if this file input stream has been closed by calling
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *             {@code close} or an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
41971
d94dec08aecc 8169556: Wrapping of FileInputStream's native skip and available methods
rriggs
parents: 41769
diff changeset
   323
    public int available() throws IOException {
d94dec08aecc 8169556: Wrapping of FileInputStream's native skip and available methods
rriggs
parents: 41769
diff changeset
   324
        return available0();
d94dec08aecc 8169556: Wrapping of FileInputStream's native skip and available methods
rriggs
parents: 41769
diff changeset
   325
    }
d94dec08aecc 8169556: Wrapping of FileInputStream's native skip and available methods
rriggs
parents: 41769
diff changeset
   326
d94dec08aecc 8169556: Wrapping of FileInputStream's native skip and available methods
rriggs
parents: 41769
diff changeset
   327
    private native int available0() throws IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * Closes this file input stream and releases any system resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * associated with the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * <p> If this stream has an associated channel then the channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
48224
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   336
     * @apiNote
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   337
     * Overriding {@link #close} to perform cleanup actions is reliable
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   338
     * only when called directly or when called by try-with-resources.
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   339
     * Do not depend on finalization to invoke {@code close};
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   340
     * finalization is not reliable and is deprecated.
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   341
     * If cleanup of native resources is needed, other mechanisms such as
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   342
     * {@linkplain java.lang.ref.Cleaner} should be used.
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   343
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52149
diff changeset
   344
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    public void close() throws IOException {
41769
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 28062
diff changeset
   350
        if (closed) {
28062
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 26190
diff changeset
   351
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
41769
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 28062
diff changeset
   353
        synchronized (closeLock) {
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 28062
diff changeset
   354
            if (closed) {
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 28062
diff changeset
   355
                return;
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 28062
diff changeset
   356
            }
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 28062
diff changeset
   357
            closed = true;
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 28062
diff changeset
   358
        }
28062
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 26190
diff changeset
   359
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 26190
diff changeset
   360
        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
   361
        if (fc != null) {
41769
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 28062
diff changeset
   362
            // possible race with getChannel(), benign since
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 28062
diff changeset
   363
            // FileChannel.close is final and idempotent
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 28062
diff changeset
   364
            fc.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
11017
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   367
        fd.closeAll(new Closeable() {
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   368
            public void close() throws IOException {
47235
9ef10c6e67b8 8187631: Refactor FileDescriptor close implementation
rriggs
parents: 47216
diff changeset
   369
               fd.close();
11017
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   370
           }
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   371
        });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   375
     * Returns the {@code FileDescriptor}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * object  that represents the connection to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * the actual file in the file system being
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   378
     * used by this {@code FileInputStream}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @return     the file descriptor object associated with this stream.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52149
diff changeset
   381
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @see        java.io.FileDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    public final FileDescriptor getFD() throws IOException {
11017
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   385
        if (fd != null) {
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   386
            return fd;
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   387
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        throw new IOException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * object associated with this file input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * <p> The initial {@link java.nio.channels.FileChannel#position()
18786
52a2658627c2 8020091: Fix HTML doclint issues in java.io
juh
parents: 17691
diff changeset
   396
     * position} of the returned channel will be equal to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * number of bytes read from the file so far.  Reading bytes from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * stream will increment the channel's position.  Changing the channel's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * position, either explicitly or by reading, will change this stream's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * file position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @return  the file channel associated with this file input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    public FileChannel getChannel() {
28062
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 26190
diff changeset
   408
        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
   409
        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
   410
            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
   411
                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
   412
                if (fc == null) {
47460
b6d959fae9ef 8189963: Remove version of FileChannelImpl::open without the 'direct' parameter
bpb
parents: 47235
diff changeset
   413
                    this.channel = fc = FileChannelImpl.open(fd, path, true,
b6d959fae9ef 8189963: Remove version of FileChannelImpl::open without the 'direct' parameter
bpb
parents: 47235
diff changeset
   414
                        false, false, this);
41769
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 28062
diff changeset
   415
                    if (closed) {
28062
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 26190
diff changeset
   416
                        try {
41769
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 28062
diff changeset
   417
                            // possible race with close(), benign since
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 28062
diff changeset
   418
                            // FileChannel.close is final and idempotent
28062
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 26190
diff changeset
   419
                            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
   420
                        } 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
   421
                            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
   422
                        }
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 26190
diff changeset
   423
                    }
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 26190
diff changeset
   424
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        }
28062
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 26190
diff changeset
   427
        return fc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
}