jdk/src/share/classes/java/io/FileOutputStream.java
author martin
Mon, 10 Mar 2008 14:32:51 -0700
changeset 46 ddf5deb2a633
parent 2 90ce3da70b43
child 47 c8f0e41aea68
permissions -rw-r--r--
6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code Reviewed-by: alanb, iris
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1994-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private boolean append = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
    63
    private final Object closeLock = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    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
    65
    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
    66
        new ThreadLocal<Boolean>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static boolean isRunningFinalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        Boolean val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if ((val = runningFinalize.get()) != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            return val.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
    76
     * Creates a file output stream to write to the file with the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * specified name. A new <code>FileDescriptor</code> object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * created to represent this file connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * First, if there is a security manager, its <code>checkWrite</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * method is called with <code>name</code> as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * If the file exists but is a directory rather than a regular file, does
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * not exist but cannot be created, or cannot be opened for any other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * reason then a <code>FileNotFoundException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param      name   the system-dependent filename
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @exception  FileNotFoundException  if the file exists but is a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *                   rather than a regular file, does not exist but cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *                   be created, or cannot be opened for any other reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *               <code>checkWrite</code> method denies write access
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *               to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public FileOutputStream(String name) throws FileNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        this(name != null ? new File(name) : null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
   101
     * Creates a file output stream to write to the file with the specified
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
   102
     * name.  If the second argument is <code>true</code>, then
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * bytes will be written to the end of the file rather than the beginning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * A new <code>FileDescriptor</code> object is created to represent this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * file connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * First, if there is a security manager, its <code>checkWrite</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * method is called with <code>name</code> as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * If the file exists but is a directory rather than a regular file, does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * not exist but cannot be created, or cannot be opened for any other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * reason then a <code>FileNotFoundException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param     name        the system-dependent file name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param     append      if <code>true</code>, then bytes will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *                   to the end of the file rather than the beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @exception  FileNotFoundException  if the file exists but is a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *                   rather than a regular file, does not exist but cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *                   be created, or cannot be opened for any other reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *               <code>checkWrite</code> method denies write access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *               to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @since     JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public FileOutputStream(String name, boolean append)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        throws FileNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        this(name != null ? new File(name) : null, append);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Creates a file output stream to write to the file represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * the specified <code>File</code> object. A new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * <code>FileDescriptor</code> object is created to represent this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * file connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * First, if there is a security manager, its <code>checkWrite</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * method is called with the path represented by the <code>file</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * argument as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * If the file exists but is a directory rather than a regular file, does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * not exist but cannot be created, or cannot be opened for any other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * reason then a <code>FileNotFoundException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param      file               the file to be opened for writing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @exception  FileNotFoundException  if the file exists but is a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *                   rather than a regular file, does not exist but cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *                   be created, or cannot be opened for any other reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *               <code>checkWrite</code> method denies write access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *               to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @see        java.io.File#getPath()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @see        java.lang.SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public FileOutputStream(File file) throws FileNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        this(file, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Creates a file output stream to write to the file represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * the specified <code>File</code> object. If the second argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * <code>true</code>, then bytes will be written to the end of the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * rather than the beginning. A new <code>FileDescriptor</code> object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * created to represent this file connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * First, if there is a security manager, its <code>checkWrite</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * method is called with the path represented by the <code>file</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * argument as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * If the file exists but is a directory rather than a regular file, does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * not exist but cannot be created, or cannot be opened for any other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * reason then a <code>FileNotFoundException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param      file               the file to be opened for writing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param     append      if <code>true</code>, then bytes will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *                   to the end of the file rather than the beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @exception  FileNotFoundException  if the file exists but is a directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *                   rather than a regular file, does not exist but cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *                   be created, or cannot be opened for any other reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *               <code>checkWrite</code> method denies write access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *               to the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @see        java.io.File#getPath()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @see        java.lang.SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @see        java.lang.SecurityManager#checkWrite(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public FileOutputStream(File file, boolean append)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        throws FileNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        String name = (file != null ? file.getPath() : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            security.checkWrite(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        fd = new FileDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        fd.incrementAndGetUseCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        this.append = append;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (append) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            openAppend(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            open(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /**
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
   212
     * Creates a file output stream to write to the specified file
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * descriptor, which represents an existing connection to an actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * file in the file system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * First, if there is a security manager, its <code>checkWrite</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * method is called with the file descriptor <code>fdObj</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * argument as its argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * If <code>fdObj</code> is null then a <code>NullPointerException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * 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
   224
     * is {@link java.io.FileDescriptor#valid() invalid}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * However, if the methods are invoked on the resulting stream to attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * I/O on the stream, an <code>IOException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @param      fdObj   the file descriptor to be opened for writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *               <code>checkWrite</code> method denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *               write access to the file descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @see        java.lang.SecurityManager#checkWrite(java.io.FileDescriptor)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public FileOutputStream(FileDescriptor fdObj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (fdObj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            security.checkWrite(fdObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        fd = fdObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
         * FileDescriptor is being shared by streams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
         * Ensure that it's GC'ed only when all the streams/channels are done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
         * using it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        fd.incrementAndGetUseCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Opens a file, with the specified name, for writing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @param name name of file to be opened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    private native void open(String name) throws FileNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * Opens a file, with the specified name, for appending.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @param name name of file to be opened
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    private native void openAppend(String name) throws FileNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Writes the specified byte to this file output stream. Implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * the <code>write</code> method of <code>OutputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @param      b   the byte to be written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public native void write(int b) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * Writes a sub array as a sequence of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @param b the data to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @param off the start offset in the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @param len the number of bytes that are written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @exception IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    private native void writeBytes(byte b[], int off, int len) throws IOException;
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>b.length</code> bytes from the specified byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * 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
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    public void write(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        writeBytes(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Writes <code>len</code> bytes from the specified byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * starting at offset <code>off</code> to this file output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @param      b     the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @param      off   the start offset in the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @param      len   the number of bytes to write.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public void write(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        writeBytes(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Closes this file output stream and releases any system resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * associated with this stream. This file output stream may no longer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * be used for writing bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * <p> If this stream has an associated channel then the channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @revised 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        synchronized (closeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if (channel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
             * Decrement FD use count associated with the channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
             * The use count is incremented whenever a new channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
             * is obtained from this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            fd.decrementAndGetUseCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            channel.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         * Decrement FD use count associated with this stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        int useCount = fd.decrementAndGetUseCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
         * If FileDescriptor is still in use by another stream, the finalizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
         * will not close it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if ((useCount <= 0) || !isRunningFinalize()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            close0();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * Returns the file descriptor associated with this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @return  the <code>FileDescriptor</code> object that represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *          the connection to the file in the file system being used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *          by this <code>FileOutputStream</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @see        java.io.FileDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     public final FileDescriptor getFD()  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        if (fd != null) return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        throw new IOException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * object associated with this file output stream. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * <p> The initial {@link java.nio.channels.FileChannel#position()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * </code>position<code>} of the returned channel will be equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * number of bytes written to the file so far unless this stream is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * append mode, in which case it will be equal to the size of the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * Writing bytes to this stream will increment the channel's position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * accordingly.  Changing the channel's position, either explicitly or by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * writing, will change this stream's file position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * @return  the file channel associated with this file output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @spec JSR-51
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    public FileChannel getChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            if (channel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                channel = FileChannelImpl.open(fd, false, true, this, append);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                 * Increment fd's use count. Invoking the channel's close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                 * method will result in decrementing the use count set for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                 * the channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                fd.incrementAndGetUseCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            return channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * Cleans up the connection to the file, and ensures that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * <code>close</code> method of this file output stream is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * called when there are no more references to this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @see        java.io.FileInputStream#close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    protected void finalize() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if (fd != null) {
46
ddf5deb2a633 6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents: 2
diff changeset
   409
            if (fd == FileDescriptor.out || fd == FileDescriptor.err) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                 * Finalizer should not release the FileDescriptor if another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                 * stream is still using it. If the user directly invokes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                 * close() then the FileDescriptor is also released.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                runningFinalize.set(Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    runningFinalize.set(Boolean.FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    private native void close0() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
}