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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * 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 * @see java.nio.file.Files#newOutputStream
50 * @since JDK1.0
51 */
52 public
53 class FileOutputStream extends OutputStream
54 {
55 /**
56 * The system dependent file descriptor.
57 */
58 private final FileDescriptor fd;
59
60 /**
61 * True if the file is opened for append.
62 */
63 private final boolean append;
64
65 /**
66 * The associated channel, initialized lazily.
67 */
68 private FileChannel channel;
69
70 private final Object closeLock = new Object();
71 private volatile boolean closed = false;
72
73 /**
74 * Creates a file output stream to write to the file with the
75 * specified name. A new <code>FileDescriptor</code> object is
76 * created to represent this file connection.
77 * <p>
78 * First, if there is a security manager, its <code>checkWrite</code>
79 * method is called with <code>name</code> as its argument.
80 * <p>
182 * to the file.
183 * @see java.io.File#getPath()
184 * @see java.lang.SecurityException
185 * @see java.lang.SecurityManager#checkWrite(java.lang.String)
186 * @since 1.4
187 */
188 public FileOutputStream(File file, boolean append)
189 throws FileNotFoundException
190 {
191 String name = (file != null ? file.getPath() : null);
192 SecurityManager security = System.getSecurityManager();
193 if (security != null) {
194 security.checkWrite(name);
195 }
196 if (name == null) {
197 throw new NullPointerException();
198 }
199 this.fd = new FileDescriptor();
200 fd.attach(this);
201 this.append = append;
202
203 open(name, append);
204 }
205
206 /**
207 * Creates a file output stream to write to the specified file
208 * descriptor, which represents an existing connection to an actual
209 * file in the file system.
210 * <p>
211 * First, if there is a security manager, its <code>checkWrite</code>
212 * method is called with the file descriptor <code>fdObj</code>
213 * argument as its argument.
214 * <p>
215 * If <code>fdObj</code> is null then a <code>NullPointerException</code>
216 * is thrown.
217 * <p>
218 * This constructor does not throw an exception if <code>fdObj</code>
219 * is {@link java.io.FileDescriptor#valid() invalid}.
220 * However, if the methods are invoked on the resulting stream to attempt
221 * I/O on the stream, an <code>IOException</code> is thrown.
222 *
223 * @param fdObj the file descriptor to be opened for writing
224 * @exception SecurityException if a security manager exists and its
225 * <code>checkWrite</code> method denies
226 * write access to the file descriptor
227 * @see java.lang.SecurityManager#checkWrite(java.io.FileDescriptor)
228 */
229 public FileOutputStream(FileDescriptor fdObj) {
230 SecurityManager security = System.getSecurityManager();
231 if (fdObj == null) {
232 throw new NullPointerException();
233 }
234 if (security != null) {
235 security.checkWrite(fdObj);
236 }
237 this.fd = fdObj;
238 this.append = false;
239
240 fd.attach(this);
241 }
242
243 /**
244 * Opens a file, with the specified name, for overwriting or appending.
245 * @param name name of file to be opened
246 * @param append whether the file is to be opened in append mode
247 */
248 private native void open(String name, boolean append)
249 throws FileNotFoundException;
250
251 /**
252 * Writes the specified byte to this file output stream.
253 *
254 * @param b the byte to be written.
255 * @param append {@code true} if the write operation first
256 * advances the position to the end of file
257 */
258 private native void write(int b, boolean append) throws IOException;
259
260 /**
261 * Writes the specified byte to this file output stream. Implements
262 * the <code>write</code> method of <code>OutputStream</code>.
263 *
264 * @param b the byte to be written.
265 * @exception IOException if an I/O error occurs.
266 */
267 public void write(int b) throws IOException {
268 write(b, append);
269 }
270
271 /**
272 * Writes a sub array as a sequence of bytes.
273 * @param b the data to be written
274 * @param off the start offset in the data
275 * @param len the number of bytes that are written
276 * @param append {@code true} to first advance the position to the
277 * end of file
278 * @exception IOException If an I/O error has occurred.
279 */
280 private native void writeBytes(byte b[], int off, int len, boolean append)
281 throws IOException;
282
283 /**
284 * Writes <code>b.length</code> bytes from the specified byte array
285 * to this file output stream.
286 *
287 * @param b the data.
288 * @exception IOException if an I/O error occurs.
289 */
290 public void write(byte b[]) throws IOException {
291 writeBytes(b, 0, b.length, append);
292 }
293
294 /**
295 * Writes <code>len</code> bytes from the specified byte array
296 * starting at offset <code>off</code> to this file output stream.
297 *
298 * @param b the data.
299 * @param off the start offset in the data.
300 * @param len the number of bytes to write.
301 * @exception IOException if an I/O error occurs.
302 */
303 public void write(byte b[], int off, int len) throws IOException {
304 writeBytes(b, off, len, append);
305 }
306
307 /**
308 * Closes this file output stream and releases any system resources
309 * associated with this stream. This file output stream may no longer
310 * be used for writing bytes.
311 *
312 * <p> If this stream has an associated channel then the channel is closed
313 * as well.
314 *
315 * @exception IOException if an I/O error occurs.
316 *
317 * @revised 1.4
318 * @spec JSR-51
319 */
320 public void close() throws IOException {
321 synchronized (closeLock) {
322 if (closed) {
323 return;
324 }
356 /**
357 * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
358 * object associated with this file output stream. </p>
359 *
360 * <p> The initial {@link java.nio.channels.FileChannel#position()
361 * </code>position<code>} of the returned channel will be equal to the
362 * number of bytes written to the file so far unless this stream is in
363 * append mode, in which case it will be equal to the size of the file.
364 * Writing bytes to this stream will increment the channel's position
365 * accordingly. Changing the channel's position, either explicitly or by
366 * writing, will change this stream's file position.
367 *
368 * @return the file channel associated with this file output stream
369 *
370 * @since 1.4
371 * @spec JSR-51
372 */
373 public FileChannel getChannel() {
374 synchronized (this) {
375 if (channel == null) {
376 channel = FileChannelImpl.open(fd, false, true, append, this);
377 }
378 return channel;
379 }
380 }
381
382 /**
383 * Cleans up the connection to the file, and ensures that the
384 * <code>close</code> method of this file output stream is
385 * called when there are no more references to this stream.
386 *
387 * @exception IOException if an I/O error occurs.
388 * @see java.io.FileInputStream#close()
389 */
390 protected void finalize() throws IOException {
391 if (fd != null) {
392 if (fd == FileDescriptor.out || fd == FileDescriptor.err) {
393 flush();
394 } else {
395 /* if fd is shared, the references in FileDescriptor
396 * will ensure that finalizer is only called when
|
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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.io;
27
28 import java.nio.channels.FileChannel;
29 import sun.nio.ch.FileChannelImpl;
30 import sun.misc.IoTrace;
31 import sun.misc.IoTraceContext;
32
33
34 /**
35 * A file output stream is an output stream for writing data to a
36 * <code>File</code> or to a <code>FileDescriptor</code>. Whether or not
37 * a file is available or may be created depends upon the underlying
38 * platform. Some platforms, in particular, allow a file to be opened
39 * for writing by only one <tt>FileOutputStream</tt> (or other
40 * file-writing object) at a time. In such situations the constructors in
41 * this class will fail if the file involved is already open.
42 *
43 * <p><code>FileOutputStream</code> is meant for writing streams of raw bytes
44 * such as image data. For writing streams of characters, consider using
45 * <code>FileWriter</code>.
46 *
47 * @author Arthur van Hoff
48 * @see java.io.File
49 * @see java.io.FileDescriptor
50 * @see java.io.FileInputStream
51 * @see java.nio.file.Files#newOutputStream
52 * @since JDK1.0
53 */
54 public
55 class FileOutputStream extends OutputStream
56 {
57 /**
58 * The system dependent file descriptor.
59 */
60 private final FileDescriptor fd;
61
62 /**
63 * The path of the referenced file (null if the stream is created with a file descriptor)
64 */
65 private final String path;
66
67 /**
68 * True if the file is opened for append.
69 */
70 private final boolean append;
71
72 /**
73 * The associated channel, initialized lazily.
74 */
75 private FileChannel channel;
76
77 private final Object closeLock = new Object();
78 private volatile boolean closed = false;
79
80 /**
81 * Creates a file output stream to write to the file with the
82 * specified name. A new <code>FileDescriptor</code> object is
83 * created to represent this file connection.
84 * <p>
85 * First, if there is a security manager, its <code>checkWrite</code>
86 * method is called with <code>name</code> as its argument.
87 * <p>
189 * to the file.
190 * @see java.io.File#getPath()
191 * @see java.lang.SecurityException
192 * @see java.lang.SecurityManager#checkWrite(java.lang.String)
193 * @since 1.4
194 */
195 public FileOutputStream(File file, boolean append)
196 throws FileNotFoundException
197 {
198 String name = (file != null ? file.getPath() : null);
199 SecurityManager security = System.getSecurityManager();
200 if (security != null) {
201 security.checkWrite(name);
202 }
203 if (name == null) {
204 throw new NullPointerException();
205 }
206 this.fd = new FileDescriptor();
207 fd.attach(this);
208 this.append = append;
209 this.path = name;
210 open(name, append);
211 }
212
213 /**
214 * Creates a file output 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 this.fd = fdObj;
245 this.path = null;
246 this.append = false;
247
248 fd.attach(this);
249 }
250
251 /**
252 * Opens a file, with the specified name, for overwriting or appending.
253 * @param name name of file to be opened
254 * @param append whether the file is to be opened in append mode
255 */
256 private native void open(String name, boolean append)
257 throws FileNotFoundException;
258
259 /**
260 * Writes the specified byte to this file output stream.
261 *
262 * @param b the byte to be written.
263 * @param append {@code true} if the write operation first
264 * advances the position to the end of file
265 */
266 private native void write(int b, boolean append) throws IOException;
267
268 /**
269 * Writes the specified byte to this file output stream. Implements
270 * the <code>write</code> method of <code>OutputStream</code>.
271 *
272 * @param b the byte to be written.
273 * @exception IOException if an I/O error occurs.
274 */
275 public void write(int b) throws IOException {
276 IoTraceContext traceContext = IoTrace.fileWriteBegin(path);
277 int bytesWritten = 0;
278 try {
279 write(b, append);
280 bytesWritten = 1;
281 } finally {
282 IoTrace.fileWriteEnd(traceContext, bytesWritten);
283 }
284 }
285
286 /**
287 * Writes a sub array as a sequence of bytes.
288 * @param b the data to be written
289 * @param off the start offset in the data
290 * @param len the number of bytes that are written
291 * @param append {@code true} to first advance the position to the
292 * end of file
293 * @exception IOException If an I/O error has occurred.
294 */
295 private native void writeBytes(byte b[], int off, int len, boolean append)
296 throws IOException;
297
298 /**
299 * Writes <code>b.length</code> bytes from the specified byte array
300 * to this file output stream.
301 *
302 * @param b the data.
303 * @exception IOException if an I/O error occurs.
304 */
305 public void write(byte b[]) throws IOException {
306 IoTraceContext traceContext = IoTrace.fileWriteBegin(path);
307 int bytesWritten = 0;
308 try {
309 writeBytes(b, 0, b.length, append);
310 bytesWritten = b.length;
311 } finally {
312 IoTrace.fileWriteEnd(traceContext, bytesWritten);
313 }
314 }
315
316 /**
317 * Writes <code>len</code> bytes from the specified byte array
318 * starting at offset <code>off</code> to this file output stream.
319 *
320 * @param b the data.
321 * @param off the start offset in the data.
322 * @param len the number of bytes to write.
323 * @exception IOException if an I/O error occurs.
324 */
325 public void write(byte b[], int off, int len) throws IOException {
326 IoTraceContext traceContext = IoTrace.fileWriteBegin(path);
327 int bytesWritten = 0;
328 try {
329 writeBytes(b, off, len, append);
330 bytesWritten = len;
331 } finally {
332 IoTrace.fileWriteEnd(traceContext, bytesWritten);
333 }
334 }
335
336 /**
337 * Closes this file output stream and releases any system resources
338 * associated with this stream. This file output stream may no longer
339 * be used for writing bytes.
340 *
341 * <p> If this stream has an associated channel then the channel is closed
342 * as well.
343 *
344 * @exception IOException if an I/O error occurs.
345 *
346 * @revised 1.4
347 * @spec JSR-51
348 */
349 public void close() throws IOException {
350 synchronized (closeLock) {
351 if (closed) {
352 return;
353 }
385 /**
386 * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
387 * object associated with this file output stream. </p>
388 *
389 * <p> The initial {@link java.nio.channels.FileChannel#position()
390 * </code>position<code>} of the returned channel will be equal to the
391 * number of bytes written to the file so far unless this stream is in
392 * append mode, in which case it will be equal to the size of the file.
393 * Writing bytes to this stream will increment the channel's position
394 * accordingly. Changing the channel's position, either explicitly or by
395 * writing, will change this stream's file position.
396 *
397 * @return the file channel associated with this file output stream
398 *
399 * @since 1.4
400 * @spec JSR-51
401 */
402 public FileChannel getChannel() {
403 synchronized (this) {
404 if (channel == null) {
405 channel = FileChannelImpl.open(fd, path, false, true, append, this);
406 }
407 return channel;
408 }
409 }
410
411 /**
412 * Cleans up the connection to the file, and ensures that the
413 * <code>close</code> method of this file output stream is
414 * called when there are no more references to this stream.
415 *
416 * @exception IOException if an I/O error occurs.
417 * @see java.io.FileInputStream#close()
418 */
419 protected void finalize() throws IOException {
420 if (fd != null) {
421 if (fd == FileDescriptor.out || fd == FileDescriptor.err) {
422 flush();
423 } else {
424 /* if fd is shared, the references in FileDescriptor
425 * will ensure that finalizer is only called when
|