26
26
package java .io ;
27
27
28
28
import java .nio .channels .FileChannel ;
29
+ import java .util .Objects ;
29
30
30
31
import jdk .internal .access .JavaIORandomAccessFileAccess ;
31
32
import jdk .internal .access .SharedSecrets ;
61
62
62
63
public class RandomAccessFile implements DataOutput , DataInput , Closeable {
63
64
64
- private FileDescriptor fd ;
65
- private volatile FileChannel channel ;
66
- private boolean rw ;
65
+ private static final int O_RDONLY = 1 ;
66
+ private static final int O_RDWR = 2 ;
67
+ private static final int O_SYNC = 4 ;
68
+ private static final int O_DSYNC = 8 ;
69
+ private static final int O_TEMPORARY = 16 ;
70
+
71
+ private final FileDescriptor fd ;
72
+
73
+ private final boolean rw ;
67
74
68
75
/**
69
76
* The path of the referenced file
@@ -73,14 +80,9 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable {
73
80
74
81
private final Object closeLock = new Object ();
75
82
83
+ private volatile FileChannel channel ;
76
84
private volatile boolean closed ;
77
85
78
- private static final int O_RDONLY = 1 ;
79
- private static final int O_RDWR = 2 ;
80
- private static final int O_SYNC = 4 ;
81
- private static final int O_DSYNC = 8 ;
82
- private static final int O_TEMPORARY = 16 ;
83
-
84
86
/**
85
87
* Creates a random access file stream to read from, and optionally
86
88
* to write to, a file with the specified name. A new
@@ -113,7 +115,7 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable {
113
115
* existing, writable regular file and a new regular file of
114
116
* that name cannot be created, or if some other error occurs
115
117
* while opening or creating the file
116
- * @throws SecurityException if a security manager exists and its
118
+ * @throws SecurityException if a security manager exists and its
117
119
* {@code checkRead} method denies read access to the file
118
120
* or the mode is {@code "rw"} and the security manager's
119
121
* {@code checkWrite} method denies write access to the file
@@ -219,6 +221,8 @@ private RandomAccessFile(File file, String mode, boolean openAndDelete)
219
221
{
220
222
String name = (file != null ? file .getPath () : null );
221
223
int imode = -1 ;
224
+
225
+ boolean rw = false ;
222
226
if (mode .equals ("r" ))
223
227
imode = O_RDONLY ;
224
228
else if (mode .startsWith ("rw" )) {
@@ -233,6 +237,8 @@ else if (mode.equals("rwd"))
233
237
imode = -1 ;
234
238
}
235
239
}
240
+ this .rw = rw ;
241
+
236
242
if (openAndDelete )
237
243
imode |= O_TEMPORARY ;
238
244
if (imode < 0 )
@@ -270,10 +276,7 @@ else if (mode.equals("rwd"))
270
276
* @see java.io.FileDescriptor
271
277
*/
272
278
public final FileDescriptor getFD () throws IOException {
273
- if (fd != null ) {
274
- return fd ;
275
- }
276
- throw new IOException ();
279
+ return fd ;
277
280
}
278
281
279
282
/**
@@ -1008,20 +1011,15 @@ public final String readLine() throws IOException {
1008
1011
1009
1012
while (!eol ) {
1010
1013
switch (c = read ()) {
1011
- case -1 :
1012
- case '\n' :
1013
- eol = true ;
1014
- break ;
1015
- case '\r' :
1016
- eol = true ;
1017
- long cur = getFilePointer ();
1018
- if ((read ()) != '\n' ) {
1019
- seek (cur );
1014
+ case -1 , '\n' -> eol = true ;
1015
+ case '\r' -> {
1016
+ eol = true ;
1017
+ long cur = getFilePointer ();
1018
+ if ((read ()) != '\n' ) {
1019
+ seek (cur );
1020
+ }
1020
1021
}
1021
- break ;
1022
- default :
1023
- input .append ((char )c );
1024
- break ;
1022
+ default -> input .append ((char ) c );
1025
1023
}
1026
1024
}
1027
1025
@@ -1245,7 +1243,7 @@ public final void writeUTF(String str) throws IOException {
1245
1243
SharedSecrets .setJavaIORandomAccessFileAccess (new JavaIORandomAccessFileAccess ()
1246
1244
{
1247
1245
// This is for j.u.z.ZipFile.OPEN_DELETE. The O_TEMPORARY flag
1248
- // is only implemented/supported on windows .
1246
+ // is only implemented/supported on Windows .
1249
1247
public RandomAccessFile openAndDelete (File file , String mode )
1250
1248
throws IOException
1251
1249
{
0 commit comments