1 | // FileOutputStream.java - Write bytes to a file.
|
---|
2 |
|
---|
3 | /* Copyright (C) 1998, 1999, 2001 Free Software Foundation
|
---|
4 |
|
---|
5 | This file is part of libgcj.
|
---|
6 |
|
---|
7 | This software is copyrighted work licensed under the terms of the
|
---|
8 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
---|
9 | details. */
|
---|
10 |
|
---|
11 | package java.io;
|
---|
12 |
|
---|
13 | import java.nio.channels.FileChannel;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * @author Tom Tromey <tromey@cygnus.com>
|
---|
17 | * @date September 24, 1998
|
---|
18 | */
|
---|
19 |
|
---|
20 | /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
|
---|
21 | * "The Java Language Specification", ISBN 0-201-63451-1
|
---|
22 | * Status: Complete to version 1.1.
|
---|
23 | */
|
---|
24 |
|
---|
25 | public class FileOutputStream extends OutputStream
|
---|
26 | {
|
---|
27 | public FileOutputStream (String path, boolean append)
|
---|
28 | throws SecurityException, FileNotFoundException
|
---|
29 | {
|
---|
30 | SecurityManager s = System.getSecurityManager();
|
---|
31 | if (s != null)
|
---|
32 | s.checkWrite(path);
|
---|
33 | fd = new FileDescriptor (path, (append
|
---|
34 | ? FileDescriptor.APPEND
|
---|
35 | : FileDescriptor.WRITE));
|
---|
36 | }
|
---|
37 |
|
---|
38 | public FileOutputStream (String path)
|
---|
39 | throws SecurityException, FileNotFoundException
|
---|
40 | {
|
---|
41 | this (path, false);
|
---|
42 | }
|
---|
43 |
|
---|
44 | public FileOutputStream (File file)
|
---|
45 | throws SecurityException, FileNotFoundException
|
---|
46 | {
|
---|
47 | this (file.getPath(), false);
|
---|
48 | }
|
---|
49 |
|
---|
50 | public FileOutputStream (FileDescriptor fdObj)
|
---|
51 | throws SecurityException
|
---|
52 | {
|
---|
53 | SecurityManager s = System.getSecurityManager();
|
---|
54 | if (s != null)
|
---|
55 | s.checkWrite(fdObj);
|
---|
56 | fd = fdObj;
|
---|
57 | }
|
---|
58 |
|
---|
59 | protected void finalize () throws IOException
|
---|
60 | {
|
---|
61 | // We don't actually need this, but we include it because it is
|
---|
62 | // mentioned in the JCL.
|
---|
63 | }
|
---|
64 |
|
---|
65 | public final FileDescriptor getFD () throws IOException
|
---|
66 | {
|
---|
67 | if (! fd.valid())
|
---|
68 | throw new IOException ();
|
---|
69 | return fd;
|
---|
70 | }
|
---|
71 |
|
---|
72 | public void write (int b) throws IOException
|
---|
73 | {
|
---|
74 | fd.write (b);
|
---|
75 | }
|
---|
76 |
|
---|
77 | public void write (byte[] b) throws IOException, NullPointerException
|
---|
78 | {
|
---|
79 | fd.write (b, 0, b.length);
|
---|
80 | }
|
---|
81 |
|
---|
82 | public void write (byte[] b, int off, int len)
|
---|
83 | throws IOException, NullPointerException, IndexOutOfBoundsException
|
---|
84 | {
|
---|
85 | if (off < 0 || len < 0 || off + len > b.length)
|
---|
86 | throw new ArrayIndexOutOfBoundsException ();
|
---|
87 | fd.write (b, off, len);
|
---|
88 | }
|
---|
89 |
|
---|
90 | public void close () throws IOException
|
---|
91 | {
|
---|
92 | if (fd.valid())
|
---|
93 | fd.close();
|
---|
94 | }
|
---|
95 |
|
---|
96 | // Instance variables.
|
---|
97 | private FileDescriptor fd;
|
---|
98 |
|
---|
99 | public FileChannel getChannel ()
|
---|
100 | {
|
---|
101 | return null;
|
---|
102 | }
|
---|
103 | }
|
---|