author | martin |
Mon, 10 Mar 2008 14:32:51 -0700 | |
changeset 46 | ddf5deb2a633 |
parent 2 | 90ce3da70b43 |
child 47 | c8f0e41aea68 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* Copyright 1994-2007 Sun Microsystems, Inc. All Rights Reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any 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 |
* @since JDK1.0 |
|
50 |
*/ |
|
51 |
public |
|
52 |
class FileOutputStream extends OutputStream |
|
53 |
{ |
|
54 |
/** |
|
46
ddf5deb2a633
6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents:
2
diff
changeset
|
55 |
* The system dependent file descriptor. |
2 | 56 |
*/ |
46
ddf5deb2a633
6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents:
2
diff
changeset
|
57 |
private final FileDescriptor fd; |
2 | 58 |
|
59 |
private FileChannel channel= null; |
|
60 |
||
61 |
private boolean append = false; |
|
62 |
||
46
ddf5deb2a633
6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents:
2
diff
changeset
|
63 |
private final Object closeLock = new Object(); |
2 | 64 |
private volatile boolean closed = false; |
46
ddf5deb2a633
6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents:
2
diff
changeset
|
65 |
private static final ThreadLocal<Boolean> runningFinalize = |
ddf5deb2a633
6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents:
2
diff
changeset
|
66 |
new ThreadLocal<Boolean>(); |
2 | 67 |
|
68 |
private static boolean isRunningFinalize() { |
|
69 |
Boolean val; |
|
70 |
if ((val = runningFinalize.get()) != null) |
|
71 |
return val.booleanValue(); |
|
72 |
return false; |
|
73 |
} |
|
74 |
||
75 |
/** |
|
46
ddf5deb2a633
6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents:
2
diff
changeset
|
76 |
* Creates a file output stream to write to the file with the |
2 | 77 |
* specified name. A new <code>FileDescriptor</code> object is |
78 |
* created to represent this file connection. |
|
79 |
* <p> |
|
80 |
* First, if there is a security manager, its <code>checkWrite</code> |
|
81 |
* method is called with <code>name</code> as its argument. |
|
82 |
* <p> |
|
83 |
* If the file exists but is a directory rather than a regular file, does |
|
84 |
* not exist but cannot be created, or cannot be opened for any other |
|
85 |
* reason then a <code>FileNotFoundException</code> is thrown. |
|
86 |
* |
|
87 |
* @param name the system-dependent filename |
|
88 |
* @exception FileNotFoundException if the file exists but is a directory |
|
89 |
* rather than a regular file, does not exist but cannot |
|
90 |
* be created, or cannot be opened for any other reason |
|
91 |
* @exception SecurityException if a security manager exists and its |
|
92 |
* <code>checkWrite</code> method denies write access |
|
93 |
* to the file. |
|
94 |
* @see java.lang.SecurityManager#checkWrite(java.lang.String) |
|
95 |
*/ |
|
96 |
public FileOutputStream(String name) throws FileNotFoundException { |
|
97 |
this(name != null ? new File(name) : null, false); |
|
98 |
} |
|
99 |
||
100 |
/** |
|
46
ddf5deb2a633
6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents:
2
diff
changeset
|
101 |
* Creates a file output stream to write to the file with the specified |
ddf5deb2a633
6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents:
2
diff
changeset
|
102 |
* name. If the second argument is <code>true</code>, then |
2 | 103 |
* bytes will be written to the end of the file rather than the beginning. |
104 |
* A new <code>FileDescriptor</code> object is created to represent this |
|
105 |
* file connection. |
|
106 |
* <p> |
|
107 |
* First, if there is a security manager, its <code>checkWrite</code> |
|
108 |
* method is called with <code>name</code> as its argument. |
|
109 |
* <p> |
|
110 |
* If the file exists but is a directory rather than a regular file, does |
|
111 |
* not exist but cannot be created, or cannot be opened for any other |
|
112 |
* reason then a <code>FileNotFoundException</code> is thrown. |
|
113 |
* |
|
114 |
* @param name the system-dependent file name |
|
115 |
* @param append if <code>true</code>, then bytes will be written |
|
116 |
* to the end of the file rather than the beginning |
|
117 |
* @exception FileNotFoundException if the file exists but is a directory |
|
118 |
* rather than a regular file, does not exist but cannot |
|
119 |
* be created, or cannot be opened for any other reason. |
|
120 |
* @exception SecurityException if a security manager exists and its |
|
121 |
* <code>checkWrite</code> method denies write access |
|
122 |
* to the file. |
|
123 |
* @see java.lang.SecurityManager#checkWrite(java.lang.String) |
|
124 |
* @since JDK1.1 |
|
125 |
*/ |
|
126 |
public FileOutputStream(String name, boolean append) |
|
127 |
throws FileNotFoundException |
|
128 |
{ |
|
129 |
this(name != null ? new File(name) : null, append); |
|
130 |
} |
|
131 |
||
132 |
/** |
|
133 |
* Creates a file output stream to write to the file represented by |
|
134 |
* the specified <code>File</code> object. A new |
|
135 |
* <code>FileDescriptor</code> object is created to represent this |
|
136 |
* file connection. |
|
137 |
* <p> |
|
138 |
* First, if there is a security manager, its <code>checkWrite</code> |
|
139 |
* method is called with the path represented by the <code>file</code> |
|
140 |
* argument as its argument. |
|
141 |
* <p> |
|
142 |
* If the file exists but is a directory rather than a regular file, does |
|
143 |
* not exist but cannot be created, or cannot be opened for any other |
|
144 |
* reason then a <code>FileNotFoundException</code> is thrown. |
|
145 |
* |
|
146 |
* @param file the file to be opened for writing. |
|
147 |
* @exception FileNotFoundException if the file exists but is a directory |
|
148 |
* rather than a regular file, does not exist but cannot |
|
149 |
* be created, or cannot be opened for any other reason |
|
150 |
* @exception SecurityException if a security manager exists and its |
|
151 |
* <code>checkWrite</code> method denies write access |
|
152 |
* to the file. |
|
153 |
* @see java.io.File#getPath() |
|
154 |
* @see java.lang.SecurityException |
|
155 |
* @see java.lang.SecurityManager#checkWrite(java.lang.String) |
|
156 |
*/ |
|
157 |
public FileOutputStream(File file) throws FileNotFoundException { |
|
158 |
this(file, false); |
|
159 |
} |
|
160 |
||
161 |
/** |
|
162 |
* Creates a file output stream to write to the file represented by |
|
163 |
* the specified <code>File</code> object. If the second argument is |
|
164 |
* <code>true</code>, then bytes will be written to the end of the file |
|
165 |
* rather than the beginning. A new <code>FileDescriptor</code> object is |
|
166 |
* created to represent this file connection. |
|
167 |
* <p> |
|
168 |
* First, if there is a security manager, its <code>checkWrite</code> |
|
169 |
* method is called with the path represented by the <code>file</code> |
|
170 |
* argument as its argument. |
|
171 |
* <p> |
|
172 |
* If the file exists but is a directory rather than a regular file, does |
|
173 |
* not exist but cannot be created, or cannot be opened for any other |
|
174 |
* reason then a <code>FileNotFoundException</code> is thrown. |
|
175 |
* |
|
176 |
* @param file the file to be opened for writing. |
|
177 |
* @param append if <code>true</code>, then bytes will be written |
|
178 |
* to the end of the file rather than the beginning |
|
179 |
* @exception FileNotFoundException if the file exists but is a directory |
|
180 |
* rather than a regular file, does not exist but cannot |
|
181 |
* be created, or cannot be opened for any other reason |
|
182 |
* @exception SecurityException if a security manager exists and its |
|
183 |
* <code>checkWrite</code> method denies write access |
|
184 |
* to the file. |
|
185 |
* @see java.io.File#getPath() |
|
186 |
* @see java.lang.SecurityException |
|
187 |
* @see java.lang.SecurityManager#checkWrite(java.lang.String) |
|
188 |
* @since 1.4 |
|
189 |
*/ |
|
190 |
public FileOutputStream(File file, boolean append) |
|
191 |
throws FileNotFoundException |
|
192 |
{ |
|
193 |
String name = (file != null ? file.getPath() : null); |
|
194 |
SecurityManager security = System.getSecurityManager(); |
|
195 |
if (security != null) { |
|
196 |
security.checkWrite(name); |
|
197 |
} |
|
198 |
if (name == null) { |
|
199 |
throw new NullPointerException(); |
|
200 |
} |
|
201 |
fd = new FileDescriptor(); |
|
202 |
fd.incrementAndGetUseCount(); |
|
203 |
this.append = append; |
|
204 |
if (append) { |
|
205 |
openAppend(name); |
|
206 |
} else { |
|
207 |
open(name); |
|
208 |
} |
|
209 |
} |
|
210 |
||
211 |
/** |
|
46
ddf5deb2a633
6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents:
2
diff
changeset
|
212 |
* Creates a file output stream to write to the specified file |
2 | 213 |
* descriptor, which represents an existing connection to an actual |
214 |
* file in the file system. |
|
215 |
* <p> |
|
216 |
* First, if there is a security manager, its <code>checkWrite</code> |
|
217 |
* method is called with the file descriptor <code>fdObj</code> |
|
218 |
* argument as its argument. |
|
219 |
* <p> |
|
220 |
* If <code>fdObj</code> is null then a <code>NullPointerException</code> |
|
221 |
* is thrown. |
|
222 |
* <p> |
|
223 |
* This constructor does not throw an exception if <code>fdObj</code> |
|
46
ddf5deb2a633
6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents:
2
diff
changeset
|
224 |
* is {@link java.io.FileDescriptor#valid() invalid}. |
2 | 225 |
* However, if the methods are invoked on the resulting stream to attempt |
226 |
* I/O on the stream, an <code>IOException</code> is thrown. |
|
227 |
* |
|
228 |
* @param fdObj the file descriptor to be opened for writing |
|
229 |
* @exception SecurityException if a security manager exists and its |
|
230 |
* <code>checkWrite</code> method denies |
|
231 |
* write access to the file descriptor |
|
232 |
* @see java.lang.SecurityManager#checkWrite(java.io.FileDescriptor) |
|
233 |
*/ |
|
234 |
public FileOutputStream(FileDescriptor fdObj) { |
|
235 |
SecurityManager security = System.getSecurityManager(); |
|
236 |
if (fdObj == null) { |
|
237 |
throw new NullPointerException(); |
|
238 |
} |
|
239 |
if (security != null) { |
|
240 |
security.checkWrite(fdObj); |
|
241 |
} |
|
242 |
fd = fdObj; |
|
243 |
||
244 |
/* |
|
245 |
* FileDescriptor is being shared by streams. |
|
246 |
* Ensure that it's GC'ed only when all the streams/channels are done |
|
247 |
* using it. |
|
248 |
*/ |
|
249 |
fd.incrementAndGetUseCount(); |
|
250 |
} |
|
251 |
||
252 |
/** |
|
253 |
* Opens a file, with the specified name, for writing. |
|
254 |
* @param name name of file to be opened |
|
255 |
*/ |
|
256 |
private native void open(String name) throws FileNotFoundException; |
|
257 |
||
258 |
/** |
|
259 |
* Opens a file, with the specified name, for appending. |
|
260 |
* @param name name of file to be opened |
|
261 |
*/ |
|
262 |
private native void openAppend(String name) throws FileNotFoundException; |
|
263 |
||
264 |
/** |
|
265 |
* Writes the specified byte to this file output stream. Implements |
|
266 |
* the <code>write</code> method of <code>OutputStream</code>. |
|
267 |
* |
|
268 |
* @param b the byte to be written. |
|
269 |
* @exception IOException if an I/O error occurs. |
|
270 |
*/ |
|
271 |
public native void write(int b) throws IOException; |
|
272 |
||
273 |
/** |
|
274 |
* Writes a sub array as a sequence of bytes. |
|
275 |
* @param b the data to be written |
|
276 |
* @param off the start offset in the data |
|
277 |
* @param len the number of bytes that are written |
|
278 |
* @exception IOException If an I/O error has occurred. |
|
279 |
*/ |
|
280 |
private native void writeBytes(byte b[], int off, int len) throws IOException; |
|
281 |
||
282 |
/** |
|
283 |
* Writes <code>b.length</code> bytes from the specified byte array |
|
284 |
* to this file output stream. |
|
285 |
* |
|
286 |
* @param b the data. |
|
287 |
* @exception IOException if an I/O error occurs. |
|
288 |
*/ |
|
289 |
public void write(byte b[]) throws IOException { |
|
290 |
writeBytes(b, 0, b.length); |
|
291 |
} |
|
292 |
||
293 |
/** |
|
294 |
* Writes <code>len</code> bytes from the specified byte array |
|
295 |
* starting at offset <code>off</code> to this file output stream. |
|
296 |
* |
|
297 |
* @param b the data. |
|
298 |
* @param off the start offset in the data. |
|
299 |
* @param len the number of bytes to write. |
|
300 |
* @exception IOException if an I/O error occurs. |
|
301 |
*/ |
|
302 |
public void write(byte b[], int off, int len) throws IOException { |
|
303 |
writeBytes(b, off, len); |
|
304 |
} |
|
305 |
||
306 |
/** |
|
307 |
* Closes this file output stream and releases any system resources |
|
308 |
* associated with this stream. This file output stream may no longer |
|
309 |
* be used for writing bytes. |
|
310 |
* |
|
311 |
* <p> If this stream has an associated channel then the channel is closed |
|
312 |
* as well. |
|
313 |
* |
|
314 |
* @exception IOException if an I/O error occurs. |
|
315 |
* |
|
316 |
* @revised 1.4 |
|
317 |
* @spec JSR-51 |
|
318 |
*/ |
|
319 |
public void close() throws IOException { |
|
320 |
synchronized (closeLock) { |
|
321 |
if (closed) { |
|
322 |
return; |
|
323 |
} |
|
324 |
closed = true; |
|
325 |
} |
|
326 |
||
327 |
if (channel != null) { |
|
328 |
/* |
|
329 |
* Decrement FD use count associated with the channel |
|
330 |
* The use count is incremented whenever a new channel |
|
331 |
* is obtained from this stream. |
|
332 |
*/ |
|
333 |
fd.decrementAndGetUseCount(); |
|
334 |
channel.close(); |
|
335 |
} |
|
336 |
||
337 |
/* |
|
338 |
* Decrement FD use count associated with this stream |
|
339 |
*/ |
|
340 |
int useCount = fd.decrementAndGetUseCount(); |
|
341 |
||
342 |
/* |
|
343 |
* If FileDescriptor is still in use by another stream, the finalizer |
|
344 |
* will not close it. |
|
345 |
*/ |
|
346 |
if ((useCount <= 0) || !isRunningFinalize()) { |
|
347 |
close0(); |
|
348 |
} |
|
349 |
} |
|
350 |
||
351 |
/** |
|
352 |
* Returns the file descriptor associated with this stream. |
|
353 |
* |
|
354 |
* @return the <code>FileDescriptor</code> object that represents |
|
355 |
* the connection to the file in the file system being used |
|
356 |
* by this <code>FileOutputStream</code> object. |
|
357 |
* |
|
358 |
* @exception IOException if an I/O error occurs. |
|
359 |
* @see java.io.FileDescriptor |
|
360 |
*/ |
|
361 |
public final FileDescriptor getFD() throws IOException { |
|
362 |
if (fd != null) return fd; |
|
363 |
throw new IOException(); |
|
364 |
} |
|
365 |
||
366 |
/** |
|
367 |
* Returns the unique {@link java.nio.channels.FileChannel FileChannel} |
|
368 |
* object associated with this file output stream. </p> |
|
369 |
* |
|
370 |
* <p> The initial {@link java.nio.channels.FileChannel#position() |
|
371 |
* </code>position<code>} of the returned channel will be equal to the |
|
372 |
* number of bytes written to the file so far unless this stream is in |
|
373 |
* append mode, in which case it will be equal to the size of the file. |
|
374 |
* Writing bytes to this stream will increment the channel's position |
|
375 |
* accordingly. Changing the channel's position, either explicitly or by |
|
376 |
* writing, will change this stream's file position. |
|
377 |
* |
|
378 |
* @return the file channel associated with this file output stream |
|
379 |
* |
|
380 |
* @since 1.4 |
|
381 |
* @spec JSR-51 |
|
382 |
*/ |
|
383 |
public FileChannel getChannel() { |
|
384 |
synchronized (this) { |
|
385 |
if (channel == null) { |
|
386 |
channel = FileChannelImpl.open(fd, false, true, this, append); |
|
387 |
||
388 |
/* |
|
389 |
* Increment fd's use count. Invoking the channel's close() |
|
390 |
* method will result in decrementing the use count set for |
|
391 |
* the channel. |
|
392 |
*/ |
|
393 |
fd.incrementAndGetUseCount(); |
|
394 |
} |
|
395 |
return channel; |
|
396 |
} |
|
397 |
} |
|
398 |
||
399 |
/** |
|
400 |
* Cleans up the connection to the file, and ensures that the |
|
401 |
* <code>close</code> method of this file output stream is |
|
402 |
* called when there are no more references to this stream. |
|
403 |
* |
|
404 |
* @exception IOException if an I/O error occurs. |
|
405 |
* @see java.io.FileInputStream#close() |
|
406 |
*/ |
|
407 |
protected void finalize() throws IOException { |
|
408 |
if (fd != null) { |
|
46
ddf5deb2a633
6631437: File{In,Out}putStream minor improvements to spec and stylistic improvements to code
martin
parents:
2
diff
changeset
|
409 |
if (fd == FileDescriptor.out || fd == FileDescriptor.err) { |
2 | 410 |
flush(); |
411 |
} else { |
|
412 |
||
413 |
/* |
|
414 |
* Finalizer should not release the FileDescriptor if another |
|
415 |
* stream is still using it. If the user directly invokes |
|
416 |
* close() then the FileDescriptor is also released. |
|
417 |
*/ |
|
418 |
runningFinalize.set(Boolean.TRUE); |
|
419 |
try { |
|
420 |
close(); |
|
421 |
} finally { |
|
422 |
runningFinalize.set(Boolean.FALSE); |
|
423 |
} |
|
424 |
} |
|
425 |
} |
|
426 |
} |
|
427 |
||
428 |
private native void close0() throws IOException; |
|
429 |
||
430 |
private static native void initIDs(); |
|
431 |
||
432 |
static { |
|
433 |
initIDs(); |
|
434 |
} |
|
435 |
||
436 |
} |