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