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