author | dxu |
Mon, 06 May 2013 14:17:59 -0700 | |
changeset 17430 | c445531b8f6b |
parent 11676 | 7e75ec031b97 |
child 18786 | 52a2658627c2 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
17430
c445531b8f6b
8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents:
11676
diff
changeset
|
2 |
* Copyright (c) 1994, 2013, 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 |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
44 |
* {@code getFilePointer} method and set by the {@code seek} |
2 | 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 |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
49 |
* read, an {@code EOFException} (which is a kind of |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
50 |
* {@code IOException}) is thrown. If any byte cannot be read for |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
51 |
* any reason other than end-of-file, an {@code IOException} other |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
52 |
* than {@code EOFException} is thrown. In particular, an |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
53 |
* {@code IOException} may be thrown if the stream has been closed. |
2 | 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> |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
85 |
* If there is a security manager, its {@code checkRead} method |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
86 |
* is called with the {@code name} argument |
2 | 87 |
* as its argument to see if read access to the file is allowed. |
88 |
* If the mode allows writing, the security manager's |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
89 |
* {@code checkWrite} method |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
90 |
* is also called with the {@code name} argument |
2 | 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 |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
106 |
* {@code checkRead} method denies read access to the file |
2 | 107 |
* or the mode is "rw" and the security manager's |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
108 |
* {@code checkWrite} method denies write access to the file |
2 | 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 |
* |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
167 |
* <p> If there is a security manager, its {@code checkRead} method is |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
168 |
* called with the pathname of the {@code file} argument as its |
2 | 169 |
* argument to see if read access to the file is allowed. If the mode |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
170 |
* allows writing, the security manager's {@code checkWrite} method is |
2 | 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 |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
188 |
* {@code checkRead} method denies read access to the file |
2 | 189 |
* or the mode is "rw" and the security manager's |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
190 |
* {@code checkWrite} method denies write access to the file |
2 | 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 |
} |
|
17430
c445531b8f6b
8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents:
11676
diff
changeset
|
231 |
if (file.isInvalid()) { |
c445531b8f6b
8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents:
11676
diff
changeset
|
232 |
throw new FileNotFoundException("Invalid file path"); |
c445531b8f6b
8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents:
11676
diff
changeset
|
233 |
} |
2 | 234 |
fd = new FileDescriptor(); |
11017
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
10586
diff
changeset
|
235 |
fd.attach(this); |
2 | 236 |
open(name, imode); |
237 |
} |
|
238 |
||
239 |
/** |
|
240 |
* Returns the opaque file descriptor object associated with this |
|
241 |
* stream. </p> |
|
242 |
* |
|
243 |
* @return the file descriptor object associated with this stream. |
|
244 |
* @exception IOException if an I/O error occurs. |
|
245 |
* @see java.io.FileDescriptor |
|
246 |
*/ |
|
247 |
public final FileDescriptor getFD() throws IOException { |
|
11017
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
10586
diff
changeset
|
248 |
if (fd != null) { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
10586
diff
changeset
|
249 |
return fd; |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
10586
diff
changeset
|
250 |
} |
2 | 251 |
throw new IOException(); |
252 |
} |
|
253 |
||
254 |
/** |
|
255 |
* Returns the unique {@link java.nio.channels.FileChannel FileChannel} |
|
256 |
* object associated with this file. |
|
257 |
* |
|
258 |
* <p> The {@link java.nio.channels.FileChannel#position() |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
259 |
* position} of the returned channel will always be equal to |
2 | 260 |
* this object's file-pointer offset as returned by the {@link |
261 |
* #getFilePointer getFilePointer} method. Changing this object's |
|
262 |
* file-pointer offset, whether explicitly or by reading or writing bytes, |
|
263 |
* will change the position of the channel, and vice versa. Changing the |
|
264 |
* file's length via this object will change the length seen via the file |
|
265 |
* channel, and vice versa. |
|
266 |
* |
|
267 |
* @return the file channel associated with this file |
|
268 |
* |
|
269 |
* @since 1.4 |
|
270 |
* @spec JSR-51 |
|
271 |
*/ |
|
272 |
public final FileChannel getChannel() { |
|
273 |
synchronized (this) { |
|
274 |
if (channel == null) { |
|
275 |
channel = FileChannelImpl.open(fd, true, rw, this); |
|
276 |
} |
|
277 |
return channel; |
|
278 |
} |
|
279 |
} |
|
280 |
||
281 |
/** |
|
282 |
* Opens a file and returns the file descriptor. The file is |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
283 |
* opened in read-write mode if the O_RDWR bit in {@code mode} |
2 | 284 |
* is true, else the file is opened as read-only. |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
285 |
* If the {@code name} refers to a directory, an IOException |
2 | 286 |
* is thrown. |
287 |
* |
|
288 |
* @param name the name of the file |
|
289 |
* @param mode the mode flags, a combination of the O_ constants |
|
290 |
* defined above |
|
291 |
*/ |
|
292 |
private native void open(String name, int mode) |
|
293 |
throws FileNotFoundException; |
|
294 |
||
295 |
// 'Read' primitives |
|
296 |
||
297 |
/** |
|
298 |
* Reads a byte of data from this file. The byte is returned as an |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
299 |
* integer in the range 0 to 255 ({@code 0x00-0x0ff}). This |
2 | 300 |
* method blocks if no input is yet available. |
301 |
* <p> |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
302 |
* Although {@code RandomAccessFile} is not a subclass of |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
303 |
* {@code InputStream}, this method behaves in exactly the same |
2 | 304 |
* way as the {@link InputStream#read()} method of |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
305 |
* {@code InputStream}. |
2 | 306 |
* |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
307 |
* @return the next byte of data, or {@code -1} if the end of the |
2 | 308 |
* file has been reached. |
309 |
* @exception IOException if an I/O error occurs. Not thrown if |
|
310 |
* end-of-file has been reached. |
|
311 |
*/ |
|
312 |
public native int read() throws IOException; |
|
313 |
||
314 |
/** |
|
315 |
* Reads a sub array as a sequence of bytes. |
|
316 |
* @param b the buffer into which the data is read. |
|
317 |
* @param off the start offset of the data. |
|
318 |
* @param len the number of bytes to read. |
|
319 |
* @exception IOException If an I/O error has occurred. |
|
320 |
*/ |
|
321 |
private native int readBytes(byte b[], int off, int len) throws IOException; |
|
322 |
||
323 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
324 |
* Reads up to {@code len} bytes of data from this file into an |
2 | 325 |
* array of bytes. This method blocks until at least one byte of input |
326 |
* is available. |
|
327 |
* <p> |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
328 |
* Although {@code RandomAccessFile} is not a subclass of |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
329 |
* {@code InputStream}, this method behaves in exactly the |
2 | 330 |
* same way as the {@link InputStream#read(byte[], int, int)} method of |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
331 |
* {@code InputStream}. |
2 | 332 |
* |
333 |
* @param b the buffer into which the data is read. |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
334 |
* @param off the start offset in array {@code b} |
2 | 335 |
* at which the data is written. |
336 |
* @param len the maximum number of bytes read. |
|
337 |
* @return the total number of bytes read into the buffer, or |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
338 |
* {@code -1} if there is no more data because the end of |
2 | 339 |
* the file has been reached. |
340 |
* @exception IOException If the first byte cannot be read for any reason |
|
341 |
* other than end of file, or if the random access file has been closed, or if |
|
342 |
* some other I/O error occurs. |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
343 |
* @exception NullPointerException If {@code b} is {@code null}. |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
344 |
* @exception IndexOutOfBoundsException If {@code off} is negative, |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
345 |
* {@code len} is negative, or {@code len} is greater than |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
346 |
* {@code b.length - off} |
2 | 347 |
*/ |
348 |
public int read(byte b[], int off, int len) throws IOException { |
|
349 |
return readBytes(b, off, len); |
|
350 |
} |
|
351 |
||
352 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
353 |
* Reads up to {@code b.length} bytes of data from this file |
2 | 354 |
* into an array of bytes. This method blocks until at least one byte |
355 |
* of input is available. |
|
356 |
* <p> |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
357 |
* Although {@code RandomAccessFile} is not a subclass of |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
358 |
* {@code InputStream}, this method behaves in exactly the |
2 | 359 |
* same way as the {@link InputStream#read(byte[])} method of |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
360 |
* {@code InputStream}. |
2 | 361 |
* |
362 |
* @param b the buffer into which the data is read. |
|
363 |
* @return the total number of bytes read into the buffer, or |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
364 |
* {@code -1} if there is no more data because the end of |
2 | 365 |
* this file has been reached. |
366 |
* @exception IOException If the first byte cannot be read for any reason |
|
367 |
* other than end of file, or if the random access file has been closed, or if |
|
368 |
* some other I/O error occurs. |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
369 |
* @exception NullPointerException If {@code b} is {@code null}. |
2 | 370 |
*/ |
371 |
public int read(byte b[]) throws IOException { |
|
372 |
return readBytes(b, 0, b.length); |
|
373 |
} |
|
374 |
||
375 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
376 |
* Reads {@code b.length} bytes from this file into the byte |
2 | 377 |
* array, starting at the current file pointer. This method reads |
378 |
* repeatedly from the file until the requested number of bytes are |
|
379 |
* read. This method blocks until the requested number of bytes are |
|
380 |
* read, the end of the stream is detected, or an exception is thrown. |
|
381 |
* |
|
382 |
* @param b the buffer into which the data is read. |
|
383 |
* @exception EOFException if this file reaches the end before reading |
|
384 |
* all the bytes. |
|
385 |
* @exception IOException if an I/O error occurs. |
|
386 |
*/ |
|
387 |
public final void readFully(byte b[]) throws IOException { |
|
388 |
readFully(b, 0, b.length); |
|
389 |
} |
|
390 |
||
391 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
392 |
* Reads exactly {@code len} bytes from this file into the byte |
2 | 393 |
* array, starting at the current file pointer. This method reads |
394 |
* repeatedly from the file until the requested number of bytes are |
|
395 |
* read. This method blocks until the requested number of bytes are |
|
396 |
* read, the end of the stream is detected, or an exception is thrown. |
|
397 |
* |
|
398 |
* @param b the buffer into which the data is read. |
|
399 |
* @param off the start offset of the data. |
|
400 |
* @param len the number of bytes to read. |
|
401 |
* @exception EOFException if this file reaches the end before reading |
|
402 |
* all the bytes. |
|
403 |
* @exception IOException if an I/O error occurs. |
|
404 |
*/ |
|
405 |
public final void readFully(byte b[], int off, int len) throws IOException { |
|
406 |
int n = 0; |
|
407 |
do { |
|
408 |
int count = this.read(b, off + n, len - n); |
|
409 |
if (count < 0) |
|
410 |
throw new EOFException(); |
|
411 |
n += count; |
|
412 |
} while (n < len); |
|
413 |
} |
|
414 |
||
415 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
416 |
* Attempts to skip over {@code n} bytes of input discarding the |
2 | 417 |
* skipped bytes. |
418 |
* <p> |
|
419 |
* |
|
420 |
* This method may skip over some smaller number of bytes, possibly zero. |
|
421 |
* This may result from any of a number of conditions; reaching end of |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
422 |
* file before {@code n} bytes have been skipped is only one |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
423 |
* possibility. This method never throws an {@code EOFException}. |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
424 |
* The actual number of bytes skipped is returned. If {@code n} |
2 | 425 |
* is negative, no bytes are skipped. |
426 |
* |
|
427 |
* @param n the number of bytes to be skipped. |
|
428 |
* @return the actual number of bytes skipped. |
|
429 |
* @exception IOException if an I/O error occurs. |
|
430 |
*/ |
|
431 |
public int skipBytes(int n) throws IOException { |
|
432 |
long pos; |
|
433 |
long len; |
|
434 |
long newpos; |
|
435 |
||
436 |
if (n <= 0) { |
|
437 |
return 0; |
|
438 |
} |
|
439 |
pos = getFilePointer(); |
|
440 |
len = length(); |
|
441 |
newpos = pos + n; |
|
442 |
if (newpos > len) { |
|
443 |
newpos = len; |
|
444 |
} |
|
445 |
seek(newpos); |
|
446 |
||
447 |
/* return the actual number of bytes skipped */ |
|
448 |
return (int) (newpos - pos); |
|
449 |
} |
|
450 |
||
451 |
// 'Write' primitives |
|
452 |
||
453 |
/** |
|
454 |
* Writes the specified byte to this file. The write starts at |
|
455 |
* the current file pointer. |
|
456 |
* |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
457 |
* @param b the {@code byte} to be written. |
2 | 458 |
* @exception IOException if an I/O error occurs. |
459 |
*/ |
|
460 |
public native void write(int b) throws IOException; |
|
461 |
||
462 |
/** |
|
463 |
* Writes a sub array as a sequence of bytes. |
|
464 |
* @param b the data to be written |
|
465 |
||
466 |
* @param off the start offset in the data |
|
467 |
* @param len the number of bytes that are written |
|
468 |
* @exception IOException If an I/O error has occurred. |
|
469 |
*/ |
|
470 |
private native void writeBytes(byte b[], int off, int len) throws IOException; |
|
471 |
||
472 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
473 |
* Writes {@code b.length} bytes from the specified byte array |
2 | 474 |
* to this file, starting at the current file pointer. |
475 |
* |
|
476 |
* @param b the data. |
|
477 |
* @exception IOException if an I/O error occurs. |
|
478 |
*/ |
|
479 |
public void write(byte b[]) throws IOException { |
|
480 |
writeBytes(b, 0, b.length); |
|
481 |
} |
|
482 |
||
483 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
484 |
* Writes {@code len} bytes from the specified byte array |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
485 |
* starting at offset {@code off} to this file. |
2 | 486 |
* |
487 |
* @param b the data. |
|
488 |
* @param off the start offset in the data. |
|
489 |
* @param len the number of bytes to write. |
|
490 |
* @exception IOException if an I/O error occurs. |
|
491 |
*/ |
|
492 |
public void write(byte b[], int off, int len) throws IOException { |
|
493 |
writeBytes(b, off, len); |
|
494 |
} |
|
495 |
||
496 |
// 'Random access' stuff |
|
497 |
||
498 |
/** |
|
499 |
* Returns the current offset in this file. |
|
500 |
* |
|
501 |
* @return the offset from the beginning of the file, in bytes, |
|
502 |
* at which the next read or write occurs. |
|
503 |
* @exception IOException if an I/O error occurs. |
|
504 |
*/ |
|
505 |
public native long getFilePointer() throws IOException; |
|
506 |
||
507 |
/** |
|
508 |
* Sets the file-pointer offset, measured from the beginning of this |
|
509 |
* file, at which the next read or write occurs. The offset may be |
|
510 |
* set beyond the end of the file. Setting the offset beyond the end |
|
511 |
* of the file does not change the file length. The file length will |
|
512 |
* change only by writing after the offset has been set beyond the end |
|
513 |
* of the file. |
|
514 |
* |
|
515 |
* @param pos the offset position, measured in bytes from the |
|
516 |
* beginning of the file, at which to set the file |
|
517 |
* pointer. |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
518 |
* @exception IOException if {@code pos} is less than |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
519 |
* {@code 0} or if an I/O error occurs. |
2 | 520 |
*/ |
521 |
public native void seek(long pos) throws IOException; |
|
522 |
||
523 |
/** |
|
524 |
* Returns the length of this file. |
|
525 |
* |
|
526 |
* @return the length of this file, measured in bytes. |
|
527 |
* @exception IOException if an I/O error occurs. |
|
528 |
*/ |
|
529 |
public native long length() throws IOException; |
|
530 |
||
531 |
/** |
|
532 |
* Sets the length of this file. |
|
533 |
* |
|
534 |
* <p> If the present length of the file as returned by the |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
535 |
* {@code length} method is greater than the {@code newLength} |
2 | 536 |
* argument then the file will be truncated. In this case, if the file |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
537 |
* offset as returned by the {@code getFilePointer} method is greater |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
538 |
* than {@code newLength} then after this method returns the offset |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
539 |
* will be equal to {@code newLength}. |
2 | 540 |
* |
541 |
* <p> If the present length of the file as returned by the |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
542 |
* {@code length} method is smaller than the {@code newLength} |
2 | 543 |
* argument then the file will be extended. In this case, the contents of |
544 |
* the extended portion of the file are not defined. |
|
545 |
* |
|
546 |
* @param newLength The desired length of the file |
|
547 |
* @exception IOException If an I/O error occurs |
|
548 |
* @since 1.2 |
|
549 |
*/ |
|
550 |
public native void setLength(long newLength) throws IOException; |
|
551 |
||
552 |
/** |
|
553 |
* Closes this random access file stream and releases any system |
|
554 |
* resources associated with the stream. A closed random access |
|
555 |
* file cannot perform input or output operations and cannot be |
|
556 |
* reopened. |
|
557 |
* |
|
558 |
* <p> If this file has an associated channel then the channel is closed |
|
559 |
* as well. |
|
560 |
* |
|
561 |
* @exception IOException if an I/O error occurs. |
|
562 |
* |
|
563 |
* @revised 1.4 |
|
564 |
* @spec JSR-51 |
|
565 |
*/ |
|
566 |
public void close() throws IOException { |
|
567 |
synchronized (closeLock) { |
|
568 |
if (closed) { |
|
569 |
return; |
|
570 |
} |
|
571 |
closed = true; |
|
572 |
} |
|
573 |
if (channel != null) { |
|
574 |
channel.close(); |
|
575 |
} |
|
576 |
||
11017
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
10586
diff
changeset
|
577 |
fd.closeAll(new Closeable() { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
10586
diff
changeset
|
578 |
public void close() throws IOException { |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
10586
diff
changeset
|
579 |
close0(); |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
10586
diff
changeset
|
580 |
} |
353f81426721
7105952: Improve finalisation for FileInputStream/FileOutputStream/RandomAccessFile
coffeys
parents:
10586
diff
changeset
|
581 |
}); |
2 | 582 |
} |
583 |
||
584 |
// |
|
585 |
// Some "reading/writing Java data types" methods stolen from |
|
586 |
// DataInputStream and DataOutputStream. |
|
587 |
// |
|
588 |
||
589 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
590 |
* Reads a {@code boolean} from this file. This method reads a |
2 | 591 |
* single byte from the file, starting at the current file pointer. |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
592 |
* A value of {@code 0} represents |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
593 |
* {@code false}. Any other value represents {@code true}. |
2 | 594 |
* This method blocks until the byte is read, the end of the stream |
595 |
* is detected, or an exception is thrown. |
|
596 |
* |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
597 |
* @return the {@code boolean} value read. |
2 | 598 |
* @exception EOFException if this file has reached the end. |
599 |
* @exception IOException if an I/O error occurs. |
|
600 |
*/ |
|
601 |
public final boolean readBoolean() throws IOException { |
|
602 |
int ch = this.read(); |
|
603 |
if (ch < 0) |
|
604 |
throw new EOFException(); |
|
605 |
return (ch != 0); |
|
606 |
} |
|
607 |
||
608 |
/** |
|
609 |
* Reads a signed eight-bit value from this file. This method reads a |
|
610 |
* byte from the file, starting from the current file pointer. |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
611 |
* If the byte read is {@code b}, where |
2 | 612 |
* <code>0 <= b <= 255</code>, |
613 |
* then the result is: |
|
614 |
* <blockquote><pre> |
|
615 |
* (byte)(b) |
|
616 |
* </pre></blockquote> |
|
617 |
* <p> |
|
618 |
* This method blocks until the byte is read, the end of the stream |
|
619 |
* is detected, or an exception is thrown. |
|
620 |
* |
|
621 |
* @return the next byte of this file as a signed eight-bit |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
622 |
* {@code byte}. |
2 | 623 |
* @exception EOFException if this file has reached the end. |
624 |
* @exception IOException if an I/O error occurs. |
|
625 |
*/ |
|
626 |
public final byte readByte() throws IOException { |
|
627 |
int ch = this.read(); |
|
628 |
if (ch < 0) |
|
629 |
throw new EOFException(); |
|
630 |
return (byte)(ch); |
|
631 |
} |
|
632 |
||
633 |
/** |
|
634 |
* Reads an unsigned eight-bit number from this file. This method reads |
|
635 |
* a byte from this file, starting at the current file pointer, |
|
636 |
* and returns that byte. |
|
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, interpreted as an unsigned |
|
642 |
* eight-bit number. |
|
643 |
* @exception EOFException if this file has reached the end. |
|
644 |
* @exception IOException if an I/O error occurs. |
|
645 |
*/ |
|
646 |
public final int readUnsignedByte() throws IOException { |
|
647 |
int ch = this.read(); |
|
648 |
if (ch < 0) |
|
649 |
throw new EOFException(); |
|
650 |
return ch; |
|
651 |
} |
|
652 |
||
653 |
/** |
|
654 |
* Reads a signed 16-bit number from this file. The method reads two |
|
655 |
* bytes from this file, starting at the current file pointer. |
|
656 |
* If the two bytes read, in order, are |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
657 |
* {@code b1} and {@code b2}, where each of the two values is |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
658 |
* between {@code 0} and {@code 255}, inclusive, then the |
2 | 659 |
* result is equal to: |
660 |
* <blockquote><pre> |
|
661 |
* (short)((b1 << 8) | b2) |
|
662 |
* </pre></blockquote> |
|
663 |
* <p> |
|
664 |
* This method blocks until the two bytes are read, the end of the |
|
665 |
* stream is detected, or an exception is thrown. |
|
666 |
* |
|
667 |
* @return the next two bytes of this file, interpreted as a signed |
|
668 |
* 16-bit number. |
|
669 |
* @exception EOFException if this file reaches the end before reading |
|
670 |
* two bytes. |
|
671 |
* @exception IOException if an I/O error occurs. |
|
672 |
*/ |
|
673 |
public final short readShort() throws IOException { |
|
674 |
int ch1 = this.read(); |
|
675 |
int ch2 = this.read(); |
|
676 |
if ((ch1 | ch2) < 0) |
|
677 |
throw new EOFException(); |
|
678 |
return (short)((ch1 << 8) + (ch2 << 0)); |
|
679 |
} |
|
680 |
||
681 |
/** |
|
682 |
* Reads an unsigned 16-bit number from this file. This method reads |
|
683 |
* two bytes from the file, starting at the current file pointer. |
|
684 |
* If the bytes read, in order, are |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
685 |
* {@code b1} and {@code b2}, where |
2 | 686 |
* <code>0 <= b1, b2 <= 255</code>, |
687 |
* then the result is equal to: |
|
688 |
* <blockquote><pre> |
|
689 |
* (b1 << 8) | b2 |
|
690 |
* </pre></blockquote> |
|
691 |
* <p> |
|
692 |
* This method blocks until the two bytes are read, the end of the |
|
693 |
* stream is detected, or an exception is thrown. |
|
694 |
* |
|
695 |
* @return the next two bytes of this file, interpreted as an unsigned |
|
696 |
* 16-bit integer. |
|
697 |
* @exception EOFException if this file reaches the end before reading |
|
698 |
* two bytes. |
|
699 |
* @exception IOException if an I/O error occurs. |
|
700 |
*/ |
|
701 |
public final int readUnsignedShort() throws IOException { |
|
702 |
int ch1 = this.read(); |
|
703 |
int ch2 = this.read(); |
|
704 |
if ((ch1 | ch2) < 0) |
|
705 |
throw new EOFException(); |
|
706 |
return (ch1 << 8) + (ch2 << 0); |
|
707 |
} |
|
708 |
||
709 |
/** |
|
710 |
* Reads a character from this file. This method reads two |
|
711 |
* bytes from the file, starting at the current file pointer. |
|
712 |
* If the bytes read, in order, are |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
713 |
* {@code b1} and {@code b2}, where |
2 | 714 |
* <code>0 <= b1, b2 <= 255</code>, |
715 |
* then the result is equal to: |
|
716 |
* <blockquote><pre> |
|
717 |
* (char)((b1 << 8) | b2) |
|
718 |
* </pre></blockquote> |
|
719 |
* <p> |
|
720 |
* This method blocks until the two bytes are read, the end of the |
|
721 |
* stream is detected, or an exception is thrown. |
|
722 |
* |
|
723 |
* @return the next two bytes of this file, interpreted as a |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
724 |
* {@code char}. |
2 | 725 |
* @exception EOFException if this file reaches the end before reading |
726 |
* two bytes. |
|
727 |
* @exception IOException if an I/O error occurs. |
|
728 |
*/ |
|
729 |
public final char readChar() throws IOException { |
|
730 |
int ch1 = this.read(); |
|
731 |
int ch2 = this.read(); |
|
732 |
if ((ch1 | ch2) < 0) |
|
733 |
throw new EOFException(); |
|
734 |
return (char)((ch1 << 8) + (ch2 << 0)); |
|
735 |
} |
|
736 |
||
737 |
/** |
|
738 |
* Reads a signed 32-bit integer from this file. This method reads 4 |
|
739 |
* bytes from the file, starting at the current file pointer. |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
740 |
* If the bytes read, in order, are {@code b1}, |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
741 |
* {@code b2}, {@code b3}, and {@code b4}, where |
2 | 742 |
* <code>0 <= b1, b2, b3, b4 <= 255</code>, |
743 |
* then the result is equal to: |
|
744 |
* <blockquote><pre> |
|
745 |
* (b1 << 24) | (b2 << 16) + (b3 << 8) + b4 |
|
746 |
* </pre></blockquote> |
|
747 |
* <p> |
|
748 |
* This method blocks until the four bytes are read, the end of the |
|
749 |
* stream is detected, or an exception is thrown. |
|
750 |
* |
|
751 |
* @return the next four bytes of this file, interpreted as an |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
752 |
* {@code int}. |
2 | 753 |
* @exception EOFException if this file reaches the end before reading |
754 |
* four bytes. |
|
755 |
* @exception IOException if an I/O error occurs. |
|
756 |
*/ |
|
757 |
public final int readInt() throws IOException { |
|
758 |
int ch1 = this.read(); |
|
759 |
int ch2 = this.read(); |
|
760 |
int ch3 = this.read(); |
|
761 |
int ch4 = this.read(); |
|
762 |
if ((ch1 | ch2 | ch3 | ch4) < 0) |
|
763 |
throw new EOFException(); |
|
764 |
return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); |
|
765 |
} |
|
766 |
||
767 |
/** |
|
768 |
* Reads a signed 64-bit integer from this file. This method reads eight |
|
769 |
* bytes from the file, starting at the current file pointer. |
|
770 |
* If the bytes read, in order, are |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
771 |
* {@code b1}, {@code b2}, {@code b3}, |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
772 |
* {@code b4}, {@code b5}, {@code b6}, |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
773 |
* {@code b7}, and {@code b8,} where: |
2 | 774 |
* <blockquote><pre> |
775 |
* 0 <= b1, b2, b3, b4, b5, b6, b7, b8 <=255, |
|
776 |
* </pre></blockquote> |
|
777 |
* <p> |
|
778 |
* then the result is equal to: |
|
779 |
* <p><blockquote><pre> |
|
780 |
* ((long)b1 << 56) + ((long)b2 << 48) |
|
781 |
* + ((long)b3 << 40) + ((long)b4 << 32) |
|
782 |
* + ((long)b5 << 24) + ((long)b6 << 16) |
|
783 |
* + ((long)b7 << 8) + b8 |
|
784 |
* </pre></blockquote> |
|
785 |
* <p> |
|
786 |
* This method blocks until the eight bytes are read, the end of the |
|
787 |
* stream is detected, or an exception is thrown. |
|
788 |
* |
|
789 |
* @return the next eight bytes of this file, interpreted as a |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
790 |
* {@code long}. |
2 | 791 |
* @exception EOFException if this file reaches the end before reading |
792 |
* eight bytes. |
|
793 |
* @exception IOException if an I/O error occurs. |
|
794 |
*/ |
|
795 |
public final long readLong() throws IOException { |
|
796 |
return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL); |
|
797 |
} |
|
798 |
||
799 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
800 |
* Reads a {@code float} from this file. This method reads an |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
801 |
* {@code int} value, starting at the current file pointer, |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
802 |
* as if by the {@code readInt} method |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
803 |
* and then converts that {@code int} to a {@code float} |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
804 |
* using the {@code intBitsToFloat} method in class |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
805 |
* {@code Float}. |
2 | 806 |
* <p> |
807 |
* This method blocks until the four bytes are read, the end of the |
|
808 |
* stream is detected, or an exception is thrown. |
|
809 |
* |
|
810 |
* @return the next four bytes of this file, interpreted as a |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
811 |
* {@code float}. |
2 | 812 |
* @exception EOFException if this file reaches the end before reading |
813 |
* four bytes. |
|
814 |
* @exception IOException if an I/O error occurs. |
|
815 |
* @see java.io.RandomAccessFile#readInt() |
|
816 |
* @see java.lang.Float#intBitsToFloat(int) |
|
817 |
*/ |
|
818 |
public final float readFloat() throws IOException { |
|
819 |
return Float.intBitsToFloat(readInt()); |
|
820 |
} |
|
821 |
||
822 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
823 |
* Reads a {@code double} from this file. This method reads a |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
824 |
* {@code long} value, starting at the current file pointer, |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
825 |
* as if by the {@code readLong} method |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
826 |
* and then converts that {@code long} to a {@code double} |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
827 |
* using the {@code longBitsToDouble} method in |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
828 |
* class {@code Double}. |
2 | 829 |
* <p> |
830 |
* This method blocks until the eight bytes are read, the end of the |
|
831 |
* stream is detected, or an exception is thrown. |
|
832 |
* |
|
833 |
* @return the next eight bytes of this file, interpreted as a |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
834 |
* {@code double}. |
2 | 835 |
* @exception EOFException if this file reaches the end before reading |
836 |
* eight bytes. |
|
837 |
* @exception IOException if an I/O error occurs. |
|
838 |
* @see java.io.RandomAccessFile#readLong() |
|
839 |
* @see java.lang.Double#longBitsToDouble(long) |
|
840 |
*/ |
|
841 |
public final double readDouble() throws IOException { |
|
842 |
return Double.longBitsToDouble(readLong()); |
|
843 |
} |
|
844 |
||
845 |
/** |
|
846 |
* Reads the next line of text from this file. This method successively |
|
847 |
* reads bytes from the file, starting at the current file pointer, |
|
848 |
* until it reaches a line terminator or the end |
|
849 |
* of the file. Each byte is converted into a character by taking the |
|
850 |
* byte's value for the lower eight bits of the character and setting the |
|
851 |
* high eight bits of the character to zero. This method does not, |
|
852 |
* therefore, support the full Unicode character set. |
|
853 |
* |
|
854 |
* <p> A line of text is terminated by a carriage-return character |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
855 |
* ({@code '\u005Cr'}), a newline character ({@code '\u005Cn'}), a |
2 | 856 |
* carriage-return character immediately followed by a newline character, |
857 |
* or the end of the file. Line-terminating characters are discarded and |
|
858 |
* are not included as part of the string returned. |
|
859 |
* |
|
860 |
* <p> This method blocks until a newline character is read, a carriage |
|
861 |
* return and the byte following it are read (to see if it is a newline), |
|
862 |
* the end of the file is reached, or an exception is thrown. |
|
863 |
* |
|
864 |
* @return the next line of text from this file, or null if end |
|
865 |
* of file is encountered before even one byte is read. |
|
866 |
* @exception IOException if an I/O error occurs. |
|
867 |
*/ |
|
868 |
||
869 |
public final String readLine() throws IOException { |
|
870 |
StringBuffer input = new StringBuffer(); |
|
871 |
int c = -1; |
|
872 |
boolean eol = false; |
|
873 |
||
874 |
while (!eol) { |
|
875 |
switch (c = read()) { |
|
876 |
case -1: |
|
877 |
case '\n': |
|
878 |
eol = true; |
|
879 |
break; |
|
880 |
case '\r': |
|
881 |
eol = true; |
|
882 |
long cur = getFilePointer(); |
|
883 |
if ((read()) != '\n') { |
|
884 |
seek(cur); |
|
885 |
} |
|
886 |
break; |
|
887 |
default: |
|
888 |
input.append((char)c); |
|
889 |
break; |
|
890 |
} |
|
891 |
} |
|
892 |
||
893 |
if ((c == -1) && (input.length() == 0)) { |
|
894 |
return null; |
|
895 |
} |
|
896 |
return input.toString(); |
|
897 |
} |
|
898 |
||
899 |
/** |
|
900 |
* Reads in a string from this file. The string has been encoded |
|
901 |
* using a |
|
902 |
* <a href="DataInput.html#modified-utf-8">modified UTF-8</a> |
|
903 |
* format. |
|
904 |
* <p> |
|
905 |
* The first two bytes are read, starting from the current file |
|
906 |
* pointer, as if by |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
907 |
* {@code readUnsignedShort}. This value gives the number of |
2 | 908 |
* following bytes that are in the encoded string, not |
909 |
* the length of the resulting string. The following bytes are then |
|
910 |
* interpreted as bytes encoding characters in the modified UTF-8 format |
|
911 |
* and are converted into characters. |
|
912 |
* <p> |
|
913 |
* This method blocks until all the bytes are read, the end of the |
|
914 |
* stream is detected, or an exception is thrown. |
|
915 |
* |
|
916 |
* @return a Unicode string. |
|
917 |
* @exception EOFException if this file reaches the end before |
|
918 |
* reading all the bytes. |
|
919 |
* @exception IOException if an I/O error occurs. |
|
920 |
* @exception UTFDataFormatException if the bytes do not represent |
|
921 |
* valid modified UTF-8 encoding of a Unicode string. |
|
922 |
* @see java.io.RandomAccessFile#readUnsignedShort() |
|
923 |
*/ |
|
924 |
public final String readUTF() throws IOException { |
|
925 |
return DataInputStream.readUTF(this); |
|
926 |
} |
|
927 |
||
928 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
929 |
* Writes a {@code boolean} to the file as a one-byte value. The |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
930 |
* value {@code true} is written out as the value |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
931 |
* {@code (byte)1}; the value {@code false} is written out |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
932 |
* as the value {@code (byte)0}. The write starts at |
2 | 933 |
* the current position of the file pointer. |
934 |
* |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
935 |
* @param v a {@code boolean} value to be written. |
2 | 936 |
* @exception IOException if an I/O error occurs. |
937 |
*/ |
|
938 |
public final void writeBoolean(boolean v) throws IOException { |
|
939 |
write(v ? 1 : 0); |
|
940 |
//written++; |
|
941 |
} |
|
942 |
||
943 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
944 |
* Writes a {@code byte} to the file as a one-byte value. The |
2 | 945 |
* write starts at the current position of the file pointer. |
946 |
* |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
947 |
* @param v a {@code byte} value to be written. |
2 | 948 |
* @exception IOException if an I/O error occurs. |
949 |
*/ |
|
950 |
public final void writeByte(int v) throws IOException { |
|
951 |
write(v); |
|
952 |
//written++; |
|
953 |
} |
|
954 |
||
955 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
956 |
* Writes a {@code short} to the file as two bytes, high byte first. |
2 | 957 |
* The write starts at the current position of the file pointer. |
958 |
* |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
959 |
* @param v a {@code short} to be written. |
2 | 960 |
* @exception IOException if an I/O error occurs. |
961 |
*/ |
|
962 |
public final void writeShort(int v) throws IOException { |
|
963 |
write((v >>> 8) & 0xFF); |
|
964 |
write((v >>> 0) & 0xFF); |
|
965 |
//written += 2; |
|
966 |
} |
|
967 |
||
968 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
969 |
* Writes a {@code char} to the file as a two-byte value, high |
2 | 970 |
* byte first. The write starts at the current position of the |
971 |
* file pointer. |
|
972 |
* |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
973 |
* @param v a {@code char} value to be written. |
2 | 974 |
* @exception IOException if an I/O error occurs. |
975 |
*/ |
|
976 |
public final void writeChar(int v) throws IOException { |
|
977 |
write((v >>> 8) & 0xFF); |
|
978 |
write((v >>> 0) & 0xFF); |
|
979 |
//written += 2; |
|
980 |
} |
|
981 |
||
982 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
983 |
* Writes an {@code int} to the file as four bytes, high byte first. |
2 | 984 |
* The write starts at the current position of the file pointer. |
985 |
* |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
986 |
* @param v an {@code int} to be written. |
2 | 987 |
* @exception IOException if an I/O error occurs. |
988 |
*/ |
|
989 |
public final void writeInt(int v) throws IOException { |
|
990 |
write((v >>> 24) & 0xFF); |
|
991 |
write((v >>> 16) & 0xFF); |
|
992 |
write((v >>> 8) & 0xFF); |
|
993 |
write((v >>> 0) & 0xFF); |
|
994 |
//written += 4; |
|
995 |
} |
|
996 |
||
997 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
998 |
* Writes a {@code long} to the file as eight bytes, high byte first. |
2 | 999 |
* The write starts at the current position of the file pointer. |
1000 |
* |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
1001 |
* @param v a {@code long} to be written. |
2 | 1002 |
* @exception IOException if an I/O error occurs. |
1003 |
*/ |
|
1004 |
public final void writeLong(long v) throws IOException { |
|
1005 |
write((int)(v >>> 56) & 0xFF); |
|
1006 |
write((int)(v >>> 48) & 0xFF); |
|
1007 |
write((int)(v >>> 40) & 0xFF); |
|
1008 |
write((int)(v >>> 32) & 0xFF); |
|
1009 |
write((int)(v >>> 24) & 0xFF); |
|
1010 |
write((int)(v >>> 16) & 0xFF); |
|
1011 |
write((int)(v >>> 8) & 0xFF); |
|
1012 |
write((int)(v >>> 0) & 0xFF); |
|
1013 |
//written += 8; |
|
1014 |
} |
|
1015 |
||
1016 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
1017 |
* Converts the float argument to an {@code int} using the |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
1018 |
* {@code floatToIntBits} method in class {@code Float}, |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
1019 |
* and then writes that {@code int} value to the file as a |
2 | 1020 |
* four-byte quantity, high byte first. The write starts at the |
1021 |
* current position of the file pointer. |
|
1022 |
* |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
1023 |
* @param v a {@code float} value to be written. |
2 | 1024 |
* @exception IOException if an I/O error occurs. |
1025 |
* @see java.lang.Float#floatToIntBits(float) |
|
1026 |
*/ |
|
1027 |
public final void writeFloat(float v) throws IOException { |
|
1028 |
writeInt(Float.floatToIntBits(v)); |
|
1029 |
} |
|
1030 |
||
1031 |
/** |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
1032 |
* Converts the double argument to a {@code long} using the |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
1033 |
* {@code doubleToLongBits} method in class {@code Double}, |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
1034 |
* and then writes that {@code long} value to the file as an |
2 | 1035 |
* eight-byte quantity, high byte first. The write starts at the current |
1036 |
* position of the file pointer. |
|
1037 |
* |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
1038 |
* @param v a {@code double} value to be written. |
2 | 1039 |
* @exception IOException if an I/O error occurs. |
1040 |
* @see java.lang.Double#doubleToLongBits(double) |
|
1041 |
*/ |
|
1042 |
public final void writeDouble(double v) throws IOException { |
|
1043 |
writeLong(Double.doubleToLongBits(v)); |
|
1044 |
} |
|
1045 |
||
1046 |
/** |
|
1047 |
* Writes the string to the file as a sequence of bytes. Each |
|
1048 |
* character in the string is written out, in sequence, by discarding |
|
1049 |
* its high eight bits. The write starts at the current position of |
|
1050 |
* the file pointer. |
|
1051 |
* |
|
1052 |
* @param s a string of bytes to be written. |
|
1053 |
* @exception IOException if an I/O error occurs. |
|
1054 |
*/ |
|
11121 | 1055 |
@SuppressWarnings("deprecation") |
2 | 1056 |
public final void writeBytes(String s) throws IOException { |
1057 |
int len = s.length(); |
|
1058 |
byte[] b = new byte[len]; |
|
1059 |
s.getBytes(0, len, b, 0); |
|
1060 |
writeBytes(b, 0, len); |
|
1061 |
} |
|
1062 |
||
1063 |
/** |
|
1064 |
* Writes a string to the file as a sequence of characters. Each |
|
1065 |
* character is written to the data output stream as if by the |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
1066 |
* {@code writeChar} method. The write starts at the current |
2 | 1067 |
* position of the file pointer. |
1068 |
* |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
1069 |
* @param s a {@code String} value to be written. |
2 | 1070 |
* @exception IOException if an I/O error occurs. |
1071 |
* @see java.io.RandomAccessFile#writeChar(int) |
|
1072 |
*/ |
|
1073 |
public final void writeChars(String s) throws IOException { |
|
1074 |
int clen = s.length(); |
|
1075 |
int blen = 2*clen; |
|
1076 |
byte[] b = new byte[blen]; |
|
1077 |
char[] c = new char[clen]; |
|
1078 |
s.getChars(0, clen, c, 0); |
|
1079 |
for (int i = 0, j = 0; i < clen; i++) { |
|
1080 |
b[j++] = (byte)(c[i] >>> 8); |
|
1081 |
b[j++] = (byte)(c[i] >>> 0); |
|
1082 |
} |
|
1083 |
writeBytes(b, 0, blen); |
|
1084 |
} |
|
1085 |
||
1086 |
/** |
|
1087 |
* Writes a string to the file using |
|
1088 |
* <a href="DataInput.html#modified-utf-8">modified UTF-8</a> |
|
1089 |
* encoding in a machine-independent manner. |
|
1090 |
* <p> |
|
1091 |
* First, two bytes are written to the file, starting at the |
|
1092 |
* current file pointer, as if by the |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11121
diff
changeset
|
1093 |
* {@code writeShort} method giving the number of bytes to |
2 | 1094 |
* follow. This value is the number of bytes actually written out, |
1095 |
* not the length of the string. Following the length, each character |
|
1096 |
* of the string is output, in sequence, using the modified UTF-8 encoding |
|
1097 |
* for each character. |
|
1098 |
* |
|
1099 |
* @param str a string to be written. |
|
1100 |
* @exception IOException if an I/O error occurs. |
|
1101 |
*/ |
|
1102 |
public final void writeUTF(String str) throws IOException { |
|
1103 |
DataOutputStream.writeUTF(str, this); |
|
1104 |
} |
|
1105 |
||
1106 |
private static native void initIDs(); |
|
1107 |
||
1108 |
private native void close0() throws IOException; |
|
1109 |
||
1110 |
static { |
|
1111 |
initIDs(); |
|
1112 |
} |
|
1113 |
} |