jdk/src/share/classes/java/io/FileOutputStream.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 715 f16baef3a20e
child 7515 43202796198e
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     2
 * Copyright (c) 1994, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.nio.channels.FileChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.nio.ch.FileChannelImpl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A file output stream is an output stream for writing data to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <code>File</code> or to a <code>FileDescriptor</code>. Whether or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * a file is available or may be created depends upon the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * platform.  Some platforms, in particular, allow a file to be opened
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * for writing by only one <tt>FileOutputStream</tt> (or other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * file-writing object) at a time.  In such situations the constructors in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * this class will fail if the file involved is already open.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p><code>FileOutputStream</code> is meant for writing streams of raw bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * such as image data. For writing streams of characters, consider using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <code>FileWriter</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author  Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @see     java.io.File
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @see     java.io.FileDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @see     java.io.FileInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
class FileOutputStream extends OutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
    55
     * The system dependent file descriptor.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
    57
    private final FileDescriptor fd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private FileChannel channel= null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
    61
    private final Object closeLock = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private volatile boolean closed = false;
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
    63
    private static final ThreadLocal<Boolean> runningFinalize =
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
    64
        new ThreadLocal<Boolean>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private static boolean isRunningFinalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        Boolean val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if ((val = runningFinalize.get()) != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            return val.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
    74
     * Creates a file output stream to write to the file with the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * specified name. A new <code>FileDescriptor</code> object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * created to represent this file connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * First, if there is a security manager, its <code>checkWrite</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * method is called with <code>name</code> as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * If the file exists but is a directory rather than a regular file, does
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * not exist but cannot be created, or cannot be opened for any other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * reason then a <code>FileNotFoundException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param      name   the system-dependent filename
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @exception  FileNotFoundException  if the file exists but is a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *                   rather than a regular file, does not exist but cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *                   be created, or cannot be opened for any other reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *               <code>checkWrite</code> method denies write access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *               to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public FileOutputStream(String name) throws FileNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        this(name != null ? new File(name) : null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
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 specified
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
   100
     * name.  If the second argument is <code>true</code>, then
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * bytes will be written to the end of the file rather than the beginning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * A new <code>FileDescriptor</code> object is created to represent this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * file connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * First, if there is a security manager, its <code>checkWrite</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * method is called with <code>name</code> as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * If the file exists but is a directory rather than a regular file, does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * not exist but cannot be created, or cannot be opened for any other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * reason then a <code>FileNotFoundException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param     name        the system-dependent file name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param     append      if <code>true</code>, then bytes will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *                   to the end of the file rather than the beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @exception  FileNotFoundException  if the file exists but is a directory
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *               <code>checkWrite</code> method denies write access
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
     * @since     JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public FileOutputStream(String name, boolean append)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        throws FileNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        this(name != null ? new File(name) : null, append);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Creates a file output stream to write to the file represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * the specified <code>File</code> object. A new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <code>FileDescriptor</code> object is created to represent this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * file connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * First, if there is a security manager, its <code>checkWrite</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * method is called with the path represented by the <code>file</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * argument as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * If the file exists but is a directory rather than a regular file, does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * not exist but cannot be created, or cannot be opened for any other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * reason then a <code>FileNotFoundException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @param      file               the file to be opened for writing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @exception  FileNotFoundException  if the file exists but is a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *                   rather than a regular file, does not exist but cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *                   be created, or cannot be opened for any other reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *               <code>checkWrite</code> method denies write access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *               to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @see        java.io.File#getPath()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @see        java.lang.SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public FileOutputStream(File file) throws FileNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        this(file, false);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * the specified <code>File</code> object. If the second argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * <code>true</code>, then bytes will be written to the end of the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * rather than the beginning. A new <code>FileDescriptor</code> object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * created to represent this file connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * First, if there is a security manager, its <code>checkWrite</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * method is called with the path represented by the <code>file</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * argument as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * If the file exists but is a directory rather than a regular file, does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * not exist but cannot be created, or cannot be opened for any other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * reason then a <code>FileNotFoundException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param      file               the file to be opened for writing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @param     append      if <code>true</code>, then bytes will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *                   to the end of the file rather than the beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @exception  FileNotFoundException  if the file exists but is a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *                   rather than a regular file, does not exist but cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *                   be created, or cannot be opened for any other reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *               <code>checkWrite</code> method denies write access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *               to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @see        java.io.File#getPath()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @see        java.lang.SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public FileOutputStream(File file, boolean append)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        throws FileNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        String name = (file != null ? file.getPath() : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            security.checkWrite(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        fd = new FileDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        fd.incrementAndGetUseCount();
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 46
diff changeset
   201
        open(name, append);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
   205
     * Creates a file output stream to write to the specified file
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * descriptor, which represents an existing connection to an actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * file in the file system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * First, if there is a security manager, its <code>checkWrite</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * method is called with the file descriptor <code>fdObj</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * argument as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * If <code>fdObj</code> is null then a <code>NullPointerException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * 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
   217
     * is {@link java.io.FileDescriptor#valid() invalid}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * However, if the methods are invoked on the resulting stream to attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * I/O on the stream, an <code>IOException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @param      fdObj   the file descriptor to be opened for writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *               <code>checkWrite</code> method denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *               write access to the file descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @see        java.lang.SecurityManager#checkWrite(java.io.FileDescriptor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public FileOutputStream(FileDescriptor fdObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (fdObj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            security.checkWrite(fdObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        fd = fdObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
         * FileDescriptor is being shared by streams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
         * Ensure that it's GC'ed only when all the streams/channels are done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
         * using it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        fd.incrementAndGetUseCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 46
diff changeset
   246
     * Opens a file, with the specified name, for overwriting or appending.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @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
   248
     * @param append whether the file is to be opened in append mode
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 46
diff changeset
   250
    private native void open(String name, boolean append)
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 46
diff changeset
   251
        throws FileNotFoundException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * Writes the specified byte to this file output stream. Implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * the <code>write</code> method of <code>OutputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @param      b   the byte to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public native void write(int b) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Writes a sub array as a sequence of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @param off the start offset in the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @param len the number of bytes that are written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @exception IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    private native void writeBytes(byte b[], int off, int len) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Writes <code>b.length</code> bytes from the specified byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * to this file output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @param      b   the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    public void write(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        writeBytes(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * Writes <code>len</code> bytes from the specified byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * starting at offset <code>off</code> to this file output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @param      b     the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @param      off   the start offset in the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @param      len   the number of bytes to write.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public void write(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        writeBytes(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Closes this file output stream and releases any system resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * associated with this stream. This file output stream may no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * be used for writing bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * <p> If this stream has an associated channel then the channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        synchronized (closeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (channel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
             * Decrement FD use count associated with the channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
             * The use count is incremented whenever a new channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
             * is obtained from this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            fd.decrementAndGetUseCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            channel.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
         * Decrement FD use count associated with this stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        int useCount = fd.decrementAndGetUseCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
         * If FileDescriptor is still in use by another stream, the finalizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
         * will not close it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if ((useCount <= 0) || !isRunningFinalize()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            close0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * Returns the file descriptor associated with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @return  the <code>FileDescriptor</code> object that represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *          the connection to the file in the file system being used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *          by this <code>FileOutputStream</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @see        java.io.FileDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     public final FileDescriptor getFD()  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (fd != null) return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        throw new IOException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * object associated with this file output stream. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * <p> The initial {@link java.nio.channels.FileChannel#position()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * </code>position<code>} of the returned channel will be equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * number of bytes written to the file so far unless this stream is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * append mode, in which case it will be equal to the size of the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * Writing bytes to this stream will increment the channel's position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * accordingly.  Changing the channel's position, either explicitly or by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * writing, will change this stream's file position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @return  the file channel associated with this file output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * @since 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 FileChannel getChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            if (channel == null) {
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 46
diff changeset
   375
                channel = FileChannelImpl.open(fd, false, true, this);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                 * Increment fd's use count. Invoking the channel's close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                 * method will result in decrementing the use count set for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                 * the channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                fd.incrementAndGetUseCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            return channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * Cleans up the connection to the file, and ensures that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * <code>close</code> method of this file output stream is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * called when there are no more references to this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @see        java.io.FileInputStream#close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    protected void finalize() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (fd != null) {
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
   398
            if (fd == FileDescriptor.out || fd == FileDescriptor.err) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                 * Finalizer should not release the FileDescriptor if another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                 * stream is still using it. If the user directly invokes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                 * close() then the FileDescriptor is also released.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                runningFinalize.set(Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    runningFinalize.set(Boolean.FALSE);
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
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    private native void close0() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
}