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