jdk/src/java.base/share/classes/java/io/FileOutputStream.java
author avstepan
Thu, 06 Aug 2015 13:20:13 +0300
changeset 32033 bf24e33c7919
parent 28062 52b80a88a63b
child 32834 e1dca5fe4de3
permissions -rw-r--r--
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math Reviewed-by: lancea, dfuchs, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 11017
diff changeset
     2
 * Copyright (c) 1994, 2013, 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;
28062
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
    29
import java.util.concurrent.atomic.AtomicBoolean;
27263
819f5f87d485 8023173: FileDescriptor should respect append flag
igerasim
parents: 26190
diff changeset
    30
import sun.misc.SharedSecrets;
819f5f87d485 8023173: FileDescriptor should respect append flag
igerasim
parents: 26190
diff changeset
    31
import sun.misc.JavaIOFileDescriptorAccess;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.nio.ch.FileChannelImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * A file output stream is an output stream for writing data to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <code>File</code> or to a <code>FileDescriptor</code>. Whether or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * a file is available or may be created depends upon the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * 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
    40
 * for writing by only one {@code FileOutputStream} (or other
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * file-writing object) at a time.  In such situations the constructors in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * this class will fail if the file involved is already open.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p><code>FileOutputStream</code> is meant for writing streams of raw bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * such as image data. For writing streams of characters, consider using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <code>FileWriter</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author  Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @see     java.io.File
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @see     java.io.FileDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @see     java.io.FileInputStream
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7816
diff changeset
    52
 * @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
    53
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
class FileOutputStream extends OutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
27263
819f5f87d485 8023173: FileDescriptor should respect append flag
igerasim
parents: 26190
diff changeset
    59
     * Access to FileDescriptor internals.
819f5f87d485 8023173: FileDescriptor should respect append flag
igerasim
parents: 26190
diff changeset
    60
     */
819f5f87d485 8023173: FileDescriptor should respect append flag
igerasim
parents: 26190
diff changeset
    61
    private static final JavaIOFileDescriptorAccess fdAccess =
819f5f87d485 8023173: FileDescriptor should respect append flag
igerasim
parents: 26190
diff changeset
    62
        SharedSecrets.getJavaIOFileDescriptorAccess();
819f5f87d485 8023173: FileDescriptor should respect append flag
igerasim
parents: 26190
diff changeset
    63
819f5f87d485 8023173: FileDescriptor should respect append flag
igerasim
parents: 26190
diff changeset
    64
    /**
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
    65
     * The system dependent file descriptor.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
    67
    private final FileDescriptor fd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
    69
    /**
10586
6e20ecfec8ed 7082769: FileInputStream/FileOutputStream/RandomAccessFile allow file descriptor be closed when still in use
coffeys
parents: 9035
diff changeset
    70
     * 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
    71
     */
28062
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
    72
    private volatile FileChannel channel;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
22945
89dd803515d8 8033917: Keep track of file paths in file streams and channels for instrumentation purposes
sla
parents: 18786
diff changeset
    74
    /**
89dd803515d8 8033917: Keep track of file paths in file streams and channels for instrumentation purposes
sla
parents: 18786
diff changeset
    75
     * 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
    76
     * (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
    77
     */
89dd803515d8 8033917: Keep track of file paths in file streams and channels for instrumentation purposes
sla
parents: 18786
diff changeset
    78
    private final String path;
89dd803515d8 8033917: Keep track of file paths in file streams and channels for instrumentation purposes
sla
parents: 18786
diff changeset
    79
28062
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
    80
    private final AtomicBoolean closed = new AtomicBoolean(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
    83
     * Creates a file output stream to write to the file with the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * specified name. A new <code>FileDescriptor</code> object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * created to represent this file connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * First, if there is a security manager, its <code>checkWrite</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * method is called with <code>name</code> as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * If the file exists but is a directory rather than a regular file, does
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * not exist but cannot be created, or cannot be opened for any other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * reason then a <code>FileNotFoundException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param      name   the system-dependent filename
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @exception  FileNotFoundException  if the file exists but is a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *                   rather than a regular file, does not exist but cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *                   be created, or cannot be opened for any other reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *               <code>checkWrite</code> method denies write access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *               to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public FileOutputStream(String name) throws FileNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        this(name != null ? new File(name) : null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
   108
     * 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
   109
     * name.  If the second argument is <code>true</code>, then
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * bytes will be written to the end of the file rather than the beginning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * A new <code>FileDescriptor</code> object is created to represent this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * file connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * First, if there is a security manager, its <code>checkWrite</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * method is called with <code>name</code> as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * If the file exists but is a directory rather than a regular file, does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * not exist but cannot be created, or cannot be opened for any other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * reason then a <code>FileNotFoundException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param     name        the system-dependent file name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param     append      if <code>true</code>, then bytes will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *                   to the end of the file rather than the beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @exception  FileNotFoundException  if the file exists but is a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *                   rather than a regular file, does not exist but cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *                   be created, or cannot be opened for any other reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *               <code>checkWrite</code> method denies write access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *               to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @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
   131
     * @since     1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public FileOutputStream(String name, boolean append)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        throws FileNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        this(name != null ? new File(name) : null, append);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Creates a file output stream to write to the file represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * the specified <code>File</code> object. A new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * <code>FileDescriptor</code> object is created to represent this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * file connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * First, if there is a security manager, its <code>checkWrite</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * method is called with the path represented by the <code>file</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * argument as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * If the file exists but is a directory rather than a regular file, does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * not exist but cannot be created, or cannot be opened for any other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * reason then a <code>FileNotFoundException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param      file               the file to be opened for writing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @exception  FileNotFoundException  if the file exists but is a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *                   rather than a regular file, does not exist but cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *                   be created, or cannot be opened for any other reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *               <code>checkWrite</code> method denies write access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *               to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @see        java.io.File#getPath()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @see        java.lang.SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public FileOutputStream(File file) throws FileNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        this(file, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Creates a file output stream to write to the file represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * the specified <code>File</code> object. If the second argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <code>true</code>, then bytes will be written to the end of the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * rather than the beginning. A new <code>FileDescriptor</code> object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * created to represent this file connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * First, if there is a security manager, its <code>checkWrite</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * method is called with the path represented by the <code>file</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * argument as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * If the file exists but is a directory rather than a regular file, does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * not exist but cannot be created, or cannot be opened for any other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * reason then a <code>FileNotFoundException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param      file               the file to be opened for writing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param     append      if <code>true</code>, then bytes will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *                   to the end of the file rather than the beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @exception  FileNotFoundException  if the file exists but is a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *                   rather than a regular file, does not exist but cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *                   be created, or cannot be opened for any other reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *               <code>checkWrite</code> method denies write access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *               to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @see        java.io.File#getPath()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @see        java.lang.SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public FileOutputStream(File file, boolean append)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        throws FileNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        String name = (file != null ? file.getPath() : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            security.checkWrite(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 11017
diff changeset
   208
        if (file.isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 11017
diff changeset
   209
            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
   210
        }
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   211
        this.fd = new FileDescriptor();
11017
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   212
        fd.attach(this);
22945
89dd803515d8 8033917: Keep track of file paths in file streams and channels for instrumentation purposes
sla
parents: 18786
diff changeset
   213
        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
   214
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 46
diff changeset
   215
        open(name, append);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
   219
     * Creates a file output stream to write to the specified file
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * descriptor, which represents an existing connection to an actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * file in the file system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * First, if there is a security manager, its <code>checkWrite</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * method is called with the file descriptor <code>fdObj</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * argument as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * If <code>fdObj</code> is null then a <code>NullPointerException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * 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
   231
     * is {@link java.io.FileDescriptor#valid() invalid}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * However, if the methods are invoked on the resulting stream to attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * I/O on the stream, an <code>IOException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param      fdObj   the file descriptor to be opened for writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *               <code>checkWrite</code> method denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *               write access to the file descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @see        java.lang.SecurityManager#checkWrite(java.io.FileDescriptor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public FileOutputStream(FileDescriptor fdObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (fdObj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            security.checkWrite(fdObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   249
        this.fd = fdObj;
22945
89dd803515d8 8033917: Keep track of file paths in file streams and channels for instrumentation purposes
sla
parents: 18786
diff changeset
   250
        this.path = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
11017
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   252
        fd.attach(this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 46
diff changeset
   256
     * Opens a file, with the specified name, for overwriting or appending.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @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
   258
     * @param append whether the file is to be opened in append mode
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
26190
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   260
    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
   261
        throws FileNotFoundException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
26190
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   263
    // wrap native call to allow instrumentation
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   264
    /**
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   265
     * 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
   266
     * @param name name of file to be opened
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   267
     * @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
   268
     */
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   269
    private void open(String name, boolean append)
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   270
        throws FileNotFoundException {
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   271
        open0(name, append);
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   272
    }
d183677673d9 8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents: 25859
diff changeset
   273
2
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
     * 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
   276
     *
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   277
     * @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
   278
     * @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
   279
     *     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
   280
     */
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   281
    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
   282
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   283
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Writes the specified byte to this file output stream. Implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * the <code>write</code> method of <code>OutputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @param      b   the byte to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   290
    public void write(int b) throws IOException {
27263
819f5f87d485 8023173: FileDescriptor should respect append flag
igerasim
parents: 26190
diff changeset
   291
        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
   292
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * Writes a sub array as a sequence of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @param off the start offset in the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @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
   299
     * @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
   300
     *     end of file
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @exception IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 5506
diff changeset
   303
    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
   304
        throws IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Writes <code>b.length</code> bytes from the specified byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * to this file output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @param      b   the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    public void write(byte b[]) throws IOException {
27263
819f5f87d485 8023173: FileDescriptor should respect append flag
igerasim
parents: 26190
diff changeset
   314
        writeBytes(b, 0, b.length, fdAccess.getAppend(fd));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * Writes <code>len</code> bytes from the specified byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * starting at offset <code>off</code> to this file output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @param      b     the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @param      off   the start offset in the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @param      len   the number of bytes to write.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    public void write(byte b[], int off, int len) throws IOException {
27263
819f5f87d485 8023173: FileDescriptor should respect append flag
igerasim
parents: 26190
diff changeset
   327
        writeBytes(b, off, len, fdAccess.getAppend(fd));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * Closes this file output stream and releases any system resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * associated with this stream. This file output stream may no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * be used for writing bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * <p> If this stream has an associated channel then the channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
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
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    public void close() throws IOException {
28062
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   344
        if (!closed.compareAndSet(false, true)) {
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   345
            // if compareAndSet() returns false closed was already true
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   346
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
28062
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   349
        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
   350
        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
   351
           fc.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
11017
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   354
        fd.closeAll(new Closeable() {
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   355
            public void close() throws IOException {
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   356
               close0();
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   357
           }
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   358
        });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * Returns the file descriptor associated with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @return  the <code>FileDescriptor</code> object that represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *          the connection to the file in the file system being used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *          by this <code>FileOutputStream</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @see        java.io.FileDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     public final FileDescriptor getFD()  throws IOException {
11017
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   372
        if (fd != null) {
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   373
            return fd;
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   374
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        throw new IOException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
18786
52a2658627c2 8020091: Fix HTML doclint issues in java.io
juh
parents: 17430
diff changeset
   380
     * object associated with this file output stream.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * <p> The initial {@link java.nio.channels.FileChannel#position()
18786
52a2658627c2 8020091: Fix HTML doclint issues in java.io
juh
parents: 17430
diff changeset
   383
     * position} of the returned channel will be equal to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * number of bytes written to the file so far unless this stream is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * append mode, in which case it will be equal to the size of the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Writing bytes to this stream will increment the channel's position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * accordingly.  Changing the channel's position, either explicitly or by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * writing, will change this stream's file position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @return  the file channel associated with this file output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    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
   396
        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
   397
        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
   398
            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
   399
                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
   400
                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
   401
                    this.channel = fc = FileChannelImpl.open(fd, path, false, true, this);
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   402
                    if (closed.get()) {
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   403
                        try {
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   404
                            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
   405
                        } 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
   406
                            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
   407
                        }
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   408
                    }
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   409
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
28062
52b80a88a63b 8025619: (fc) FileInputStream.getChannel on closed stream returns FileChannel that doesn't know that stream is closed
bpb
parents: 27263
diff changeset
   412
        return fc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * Cleans up the connection to the file, and ensures that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * <code>close</code> method of this file output stream is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * called when there are no more references to this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @see        java.io.FileInputStream#close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    protected void finalize() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        if (fd != null) {
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
   425
            if (fd == FileDescriptor.out || fd == FileDescriptor.err) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            } else {
11017
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   428
                /* if fd is shared, the references in FileDescriptor
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   429
                 * will ensure that finalizer is only called when
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   430
                 * safe to do so. All references using the fd have
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   431
                 * become unreachable. We can call close()
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   432
                 */
353f81426721 7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents: 10586
diff changeset
   433
                close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    private native void close0() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
}