author | coffeys |
Tue, 13 Sep 2011 11:21:51 +0100 | |
changeset 10586 | 6e20ecfec8ed |
parent 5506 | 202f599c92aa |
child 11017 | 353f81426721 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10586
6e20ecfec8ed
7082769: FileInputStream/FileOutputStream/RandomAccessFile allow file descriptor be closed when still in use
coffeys
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.io; |
|
27 |
||
28 |
import java.nio.channels.FileChannel; |
|
29 |
import sun.nio.ch.FileChannelImpl; |
|
30 |
||
31 |
||
32 |
/** |
|
33 |
* Instances of this class support both reading and writing to a |
|
34 |
* random access file. A random access file behaves like a large |
|
35 |
* array of bytes stored in the file system. There is a kind of cursor, |
|
36 |
* or index into the implied array, called the <em>file pointer</em>; |
|
37 |
* input operations read bytes starting at the file pointer and advance |
|
38 |
* the file pointer past the bytes read. If the random access file is |
|
39 |
* created in read/write mode, then output operations are also available; |
|
40 |
* output operations write bytes starting at the file pointer and advance |
|
41 |
* the file pointer past the bytes written. Output operations that write |
|
42 |
* past the current end of the implied array cause the array to be |
|
43 |
* extended. The file pointer can be read by the |
|
44 |
* <code>getFilePointer</code> method and set by the <code>seek</code> |
|
45 |
* method. |
|
46 |
* <p> |
|
47 |
* It is generally true of all the reading routines in this class that |
|
48 |
* if end-of-file is reached before the desired number of bytes has been |
|
49 |
* read, an <code>EOFException</code> (which is a kind of |
|
50 |
* <code>IOException</code>) is thrown. If any byte cannot be read for |
|
51 |
* any reason other than end-of-file, an <code>IOException</code> other |
|
52 |
* than <code>EOFException</code> is thrown. In particular, an |
|
53 |
* <code>IOException</code> may be thrown if the stream has been closed. |
|
54 |
* |
|
55 |
* @author unascribed |
|
56 |
* @since JDK1.0 |
|
57 |
*/ |
|
58 |
||
59 |
public class RandomAccessFile implements DataOutput, DataInput, Closeable { |
|
60 |
||
61 |
private FileDescriptor fd; |
|
62 |
private FileChannel channel = null; |
|
63 |
private boolean rw; |
|
64 |
||
65 |
private Object closeLock = new Object(); |
|
66 |
private volatile boolean closed = false; |
|
67 |
||
68 |
private static final int O_RDONLY = 1; |
|
69 |
private static final int O_RDWR = 2; |
|
70 |
private static final int O_SYNC = 4; |
|
71 |
private static final int O_DSYNC = 8; |
|
72 |
||
73 |
/** |
|
74 |
* Creates a random access file stream to read from, and optionally |
|
75 |
* to write to, a file with the specified name. A new |
|
76 |
* {@link FileDescriptor} object is created to represent the |
|
77 |
* connection to the file. |
|
78 |
* |
|
79 |
* <p> The <tt>mode</tt> argument specifies the access mode with which the |
|
80 |
* file is to be opened. The permitted values and their meanings are as |
|
81 |
* specified for the <a |
|
82 |
* href="#mode"><tt>RandomAccessFile(File,String)</tt></a> constructor. |
|
83 |
* |
|
84 |
* <p> |
|
85 |
* If there is a security manager, its <code>checkRead</code> method |
|
86 |
* is called with the <code>name</code> argument |
|
87 |
* as its argument to see if read access to the file is allowed. |
|
88 |
* If the mode allows writing, the security manager's |
|
89 |
* <code>checkWrite</code> method |
|
90 |
* is also called with the <code>name</code> argument |
|
91 |
* as its argument to see if write access to the file is allowed. |
|
92 |
* |
|
93 |
* @param name the system-dependent filename |
|
94 |
* @param mode the access <a href="#mode">mode</a> |
|
95 |
* @exception IllegalArgumentException if the mode argument is not equal |
|
96 |
* to one of <tt>"r"</tt>, <tt>"rw"</tt>, <tt>"rws"</tt>, or |
|
97 |
* <tt>"rwd"</tt> |
|
98 |
* @exception FileNotFoundException |
|
99 |
* if the mode is <tt>"r"</tt> but the given string does not |
|
100 |
* denote an existing regular file, or if the mode begins with |
|
101 |
* <tt>"rw"</tt> but the given string does not denote an |
|
102 |
* existing, writable regular file and a new regular file of |
|
103 |
* that name cannot be created, or if some other error occurs |
|
104 |
* while opening or creating the file |
|
105 |
* @exception SecurityException if a security manager exists and its |
|
106 |
* <code>checkRead</code> method denies read access to the file |
|
107 |
* or the mode is "rw" and the security manager's |
|
108 |
* <code>checkWrite</code> method denies write access to the file |
|
109 |
* @see java.lang.SecurityException |
|
110 |
* @see java.lang.SecurityManager#checkRead(java.lang.String) |
|
111 |
* @see java.lang.SecurityManager#checkWrite(java.lang.String) |
|
112 |
* @revised 1.4 |
|
113 |
* @spec JSR-51 |
|
114 |
*/ |
|
115 |
public RandomAccessFile(String name, String mode) |
|
116 |
throws FileNotFoundException |
|
117 |
{ |
|
118 |
this(name != null ? new File(name) : null, mode); |
|
119 |
} |
|
120 |
||
121 |
/** |
|
122 |
* Creates a random access file stream to read from, and optionally to |
|
123 |
* write to, the file specified by the {@link File} argument. A new {@link |
|
124 |
* FileDescriptor} object is created to represent this file connection. |
|
125 |
* |
|
126 |
* <a name="mode"><p> The <tt>mode</tt> argument specifies the access mode |
|
127 |
* in which the file is to be opened. The permitted values and their |
|
128 |
* meanings are: |
|
129 |
* |
|
130 |
* <blockquote><table summary="Access mode permitted values and meanings"> |
|
131 |
* <tr><th><p align="left">Value</p></th><th><p align="left">Meaning</p></th></tr> |
|
132 |
* <tr><td valign="top"><tt>"r"</tt></td> |
|
133 |
* <td> Open for reading only. Invoking any of the <tt>write</tt> |
|
134 |
* methods of the resulting object will cause an {@link |
|
135 |
* java.io.IOException} to be thrown. </td></tr> |
|
136 |
* <tr><td valign="top"><tt>"rw"</tt></td> |
|
137 |
* <td> Open for reading and writing. If the file does not already |
|
138 |
* exist then an attempt will be made to create it. </td></tr> |
|
139 |
* <tr><td valign="top"><tt>"rws"</tt></td> |
|
140 |
* <td> Open for reading and writing, as with <tt>"rw"</tt>, and also |
|
141 |
* require that every update to the file's content or metadata be |
|
142 |
* written synchronously to the underlying storage device. </td></tr> |
|
143 |
* <tr><td valign="top"><tt>"rwd" </tt></td> |
|
144 |
* <td> Open for reading and writing, as with <tt>"rw"</tt>, and also |
|
145 |
* require that every update to the file's content be written |
|
146 |
* synchronously to the underlying storage device. </td></tr> |
|
147 |
* </table></blockquote> |
|
148 |
* |
|
149 |
* The <tt>"rws"</tt> and <tt>"rwd"</tt> modes work much like the {@link |
|
150 |
* java.nio.channels.FileChannel#force(boolean) force(boolean)} method of |
|
151 |
* the {@link java.nio.channels.FileChannel} class, passing arguments of |
|
152 |
* <tt>true</tt> and <tt>false</tt>, respectively, except that they always |
|
153 |
* apply to every I/O operation and are therefore often more efficient. If |
|
154 |
* the file resides on a local storage device then when an invocation of a |
|
155 |
* method of this class returns it is guaranteed that all changes made to |
|
156 |
* the file by that invocation will have been written to that device. This |
|
157 |
* is useful for ensuring that critical information is not lost in the |
|
158 |
* event of a system crash. If the file does not reside on a local device |
|
159 |
* then no such guarantee is made. |
|
160 |
* |
|
161 |
* <p> The <tt>"rwd"</tt> mode can be used to reduce the number of I/O |
|
162 |
* operations performed. Using <tt>"rwd"</tt> only requires updates to the |
|
163 |
* file's content to be written to storage; using <tt>"rws"</tt> requires |
|
164 |
* updates to both the file's content and its metadata to be written, which |
|
165 |
* generally requires at least one more low-level I/O operation. |
|
166 |
* |
|
167 |
* <p> If there is a security manager, its <code>checkRead</code> method is |
|
168 |
* called with the pathname of the <code>file</code> argument as its |
|
169 |
* argument to see if read access to the file is allowed. If the mode |
|
170 |
* allows writing, the security manager's <code>checkWrite</code> method is |
|
171 |
* also called with the path argument to see if write access to the file is |
|
172 |
* allowed. |
|
173 |
* |
|
174 |
* @param file the file object |
|
175 |
* @param mode the access mode, as described |
|
176 |
* <a href="#mode">above</a> |
|
177 |
* @exception IllegalArgumentException if the mode argument is not equal |
|
178 |
* to one of <tt>"r"</tt>, <tt>"rw"</tt>, <tt>"rws"</tt>, or |
|
179 |
* <tt>"rwd"</tt> |
|
180 |
* @exception FileNotFoundException |
|
181 |
* if the mode is <tt>"r"</tt> but the given file object does |
|
182 |
* not denote an existing regular file, or if the mode begins |
|
183 |
* with <tt>"rw"</tt> but the given file object does not denote |
|
184 |
* an existing, writable regular file and a new regular file of |
|
185 |
* that name cannot be created, or if some other error occurs |
|
186 |
* while opening or creating the file |
|
187 |
* @exception SecurityException if a security manager exists and its |
|
188 |
* <code>checkRead</code> method denies read access to the file |
|
189 |
* or the mode is "rw" and the security manager's |
|
190 |
* <code>checkWrite</code> method denies write access to the file |
|
191 |
* @see java.lang.SecurityManager#checkRead(java.lang.String) |
|
192 |
* @see java.lang.SecurityManager#checkWrite(java.lang.String) |
|
193 |
* @see java.nio.channels.FileChannel#force(boolean) |
|
194 |
* @revised 1.4 |
|
195 |
* @spec JSR-51 |
|
196 |
*/ |
|
197 |
public RandomAccessFile(File file, String mode) |
|
198 |
throws FileNotFoundException |
|
199 |
{ |
|
200 |
String name = (file != null ? file.getPath() : null); |
|
201 |
int imode = -1; |
|
202 |
if (mode.equals("r")) |
|
203 |
imode = O_RDONLY; |
|
204 |
else if (mode.startsWith("rw")) { |
|
205 |
imode = O_RDWR; |
|
206 |
rw = true; |
|
207 |
if (mode.length() > 2) { |
|
208 |
if (mode.equals("rws")) |
|
209 |
imode |= O_SYNC; |
|
210 |
else if (mode.equals("rwd")) |
|
211 |
imode |= O_DSYNC; |
|
212 |
else |
|
213 |
imode = -1; |
|
214 |
} |
|
215 |
} |
|
216 |
if (imode < 0) |
|
217 |
throw new IllegalArgumentException("Illegal mode \"" + mode |
|
218 |
+ "\" must be one of " |
|
219 |
+ "\"r\", \"rw\", \"rws\"," |
|
220 |
+ " or \"rwd\""); |
|
221 |
SecurityManager security = System.getSecurityManager(); |
|
222 |
if (security != null) { |
|
223 |
security.checkRead(name); |
|
224 |
if (rw) { |
|
225 |
security.checkWrite(name); |
|
226 |
} |
|
227 |
} |
|
228 |
if (name == null) { |
|
229 |
throw new NullPointerException(); |
|
230 |
} |
|
231 |
fd = new FileDescriptor(); |
|
232 |
fd.incrementAndGetUseCount(); |
|
233 |
open(name, imode); |
|
234 |
} |
|
235 |
||
236 |
/** |
|
237 |
* Returns the opaque file descriptor object associated with this |
|
238 |
* stream. </p> |
|
239 |
* |
|
240 |
* @return the file descriptor object associated with this stream. |
|
241 |
* @exception IOException if an I/O error occurs. |
|
242 |
* @see java.io.FileDescriptor |
|
243 |
*/ |
|
244 |
public final FileDescriptor getFD() throws IOException { |
|
245 |
if (fd != null) return fd; |
|
246 |
throw new IOException(); |
|
247 |
} |
|
248 |
||
249 |
/** |
|
250 |
* Returns the unique {@link java.nio.channels.FileChannel FileChannel} |
|
251 |
* object associated with this file. |
|
252 |
* |
|
253 |
* <p> The {@link java.nio.channels.FileChannel#position() |
|
254 |
* </code>position<code>} of the returned channel will always be equal to |
|
255 |
* this object's file-pointer offset as returned by the {@link |
|
256 |
* #getFilePointer getFilePointer} method. Changing this object's |
|
257 |
* file-pointer offset, whether explicitly or by reading or writing bytes, |
|
258 |
* will change the position of the channel, and vice versa. Changing the |
|
259 |
* file's length via this object will change the length seen via the file |
|
260 |
* channel, and vice versa. |
|
261 |
* |
|
262 |
* @return the file channel associated with this file |
|
263 |
* |
|
264 |
* @since 1.4 |
|
265 |
* @spec JSR-51 |
|
266 |
*/ |
|
267 |
public final FileChannel getChannel() { |
|
268 |
synchronized (this) { |
|
269 |
if (channel == null) { |
|
270 |
channel = FileChannelImpl.open(fd, true, rw, this); |
|
271 |
||
272 |
/* |
|
273 |
* FileDescriptor could be shared by FileInputStream or |
|
274 |
* FileOutputStream. |
|
275 |
* Ensure that FD is GC'ed only when all the streams/channels |
|
276 |
* are done using it. |
|
277 |
* Increment fd's use count. Invoking the channel's close() |
|
278 |
* method will result in decrementing the use count set for |
|
279 |
* the channel. |
|
280 |
*/ |
|
281 |
fd.incrementAndGetUseCount(); |
|
282 |
} |
|
283 |
return channel; |
|
284 |
} |
|
285 |
} |
|
286 |
||
287 |
/** |
|
288 |
* Opens a file and returns the file descriptor. The file is |
|
289 |
* opened in read-write mode if the O_RDWR bit in <code>mode</code> |
|
290 |
* is true, else the file is opened as read-only. |
|
291 |
* If the <code>name</code> refers to a directory, an IOException |
|
292 |
* is thrown. |
|
293 |
* |
|
294 |
* @param name the name of the file |
|
295 |
* @param mode the mode flags, a combination of the O_ constants |
|
296 |
* defined above |
|
297 |
*/ |
|
298 |
private native void open(String name, int mode) |
|
299 |
throws FileNotFoundException; |
|
300 |
||
301 |
// 'Read' primitives |
|
302 |
||
303 |
/** |
|
304 |
* Reads a byte of data from this file. The byte is returned as an |
|
305 |
* integer in the range 0 to 255 (<code>0x00-0x0ff</code>). This |
|
306 |
* method blocks if no input is yet available. |
|
307 |
* <p> |
|
308 |
* Although <code>RandomAccessFile</code> is not a subclass of |
|
309 |
* <code>InputStream</code>, this method behaves in exactly the same |
|
310 |
* way as the {@link InputStream#read()} method of |
|
311 |
* <code>InputStream</code>. |
|
312 |
* |
|
313 |
* @return the next byte of data, or <code>-1</code> if the end of the |
|
314 |
* file has been reached. |
|
315 |
* @exception IOException if an I/O error occurs. Not thrown if |
|
316 |
* end-of-file has been reached. |
|
317 |
*/ |
|
318 |
public native int read() throws IOException; |
|
319 |
||
320 |
/** |
|
321 |
* Reads a sub array as a sequence of bytes. |
|
322 |
* @param b the buffer into which the data is read. |
|
323 |
* @param off the start offset of the data. |
|
324 |
* @param len the number of bytes to read. |
|
325 |
* @exception IOException If an I/O error has occurred. |
|
326 |
*/ |
|
327 |
private native int readBytes(byte b[], int off, int len) throws IOException; |
|
328 |
||
329 |
/** |
|
330 |
* Reads up to <code>len</code> bytes of data from this file into an |
|
331 |
* array of bytes. This method blocks until at least one byte of input |
|
332 |
* is available. |
|
333 |
* <p> |
|
334 |
* Although <code>RandomAccessFile</code> is not a subclass of |
|
335 |
* <code>InputStream</code>, this method behaves in exactly the |
|
336 |
* same way as the {@link InputStream#read(byte[], int, int)} method of |
|
337 |
* <code>InputStream</code>. |
|
338 |
* |
|
339 |
* @param b the buffer into which the data is read. |
|
340 |
* @param off the start offset in array <code>b</code> |
|
341 |
* at which the data is written. |
|
342 |
* @param len the maximum number of bytes read. |
|
343 |
* @return the total number of bytes read into the buffer, or |
|
344 |
* <code>-1</code> if there is no more data because the end of |
|
345 |
* the file has been reached. |
|
346 |
* @exception IOException If the first byte cannot be read for any reason |
|
347 |
* other than end of file, or if the random access file has been closed, or if |
|
348 |
* some other I/O error occurs. |
|
349 |
* @exception NullPointerException If <code>b</code> is <code>null</code>. |
|
350 |
* @exception IndexOutOfBoundsException If <code>off</code> is negative, |
|
351 |
* <code>len</code> is negative, or <code>len</code> is greater than |
|
352 |
* <code>b.length - off</code> |
|
353 |
*/ |
|
354 |
public int read(byte b[], int off, int len) throws IOException { |
|
355 |
return readBytes(b, off, len); |
|
356 |
} |
|
357 |
||
358 |
/** |
|
359 |
* Reads up to <code>b.length</code> bytes of data from this file |
|
360 |
* into an array of bytes. This method blocks until at least one byte |
|
361 |
* of input is available. |
|
362 |
* <p> |
|
363 |
* Although <code>RandomAccessFile</code> is not a subclass of |
|
364 |
* <code>InputStream</code>, this method behaves in exactly the |
|
365 |
* same way as the {@link InputStream#read(byte[])} method of |
|
366 |
* <code>InputStream</code>. |
|
367 |
* |
|
368 |
* @param b the buffer into which the data is read. |
|
369 |
* @return the total number of bytes read into the buffer, or |
|
370 |
* <code>-1</code> if there is no more data because the end of |
|
371 |
* this file has been reached. |
|
372 |
* @exception IOException If the first byte cannot be read for any reason |
|
373 |
* other than end of file, or if the random access file has been closed, or if |
|
374 |
* some other I/O error occurs. |
|
375 |
* @exception NullPointerException If <code>b</code> is <code>null</code>. |
|
376 |
*/ |
|
377 |
public int read(byte b[]) throws IOException { |
|
378 |
return readBytes(b, 0, b.length); |
|
379 |
} |
|
380 |
||
381 |
/** |
|
382 |
* Reads <code>b.length</code> bytes from this file into the byte |
|
383 |
* array, starting at the current file pointer. This method reads |
|
384 |
* repeatedly from the file until the requested number of bytes are |
|
385 |
* read. This method blocks until the requested number of bytes are |
|
386 |
* read, the end of the stream is detected, or an exception is thrown. |
|
387 |
* |
|
388 |
* @param b the buffer into which the data is read. |
|
389 |
* @exception EOFException if this file reaches the end before reading |
|
390 |
* all the bytes. |
|
391 |
* @exception IOException if an I/O error occurs. |
|
392 |
*/ |
|
393 |
public final void readFully(byte b[]) throws IOException { |
|
394 |
readFully(b, 0, b.length); |
|
395 |
} |
|
396 |
||
397 |
/** |
|
398 |
* Reads exactly <code>len</code> bytes from this file into the byte |
|
399 |
* array, starting at the current file pointer. This method reads |
|
400 |
* repeatedly from the file until the requested number of bytes are |
|
401 |
* read. This method blocks until the requested number of bytes are |
|
402 |
* read, the end of the stream is detected, or an exception is thrown. |
|
403 |
* |
|
404 |
* @param b the buffer into which the data is read. |
|
405 |
* @param off the start offset of the data. |
|
406 |
* @param len the number of bytes to read. |
|
407 |
* @exception EOFException if this file reaches the end before reading |
|
408 |
* all the bytes. |
|
409 |
* @exception IOException if an I/O error occurs. |
|
410 |
*/ |
|
411 |
public final void readFully(byte b[], int off, int len) throws IOException { |
|
412 |
int n = 0; |
|
413 |
do { |
|
414 |
int count = this.read(b, off + n, len - n); |
|
415 |
if (count < 0) |
|
416 |
throw new EOFException(); |
|
417 |
n += count; |
|
418 |
} while (n < len); |
|
419 |
} |
|
420 |
||
421 |
/** |
|
422 |
* Attempts to skip over <code>n</code> bytes of input discarding the |
|
423 |
* skipped bytes. |
|
424 |
* <p> |
|
425 |
* |
|
426 |
* This method may skip over some smaller number of bytes, possibly zero. |
|
427 |
* This may result from any of a number of conditions; reaching end of |
|
428 |
* file before <code>n</code> bytes have been skipped is only one |
|
429 |
* possibility. This method never throws an <code>EOFException</code>. |
|
430 |
* The actual number of bytes skipped is returned. If <code>n</code> |
|
431 |
* is negative, no bytes are skipped. |
|
432 |
* |
|
433 |
* @param n the number of bytes to be skipped. |
|
434 |
* @return the actual number of bytes skipped. |
|
435 |
* @exception IOException if an I/O error occurs. |
|
436 |
*/ |
|
437 |
public int skipBytes(int n) throws IOException { |
|
438 |
long pos; |
|
439 |
long len; |
|
440 |
long newpos; |
|
441 |
||
442 |
if (n <= 0) { |
|
443 |
return 0; |
|
444 |
} |
|
445 |
pos = getFilePointer(); |
|
446 |
len = length(); |
|
447 |
newpos = pos + n; |
|
448 |
if (newpos > len) { |
|
449 |
newpos = len; |
|
450 |
} |
|
451 |
seek(newpos); |
|
452 |
||
453 |
/* return the actual number of bytes skipped */ |
|
454 |
return (int) (newpos - pos); |
|
455 |
} |
|
456 |
||
457 |
// 'Write' primitives |
|
458 |
||
459 |
/** |
|
460 |
* Writes the specified byte to this file. The write starts at |
|
461 |
* the current file pointer. |
|
462 |
* |
|
463 |
* @param b the <code>byte</code> to be written. |
|
464 |
* @exception IOException if an I/O error occurs. |
|
465 |
*/ |
|
466 |
public native void write(int b) throws IOException; |
|
467 |
||
468 |
/** |
|
469 |
* Writes a sub array as a sequence of bytes. |
|
470 |
* @param b the data to be written |
|
471 |
||
472 |
* @param off the start offset in the data |
|
473 |
* @param len the number of bytes that are written |
|
474 |
* @exception IOException If an I/O error has occurred. |
|
475 |
*/ |
|
476 |
private native void writeBytes(byte b[], int off, int len) throws IOException; |
|
477 |
||
478 |
/** |
|
479 |
* Writes <code>b.length</code> bytes from the specified byte array |
|
480 |
* to this file, starting at the current file pointer. |
|
481 |
* |
|
482 |
* @param b the data. |
|
483 |
* @exception IOException if an I/O error occurs. |
|
484 |
*/ |
|
485 |
public void write(byte b[]) throws IOException { |
|
486 |
writeBytes(b, 0, b.length); |
|
487 |
} |
|
488 |
||
489 |
/** |
|
490 |
* Writes <code>len</code> bytes from the specified byte array |
|
491 |
* starting at offset <code>off</code> to this file. |
|
492 |
* |
|
493 |
* @param b the data. |
|
494 |
* @param off the start offset in the data. |
|
495 |
* @param len the number of bytes to write. |
|
496 |
* @exception IOException if an I/O error occurs. |
|
497 |
*/ |
|
498 |
public void write(byte b[], int off, int len) throws IOException { |
|
499 |
writeBytes(b, off, len); |
|
500 |
} |
|
501 |
||
502 |
// 'Random access' stuff |
|
503 |
||
504 |
/** |
|
505 |
* Returns the current offset in this file. |
|
506 |
* |
|
507 |
* @return the offset from the beginning of the file, in bytes, |
|
508 |
* at which the next read or write occurs. |
|
509 |
* @exception IOException if an I/O error occurs. |
|
510 |
*/ |
|
511 |
public native long getFilePointer() throws IOException; |
|
512 |
||
513 |
/** |
|
514 |
* Sets the file-pointer offset, measured from the beginning of this |
|
515 |
* file, at which the next read or write occurs. The offset may be |
|
516 |
* set beyond the end of the file. Setting the offset beyond the end |
|
517 |
* of the file does not change the file length. The file length will |
|
518 |
* change only by writing after the offset has been set beyond the end |
|
519 |
* of the file. |
|
520 |
* |
|
521 |
* @param pos the offset position, measured in bytes from the |
|
522 |
* beginning of the file, at which to set the file |
|
523 |
* pointer. |
|
524 |
* @exception IOException if <code>pos</code> is less than |
|
525 |
* <code>0</code> or if an I/O error occurs. |
|
526 |
*/ |
|
527 |
public native void seek(long pos) throws IOException; |
|
528 |
||
529 |
/** |
|
530 |
* Returns the length of this file. |
|
531 |
* |
|
532 |
* @return the length of this file, measured in bytes. |
|
533 |
* @exception IOException if an I/O error occurs. |
|
534 |
*/ |
|
535 |
public native long length() throws IOException; |
|
536 |
||
537 |
/** |
|
538 |
* Sets the length of this file. |
|
539 |
* |
|
540 |
* <p> If the present length of the file as returned by the |
|
541 |
* <code>length</code> method is greater than the <code>newLength</code> |
|
542 |
* argument then the file will be truncated. In this case, if the file |
|
543 |
* offset as returned by the <code>getFilePointer</code> method is greater |
|
544 |
* than <code>newLength</code> then after this method returns the offset |
|
545 |
* will be equal to <code>newLength</code>. |
|
546 |
* |
|
547 |
* <p> If the present length of the file as returned by the |
|
548 |
* <code>length</code> method is smaller than the <code>newLength</code> |
|
549 |
* argument then the file will be extended. In this case, the contents of |
|
550 |
* the extended portion of the file are not defined. |
|
551 |
* |
|
552 |
* @param newLength The desired length of the file |
|
553 |
* @exception IOException If an I/O error occurs |
|
554 |
* @since 1.2 |
|
555 |
*/ |
|
556 |
public native void setLength(long newLength) throws IOException; |
|
557 |
||
558 |
/** |
|
559 |
* Closes this random access file stream and releases any system |
|
560 |
* resources associated with the stream. A closed random access |
|
561 |
* file cannot perform input or output operations and cannot be |
|
562 |
* reopened. |
|
563 |
* |
|
564 |
* <p> If this file has an associated channel then the channel is closed |
|
565 |
* as well. |
|
566 |
* |
|
567 |
* @exception IOException if an I/O error occurs. |
|
568 |
* |
|
569 |
* @revised 1.4 |
|
570 |
* @spec JSR-51 |
|
571 |
*/ |
|
572 |
public void close() throws IOException { |
|
573 |
synchronized (closeLock) { |
|
574 |
if (closed) { |
|
575 |
return; |
|
576 |
} |
|
577 |
closed = true; |
|
578 |
} |
|
579 |
if (channel != null) { |
|
580 |
/* |
|
581 |
* Decrement FD use count associated with the channel. The FD use |
|
582 |
* count is incremented whenever a new channel is obtained from |
|
583 |
* this stream. |
|
584 |
*/ |
|
585 |
fd.decrementAndGetUseCount(); |
|
586 |
channel.close(); |
|
587 |
} |
|
588 |
||
589 |
/* |
|
590 |
* Decrement FD use count associated with this stream. |
|
591 |
* The count got incremented by FileDescriptor during its construction. |
|
592 |
*/ |
|
10586
6e20ecfec8ed
7082769: FileInputStream/FileOutputStream/RandomAccessFile allow file descriptor be closed when still in use
coffeys
parents:
5506
diff
changeset
|
593 |
int useCount = fd.decrementAndGetUseCount(); |
6e20ecfec8ed
7082769: FileInputStream/FileOutputStream/RandomAccessFile allow file descriptor be closed when still in use
coffeys
parents:
5506
diff
changeset
|
594 |
|
6e20ecfec8ed
7082769: FileInputStream/FileOutputStream/RandomAccessFile allow file descriptor be closed when still in use
coffeys
parents:
5506
diff
changeset
|
595 |
/* |
6e20ecfec8ed
7082769: FileInputStream/FileOutputStream/RandomAccessFile allow file descriptor be closed when still in use
coffeys
parents:
5506
diff
changeset
|
596 |
* If FileDescriptor is still in use by another stream, we |
6e20ecfec8ed
7082769: FileInputStream/FileOutputStream/RandomAccessFile allow file descriptor be closed when still in use
coffeys
parents:
5506
diff
changeset
|
597 |
* will not close it. |
6e20ecfec8ed
7082769: FileInputStream/FileOutputStream/RandomAccessFile allow file descriptor be closed when still in use
coffeys
parents:
5506
diff
changeset
|
598 |
*/ |
6e20ecfec8ed
7082769: FileInputStream/FileOutputStream/RandomAccessFile allow file descriptor be closed when still in use
coffeys
parents:
5506
diff
changeset
|
599 |
if (useCount <= 0) { |
6e20ecfec8ed
7082769: FileInputStream/FileOutputStream/RandomAccessFile allow file descriptor be closed when still in use
coffeys
parents:
5506
diff
changeset
|
600 |
close0(); |
6e20ecfec8ed
7082769: FileInputStream/FileOutputStream/RandomAccessFile allow file descriptor be closed when still in use
coffeys
parents:
5506
diff
changeset
|
601 |
} |
2 | 602 |
} |
603 |
||
604 |
// |
|
605 |
// Some "reading/writing Java data types" methods stolen from |
|
606 |
// DataInputStream and DataOutputStream. |
|
607 |
// |
|
608 |
||
609 |
/** |
|
610 |
* Reads a <code>boolean</code> from this file. This method reads a |
|
611 |
* single byte from the file, starting at the current file pointer. |
|
612 |
* A value of <code>0</code> represents |
|
613 |
* <code>false</code>. Any other value represents <code>true</code>. |
|
614 |
* This method blocks until the byte is read, the end of the stream |
|
615 |
* is detected, or an exception is thrown. |
|
616 |
* |
|
617 |
* @return the <code>boolean</code> value read. |
|
618 |
* @exception EOFException if this file has reached the end. |
|
619 |
* @exception IOException if an I/O error occurs. |
|
620 |
*/ |
|
621 |
public final boolean readBoolean() throws IOException { |
|
622 |
int ch = this.read(); |
|
623 |
if (ch < 0) |
|
624 |
throw new EOFException(); |
|
625 |
return (ch != 0); |
|
626 |
} |
|
627 |
||
628 |
/** |
|
629 |
* Reads a signed eight-bit value from this file. This method reads a |
|
630 |
* byte from the file, starting from the current file pointer. |
|
631 |
* If the byte read is <code>b</code>, where |
|
632 |
* <code>0 <= b <= 255</code>, |
|
633 |
* then the result is: |
|
634 |
* <blockquote><pre> |
|
635 |
* (byte)(b) |
|
636 |
* </pre></blockquote> |
|
637 |
* <p> |
|
638 |
* This method blocks until the byte is read, the end of the stream |
|
639 |
* is detected, or an exception is thrown. |
|
640 |
* |
|
641 |
* @return the next byte of this file as a signed eight-bit |
|
642 |
* <code>byte</code>. |
|
643 |
* @exception EOFException if this file has reached the end. |
|
644 |
* @exception IOException if an I/O error occurs. |
|
645 |
*/ |
|
646 |
public final byte readByte() throws IOException { |
|
647 |
int ch = this.read(); |
|
648 |
if (ch < 0) |
|
649 |
throw new EOFException(); |
|
650 |
return (byte)(ch); |
|
651 |
} |
|
652 |
||
653 |
/** |
|
654 |
* Reads an unsigned eight-bit number from this file. This method reads |
|
655 |
* a byte from this file, starting at the current file pointer, |
|
656 |
* and returns that byte. |
|
657 |
* <p> |
|
658 |
* This method blocks until the byte is read, the end of the stream |
|
659 |
* is detected, or an exception is thrown. |
|
660 |
* |
|
661 |
* @return the next byte of this file, interpreted as an unsigned |
|
662 |
* eight-bit number. |
|
663 |
* @exception EOFException if this file has reached the end. |
|
664 |
* @exception IOException if an I/O error occurs. |
|
665 |
*/ |
|
666 |
public final int readUnsignedByte() throws IOException { |
|
667 |
int ch = this.read(); |
|
668 |
if (ch < 0) |
|
669 |
throw new EOFException(); |
|
670 |
return ch; |
|
671 |
} |
|
672 |
||
673 |
/** |
|
674 |
* Reads a signed 16-bit number from this file. The method reads two |
|
675 |
* bytes from this file, starting at the current file pointer. |
|
676 |
* If the two bytes read, in order, are |
|
677 |
* <code>b1</code> and <code>b2</code>, where each of the two values is |
|
678 |
* between <code>0</code> and <code>255</code>, inclusive, then the |
|
679 |
* result is equal to: |
|
680 |
* <blockquote><pre> |
|
681 |
* (short)((b1 << 8) | b2) |
|
682 |
* </pre></blockquote> |
|
683 |
* <p> |
|
684 |
* This method blocks until the two bytes are read, the end of the |
|
685 |
* stream is detected, or an exception is thrown. |
|
686 |
* |
|
687 |
* @return the next two bytes of this file, interpreted as a signed |
|
688 |
* 16-bit number. |
|
689 |
* @exception EOFException if this file reaches the end before reading |
|
690 |
* two bytes. |
|
691 |
* @exception IOException if an I/O error occurs. |
|
692 |
*/ |
|
693 |
public final short readShort() throws IOException { |
|
694 |
int ch1 = this.read(); |
|
695 |
int ch2 = this.read(); |
|
696 |
if ((ch1 | ch2) < 0) |
|
697 |
throw new EOFException(); |
|
698 |
return (short)((ch1 << 8) + (ch2 << 0)); |
|
699 |
} |
|
700 |
||
701 |
/** |
|
702 |
* Reads an unsigned 16-bit number from this file. This method reads |
|
703 |
* two bytes from the file, starting at the current file pointer. |
|
704 |
* If the bytes read, in order, are |
|
705 |
* <code>b1</code> and <code>b2</code>, where |
|
706 |
* <code>0 <= b1, b2 <= 255</code>, |
|
707 |
* then the result is equal to: |
|
708 |
* <blockquote><pre> |
|
709 |
* (b1 << 8) | b2 |
|
710 |
* </pre></blockquote> |
|
711 |
* <p> |
|
712 |
* This method blocks until the two bytes are read, the end of the |
|
713 |
* stream is detected, or an exception is thrown. |
|
714 |
* |
|
715 |
* @return the next two bytes of this file, interpreted as an unsigned |
|
716 |
* 16-bit integer. |
|
717 |
* @exception EOFException if this file reaches the end before reading |
|
718 |
* two bytes. |
|
719 |
* @exception IOException if an I/O error occurs. |
|
720 |
*/ |
|
721 |
public final int readUnsignedShort() throws IOException { |
|
722 |
int ch1 = this.read(); |
|
723 |
int ch2 = this.read(); |
|
724 |
if ((ch1 | ch2) < 0) |
|
725 |
throw new EOFException(); |
|
726 |
return (ch1 << 8) + (ch2 << 0); |
|
727 |
} |
|
728 |
||
729 |
/** |
|
730 |
* Reads a character from this file. This method reads two |
|
731 |
* bytes from the file, starting at the current file pointer. |
|
732 |
* If the bytes read, in order, are |
|
733 |
* <code>b1</code> and <code>b2</code>, where |
|
734 |
* <code>0 <= b1, b2 <= 255</code>, |
|
735 |
* then the result is equal to: |
|
736 |
* <blockquote><pre> |
|
737 |
* (char)((b1 << 8) | b2) |
|
738 |
* </pre></blockquote> |
|
739 |
* <p> |
|
740 |
* This method blocks until the two bytes are read, the end of the |
|
741 |
* stream is detected, or an exception is thrown. |
|
742 |
* |
|
743 |
* @return the next two bytes of this file, interpreted as a |
|
744 |
* <code>char</code>. |
|
745 |
* @exception EOFException if this file reaches the end before reading |
|
746 |
* two bytes. |
|
747 |
* @exception IOException if an I/O error occurs. |
|
748 |
*/ |
|
749 |
public final char readChar() throws IOException { |
|
750 |
int ch1 = this.read(); |
|
751 |
int ch2 = this.read(); |
|
752 |
if ((ch1 | ch2) < 0) |
|
753 |
throw new EOFException(); |
|
754 |
return (char)((ch1 << 8) + (ch2 << 0)); |
|
755 |
} |
|
756 |
||
757 |
/** |
|
758 |
* Reads a signed 32-bit integer from this file. This method reads 4 |
|
759 |
* bytes from the file, starting at the current file pointer. |
|
760 |
* If the bytes read, in order, are <code>b1</code>, |
|
761 |
* <code>b2</code>, <code>b3</code>, and <code>b4</code>, where |
|
762 |
* <code>0 <= b1, b2, b3, b4 <= 255</code>, |
|
763 |
* then the result is equal to: |
|
764 |
* <blockquote><pre> |
|
765 |
* (b1 << 24) | (b2 << 16) + (b3 << 8) + b4 |
|
766 |
* </pre></blockquote> |
|
767 |
* <p> |
|
768 |
* This method blocks until the four bytes are read, the end of the |
|
769 |
* stream is detected, or an exception is thrown. |
|
770 |
* |
|
771 |
* @return the next four bytes of this file, interpreted as an |
|
772 |
* <code>int</code>. |
|
773 |
* @exception EOFException if this file reaches the end before reading |
|
774 |
* four bytes. |
|
775 |
* @exception IOException if an I/O error occurs. |
|
776 |
*/ |
|
777 |
public final int readInt() throws IOException { |
|
778 |
int ch1 = this.read(); |
|
779 |
int ch2 = this.read(); |
|
780 |
int ch3 = this.read(); |
|
781 |
int ch4 = this.read(); |
|
782 |
if ((ch1 | ch2 | ch3 | ch4) < 0) |
|
783 |
throw new EOFException(); |
|
784 |
return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); |
|
785 |
} |
|
786 |
||
787 |
/** |
|
788 |
* Reads a signed 64-bit integer from this file. This method reads eight |
|
789 |
* bytes from the file, starting at the current file pointer. |
|
790 |
* If the bytes read, in order, are |
|
791 |
* <code>b1</code>, <code>b2</code>, <code>b3</code>, |
|
792 |
* <code>b4</code>, <code>b5</code>, <code>b6</code>, |
|
793 |
* <code>b7</code>, and <code>b8,</code> where: |
|
794 |
* <blockquote><pre> |
|
795 |
* 0 <= b1, b2, b3, b4, b5, b6, b7, b8 <=255, |
|
796 |
* </pre></blockquote> |
|
797 |
* <p> |
|
798 |
* then the result is equal to: |
|
799 |
* <p><blockquote><pre> |
|
800 |
* ((long)b1 << 56) + ((long)b2 << 48) |
|
801 |
* + ((long)b3 << 40) + ((long)b4 << 32) |
|
802 |
* + ((long)b5 << 24) + ((long)b6 << 16) |
|
803 |
* + ((long)b7 << 8) + b8 |
|
804 |
* </pre></blockquote> |
|
805 |
* <p> |
|
806 |
* This method blocks until the eight bytes are read, the end of the |
|
807 |
* stream is detected, or an exception is thrown. |
|
808 |
* |
|
809 |
* @return the next eight bytes of this file, interpreted as a |
|
810 |
* <code>long</code>. |
|
811 |
* @exception EOFException if this file reaches the end before reading |
|
812 |
* eight bytes. |
|
813 |
* @exception IOException if an I/O error occurs. |
|
814 |
*/ |
|
815 |
public final long readLong() throws IOException { |
|
816 |
return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL); |
|
817 |
} |
|
818 |
||
819 |
/** |
|
820 |
* Reads a <code>float</code> from this file. This method reads an |
|
821 |
* <code>int</code> value, starting at the current file pointer, |
|
822 |
* as if by the <code>readInt</code> method |
|
823 |
* and then converts that <code>int</code> to a <code>float</code> |
|
824 |
* using the <code>intBitsToFloat</code> method in class |
|
825 |
* <code>Float</code>. |
|
826 |
* <p> |
|
827 |
* This method blocks until the four bytes are read, the end of the |
|
828 |
* stream is detected, or an exception is thrown. |
|
829 |
* |
|
830 |
* @return the next four bytes of this file, interpreted as a |
|
831 |
* <code>float</code>. |
|
832 |
* @exception EOFException if this file reaches the end before reading |
|
833 |
* four bytes. |
|
834 |
* @exception IOException if an I/O error occurs. |
|
835 |
* @see java.io.RandomAccessFile#readInt() |
|
836 |
* @see java.lang.Float#intBitsToFloat(int) |
|
837 |
*/ |
|
838 |
public final float readFloat() throws IOException { |
|
839 |
return Float.intBitsToFloat(readInt()); |
|
840 |
} |
|
841 |
||
842 |
/** |
|
843 |
* Reads a <code>double</code> from this file. This method reads a |
|
844 |
* <code>long</code> value, starting at the current file pointer, |
|
845 |
* as if by the <code>readLong</code> method |
|
846 |
* and then converts that <code>long</code> to a <code>double</code> |
|
847 |
* using the <code>longBitsToDouble</code> method in |
|
848 |
* class <code>Double</code>. |
|
849 |
* <p> |
|
850 |
* This method blocks until the eight bytes are read, the end of the |
|
851 |
* stream is detected, or an exception is thrown. |
|
852 |
* |
|
853 |
* @return the next eight bytes of this file, interpreted as a |
|
854 |
* <code>double</code>. |
|
855 |
* @exception EOFException if this file reaches the end before reading |
|
856 |
* eight bytes. |
|
857 |
* @exception IOException if an I/O error occurs. |
|
858 |
* @see java.io.RandomAccessFile#readLong() |
|
859 |
* @see java.lang.Double#longBitsToDouble(long) |
|
860 |
*/ |
|
861 |
public final double readDouble() throws IOException { |
|
862 |
return Double.longBitsToDouble(readLong()); |
|
863 |
} |
|
864 |
||
865 |
/** |
|
866 |
* Reads the next line of text from this file. This method successively |
|
867 |
* reads bytes from the file, starting at the current file pointer, |
|
868 |
* until it reaches a line terminator or the end |
|
869 |
* of the file. Each byte is converted into a character by taking the |
|
870 |
* byte's value for the lower eight bits of the character and setting the |
|
871 |
* high eight bits of the character to zero. This method does not, |
|
872 |
* therefore, support the full Unicode character set. |
|
873 |
* |
|
874 |
* <p> A line of text is terminated by a carriage-return character |
|
875 |
* (<code>'\r'</code>), a newline character (<code>'\n'</code>), a |
|
876 |
* carriage-return character immediately followed by a newline character, |
|
877 |
* or the end of the file. Line-terminating characters are discarded and |
|
878 |
* are not included as part of the string returned. |
|
879 |
* |
|
880 |
* <p> This method blocks until a newline character is read, a carriage |
|
881 |
* return and the byte following it are read (to see if it is a newline), |
|
882 |
* the end of the file is reached, or an exception is thrown. |
|
883 |
* |
|
884 |
* @return the next line of text from this file, or null if end |
|
885 |
* of file is encountered before even one byte is read. |
|
886 |
* @exception IOException if an I/O error occurs. |
|
887 |
*/ |
|
888 |
||
889 |
public final String readLine() throws IOException { |
|
890 |
StringBuffer input = new StringBuffer(); |
|
891 |
int c = -1; |
|
892 |
boolean eol = false; |
|
893 |
||
894 |
while (!eol) { |
|
895 |
switch (c = read()) { |
|
896 |
case -1: |
|
897 |
case '\n': |
|
898 |
eol = true; |
|
899 |
break; |
|
900 |
case '\r': |
|
901 |
eol = true; |
|
902 |
long cur = getFilePointer(); |
|
903 |
if ((read()) != '\n') { |
|
904 |
seek(cur); |
|
905 |
} |
|
906 |
break; |
|
907 |
default: |
|
908 |
input.append((char)c); |
|
909 |
break; |
|
910 |
} |
|
911 |
} |
|
912 |
||
913 |
if ((c == -1) && (input.length() == 0)) { |
|
914 |
return null; |
|
915 |
} |
|
916 |
return input.toString(); |
|
917 |
} |
|
918 |
||
919 |
/** |
|
920 |
* Reads in a string from this file. The string has been encoded |
|
921 |
* using a |
|
922 |
* <a href="DataInput.html#modified-utf-8">modified UTF-8</a> |
|
923 |
* format. |
|
924 |
* <p> |
|
925 |
* The first two bytes are read, starting from the current file |
|
926 |
* pointer, as if by |
|
927 |
* <code>readUnsignedShort</code>. This value gives the number of |
|
928 |
* following bytes that are in the encoded string, not |
|
929 |
* the length of the resulting string. The following bytes are then |
|
930 |
* interpreted as bytes encoding characters in the modified UTF-8 format |
|
931 |
* and are converted into characters. |
|
932 |
* <p> |
|
933 |
* This method blocks until all the bytes are read, the end of the |
|
934 |
* stream is detected, or an exception is thrown. |
|
935 |
* |
|
936 |
* @return a Unicode string. |
|
937 |
* @exception EOFException if this file reaches the end before |
|
938 |
* reading all the bytes. |
|
939 |
* @exception IOException if an I/O error occurs. |
|
940 |
* @exception UTFDataFormatException if the bytes do not represent |
|
941 |
* valid modified UTF-8 encoding of a Unicode string. |
|
942 |
* @see java.io.RandomAccessFile#readUnsignedShort() |
|
943 |
*/ |
|
944 |
public final String readUTF() throws IOException { |
|
945 |
return DataInputStream.readUTF(this); |
|
946 |
} |
|
947 |
||
948 |
/** |
|
949 |
* Writes a <code>boolean</code> to the file as a one-byte value. The |
|
950 |
* value <code>true</code> is written out as the value |
|
951 |
* <code>(byte)1</code>; the value <code>false</code> is written out |
|
952 |
* as the value <code>(byte)0</code>. The write starts at |
|
953 |
* the current position of the file pointer. |
|
954 |
* |
|
955 |
* @param v a <code>boolean</code> value to be written. |
|
956 |
* @exception IOException if an I/O error occurs. |
|
957 |
*/ |
|
958 |
public final void writeBoolean(boolean v) throws IOException { |
|
959 |
write(v ? 1 : 0); |
|
960 |
//written++; |
|
961 |
} |
|
962 |
||
963 |
/** |
|
964 |
* Writes a <code>byte</code> to the file as a one-byte value. The |
|
965 |
* write starts at the current position of the file pointer. |
|
966 |
* |
|
967 |
* @param v a <code>byte</code> value to be written. |
|
968 |
* @exception IOException if an I/O error occurs. |
|
969 |
*/ |
|
970 |
public final void writeByte(int v) throws IOException { |
|
971 |
write(v); |
|
972 |
//written++; |
|
973 |
} |
|
974 |
||
975 |
/** |
|
976 |
* Writes a <code>short</code> to the file as two bytes, high byte first. |
|
977 |
* The write starts at the current position of the file pointer. |
|
978 |
* |
|
979 |
* @param v a <code>short</code> to be written. |
|
980 |
* @exception IOException if an I/O error occurs. |
|
981 |
*/ |
|
982 |
public final void writeShort(int v) throws IOException { |
|
983 |
write((v >>> 8) & 0xFF); |
|
984 |
write((v >>> 0) & 0xFF); |
|
985 |
//written += 2; |
|
986 |
} |
|
987 |
||
988 |
/** |
|
989 |
* Writes a <code>char</code> to the file as a two-byte value, high |
|
990 |
* byte first. The write starts at the current position of the |
|
991 |
* file pointer. |
|
992 |
* |
|
993 |
* @param v a <code>char</code> value to be written. |
|
994 |
* @exception IOException if an I/O error occurs. |
|
995 |
*/ |
|
996 |
public final void writeChar(int v) throws IOException { |
|
997 |
write((v >>> 8) & 0xFF); |
|
998 |
write((v >>> 0) & 0xFF); |
|
999 |
//written += 2; |
|
1000 |
} |
|
1001 |
||
1002 |
/** |
|
1003 |
* Writes an <code>int</code> to the file as four bytes, high byte first. |
|
1004 |
* The write starts at the current position of the file pointer. |
|
1005 |
* |
|
1006 |
* @param v an <code>int</code> to be written. |
|
1007 |
* @exception IOException if an I/O error occurs. |
|
1008 |
*/ |
|
1009 |
public final void writeInt(int v) throws IOException { |
|
1010 |
write((v >>> 24) & 0xFF); |
|
1011 |
write((v >>> 16) & 0xFF); |
|
1012 |
write((v >>> 8) & 0xFF); |
|
1013 |
write((v >>> 0) & 0xFF); |
|
1014 |
//written += 4; |
|
1015 |
} |
|
1016 |
||
1017 |
/** |
|
1018 |
* Writes a <code>long</code> to the file as eight bytes, high byte first. |
|
1019 |
* The write starts at the current position of the file pointer. |
|
1020 |
* |
|
1021 |
* @param v a <code>long</code> to be written. |
|
1022 |
* @exception IOException if an I/O error occurs. |
|
1023 |
*/ |
|
1024 |
public final void writeLong(long v) throws IOException { |
|
1025 |
write((int)(v >>> 56) & 0xFF); |
|
1026 |
write((int)(v >>> 48) & 0xFF); |
|
1027 |
write((int)(v >>> 40) & 0xFF); |
|
1028 |
write((int)(v >>> 32) & 0xFF); |
|
1029 |
write((int)(v >>> 24) & 0xFF); |
|
1030 |
write((int)(v >>> 16) & 0xFF); |
|
1031 |
write((int)(v >>> 8) & 0xFF); |
|
1032 |
write((int)(v >>> 0) & 0xFF); |
|
1033 |
//written += 8; |
|
1034 |
} |
|
1035 |
||
1036 |
/** |
|
1037 |
* Converts the float argument to an <code>int</code> using the |
|
1038 |
* <code>floatToIntBits</code> method in class <code>Float</code>, |
|
1039 |
* and then writes that <code>int</code> value to the file as a |
|
1040 |
* four-byte quantity, high byte first. The write starts at the |
|
1041 |
* current position of the file pointer. |
|
1042 |
* |
|
1043 |
* @param v a <code>float</code> value to be written. |
|
1044 |
* @exception IOException if an I/O error occurs. |
|
1045 |
* @see java.lang.Float#floatToIntBits(float) |
|
1046 |
*/ |
|
1047 |
public final void writeFloat(float v) throws IOException { |
|
1048 |
writeInt(Float.floatToIntBits(v)); |
|
1049 |
} |
|
1050 |
||
1051 |
/** |
|
1052 |
* Converts the double argument to a <code>long</code> using the |
|
1053 |
* <code>doubleToLongBits</code> method in class <code>Double</code>, |
|
1054 |
* and then writes that <code>long</code> value to the file as an |
|
1055 |
* eight-byte quantity, high byte first. The write starts at the current |
|
1056 |
* position of the file pointer. |
|
1057 |
* |
|
1058 |
* @param v a <code>double</code> value to be written. |
|
1059 |
* @exception IOException if an I/O error occurs. |
|
1060 |
* @see java.lang.Double#doubleToLongBits(double) |
|
1061 |
*/ |
|
1062 |
public final void writeDouble(double v) throws IOException { |
|
1063 |
writeLong(Double.doubleToLongBits(v)); |
|
1064 |
} |
|
1065 |
||
1066 |
/** |
|
1067 |
* Writes the string to the file as a sequence of bytes. Each |
|
1068 |
* character in the string is written out, in sequence, by discarding |
|
1069 |
* its high eight bits. The write starts at the current position of |
|
1070 |
* the file pointer. |
|
1071 |
* |
|
1072 |
* @param s a string of bytes to be written. |
|
1073 |
* @exception IOException if an I/O error occurs. |
|
1074 |
*/ |
|
1075 |
public final void writeBytes(String s) throws IOException { |
|
1076 |
int len = s.length(); |
|
1077 |
byte[] b = new byte[len]; |
|
1078 |
s.getBytes(0, len, b, 0); |
|
1079 |
writeBytes(b, 0, len); |
|
1080 |
} |
|
1081 |
||
1082 |
/** |
|
1083 |
* Writes a string to the file as a sequence of characters. Each |
|
1084 |
* character is written to the data output stream as if by the |
|
1085 |
* <code>writeChar</code> method. The write starts at the current |
|
1086 |
* position of the file pointer. |
|
1087 |
* |
|
1088 |
* @param s a <code>String</code> value to be written. |
|
1089 |
* @exception IOException if an I/O error occurs. |
|
1090 |
* @see java.io.RandomAccessFile#writeChar(int) |
|
1091 |
*/ |
|
1092 |
public final void writeChars(String s) throws IOException { |
|
1093 |
int clen = s.length(); |
|
1094 |
int blen = 2*clen; |
|
1095 |
byte[] b = new byte[blen]; |
|
1096 |
char[] c = new char[clen]; |
|
1097 |
s.getChars(0, clen, c, 0); |
|
1098 |
for (int i = 0, j = 0; i < clen; i++) { |
|
1099 |
b[j++] = (byte)(c[i] >>> 8); |
|
1100 |
b[j++] = (byte)(c[i] >>> 0); |
|
1101 |
} |
|
1102 |
writeBytes(b, 0, blen); |
|
1103 |
} |
|
1104 |
||
1105 |
/** |
|
1106 |
* Writes a string to the file using |
|
1107 |
* <a href="DataInput.html#modified-utf-8">modified UTF-8</a> |
|
1108 |
* encoding in a machine-independent manner. |
|
1109 |
* <p> |
|
1110 |
* First, two bytes are written to the file, starting at the |
|
1111 |
* current file pointer, as if by the |
|
1112 |
* <code>writeShort</code> method giving the number of bytes to |
|
1113 |
* follow. This value is the number of bytes actually written out, |
|
1114 |
* not the length of the string. Following the length, each character |
|
1115 |
* of the string is output, in sequence, using the modified UTF-8 encoding |
|
1116 |
* for each character. |
|
1117 |
* |
|
1118 |
* @param str a string to be written. |
|
1119 |
* @exception IOException if an I/O error occurs. |
|
1120 |
*/ |
|
1121 |
public final void writeUTF(String str) throws IOException { |
|
1122 |
DataOutputStream.writeUTF(str, this); |
|
1123 |
} |
|
1124 |
||
1125 |
private static native void initIDs(); |
|
1126 |
||
1127 |
private native void close0() throws IOException; |
|
1128 |
||
1129 |
static { |
|
1130 |
initIDs(); |
|
1131 |
} |
|
1132 |
} |