src/java.base/share/classes/java/io/FileOutputStream.java
author redestad
Thu, 26 Apr 2018 17:14:04 +0200
changeset 49900 770679787db5
parent 48941 4f11514fe783
child 52149 0edbbc64393c
permissions -rw-r--r--
8202324: Avoid loading FileInput-/OutputStream$AltFinalizer Reviewed-by: alanb
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: 41769
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;
32834
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 32033
diff changeset
    29
import jdk.internal.misc.SharedSecrets;
e1dca5fe4de3 8137056: Move SharedSecrets and interface friends out of sun.misc
chegar
parents: 32033
diff changeset
    30
import jdk.internal.misc.JavaIOFileDescriptorAccess;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.nio.ch.FileChannelImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * A file output stream is an output stream for writing data to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <code>File</code> or to a <code>FileDescriptor</code>. Whether or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * a file is available or may be created depends upon the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * platform.  Some platforms, in particular, allow a file to be opened
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 28062
diff changeset
    39
 * for writing by only one {@code FileOutputStream} (or other
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * file-writing object) at a time.  In such situations the constructors in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * this class will fail if the file involved is already open.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p><code>FileOutputStream</code> is meant for writing streams of raw bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * such as image data. For writing streams of characters, consider using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <code>FileWriter</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
48224
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    47
 * @apiNote
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    48
 * 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
    49
 * 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
    50
 * of resources acquired by the subclass.
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    51
 * Subclasses that override {@link #finalize} in order to perform cleanup
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    52
 * should be modified to use alternative cleanup mechanisms such as
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    53
 * {@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
    54
 *
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    55
 * @implSpec
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    56
 * If this FileOutputStream has been subclassed and the {@link #close}
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    57
 * method has been overridden, the {@link #close} method will be
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    58
 * called when the FileInputStream is unreachable.
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    59
 * 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
    60
 * {@link #close} is performed.
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    61
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @author  Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @see     java.io.File
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @see     java.io.FileDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * @see     java.io.FileInputStream
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7816
diff changeset
    66
 * @see     java.nio.file.Files#newOutputStream
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 22945
diff changeset
    67
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
class FileOutputStream extends OutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
27263
819f5f87d485 8023173: FileDescriptor should respect append flag
igerasim
parents: 26190
diff changeset
    73
     * Access to FileDescriptor internals.
819f5f87d485 8023173: FileDescriptor should respect append flag
igerasim
parents: 26190
diff changeset
    74
     */
819f5f87d485 8023173: FileDescriptor should respect append flag
igerasim
parents: 26190
diff changeset
    75
    private static final JavaIOFileDescriptorAccess fdAccess =
819f5f87d485 8023173: FileDescriptor should respect append flag
igerasim
parents: 26190
diff changeset
    76
        SharedSecrets.getJavaIOFileDescriptorAccess();
819f5f87d485 8023173: FileDescriptor should respect append flag
igerasim
parents: 26190
diff changeset
    77
819f5f87d485 8023173: FileDescriptor should respect append flag
igerasim
parents: 26190
diff changeset
    78
    /**
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
    79
     * The system dependent file descriptor.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
    81
    private final FileDescriptor fd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
    83
    /**
10586
6e20ecfec8ed 7082769: FileInputStream/FileOutputStream/RandomAccessFile allow file descriptor be closed when still in use
coffeys
parents: 9035
diff changeset
    84
     * The associated channel, initialized lazily.
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
    85
     */
28062
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
    86
    private volatile FileChannel channel;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
22945
89dd803515d8 8033917: Keep track of file paths in file streams and channels for instrumentation purposes
sla
parents: 18786
diff changeset
    88
    /**
89dd803515d8 8033917: Keep track of file paths in file streams and channels for instrumentation purposes
sla
parents: 18786
diff changeset
    89
     * 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
    90
     * (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
    91
     */
89dd803515d8 8033917: Keep track of file paths in file streams and channels for instrumentation purposes
sla
parents: 18786
diff changeset
    92
    private final String path;
89dd803515d8 8033917: Keep track of file paths in file streams and channels for instrumentation purposes
sla
parents: 18786
diff changeset
    93
41769
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
    94
    private final Object closeLock = new Object();
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
    95
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
    96
    private volatile boolean closed;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
49900
770679787db5 8202324: Avoid loading FileInput-/OutputStream$AltFinalizer
redestad
parents: 48941
diff changeset
    98
    private final Object altFinalizer;
48224
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
    99
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
   101
     * Creates a file output stream to write to the file with the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * specified name. A new <code>FileDescriptor</code> object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * 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, its <code>checkWrite</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * method is called with <code>name</code> as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * If the file exists but is a directory rather than a regular file, does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * not exist but cannot be created, or cannot be opened for any other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * reason then a <code>FileNotFoundException</code> is thrown.
47117
2e7eed499598 8187021: Remove 2 redundant <p> tags in java.base API docs
jjg
parents: 45330
diff changeset
   111
     *
45330
d33052a04c7b 8180353: FileOutputStream documentation does not indicate properly whether files get truncated or not
bpb
parents: 44534
diff changeset
   112
     * @implSpec Invoking this constructor with the parameter {@code name} is
d33052a04c7b 8180353: FileOutputStream documentation does not indicate properly whether files get truncated or not
bpb
parents: 44534
diff changeset
   113
     * equivalent to invoking {@link #FileOutputStream(String,boolean)
d33052a04c7b 8180353: FileOutputStream documentation does not indicate properly whether files get truncated or not
bpb
parents: 44534
diff changeset
   114
     * new FileOutputStream(name, false)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param      name   the system-dependent filename
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @exception  FileNotFoundException  if the file exists but is a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *                   rather than a regular file, does not exist but cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *                   be created, or cannot be opened for any other reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *               <code>checkWrite</code> method denies write access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *               to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public FileOutputStream(String name) throws FileNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        this(name != null ? new File(name) : null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
   130
     * Creates a file output stream to write to the file with the specified
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
   131
     * name.  If the second argument is <code>true</code>, then
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * bytes will be written to the end of the file rather than the beginning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * A new <code>FileDescriptor</code> object is created to represent this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * file connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * First, if there is a security manager, its <code>checkWrite</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * method is called with <code>name</code> as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * If the file exists but is a directory rather than a regular file, does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * not exist but cannot be created, or cannot be opened for any other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * reason then a <code>FileNotFoundException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param     name        the system-dependent file name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param     append      if <code>true</code>, then bytes will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *                   to the end of the file rather than the beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @exception  FileNotFoundException  if the file exists but is a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *                   rather than a regular file, does not exist but cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *                   be created, or cannot be opened for any other reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *               <code>checkWrite</code> method denies write access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *               to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 22945
diff changeset
   153
     * @since     1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public FileOutputStream(String name, boolean append)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        throws FileNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        this(name != null ? new File(name) : null, append);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Creates a file output stream to write to the file represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * the specified <code>File</code> object. A new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * <code>FileDescriptor</code> object is created to represent this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * file connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * First, if there is a security manager, its <code>checkWrite</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * method is called with the path represented by the <code>file</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * argument as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * If the file exists but is a directory rather than a regular file, does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * not exist but cannot be created, or cannot be opened for any other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * reason then a <code>FileNotFoundException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @param      file               the file to be opened for writing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @exception  FileNotFoundException  if the file exists but is a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *                   rather than a regular file, does not exist but cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *                   be created, or cannot be opened for any other reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *               <code>checkWrite</code> method denies write access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *               to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @see        java.io.File#getPath()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @see        java.lang.SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public FileOutputStream(File file) throws FileNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        this(file, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * Creates a file output stream to write to the file represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * the specified <code>File</code> object. If the second argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * <code>true</code>, then bytes will be written to the end of the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * rather than the beginning. A new <code>FileDescriptor</code> object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * created to represent this file connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * First, if there is a security manager, its <code>checkWrite</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * method is called with the path represented by the <code>file</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * argument as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * If the file exists but is a directory rather than a regular file, does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * not exist but cannot be created, or cannot be opened for any other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * reason then a <code>FileNotFoundException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @param      file               the file to be opened for writing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param     append      if <code>true</code>, then bytes will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *                   to the end of the file rather than the beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @exception  FileNotFoundException  if the file exists but is a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *                   rather than a regular file, does not exist but cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *                   be created, or cannot be opened for any other reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *               <code>checkWrite</code> method denies write access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *               to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @see        java.io.File#getPath()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @see        java.lang.SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public FileOutputStream(File file, boolean append)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        throws FileNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        String name = (file != null ? file.getPath() : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            security.checkWrite(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 11017
diff changeset
   230
        if (file.isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 11017
diff changeset
   231
            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
   232
        }
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   233
        this.fd = new FileDescriptor();
11017
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   234
        fd.attach(this);
22945
89dd803515d8 8033917: Keep track of file paths in file streams and channels for instrumentation purposes
sla
parents: 18786
diff changeset
   235
        this.path = name;
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   236
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 46
diff changeset
   237
        open(name, append);
49900
770679787db5 8202324: Avoid loading FileInput-/OutputStream$AltFinalizer
redestad
parents: 48941
diff changeset
   238
        altFinalizer = getFinalizer(this);
48224
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   239
        if (altFinalizer == null) {
48941
4f11514fe783 8189330: Cleanup FileDescriptor implementation
rriggs
parents: 48224
diff changeset
   240
            FileCleanable.register(fd);   // open sets the fd, register the cleanup
48224
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   241
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
   245
     * Creates a file output stream to write to the specified file
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * descriptor, which represents an existing connection to an actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * file in the file system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * First, if there is a security manager, its <code>checkWrite</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * method is called with the file descriptor <code>fdObj</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * argument as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * If <code>fdObj</code> is null then a <code>NullPointerException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * 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
   257
     * is {@link java.io.FileDescriptor#valid() invalid}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * However, if the methods are invoked on the resulting stream to attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * I/O on the stream, an <code>IOException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @param      fdObj   the file descriptor to be opened for writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *               <code>checkWrite</code> method denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *               write access to the file descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @see        java.lang.SecurityManager#checkWrite(java.io.FileDescriptor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public FileOutputStream(FileDescriptor fdObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (fdObj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            security.checkWrite(fdObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   275
        this.fd = fdObj;
22945
89dd803515d8 8033917: Keep track of file paths in file streams and channels for instrumentation purposes
sla
parents: 18786
diff changeset
   276
        this.path = null;
48224
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   277
        this.altFinalizer = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
11017
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   279
        fd.attach(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 46
diff changeset
   283
     * Opens a file, with the specified name, for overwriting or appending.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @param name name of file to be opened
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 46
diff changeset
   285
     * @param append whether the file is to be opened in append mode
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
26190
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   287
    private native void open0(String name, boolean append)
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 46
diff changeset
   288
        throws FileNotFoundException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
26190
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   290
    // wrap native call to allow instrumentation
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   291
    /**
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   292
     * Opens a file, with the specified name, for overwriting or appending.
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   293
     * @param name name of file to be opened
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   294
     * @param append whether the file is to be opened in append mode
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   295
     */
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   296
    private void open(String name, boolean append)
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   297
        throws FileNotFoundException {
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   298
        open0(name, append);
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   299
    }
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   300
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /**
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   302
     * Writes the specified byte to this file output stream.
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   303
     *
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   304
     * @param   b   the byte to be written.
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   305
     * @param   append   {@code true} if the write operation first
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   306
     *     advances the position to the end of file
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   307
     */
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   308
    private native void write(int b, boolean append) throws IOException;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   309
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   310
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Writes the specified byte to this file output stream. Implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * the <code>write</code> method of <code>OutputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @param      b   the byte to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   317
    public void write(int b) throws IOException {
27263
819f5f87d485 8023173: FileDescriptor should respect append flag
igerasim
parents: 26190
diff changeset
   318
        write(b, fdAccess.getAppend(fd));
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   319
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Writes a sub array as a sequence of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @param off the start offset in the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @param len the number of bytes that are written
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   326
     * @param append {@code true} to first advance the position to the
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   327
     *     end of file
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @exception IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   330
    private native void writeBytes(byte b[], int off, int len, boolean append)
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   331
        throws IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Writes <code>b.length</code> bytes from the specified byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * to this file output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @param      b   the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public void write(byte b[]) throws IOException {
27263
819f5f87d485 8023173: FileDescriptor should respect append flag
igerasim
parents: 26190
diff changeset
   341
        writeBytes(b, 0, b.length, fdAccess.getAppend(fd));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * Writes <code>len</code> bytes from the specified byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * starting at offset <code>off</code> to this file output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @param      b     the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @param      off   the start offset in the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @param      len   the number of bytes to write.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    public void write(byte b[], int off, int len) throws IOException {
27263
819f5f87d485 8023173: FileDescriptor should respect append flag
igerasim
parents: 26190
diff changeset
   354
        writeBytes(b, off, len, fdAccess.getAppend(fd));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Closes this file output stream and releases any system resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * associated with this stream. This file output stream may no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * be used for writing bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * <p> If this stream has an associated channel then the channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *
48224
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   365
     * @apiNote
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   366
     * Overriding {@link #close} to perform cleanup actions is reliable
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   367
     * 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
   368
     * Do not depend on finalization to invoke {@code close};
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   369
     * finalization is not reliable and is deprecated.
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   370
     * 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
   371
     * {@linkplain java.lang.ref.Cleaner} should be used.
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   372
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    public void close() throws IOException {
41769
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
   379
        if (closed) {
28062
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   380
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
41769
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
   382
        synchronized (closeLock) {
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
   383
            if (closed) {
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
   384
                return;
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
   385
            }
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
   386
            closed = true;
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
   387
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
28062
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   389
        FileChannel fc = channel;
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   390
        if (fc != null) {
41769
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
   391
            // possible race with getChannel(), benign since
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
   392
            // FileChannel.close is final and idempotent
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
   393
            fc.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
11017
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   396
        fd.closeAll(new Closeable() {
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   397
            public void close() throws IOException {
47235
9ef10c6e67b8 8187631: Refactor FileDescriptor close implementation
rriggs
parents: 47216
diff changeset
   398
               fd.close();
11017
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   399
           }
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   400
        });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * Returns the file descriptor associated with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @return  the <code>FileDescriptor</code> object that represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *          the connection to the file in the file system being used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *          by this <code>FileOutputStream</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @see        java.io.FileDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     public final FileDescriptor getFD()  throws IOException {
11017
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   414
        if (fd != null) {
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   415
            return fd;
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   416
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        throw new IOException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
18786
52a2658627c2 8020091: Fix HTML doclint issues in java.io
juh
parents: 17430
diff changeset
   422
     * object associated with this file output stream.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * <p> The initial {@link java.nio.channels.FileChannel#position()
18786
52a2658627c2 8020091: Fix HTML doclint issues in java.io
juh
parents: 17430
diff changeset
   425
     * position} of the returned channel will be equal to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * number of bytes written to the file so far unless this stream is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * append mode, in which case it will be equal to the size of the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * Writing bytes to this stream will increment the channel's position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * accordingly.  Changing the channel's position, either explicitly or by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * writing, will change this stream's file position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @return  the file channel associated with this file output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    public FileChannel getChannel() {
28062
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   438
        FileChannel fc = this.channel;
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   439
        if (fc == null) {
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   440
            synchronized (this) {
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   441
                fc = this.channel;
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   442
                if (fc == null) {
47460
b6d959fae9ef 8189963: Remove version of FileChannelImpl::open without the 'direct' parameter
bpb
parents: 47235
diff changeset
   443
                    this.channel = fc = FileChannelImpl.open(fd, path, false,
b6d959fae9ef 8189963: Remove version of FileChannelImpl::open without the 'direct' parameter
bpb
parents: 47235
diff changeset
   444
                        true, false, this);
41769
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
   445
                    if (closed) {
28062
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   446
                        try {
41769
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
   447
                            // possible race with close(), benign since
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
   448
                            // 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: 27263
diff changeset
   449
                            fc.close();
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   450
                        } catch (IOException ioe) {
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   451
                            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: 27263
diff changeset
   452
                        }
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   453
                    }
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   454
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        }
28062
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   457
        return fc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * Cleans up the connection to the file, and ensures that the
48224
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   462
     * {@link #close} method of this file output stream is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * called when there are no more references to this stream.
48224
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   464
     * The {@link #finalize} method does not call {@link #close} directly.
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   465
     *
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   466
     * @apiNote
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   467
     * 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
   468
     * directly or by try-with-resources.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *
48224
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   470
     * @implSpec
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   471
     * If this FileOutputStream has been subclassed and the {@link #close}
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   472
     * method has been overridden, the {@link #close} method will be
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   473
     * called when the FileOutputStream is unreachable.
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   474
     * 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
   475
     * {@link #close} is performed.
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   476
     *
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   477
     * @deprecated The {@code finalize} method has been deprecated and will be removed.
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   478
     *     Subclasses that override {@code finalize} in order to perform cleanup
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   479
     *     should be modified to use alternative cleanup mechanisms and
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   480
     *     to remove the overriding {@code finalize} method.
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   481
     *     When overriding the {@code finalize} method, its implementation must explicitly
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   482
     *     ensure that {@code super.finalize()} is invoked as described in {@link Object#finalize}.
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   483
     *     See the specification for {@link Object#finalize()} for further
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   484
     *     information about migration options.
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   485
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @see        java.io.FileInputStream#close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     */
48224
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   489
    @Deprecated(since="9", forRemoval = true)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    protected void finalize() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
49900
770679787db5 8202324: Avoid loading FileInput-/OutputStream$AltFinalizer
redestad
parents: 48941
diff changeset
   499
    /*
770679787db5 8202324: Avoid loading FileInput-/OutputStream$AltFinalizer
redestad
parents: 48941
diff changeset
   500
     * Returns a finalizer object if the FOS needs a finalizer; otherwise null.
770679787db5 8202324: Avoid loading FileInput-/OutputStream$AltFinalizer
redestad
parents: 48941
diff changeset
   501
     * If the FOS has a close method; it needs an AltFinalizer.
770679787db5 8202324: Avoid loading FileInput-/OutputStream$AltFinalizer
redestad
parents: 48941
diff changeset
   502
     */
770679787db5 8202324: Avoid loading FileInput-/OutputStream$AltFinalizer
redestad
parents: 48941
diff changeset
   503
    private static Object getFinalizer(FileOutputStream fos) {
770679787db5 8202324: Avoid loading FileInput-/OutputStream$AltFinalizer
redestad
parents: 48941
diff changeset
   504
        Class<?> clazz = fos.getClass();
770679787db5 8202324: Avoid loading FileInput-/OutputStream$AltFinalizer
redestad
parents: 48941
diff changeset
   505
        while (clazz != FileOutputStream.class) {
770679787db5 8202324: Avoid loading FileInput-/OutputStream$AltFinalizer
redestad
parents: 48941
diff changeset
   506
            try {
770679787db5 8202324: Avoid loading FileInput-/OutputStream$AltFinalizer
redestad
parents: 48941
diff changeset
   507
                clazz.getDeclaredMethod("close");
770679787db5 8202324: Avoid loading FileInput-/OutputStream$AltFinalizer
redestad
parents: 48941
diff changeset
   508
                return new AltFinalizer(fos);
770679787db5 8202324: Avoid loading FileInput-/OutputStream$AltFinalizer
redestad
parents: 48941
diff changeset
   509
            } catch (NoSuchMethodException nsme) {
770679787db5 8202324: Avoid loading FileInput-/OutputStream$AltFinalizer
redestad
parents: 48941
diff changeset
   510
                // ignore
770679787db5 8202324: Avoid loading FileInput-/OutputStream$AltFinalizer
redestad
parents: 48941
diff changeset
   511
            }
770679787db5 8202324: Avoid loading FileInput-/OutputStream$AltFinalizer
redestad
parents: 48941
diff changeset
   512
            clazz = clazz.getSuperclass();
770679787db5 8202324: Avoid loading FileInput-/OutputStream$AltFinalizer
redestad
parents: 48941
diff changeset
   513
        }
770679787db5 8202324: Avoid loading FileInput-/OutputStream$AltFinalizer
redestad
parents: 48941
diff changeset
   514
        return null;
770679787db5 8202324: Avoid loading FileInput-/OutputStream$AltFinalizer
redestad
parents: 48941
diff changeset
   515
    }
770679787db5 8202324: Avoid loading FileInput-/OutputStream$AltFinalizer
redestad
parents: 48941
diff changeset
   516
48224
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   517
    /**
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   518
     * Class to call {@code FileOutputStream.close} when finalized.
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   519
     * If finalization of the stream is needed, an instance is created
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   520
     * in its constructor(s).  When the set of instances
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   521
     * related to the stream is unreachable, the AltFinalizer performs
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   522
     * the needed call to the stream's {@code close} method.
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   523
     */
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   524
    static class AltFinalizer {
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   525
        private final FileOutputStream fos;
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   526
49900
770679787db5 8202324: Avoid loading FileInput-/OutputStream$AltFinalizer
redestad
parents: 48941
diff changeset
   527
        AltFinalizer(FileOutputStream fos) {
48224
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   528
            this.fos = fos;
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   529
        }
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   530
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   531
        @Override
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   532
        @SuppressWarnings("deprecation")
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   533
        protected final void finalize() {
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   534
            try {
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   535
                if (fos.fd != null) {
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   536
                    if (fos.fd == FileDescriptor.out || fos.fd == FileDescriptor.err) {
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   537
                        // Subclass may override flush; otherwise it is no-op
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   538
                        fos.flush();
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   539
                    } else {
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   540
                        /* if fd is shared, the references in FileDescriptor
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   541
                         * will ensure that finalizer is only called when
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   542
                         * safe to do so. All references using the fd have
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   543
                         * become unreachable. We can call close()
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   544
                         */
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   545
                        fos.close();
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   546
                    }
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   547
                }
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   548
            } catch (IOException ioe) {
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   549
                // ignore
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   550
            }
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   551
        }
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   552
    }
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   553
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
}