FileOutputStreampublic class FileOutputStream extends OutputStream implements CloseableA specialized {@link OutputStream} that writes to a file in the file system.
All write requests made by calling methods in this class are directly
forwarded to the equivalent function of the underlying operating system.
Since this may induce some performance penalty, in particular if many small
write requests are made, a FileOutputStream is often wrapped by a
BufferedOutputStream. |
Fields Summary |
---|
FileDescriptor | fdThe FileDescriptor representing this FileOutputStream. | boolean | innerFD | private FileChannel | channel | private org.apache.harmony.luni.platform.IFileSystem | fileSystem |
Constructors Summary |
---|
public FileOutputStream(File file)Constructs a new FileOutputStream on the File {@code file}. If the file
exists, it is overwritten.
this(file, false);
| public FileOutputStream(File file, boolean append)Constructs a new FileOutputStream on the File {@code file}. The
parameter {@code append} determines whether or not the file is opened and
appended to or just opened and overwritten.
super();
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(file.getPath());
}
fd = new FileDescriptor();
fd.descriptor = fileSystem.open(file.properPath(true),
append ? IFileSystem.O_APPEND : IFileSystem.O_WRONLY);
innerFD = true;
channel = FileChannelFactory.getFileChannel(this, fd.descriptor,
append ? IFileSystem.O_APPEND : IFileSystem.O_WRONLY);
| public FileOutputStream(FileDescriptor fd)Constructs a new FileOutputStream on the FileDescriptor {@code fd}. The
file must already be open, therefore no {@code FileNotFoundException}
will be thrown.
super();
if (fd == null) {
throw new NullPointerException(Msg.getString("K006c")); //$NON-NLS-1$
}
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(fd);
}
this.fd = fd;
innerFD = false;
channel = FileChannelFactory.getFileChannel(this, fd.descriptor,
IFileSystem.O_WRONLY);
| public FileOutputStream(String filename)Constructs a new FileOutputStream on the file named {@code filename}. If
the file exists, it is overwritten. The {@code filename} may be absolute
or relative to the system property {@code "user.dir"}.
this(filename, false);
| public FileOutputStream(String filename, boolean append)Constructs a new FileOutputStream on the file named {@code filename}.
The parameter {@code append} determines whether or not the file is opened
and appended to or just opened and overwritten. The {@code filename} may
be absolute or relative to the system property {@code "user.dir"}.
this(new File(filename), append);
|
Methods Summary |
---|
public void | close()Closes this stream. This implementation closes the underlying operating
system resources allocated to represent this stream.
if (fd == null) {
// if fd is null, then the underlying file is not opened, so nothing
// to close
return;
}
if (channel != null) {
synchronized (channel) {
if (channel.isOpen() && fd.descriptor >= 0) {
channel.close();
}
}
}
synchronized (this) {
if (fd.descriptor >= 0 && innerFD) {
fileSystem.close(fd.descriptor);
fd.descriptor = -1;
}
}
| protected void | finalize()Frees any resources allocated for this stream before it is garbage
collected. This method is called from the Java Virtual Machine.
close();
| public java.nio.channels.FileChannel | getChannel()Returns the FileChannel equivalent to this output stream.
The file channel is write-only and has an initial position within the
file that is the same as the current position of this stream within the
file. All changes made to the underlying file descriptor state via the
channel are visible by the output stream and vice versa.
return channel;
| public final java.io.FileDescriptor | getFD()Returns a FileDescriptor which represents the lowest level representation
of an operating system stream resource.
return fd;
| private synchronized void | openCheck()
if (fd.descriptor < 0) {
throw new IOException();
}
| public void | write(byte[] buffer)Writes the entire contents of the byte array {@code buffer} to this
stream.
write(buffer, 0, buffer.length);
| public void | write(byte[] buffer, int offset, int count)Writes {@code count} bytes from the byte array {@code buffer} starting at
{@code offset} to this stream.
// BEGIN android-changed
// Exception priorities (in case of multiple errors) differ from
// RI, but are spec-compliant.
// removed redundant check, made implicit null check explicit,
// used (offset | count) < 0 instead of (offset < 0) || (count < 0)
// to safe one operation
if (buffer == null) {
throw new NullPointerException(Msg.getString("K0047")); //$NON-NLS-1$
}
if ((count | offset) < 0 || count > buffer.length - offset) {
throw new IndexOutOfBoundsException(Msg.getString("K002f")); //$NON-NLS-1$
}
// END android-changed
if (count == 0) {
return;
}
openCheck();
fileSystem.write(fd.descriptor, buffer, offset, count);
| public void | write(int oneByte)Writes the specified byte {@code oneByte} to this stream. Only the low
order byte of the integer {@code oneByte} is written.
openCheck();
byte[] byteArray = new byte[1];
byteArray[0] = (byte) oneByte;
fileSystem.write(fd.descriptor, byteArray, 0, 1);
|
|