0N/A/*
6092N/A * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.io;
0N/A
0N/Aimport java.nio.channels.FileChannel;
0N/Aimport sun.nio.ch.FileChannelImpl;
5503N/Aimport sun.misc.IoTrace;
0N/A
0N/A
0N/A/**
0N/A * A file output stream is an output stream for writing data to a
0N/A * <code>File</code> or to a <code>FileDescriptor</code>. Whether or not
0N/A * a file is available or may be created depends upon the underlying
0N/A * platform. Some platforms, in particular, allow a file to be opened
0N/A * for writing by only one <tt>FileOutputStream</tt> (or other
0N/A * file-writing object) at a time. In such situations the constructors in
0N/A * this class will fail if the file involved is already open.
0N/A *
0N/A * <p><code>FileOutputStream</code> is meant for writing streams of raw bytes
0N/A * such as image data. For writing streams of characters, consider using
0N/A * <code>FileWriter</code>.
0N/A *
0N/A * @author Arthur van Hoff
0N/A * @see java.io.File
0N/A * @see java.io.FileDescriptor
0N/A * @see java.io.FileInputStream
3471N/A * @see java.nio.file.Files#newOutputStream
0N/A * @since JDK1.0
0N/A */
0N/Apublic
0N/Aclass FileOutputStream extends OutputStream
0N/A{
0N/A /**
23N/A * The system dependent file descriptor.
0N/A */
23N/A private final FileDescriptor fd;
0N/A
3200N/A /**
5503N/A * The path of the referenced file (null if the stream is created with a file descriptor)
5503N/A */
5503N/A private final String path;
5503N/A
5503N/A /**
3200N/A * True if the file is opened for append.
3200N/A */
3200N/A private final boolean append;
3200N/A
3200N/A /**
4474N/A * The associated channel, initalized lazily.
3200N/A */
3200N/A private FileChannel channel;
0N/A
23N/A private final Object closeLock = new Object();
0N/A private volatile boolean closed = false;
5182N/A private static final ThreadLocal<Boolean> runningFinalize =
5182N/A new ThreadLocal<>();
5182N/A
5182N/A private static boolean isRunningFinalize() {
5182N/A Boolean val;
5182N/A if ((val = runningFinalize.get()) != null)
5182N/A return val.booleanValue();
5182N/A return false;
5182N/A }
0N/A
0N/A /**
23N/A * Creates a file output stream to write to the file with the
0N/A * specified name. A new <code>FileDescriptor</code> object is
0N/A * created to represent this file connection.
0N/A * <p>
0N/A * First, if there is a security manager, its <code>checkWrite</code>
0N/A * method is called with <code>name</code> as its argument.
0N/A * <p>
0N/A * If the file exists but is a directory rather than a regular file, does
0N/A * not exist but cannot be created, or cannot be opened for any other
0N/A * reason then a <code>FileNotFoundException</code> is thrown.
0N/A *
0N/A * @param name the system-dependent filename
0N/A * @exception FileNotFoundException if the file exists but is a directory
0N/A * rather than a regular file, does not exist but cannot
0N/A * be created, or cannot be opened for any other reason
0N/A * @exception SecurityException if a security manager exists and its
0N/A * <code>checkWrite</code> method denies write access
0N/A * to the file.
0N/A * @see java.lang.SecurityManager#checkWrite(java.lang.String)
0N/A */
0N/A public FileOutputStream(String name) throws FileNotFoundException {
0N/A this(name != null ? new File(name) : null, false);
0N/A }
0N/A
0N/A /**
23N/A * Creates a file output stream to write to the file with the specified
23N/A * name. If the second argument is <code>true</code>, then
0N/A * bytes will be written to the end of the file rather than the beginning.
0N/A * A new <code>FileDescriptor</code> object is created to represent this
0N/A * file connection.
0N/A * <p>
0N/A * First, if there is a security manager, its <code>checkWrite</code>
0N/A * method is called with <code>name</code> as its argument.
0N/A * <p>
0N/A * If the file exists but is a directory rather than a regular file, does
0N/A * not exist but cannot be created, or cannot be opened for any other
0N/A * reason then a <code>FileNotFoundException</code> is thrown.
0N/A *
0N/A * @param name the system-dependent file name
0N/A * @param append if <code>true</code>, then bytes will be written
0N/A * to the end of the file rather than the beginning
0N/A * @exception FileNotFoundException if the file exists but is a directory
0N/A * rather than a regular file, does not exist but cannot
0N/A * be created, or cannot be opened for any other reason.
0N/A * @exception SecurityException if a security manager exists and its
0N/A * <code>checkWrite</code> method denies write access
0N/A * to the file.
0N/A * @see java.lang.SecurityManager#checkWrite(java.lang.String)
0N/A * @since JDK1.1
0N/A */
0N/A public FileOutputStream(String name, boolean append)
0N/A throws FileNotFoundException
0N/A {
0N/A this(name != null ? new File(name) : null, append);
0N/A }
0N/A
0N/A /**
0N/A * Creates a file output stream to write to the file represented by
0N/A * the specified <code>File</code> object. A new
0N/A * <code>FileDescriptor</code> object is created to represent this
0N/A * file connection.
0N/A * <p>
0N/A * First, if there is a security manager, its <code>checkWrite</code>
0N/A * method is called with the path represented by the <code>file</code>
0N/A * argument as its argument.
0N/A * <p>
0N/A * If the file exists but is a directory rather than a regular file, does
0N/A * not exist but cannot be created, or cannot be opened for any other
0N/A * reason then a <code>FileNotFoundException</code> is thrown.
0N/A *
0N/A * @param file the file to be opened for writing.
0N/A * @exception FileNotFoundException if the file exists but is a directory
0N/A * rather than a regular file, does not exist but cannot
0N/A * be created, or cannot be opened for any other reason
0N/A * @exception SecurityException if a security manager exists and its
0N/A * <code>checkWrite</code> method denies write access
0N/A * to the file.
0N/A * @see java.io.File#getPath()
0N/A * @see java.lang.SecurityException
0N/A * @see java.lang.SecurityManager#checkWrite(java.lang.String)
0N/A */
0N/A public FileOutputStream(File file) throws FileNotFoundException {
0N/A this(file, false);
0N/A }
0N/A
0N/A /**
0N/A * Creates a file output stream to write to the file represented by
0N/A * the specified <code>File</code> object. If the second argument is
0N/A * <code>true</code>, then bytes will be written to the end of the file
0N/A * rather than the beginning. A new <code>FileDescriptor</code> object is
0N/A * created to represent this file connection.
0N/A * <p>
0N/A * First, if there is a security manager, its <code>checkWrite</code>
0N/A * method is called with the path represented by the <code>file</code>
0N/A * argument as its argument.
0N/A * <p>
0N/A * If the file exists but is a directory rather than a regular file, does
0N/A * not exist but cannot be created, or cannot be opened for any other
0N/A * reason then a <code>FileNotFoundException</code> is thrown.
0N/A *
0N/A * @param file the file to be opened for writing.
0N/A * @param append if <code>true</code>, then bytes will be written
0N/A * to the end of the file rather than the beginning
0N/A * @exception FileNotFoundException if the file exists but is a directory
0N/A * rather than a regular file, does not exist but cannot
0N/A * be created, or cannot be opened for any other reason
0N/A * @exception SecurityException if a security manager exists and its
0N/A * <code>checkWrite</code> method denies write access
0N/A * to the file.
0N/A * @see java.io.File#getPath()
0N/A * @see java.lang.SecurityException
0N/A * @see java.lang.SecurityManager#checkWrite(java.lang.String)
0N/A * @since 1.4
0N/A */
0N/A public FileOutputStream(File file, boolean append)
0N/A throws FileNotFoundException
0N/A {
0N/A String name = (file != null ? file.getPath() : null);
0N/A SecurityManager security = System.getSecurityManager();
0N/A if (security != null) {
0N/A security.checkWrite(name);
0N/A }
0N/A if (name == null) {
0N/A throw new NullPointerException();
0N/A }
6092N/A if (file.isInvalid()) {
6092N/A throw new FileNotFoundException("Invalid file path");
6092N/A }
3200N/A this.fd = new FileDescriptor();
3200N/A this.append = append;
5503N/A this.path = name;
5182N/A fd.incrementAndGetUseCount();
24N/A open(name, append);
0N/A }
0N/A
0N/A /**
23N/A * Creates a file output stream to write to the specified file
0N/A * descriptor, which represents an existing connection to an actual
0N/A * file in the file system.
0N/A * <p>
0N/A * First, if there is a security manager, its <code>checkWrite</code>
0N/A * method is called with the file descriptor <code>fdObj</code>
0N/A * argument as its argument.
0N/A * <p>
0N/A * If <code>fdObj</code> is null then a <code>NullPointerException</code>
0N/A * is thrown.
0N/A * <p>
0N/A * This constructor does not throw an exception if <code>fdObj</code>
23N/A * is {@link java.io.FileDescriptor#valid() invalid}.
0N/A * However, if the methods are invoked on the resulting stream to attempt
0N/A * I/O on the stream, an <code>IOException</code> is thrown.
0N/A *
0N/A * @param fdObj the file descriptor to be opened for writing
0N/A * @exception SecurityException if a security manager exists and its
0N/A * <code>checkWrite</code> method denies
0N/A * write access to the file descriptor
0N/A * @see java.lang.SecurityManager#checkWrite(java.io.FileDescriptor)
0N/A */
0N/A public FileOutputStream(FileDescriptor fdObj) {
0N/A SecurityManager security = System.getSecurityManager();
0N/A if (fdObj == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A if (security != null) {
0N/A security.checkWrite(fdObj);
0N/A }
3200N/A this.fd = fdObj;
5503N/A this.path = null;
3200N/A this.append = false;
5182N/A
5182N/A /*
5182N/A * FileDescriptor is being shared by streams.
5182N/A * Ensure that it's GC'ed only when all the streams/channels are done
5182N/A * using it.
5182N/A */
5182N/A fd.incrementAndGetUseCount();
0N/A }
0N/A
0N/A /**
24N/A * Opens a file, with the specified name, for overwriting or appending.
0N/A * @param name name of file to be opened
24N/A * @param append whether the file is to be opened in append mode
0N/A */
24N/A private native void open(String name, boolean append)
24N/A throws FileNotFoundException;
0N/A
0N/A /**
3200N/A * Writes the specified byte to this file output stream.
3200N/A *
3200N/A * @param b the byte to be written.
3200N/A * @param append {@code true} if the write operation first
3200N/A * advances the position to the end of file
3200N/A */
3200N/A private native void write(int b, boolean append) throws IOException;
3200N/A
3200N/A /**
0N/A * Writes the specified byte to this file output stream. Implements
0N/A * the <code>write</code> method of <code>OutputStream</code>.
0N/A *
0N/A * @param b the byte to be written.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
3200N/A public void write(int b) throws IOException {
5503N/A Object traceContext = IoTrace.fileWriteBegin(path);
5503N/A int bytesWritten = 0;
5503N/A try {
5503N/A write(b, append);
5503N/A bytesWritten = 1;
5503N/A } finally {
5503N/A IoTrace.fileWriteEnd(traceContext, bytesWritten);
5503N/A }
3200N/A }
0N/A
0N/A /**
0N/A * Writes a sub array as a sequence of bytes.
0N/A * @param b the data to be written
0N/A * @param off the start offset in the data
0N/A * @param len the number of bytes that are written
3200N/A * @param append {@code true} to first advance the position to the
3200N/A * end of file
0N/A * @exception IOException If an I/O error has occurred.
0N/A */
3200N/A private native void writeBytes(byte b[], int off, int len, boolean append)
3200N/A throws IOException;
0N/A
0N/A /**
0N/A * Writes <code>b.length</code> bytes from the specified byte array
0N/A * to this file output stream.
0N/A *
0N/A * @param b the data.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public void write(byte b[]) throws IOException {
5503N/A Object traceContext = IoTrace.fileWriteBegin(path);
5503N/A int bytesWritten = 0;
5503N/A try {
5503N/A writeBytes(b, 0, b.length, append);
5503N/A bytesWritten = b.length;
5503N/A } finally {
5503N/A IoTrace.fileWriteEnd(traceContext, bytesWritten);
5503N/A }
0N/A }
0N/A
0N/A /**
0N/A * Writes <code>len</code> bytes from the specified byte array
0N/A * starting at offset <code>off</code> to this file output stream.
0N/A *
0N/A * @param b the data.
0N/A * @param off the start offset in the data.
0N/A * @param len the number of bytes to write.
0N/A * @exception IOException if an I/O error occurs.
0N/A */
0N/A public void write(byte b[], int off, int len) throws IOException {
5503N/A Object traceContext = IoTrace.fileWriteBegin(path);
5503N/A int bytesWritten = 0;
5503N/A try {
5503N/A writeBytes(b, off, len, append);
5503N/A bytesWritten = len;
5503N/A } finally {
5503N/A IoTrace.fileWriteEnd(traceContext, bytesWritten);
5503N/A }
0N/A }
0N/A
0N/A /**
0N/A * Closes this file output stream and releases any system resources
0N/A * associated with this stream. This file output stream may no longer
0N/A * be used for writing bytes.
0N/A *
0N/A * <p> If this stream has an associated channel then the channel is closed
0N/A * as well.
0N/A *
0N/A * @exception IOException if an I/O error occurs.
0N/A *
0N/A * @revised 1.4
0N/A * @spec JSR-51
0N/A */
0N/A public void close() throws IOException {
0N/A synchronized (closeLock) {
0N/A if (closed) {
0N/A return;
0N/A }
0N/A closed = true;
0N/A }
0N/A
0N/A if (channel != null) {
5182N/A /*
5182N/A * Decrement FD use count associated with the channel
5182N/A * The use count is incremented whenever a new channel
5182N/A * is obtained from this stream.
5182N/A */
5182N/A fd.decrementAndGetUseCount();
0N/A channel.close();
0N/A }
5182N/A
5182N/A /*
5182N/A * Decrement FD use count associated with this stream
5182N/A */
5182N/A int useCount = fd.decrementAndGetUseCount();
5182N/A
5182N/A /*
5182N/A * If FileDescriptor is still in use by another stream, the finalizer
5182N/A * will not close it.
5182N/A */
5182N/A if ((useCount <= 0) || !isRunningFinalize()) {
5182N/A close0();
5182N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the file descriptor associated with this stream.
0N/A *
0N/A * @return the <code>FileDescriptor</code> object that represents
0N/A * the connection to the file in the file system being used
0N/A * by this <code>FileOutputStream</code> object.
0N/A *
0N/A * @exception IOException if an I/O error occurs.
0N/A * @see java.io.FileDescriptor
0N/A */
0N/A public final FileDescriptor getFD() throws IOException {
5182N/A if (fd != null) return fd;
0N/A throw new IOException();
0N/A }
0N/A
0N/A /**
0N/A * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
0N/A * object associated with this file output stream. </p>
0N/A *
0N/A * <p> The initial {@link java.nio.channels.FileChannel#position()
0N/A * </code>position<code>} of the returned channel will be equal to the
0N/A * number of bytes written to the file so far unless this stream is in
0N/A * append mode, in which case it will be equal to the size of the file.
0N/A * Writing bytes to this stream will increment the channel's position
0N/A * accordingly. Changing the channel's position, either explicitly or by
0N/A * writing, will change this stream's file position.
0N/A *
0N/A * @return the file channel associated with this file output stream
0N/A *
0N/A * @since 1.4
0N/A * @spec JSR-51
0N/A */
0N/A public FileChannel getChannel() {
0N/A synchronized (this) {
0N/A if (channel == null) {
5503N/A channel = FileChannelImpl.open(fd, path, false, true, append, this);
5182N/A
5182N/A /*
5182N/A * Increment fd's use count. Invoking the channel's close()
5182N/A * method will result in decrementing the use count set for
5182N/A * the channel.
5182N/A */
5182N/A fd.incrementAndGetUseCount();
0N/A }
0N/A return channel;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Cleans up the connection to the file, and ensures that the
0N/A * <code>close</code> method of this file output stream is
0N/A * called when there are no more references to this stream.
0N/A *
0N/A * @exception IOException if an I/O error occurs.
0N/A * @see java.io.FileInputStream#close()
0N/A */
0N/A protected void finalize() throws IOException {
0N/A if (fd != null) {
23N/A if (fd == FileDescriptor.out || fd == FileDescriptor.err) {
0N/A flush();
0N/A } else {
5182N/A
5182N/A /*
5182N/A * Finalizer should not release the FileDescriptor if another
5182N/A * stream is still using it. If the user directly invokes
5182N/A * close() then the FileDescriptor is also released.
4474N/A */
5182N/A runningFinalize.set(Boolean.TRUE);
5182N/A try {
5182N/A close();
5182N/A } finally {
5182N/A runningFinalize.set(Boolean.FALSE);
5182N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A private native void close0() throws IOException;
0N/A
0N/A private static native void initIDs();
0N/A
0N/A static {
0N/A initIDs();
0N/A }
0N/A
0N/A}