Skip to content

Commit 81f57d5

Browse files
minborgRoger Riggs
authored and
Roger Riggs
committed
8298567: Make field in RandomAccessFile final
Reviewed-by: rriggs, chegar
1 parent 56c438b commit 81f57d5

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

src/java.base/share/classes/java/io/RandomAccessFile.java

+26-28
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
package java.io;
2727

2828
import java.nio.channels.FileChannel;
29+
import java.util.Objects;
2930

3031
import jdk.internal.access.JavaIORandomAccessFileAccess;
3132
import jdk.internal.access.SharedSecrets;
@@ -61,9 +62,15 @@
6162

6263
public class RandomAccessFile implements DataOutput, DataInput, Closeable {
6364

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;
6774

6875
/**
6976
* The path of the referenced file
@@ -73,14 +80,9 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable {
7380

7481
private final Object closeLock = new Object();
7582

83+
private volatile FileChannel channel;
7684
private volatile boolean closed;
7785

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-
8486
/**
8587
* Creates a random access file stream to read from, and optionally
8688
* to write to, a file with the specified name. A new
@@ -113,7 +115,7 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable {
113115
* existing, writable regular file and a new regular file of
114116
* that name cannot be created, or if some other error occurs
115117
* 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
117119
* {@code checkRead} method denies read access to the file
118120
* or the mode is {@code "rw"} and the security manager's
119121
* {@code checkWrite} method denies write access to the file
@@ -219,6 +221,8 @@ private RandomAccessFile(File file, String mode, boolean openAndDelete)
219221
{
220222
String name = (file != null ? file.getPath() : null);
221223
int imode = -1;
224+
225+
boolean rw = false;
222226
if (mode.equals("r"))
223227
imode = O_RDONLY;
224228
else if (mode.startsWith("rw")) {
@@ -233,6 +237,8 @@ else if (mode.equals("rwd"))
233237
imode = -1;
234238
}
235239
}
240+
this.rw = rw;
241+
236242
if (openAndDelete)
237243
imode |= O_TEMPORARY;
238244
if (imode < 0)
@@ -270,10 +276,7 @@ else if (mode.equals("rwd"))
270276
* @see java.io.FileDescriptor
271277
*/
272278
public final FileDescriptor getFD() throws IOException {
273-
if (fd != null) {
274-
return fd;
275-
}
276-
throw new IOException();
279+
return fd;
277280
}
278281

279282
/**
@@ -1008,20 +1011,15 @@ public final String readLine() throws IOException {
10081011

10091012
while (!eol) {
10101013
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+
}
10201021
}
1021-
break;
1022-
default:
1023-
input.append((char)c);
1024-
break;
1022+
default -> input.append((char) c);
10251023
}
10261024
}
10271025

@@ -1245,7 +1243,7 @@ public final void writeUTF(String str) throws IOException {
12451243
SharedSecrets.setJavaIORandomAccessFileAccess(new JavaIORandomAccessFileAccess()
12461244
{
12471245
// 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.
12491247
public RandomAccessFile openAndDelete(File file, String mode)
12501248
throws IOException
12511249
{

0 commit comments

Comments
 (0)