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