src/java.base/share/classes/java/io/FileOutputStream.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58288 48e480e56aad
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52427
diff changeset
     2
 * Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.nio.channels.FileChannel;
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 52149
diff changeset
    29
import jdk.internal.access.SharedSecrets;
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 52149
diff changeset
    30
import jdk.internal.access.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
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    36
 * {@code File} or to a {@code FileDescriptor}. Whether or not
2
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
 *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    43
 * <p>{@code FileOutputStream} is meant for writing streams of raw bytes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * such as image data. For writing streams of characters, consider using
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    45
 * {@code FileWriter}.
2
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
    99
     * Creates a file output stream to write to the file with the
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   100
     * specified name. A new {@code FileDescriptor} object is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * created to represent this file connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   103
     * First, if there is a security manager, its {@code checkWrite}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   104
     * method is called with {@code name} as its argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * If the file exists but is a directory rather than a regular file, does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * not exist but cannot be created, or cannot be opened for any other
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   108
     * reason then a {@code FileNotFoundException} is thrown.
47117
2e7eed499598 8187021: Remove 2 redundant <p> tags in java.base API docs
jjg
parents: 45330
diff changeset
   109
     *
45330
d33052a04c7b 8180353: FileOutputStream documentation does not indicate properly whether files get truncated or not
bpb
parents: 44534
diff changeset
   110
     * @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
   111
     * 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
   112
     * new FileOutputStream(name, false)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param      name   the system-dependent filename
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52427
diff changeset
   115
     * @throws     FileNotFoundException  if the file exists but is a directory
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *                   rather than a regular file, does not exist but cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *                   be created, or cannot be opened for any other reason
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52427
diff changeset
   118
     * @throws     SecurityException  if a security manager exists and its
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   119
     *               {@code checkWrite} method denies write access
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *               to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public FileOutputStream(String name) throws FileNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        this(name != null ? new File(name) : null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
   128
     * Creates a file output stream to write to the file with the specified
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   129
     * name.  If the second argument is {@code true}, then
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * bytes will be written to the end of the file rather than the beginning.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   131
     * A new {@code FileDescriptor} object is created to represent this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * file connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   134
     * First, if there is a security manager, its {@code checkWrite}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   135
     * method is called with {@code name} as its argument.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * If the file exists but is a directory rather than a regular file, does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * not exist but cannot be created, or cannot be opened for any other
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   139
     * reason then a {@code FileNotFoundException} is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param     name        the system-dependent file name
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   142
     * @param     append      if {@code true}, then bytes will be written
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *                   to the end of the file rather than the beginning
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52427
diff changeset
   144
     * @throws     FileNotFoundException  if the file exists but is a directory
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *                   rather than a regular file, does not exist but cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *                   be created, or cannot be opened for any other reason.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52427
diff changeset
   147
     * @throws     SecurityException  if a security manager exists and its
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   148
     *               {@code checkWrite} method denies write access
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *               to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @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
   151
     * @since     1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public FileOutputStream(String name, boolean append)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        throws FileNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        this(name != null ? new File(name) : null, append);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Creates a file output stream to write to the file represented by
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   161
     * the specified {@code File} object. A new
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   162
     * {@code FileDescriptor} object is created to represent this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * file connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   165
     * First, if there is a security manager, its {@code checkWrite}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   166
     * method is called with the path represented by the {@code file}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * argument as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * If the file exists but is a directory rather than a regular file, does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * not exist but cannot be created, or cannot be opened for any other
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   171
     * reason then a {@code FileNotFoundException} is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param      file               the file to be opened for writing.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52427
diff changeset
   174
     * @throws     FileNotFoundException  if the file exists but is a directory
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *                   rather than a regular file, does not exist but cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *                   be created, or cannot be opened for any other reason
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52427
diff changeset
   177
     * @throws     SecurityException  if a security manager exists and its
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   178
     *               {@code checkWrite} method denies write access
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *               to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @see        java.io.File#getPath()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @see        java.lang.SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public FileOutputStream(File file) throws FileNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        this(file, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Creates a file output stream to write to the file represented by
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   190
     * the specified {@code File} object. If the second argument is
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   191
     * {@code true}, then bytes will be written to the end of the file
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   192
     * rather than the beginning. A new {@code FileDescriptor} object is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * created to represent this file connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   195
     * First, if there is a security manager, its {@code checkWrite}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   196
     * method is called with the path represented by the {@code file}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * argument as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * If the file exists but is a directory rather than a regular file, does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * not exist but cannot be created, or cannot be opened for any other
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   201
     * reason then a {@code FileNotFoundException} is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @param      file               the file to be opened for writing.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   204
     * @param     append      if {@code true}, then bytes will be written
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *                   to the end of the file rather than the beginning
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52427
diff changeset
   206
     * @throws     FileNotFoundException  if the file exists but is a directory
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *                   rather than a regular file, does not exist but cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *                   be created, or cannot be opened for any other reason
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52427
diff changeset
   209
     * @throws     SecurityException  if a security manager exists and its
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   210
     *               {@code checkWrite} method denies write access
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *               to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @see        java.io.File#getPath()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @see        java.lang.SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public FileOutputStream(File file, boolean append)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        throws FileNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        String name = (file != null ? file.getPath() : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            security.checkWrite(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 11017
diff changeset
   228
        if (file.isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 11017
diff changeset
   229
            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
   230
        }
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   231
        this.fd = new FileDescriptor();
11017
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   232
        fd.attach(this);
22945
89dd803515d8 8033917: Keep track of file paths in file streams and channels for instrumentation purposes
sla
parents: 18786
diff changeset
   233
        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
   234
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 46
diff changeset
   235
        open(name, append);
52149
0edbbc64393c 8192939: Remove Finalize methods from FileInputStream and FileOutputStream
rriggs
parents: 49900
diff changeset
   236
        FileCleanable.register(fd);   // open sets the fd, register the cleanup
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
   240
     * Creates a file output stream to write to the specified file
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * descriptor, which represents an existing connection to an actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * file in the file system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   244
     * First, if there is a security manager, its {@code checkWrite}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   245
     * method is called with the file descriptor {@code fdObj}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * argument as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   248
     * If {@code fdObj} is null then a {@code NullPointerException}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   251
     * This constructor does not throw an exception if {@code fdObj}
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
   252
     * is {@link java.io.FileDescriptor#valid() invalid}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * However, if the methods are invoked on the resulting stream to attempt
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   254
     * I/O on the stream, an {@code IOException} is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @param      fdObj   the file descriptor to be opened for writing
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52427
diff changeset
   257
     * @throws     SecurityException  if a security manager exists and its
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   258
     *               {@code checkWrite} method denies
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *               write access to the file descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @see        java.lang.SecurityManager#checkWrite(java.io.FileDescriptor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    public FileOutputStream(FileDescriptor fdObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (fdObj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            security.checkWrite(fdObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   270
        this.fd = fdObj;
22945
89dd803515d8 8033917: Keep track of file paths in file streams and channels for instrumentation purposes
sla
parents: 18786
diff changeset
   271
        this.path = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
11017
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   273
        fd.attach(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 46
diff changeset
   277
     * Opens a file, with the specified name, for overwriting or appending.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @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
   279
     * @param append whether the file is to be opened in append mode
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
26190
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   281
    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
   282
        throws FileNotFoundException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
26190
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   284
    // wrap native call to allow instrumentation
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   285
    /**
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   286
     * 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
   287
     * @param name name of file to be opened
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   288
     * @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
   289
     */
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   290
    private void open(String name, boolean append)
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   291
        throws FileNotFoundException {
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   292
        open0(name, append);
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   293
    }
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   294
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   296
     * 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
   297
     *
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   298
     * @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
   299
     * @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
   300
     *     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
   301
     */
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   302
    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
   303
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   304
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * Writes the specified byte to this file output stream. Implements
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   306
     * the {@code write} method of {@code OutputStream}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @param      b   the byte to be written.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52427
diff changeset
   309
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   311
    public void write(int b) throws IOException {
27263
819f5f87d485 8023173: FileDescriptor should respect append flag
igerasim
parents: 26190
diff changeset
   312
        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
   313
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Writes a sub array as a sequence of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @param off the start offset in the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @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
   320
     * @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
   321
     *     end of file
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52427
diff changeset
   322
     * @throws    IOException If an I/O error has occurred.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     */
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   324
    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
   325
        throws IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   328
     * Writes {@code b.length} bytes from the specified byte array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * to this file output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @param      b   the data.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52427
diff changeset
   332
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    public void write(byte b[]) throws IOException {
27263
819f5f87d485 8023173: FileDescriptor should respect append flag
igerasim
parents: 26190
diff changeset
   335
        writeBytes(b, 0, b.length, fdAccess.getAppend(fd));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   339
     * Writes {@code len} bytes from the specified byte array
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   340
     * starting at offset {@code off} to this file output stream.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @param      b     the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @param      off   the start offset in the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @param      len   the number of bytes to write.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52427
diff changeset
   345
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public void write(byte b[], int off, int len) throws IOException {
27263
819f5f87d485 8023173: FileDescriptor should respect append flag
igerasim
parents: 26190
diff changeset
   348
        writeBytes(b, off, len, fdAccess.getAppend(fd));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * Closes this file output stream and releases any system resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * associated with this stream. This file output stream may no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * be used for writing bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * <p> If this stream has an associated channel then the channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
48224
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   359
     * @apiNote
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   360
     * Overriding {@link #close} to perform cleanup actions is reliable
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   361
     * 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
   362
     * Do not depend on finalization to invoke {@code close};
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   363
     * finalization is not reliable and is deprecated.
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   364
     * 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
   365
     * {@linkplain java.lang.ref.Cleaner} should be used.
be0df5ab3093 8080225: FileInput/OutputStream/FileChannel cleanup should be improved
rriggs
parents: 47460
diff changeset
   366
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52427
diff changeset
   367
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    public void close() throws IOException {
41769
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
   373
        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
   374
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
41769
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
   376
        synchronized (closeLock) {
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
   377
            if (closed) {
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
   378
                return;
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
   379
            }
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
   380
            closed = true;
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
   381
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
28062
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   383
        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
   384
        if (fc != null) {
41769
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
   385
            // possible race with getChannel(), benign since
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
   386
            // FileChannel.close is final and idempotent
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
   387
            fc.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
11017
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   390
        fd.closeAll(new Closeable() {
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   391
            public void close() throws IOException {
47235
9ef10c6e67b8 8187631: Refactor FileDescriptor close implementation
rriggs
parents: 47216
diff changeset
   392
               fd.close();
11017
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   393
           }
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   394
        });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * Returns the file descriptor associated with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   400
     * @return  the {@code FileDescriptor} object that represents
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *          the connection to the file in the file system being used
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   402
     *          by this {@code FileOutputStream} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 52427
diff changeset
   404
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @see        java.io.FileDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     public final FileDescriptor getFD()  throws IOException {
11017
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   408
        if (fd != null) {
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   409
            return fd;
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   410
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        throw new IOException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
18786
52a2658627c2 8020091: Fix HTML doclint issues in java.io
juh
parents: 17430
diff changeset
   416
     * object associated with this file output stream.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * <p> The initial {@link java.nio.channels.FileChannel#position()
18786
52a2658627c2 8020091: Fix HTML doclint issues in java.io
juh
parents: 17430
diff changeset
   419
     * position} of the returned channel will be equal to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * number of bytes written to the file so far unless this stream is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * append mode, in which case it will be equal to the size of the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * Writing bytes to this stream will increment the channel's position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * accordingly.  Changing the channel's position, either explicitly or by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * writing, will change this stream's file position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @return  the file channel associated with this file output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    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
   432
        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
   433
        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
   434
            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
   435
                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
   436
                if (fc == null) {
47460
b6d959fae9ef 8189963: Remove version of FileChannelImpl::open without the 'direct' parameter
bpb
parents: 47235
diff changeset
   437
                    this.channel = fc = FileChannelImpl.open(fd, path, false,
b6d959fae9ef 8189963: Remove version of FileChannelImpl::open without the 'direct' parameter
bpb
parents: 47235
diff changeset
   438
                        true, false, this);
41769
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
   439
                    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
   440
                        try {
41769
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
   441
                            // possible race with close(), benign since
f6f8a00fdba9 8168640: (fc) Avoiding AtomicBoolean in FileInput/-OutputStream improves startup
redestad
parents: 32834
diff changeset
   442
                            // 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
   443
                            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
   444
                        } 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
   445
                            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
   446
                        }
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   447
                    }
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   448
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        }
28062
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   451
        return fc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
}