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