1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. Oracle designates this 9 * particular file as subject to the "Classpath" exception as provided 10 * by Oracle in the LICENSE file that accompanied this code. 11 * 12 * This code is distributed in the hope that it will be useful, but WITHOUT 13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15 * version 2 for more details (a copy is included in the LICENSE file that 16 * accompanied this code). 17 * 18 * You should have received a copy of the GNU General Public License version 19 * 2 along with this work; if not, write to the Free Software Foundation, 20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 21 * 22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 23 * or visit www.oracle.com if you need additional information or have any 24 * questions. 25 */ 26 27 package java.io; 28 29 import static android.system.OsConstants.O_APPEND; 30 import static android.system.OsConstants.O_CREAT; 31 import static android.system.OsConstants.O_TRUNC; 32 import static android.system.OsConstants.O_WRONLY; 33 34 import java.nio.channels.FileChannel; 35 36 import dalvik.annotation.optimization.ReachabilitySensitive; 37 import dalvik.system.BlockGuard; 38 import dalvik.system.CloseGuard; 39 import sun.nio.ch.FileChannelImpl; 40 import libcore.io.IoBridge; 41 import libcore.io.IoTracker; 42 import libcore.io.IoUtils; 43 44 /** 45 * A file output stream is an output stream for writing data to a 46 * <code>File</code> or to a <code>FileDescriptor</code>. Whether or not 47 * a file is available or may be created depends upon the underlying 48 * platform. Some platforms, in particular, allow a file to be opened 49 * for writing by only one <tt>FileOutputStream</tt> (or other 50 * file-writing object) at a time. In such situations the constructors in 51 * this class will fail if the file involved is already open. 52 * 53 * <p><code>FileOutputStream</code> is meant for writing streams of raw bytes 54 * such as image data. For writing streams of characters, consider using 55 * <code>FileWriter</code>. 56 * 57 * @author Arthur van Hoff 58 * @see java.io.File 59 * @see java.io.FileDescriptor 60 * @see java.io.FileInputStream 61 * @see java.nio.file.Files#newOutputStream 62 * @since JDK1.0 63 */ 64 public 65 class FileOutputStream extends OutputStream 66 { 67 /** 68 * The system dependent file descriptor. 69 */ 70 // Android-added: @ReachabilitySensitive 71 @ReachabilitySensitive 72 private final FileDescriptor fd; 73 74 /** 75 * True if the file is opened for append. 76 */ 77 private final boolean append; 78 79 /** 80 * The associated channel, initialized lazily. 81 */ 82 private FileChannel channel; 83 84 /** 85 * The path of the referenced file 86 * (null if the stream is created with a file descriptor) 87 */ 88 private final String path; 89 90 private final Object closeLock = new Object(); 91 private volatile boolean closed = false; 92 93 // Android-added: CloseGuard support: Log if the stream is not closed. 94 @ReachabilitySensitive 95 private final CloseGuard guard = CloseGuard.get(); 96 97 // Android-added: Field for tracking whether the stream owns the underlying FileDescriptor. 98 private final boolean isFdOwner; 99 100 // Android-added: Tracking of unbuffered I/O. 101 private final IoTracker tracker = new IoTracker(); 102 103 /** 104 * Creates a file output stream to write to the file with the 105 * specified name. A new <code>FileDescriptor</code> object is 106 * created to represent this file connection. 107 * <p> 108 * First, if there is a security manager, its <code>checkWrite</code> 109 * method is called with <code>name</code> as its argument. 110 * <p> 111 * If the file exists but is a directory rather than a regular file, does 112 * not exist but cannot be created, or cannot be opened for any other 113 * reason then a <code>FileNotFoundException</code> is thrown. 114 * 115 * @param name the system-dependent filename 116 * @exception FileNotFoundException if the file exists but is a directory 117 * rather than a regular file, does not exist but cannot 118 * be created, or cannot be opened for any other reason 119 * @exception SecurityException if a security manager exists and its 120 * <code>checkWrite</code> method denies write access 121 * to the file. 122 * @see java.lang.SecurityManager#checkWrite(java.lang.String) 123 */ FileOutputStream(String name)124 public FileOutputStream(String name) throws FileNotFoundException { 125 this(name != null ? new File(name) : null, false); 126 } 127 128 /** 129 * Creates a file output stream to write to the file with the specified 130 * name. If the second argument is <code>true</code>, then 131 * bytes will be written to the end of the file rather than the beginning. 132 * A new <code>FileDescriptor</code> object is created to represent this 133 * file connection. 134 * <p> 135 * First, if there is a security manager, its <code>checkWrite</code> 136 * method is called with <code>name</code> as its argument. 137 * <p> 138 * If the file exists but is a directory rather than a regular file, does 139 * not exist but cannot be created, or cannot be opened for any other 140 * reason then a <code>FileNotFoundException</code> is thrown. 141 * 142 * @param name the system-dependent file name 143 * @param append if <code>true</code>, then bytes will be written 144 * to the end of the file rather than the beginning 145 * @exception FileNotFoundException if the file exists but is a directory 146 * rather than a regular file, does not exist but cannot 147 * be created, or cannot be opened for any other reason. 148 * @exception SecurityException if a security manager exists and its 149 * <code>checkWrite</code> method denies write access 150 * to the file. 151 * @see java.lang.SecurityManager#checkWrite(java.lang.String) 152 * @since JDK1.1 153 */ FileOutputStream(String name, boolean append)154 public FileOutputStream(String name, boolean append) 155 throws FileNotFoundException 156 { 157 this(name != null ? new File(name) : null, append); 158 } 159 160 /** 161 * Creates a file output stream to write to the file represented by 162 * the specified <code>File</code> object. A new 163 * <code>FileDescriptor</code> object is created to represent this 164 * file connection. 165 * <p> 166 * First, if there is a security manager, its <code>checkWrite</code> 167 * method is called with the path represented by the <code>file</code> 168 * argument as its argument. 169 * <p> 170 * If the file exists but is a directory rather than a regular file, does 171 * not exist but cannot be created, or cannot be opened for any other 172 * reason then a <code>FileNotFoundException</code> is thrown. 173 * 174 * @param file the file to be opened for writing. 175 * @exception FileNotFoundException if the file exists but is a directory 176 * rather than a regular file, does not exist but cannot 177 * be created, or cannot be opened for any other reason 178 * @exception SecurityException if a security manager exists and its 179 * <code>checkWrite</code> method denies write access 180 * to the file. 181 * @see java.io.File#getPath() 182 * @see java.lang.SecurityException 183 * @see java.lang.SecurityManager#checkWrite(java.lang.String) 184 */ FileOutputStream(File file)185 public FileOutputStream(File file) throws FileNotFoundException { 186 this(file, false); 187 } 188 189 /** 190 * Creates a file output stream to write to the file represented by 191 * the specified <code>File</code> object. If the second argument is 192 * <code>true</code>, then bytes will be written to the end of the file 193 * rather than the beginning. A new <code>FileDescriptor</code> object is 194 * created to represent this file connection. 195 * <p> 196 * First, if there is a security manager, its <code>checkWrite</code> 197 * method is called with the path represented by the <code>file</code> 198 * argument as its argument. 199 * <p> 200 * If the file exists but is a directory rather than a regular file, does 201 * not exist but cannot be created, or cannot be opened for any other 202 * reason then a <code>FileNotFoundException</code> is thrown. 203 * 204 * @param file the file to be opened for writing. 205 * @param append if <code>true</code>, then bytes will be written 206 * to the end of the file rather than the beginning 207 * @exception FileNotFoundException if the file exists but is a directory 208 * rather than a regular file, does not exist but cannot 209 * be created, or cannot be opened for any other reason 210 * @exception SecurityException if a security manager exists and its 211 * <code>checkWrite</code> method denies write access 212 * to the file. 213 * @see java.io.File#getPath() 214 * @see java.lang.SecurityException 215 * @see java.lang.SecurityManager#checkWrite(java.lang.String) 216 * @since 1.4 217 */ FileOutputStream(File file, boolean append)218 public FileOutputStream(File file, boolean append) 219 throws FileNotFoundException 220 { 221 String name = (file != null ? file.getPath() : null); 222 SecurityManager security = System.getSecurityManager(); 223 if (security != null) { 224 security.checkWrite(name); 225 } 226 if (name == null) { 227 throw new NullPointerException(); 228 } 229 if (file.isInvalid()) { 230 throw new FileNotFoundException("Invalid file path"); 231 } 232 // BEGIN Android-changed: Open files through common bridge code. 233 // this.fd = new FileDescriptor(); 234 int flags = O_WRONLY | O_CREAT | (append ? O_APPEND : O_TRUNC); 235 this.fd = IoBridge.open(name, flags); 236 // END Android-changed: Open files through common bridge code. 237 238 // Android-changed: Tracking mechanism for FileDescriptor sharing. 239 // fd.attach(this); 240 this.isFdOwner = true; 241 242 this.append = append; 243 this.path = name; 244 245 // Android-removed: Open files through common bridge code. 246 // open(name, append); 247 248 // Android-added: File descriptor ownership tracking. 249 IoUtils.setFdOwner(this.fd, this); 250 251 // Android-added: CloseGuard support. 252 guard.open("close"); 253 } 254 255 // Android-removed: Documentation around SecurityException. Not thrown on Android. 256 /** 257 * Creates a file output stream to write to the specified file 258 * descriptor, which represents an existing connection to an actual 259 * file in the file system. 260 * <p> 261 * First, if there is a security manager, its <code>checkWrite</code> 262 * method is called with the file descriptor <code>fdObj</code> 263 * argument as its argument. 264 * <p> 265 * If <code>fdObj</code> is null then a <code>NullPointerException</code> 266 * is thrown. 267 * <p> 268 * This constructor does not throw an exception if <code>fdObj</code> 269 * is {@link java.io.FileDescriptor#valid() invalid}. 270 * However, if the methods are invoked on the resulting stream to attempt 271 * I/O on the stream, an <code>IOException</code> is thrown. 272 * 273 * @param fdObj the file descriptor to be opened for writing 274 */ FileOutputStream(FileDescriptor fdObj)275 public FileOutputStream(FileDescriptor fdObj) { 276 // Android-changed: Delegate to added hidden constructor. 277 this(fdObj, false /* isOwner */); 278 } 279 280 // Android-added: Internal/hidden constructor for specifying FileDescriptor ownership. 281 // Android-removed: SecurityManager calls. 282 /** 283 * Internal constructor for {@code FileOutputStream} objects where the file descriptor 284 * is owned by this tream. 285 * 286 * @hide 287 */ FileOutputStream(FileDescriptor fdObj, boolean isFdOwner)288 public FileOutputStream(FileDescriptor fdObj, boolean isFdOwner) { 289 if (fdObj == null) { 290 // Android-changed: Improved NullPointerException message. 291 throw new NullPointerException("fdObj == null"); 292 } 293 294 this.fd = fdObj; 295 this.append = false; 296 this.path = null; 297 298 // Android-changed: FileDescriptor ownership tracking mechanism. 299 // fd.attach(this); 300 this.isFdOwner = isFdOwner; 301 } 302 303 /** 304 * Opens a file, with the specified name, for overwriting or appending. 305 * @param name name of file to be opened 306 * @param append whether the file is to be opened in append mode 307 */ open0(String name, boolean append)308 private native void open0(String name, boolean append) 309 throws FileNotFoundException; 310 311 // wrap native call to allow instrumentation 312 /** 313 * Opens a file, with the specified name, for overwriting or appending. 314 * @param name name of file to be opened 315 * @param append whether the file is to be opened in append mode 316 */ open(String name, boolean append)317 private void open(String name, boolean append) 318 throws FileNotFoundException { 319 open0(name, append); 320 } 321 322 // Android-removed: write(int, boolean), use IoBridge instead. 323 /* 324 /** 325 * Writes the specified byte to this file output stream. 326 * 327 * @param b the byte to be written. 328 * @param append {@code true} if the write operation first 329 * advances the position to the end of file 330 * 331 private native void write(int b, boolean append) throws IOException; 332 */ 333 334 /** 335 * Writes the specified byte to this file output stream. Implements 336 * the <code>write</code> method of <code>OutputStream</code>. 337 * 338 * @param b the byte to be written. 339 * @exception IOException if an I/O error occurs. 340 */ write(int b)341 public void write(int b) throws IOException { 342 // Android-changed: Write methods delegate to write(byte[],int,int) to share Android logic. 343 write(new byte[] { (byte) b }, 0, 1); 344 } 345 346 // Android-removed: Write methods delegate to write(byte[],int,int) to share Android logic. 347 /* 348 /** 349 * Writes a sub array as a sequence of bytes. 350 * @param b the data to be written 351 * @param off the start offset in the data 352 * @param len the number of bytes that are written 353 * @param append {@code true} to first advance the position to the 354 * end of file 355 * @exception IOException If an I/O error has occurred. 356 * 357 private native void writeBytes(byte b[], int off, int len, boolean append) 358 throws IOException; 359 */ 360 361 /** 362 * Writes <code>b.length</code> bytes from the specified byte array 363 * to this file output stream. 364 * 365 * @param b the data. 366 * @exception IOException if an I/O error occurs. 367 */ write(byte b[])368 public void write(byte b[]) throws IOException { 369 // Android-changed: Write methods delegate to write(byte[],int,int) to share Android logic. 370 write(b, 0, b.length); 371 } 372 373 /** 374 * Writes <code>len</code> bytes from the specified byte array 375 * starting at offset <code>off</code> to this file output stream. 376 * 377 * @param b the data. 378 * @param off the start offset in the data. 379 * @param len the number of bytes to write. 380 * @exception IOException if an I/O error occurs. 381 */ write(byte b[], int off, int len)382 public void write(byte b[], int off, int len) throws IOException { 383 // Android-added: close() check before I/O. 384 if (closed && len > 0) { 385 throw new IOException("Stream Closed"); 386 } 387 388 // Android-added: Tracking of unbuffered I/O. 389 tracker.trackIo(len); 390 391 // Android-changed: Use IoBridge instead of calling native method. 392 IoBridge.write(fd, b, off, len); 393 } 394 395 /** 396 * Closes this file output stream and releases any system resources 397 * associated with this stream. This file output stream may no longer 398 * be used for writing bytes. 399 * 400 * <p> If this stream has an associated channel then the channel is closed 401 * as well. 402 * 403 * @exception IOException if an I/O error occurs. 404 * 405 * @revised 1.4 406 * @spec JSR-51 407 */ close()408 public void close() throws IOException { 409 synchronized (closeLock) { 410 if (closed) { 411 return; 412 } 413 closed = true; 414 } 415 416 // Android-added: CloseGuard support. 417 guard.close(); 418 419 if (channel != null) { 420 channel.close(); 421 } 422 423 // BEGIN Android-changed: Close handling / notification of blocked threads. 424 if (isFdOwner) { 425 IoBridge.closeAndSignalBlockedThreads(fd); 426 } 427 // END Android-changed: Close handling / notification of blocked threads. 428 } 429 430 /** 431 * Returns the file descriptor associated with this stream. 432 * 433 * @return the <code>FileDescriptor</code> object that represents 434 * the connection to the file in the file system being used 435 * by this <code>FileOutputStream</code> object. 436 * 437 * @exception IOException if an I/O error occurs. 438 * @see java.io.FileDescriptor 439 */ 440 // Android-added: @ReachabilitySensitive 441 @ReachabilitySensitive getFD()442 public final FileDescriptor getFD() throws IOException { 443 if (fd != null) { 444 return fd; 445 } 446 throw new IOException(); 447 } 448 449 /** 450 * Returns the unique {@link java.nio.channels.FileChannel FileChannel} 451 * object associated with this file output stream. 452 * 453 * <p> The initial {@link java.nio.channels.FileChannel#position() 454 * position} of the returned channel will be equal to the 455 * number of bytes written to the file so far unless this stream is in 456 * append mode, in which case it will be equal to the size of the file. 457 * Writing bytes to this stream will increment the channel's position 458 * accordingly. Changing the channel's position, either explicitly or by 459 * writing, will change this stream's file position. 460 * 461 * @return the file channel associated with this file output stream 462 * 463 * @since 1.4 464 * @spec JSR-51 465 */ getChannel()466 public FileChannel getChannel() { 467 synchronized (this) { 468 if (channel == null) { 469 channel = FileChannelImpl.open(fd, path, false, true, append, this); 470 } 471 return channel; 472 } 473 } 474 475 /** 476 * Cleans up the connection to the file, and ensures that the 477 * <code>close</code> method of this file output stream is 478 * called when there are no more references to this stream. 479 * 480 * @exception IOException if an I/O error occurs. 481 * @see java.io.FileInputStream#close() 482 */ finalize()483 protected void finalize() throws IOException { 484 // Android-added: CloseGuard support. 485 if (guard != null) { 486 guard.warnIfOpen(); 487 } 488 489 if (fd != null) { 490 if (fd == FileDescriptor.out || fd == FileDescriptor.err) { 491 flush(); 492 } else { 493 // Android-removed: Obsoleted comment about shared FileDescriptor handling. 494 close(); 495 } 496 } 497 } 498 499 // BEGIN Android-removed: Unused code. 500 /* 501 private native void close0() throws IOException; 502 503 private static native void initIDs(); 504 505 static { 506 initIDs(); 507 } 508 */ 509 // END Android-removed: Unused code. 510 511 } 512