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