001: /*
002: * @(#)FDOutputStream.java 1.37 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.io;
029:
030: import java.io.*;
031:
032: /**
033: * A file output stream is an output stream for writing data to a
034: * <code>File</code> or to a <code>FileDescriptor</code>.
035: *
036: * @author Arthur van Hoff
037: * @version 1.33, 08/19/02
038: * @see java.io.File
039: * @see java.io.FileDescriptor
040: * @see java.io.FileInputStream
041: * @since JDK1.0
042: */
043: public class FDOutputStream extends OutputStream {
044: static {
045: java.security.AccessController
046: .doPrivileged(new sun.security.action.LoadLibraryAction(
047: "javafile"));
048: }
049: /**
050: * The system dependent file descriptor. The value is
051: * 1 more than actual file descriptor. This means that
052: * the default value 0 indicates that the file is not open.
053: */
054: private FileDescriptor fd;
055:
056: /**
057: * Creates an output file stream to write to the file with the
058: * specified name.
059: *
060: * @param name the system-dependent filename.
061: * @exception IOException if the file could not be opened for writing.
062: * @exception SecurityException if a security manager exists, its
063: * <code>checkWrite</code> method is called with the name
064: * argument to see if the application is allowed write access
065: * to the file.
066: * @see java.lang.SecurityManager#checkWrite(java.lang.String)
067: * @since JDK1.0
068: */
069: public FDOutputStream(String name) throws IOException {
070: SecurityManager security = System.getSecurityManager();
071: if (security != null) {
072: security.checkWrite(name);
073: }
074: try {
075: fd = new FileDescriptor();
076: open(name);
077: } catch (IOException e) {
078: throw new IOException(name);
079: }
080: }
081:
082: /**
083: * Creates an output file with the specified system dependent
084: * file name.
085: * @param name the system dependent file name
086: * @exception IOException If the file is not found.
087: * @since JDK1.1
088: */
089: public FDOutputStream(String name, boolean append)
090: throws IOException {
091: SecurityManager security = System.getSecurityManager();
092: if (security != null) {
093: security.checkWrite(name);
094: }
095: try {
096: fd = new FileDescriptor();
097: if (append)
098: openAppend(name);
099: else
100: open(name);
101: } catch (IOException e) {
102: throw new IOException(name);
103: }
104: }
105:
106: /**
107: * Creates an output file stream to write to the specified file descriptor.
108: *
109: * @param fdObj the file descriptor to be opened for writing.
110: * @exception SecurityException if a security manager exists, its
111: * <code>checkWrite</code> method is called with the file
112: * descriptor to see if the application is allowed to write
113: * to the specified file descriptor.
114: * @see java.lang.SecurityManager#checkWrite(java.io.FileDescriptor)
115: * @since JDK1.0
116: */
117: public FDOutputStream(FileDescriptor fdObj) {
118: SecurityManager security = System.getSecurityManager();
119: if (fdObj == null) {
120: throw new NullPointerException();
121: }
122: if (security != null) {
123: security.checkWrite(fdObj);
124: }
125: fd = fdObj;
126: }
127:
128: /**
129: * Opens a file, with the specified name, for writing.
130: * @param name name of file to be opened
131: */
132: public native void open(String name) throws IOException;
133:
134: /**
135: * Opens a file, with the specified name, for appending.
136: * @param name name of file to be opened
137: */
138: public native void openAppend(String name) throws IOException;
139:
140: /**
141: * Writes the specified byte to this file output stream.
142: *
143: * @param b the byte to be written.
144: * @exception IOException if an I/O error occurs.
145: * @since JDK1.0
146: */
147: public native void write(int b) throws IOException;
148:
149: /**
150: * Writes a sub array as a sequence of bytes.
151: * @param b the data to be written
152: * @param off the start offset in the data
153: * @param len the number of bytes that are written
154: * @exception IOException If an I/O error has occurred.
155: */
156: public native void writeBytes(byte b[], int off, int len)
157: throws IOException;
158:
159: /**
160: * Writes <code>b.length</code> bytes from the specified byte array
161: * to this file output stream.
162: *
163: * @param b the data.
164: * @exception IOException if an I/O error occurs.
165: * @since JDK1.0
166: */
167: public void write(byte b[]) throws IOException {
168: writeBytes(b, 0, b.length);
169: }
170:
171: /**
172: * Writes <code>len</code> bytes from the specified byte array
173: * starting at offset <code>off</code> to this file output stream.
174: *
175: * @param b the data.
176: * @param off the start offset in the data.
177: * @param len the number of bytes to write.
178: * @exception IOException if an I/O error occurs.
179: * @since JDK1.0
180: */
181: public void write(byte b[], int off, int len) throws IOException {
182: writeBytes(b, off, len);
183: }
184:
185: /**
186: * Closes this file output stream and releases any system resources
187: * associated with this stream.
188: *
189: * @exception IOException if an I/O error occurs.
190: * @since JDK1.0
191: */
192: public native void close() throws IOException;
193:
194: /**
195: * Returns the file descriptor associated with this stream.
196: *
197: * @return the file descriptor object associated with this stream.
198: * @exception IOException if an I/O error occurs.
199: * @see java.io.FileDescriptor
200: * @since JDK1.0
201: */
202: public final FileDescriptor getFD() throws IOException {
203: if (fd != null)
204: return fd;
205: throw new IOException();
206: }
207:
208: /**
209: * Ensures that the <code>close</code> method of this file output stream is
210: * called when there are no more references to this stream.
211: *
212: * @exception IOException if an I/O error occurs.
213: * @see java.io.FileInputStream#close()
214: * @since JDK1.0
215: */
216: protected void finalize() throws IOException {
217: if (fd != null) {
218: if (fd == fd.out || fd == fd.err) {
219: flush();
220: } else {
221: close();
222: }
223: }
224: }
225: }
|