50 |
* This classes allows a stream of data to be written to a disk file or |
* This classes allows a stream of data to be written to a disk file or |
51 |
* any open <code>FileDescriptor</code>. |
* any open <code>FileDescriptor</code>. |
52 |
* |
* |
53 |
* @author Aaron M. Renn (arenn@urbanophile.com) |
* @author Aaron M. Renn <arenn@urbanophile.com> |
54 |
|
* @author Tom Tromey <tromey@cygnus.com> |
55 |
*/ |
*/ |
56 |
public class FileOutputStream extends OutputStream |
public class FileOutputStream extends OutputStream |
57 |
{ |
{ |
78 |
* @exception SecurityException If write access to the file is not allowed |
* @exception SecurityException If write access to the file is not allowed |
79 |
* @exception FileNotFoundException If a non-security error occurs |
* @exception FileNotFoundException If a non-security error occurs |
80 |
*/ |
*/ |
81 |
public FileOutputStream (String name, boolean append) |
public FileOutputStream (String path, boolean append) |
82 |
throws SecurityException, FileNotFoundException |
throws SecurityException, FileNotFoundException |
83 |
{ |
{ |
84 |
SecurityManager sm = System.getSecurityManager(); |
SecurityManager s = System.getSecurityManager(); |
85 |
if (sm != null) |
if (s != null) |
86 |
sm.checkWrite(name); |
s.checkWrite(path); |
87 |
|
|
88 |
fd = new FileDescriptor(); |
fd = new FileDescriptor(); |
89 |
|
|
90 |
try |
try |
91 |
{ |
{ |
92 |
if (append) |
if (append) |
93 |
fd.open(name, "a"); |
fd.open(path, "a"); |
94 |
else |
else |
95 |
fd.open(name, "w"); |
fd.open(path, "w"); |
96 |
} |
} |
97 |
catch(IOException e) |
catch(IOException e) |
98 |
{ |
{ |
99 |
throw new FileNotFoundException(name + ": " + e.getMessage()); |
throw new FileNotFoundException(path + ": " + e.getMessage()); |
100 |
} |
} |
101 |
} |
} |
102 |
|
|
115 |
* @exception SecurityException If write access to the file is not allowed |
* @exception SecurityException If write access to the file is not allowed |
116 |
* @exception FileNotFoundException If a non-security error occurs |
* @exception FileNotFoundException If a non-security error occurs |
117 |
*/ |
*/ |
118 |
public |
public FileOutputStream (String path) |
119 |
FileOutputStream(String name) throws SecurityException, FileNotFoundException |
throws SecurityException, FileNotFoundException |
120 |
{ |
{ |
121 |
this (name, false); |
this (path, false); |
122 |
} |
} |
123 |
|
|
124 |
/** |
/** |
185 |
* |
* |
186 |
* @exception SecurityException If write access to the file is not allowed |
* @exception SecurityException If write access to the file is not allowed |
187 |
*/ |
*/ |
188 |
public FileOutputStream (FileDescriptor fd) throws SecurityException |
public FileOutputStream (FileDescriptor fdObj) |
189 |
|
throws SecurityException |
190 |
{ |
{ |
191 |
// Hmm, no other exception but this one to throw, but if the descriptor |
// Hmm, no other exception but this one to throw, but if the descriptor |
192 |
// isn't valid, we surely don't have "permission" to write to it. |
// isn't valid, we surely don't have "permission" to write to it. |
193 |
if (!fd.valid()) |
if (!fdObj.valid()) |
194 |
throw new SecurityException("Invalid FileDescriptor"); |
throw new SecurityException("Invalid FileDescriptor"); |
195 |
|
|
196 |
SecurityManager sm = System.getSecurityManager(); |
SecurityManager s = System.getSecurityManager(); |
197 |
if (sm != null) |
if (s != null) |
198 |
sm.checkWrite(fd); |
s.checkWrite(fdObj); |
199 |
|
|
200 |
|
fd = fdObj; |
201 |
|
} |
202 |
|
|
203 |
this.fd = fd; |
protected void finalize () throws IOException |
204 |
|
{ |
205 |
|
// We don't actually need this, but we include it because it is |
206 |
|
// mentioned in the JCL. |
207 |
} |
} |
208 |
|
|
209 |
/** |
/** |
241 |
* |
* |
242 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
243 |
*/ |
*/ |
244 |
public void write(byte[] buf) throws IOException |
public void write (byte[] buf) |
245 |
|
throws IOException |
246 |
{ |
{ |
247 |
write (buf, 0, buf.length); |
write (buf, 0, buf.length); |
248 |
} |
} |
257 |
* |
* |
258 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
259 |
*/ |
*/ |
260 |
public void write(byte[] buf, int offset, int len) throws IOException |
public void write (byte[] buf, int offset, int len) |
261 |
|
throws IOException |
262 |
{ |
{ |
263 |
|
if (offset < 0 |
264 |
|
|| len < 0 |
265 |
|
|| offset + len > buf.length) |
266 |
|
throw new ArrayIndexOutOfBoundsException (); |
267 |
|
|
268 |
fd.write (buf, offset, len); |
fd.write (buf, offset, len); |
269 |
} |
} |
270 |
|
|