Skip to content

Commit 1282539

Browse files
Brian Burkhalterpull[bot]
authored andcommitted
8324048: (fc) Make FileKey fields final
Reviewed-by: djelinski, alanb, jpai
1 parent 76830fa commit 1282539

File tree

4 files changed

+43
-56
lines changed

4 files changed

+43
-56
lines changed

src/java.base/unix/classes/sun/nio/ch/FileKey.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -33,15 +33,18 @@
3333
*/
3434
public class FileKey {
3535

36-
private long st_dev; // ID of device
37-
private long st_ino; // Inode number
36+
private final long st_dev; // ID of device
37+
private final long st_ino; // Inode number
3838

39-
private FileKey() { }
39+
private FileKey(long st_dev, long st_ino) {
40+
this.st_dev = st_dev;
41+
this.st_ino = st_ino;
42+
}
4043

4144
public static FileKey create(FileDescriptor fd) throws IOException {
42-
FileKey fk = new FileKey();
43-
fk.init(fd);
44-
return fk;
45+
long finfo[] = new long[2];
46+
init(fd, finfo);
47+
return new FileKey(finfo[0], finfo[1]);
4548
}
4649

4750
@Override
@@ -59,10 +62,10 @@ public boolean equals(Object obj) {
5962
&& (this.st_ino == other.st_ino);
6063
}
6164

62-
private native void init(FileDescriptor fd) throws IOException;
63-
private static native void initIDs();
65+
private static native void init(FileDescriptor fd, long[] finfo)
66+
throws IOException;
6467

6568
static {
66-
initIDs();
69+
IOUtil.load();
6770
}
6871
}

src/java.base/unix/native/libnio/ch/FileKey.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,21 @@
3030
#include "nio_util.h"
3131
#include "sun_nio_ch_FileKey.h"
3232

33-
static jfieldID key_st_dev; /* id for FileKey.st_dev */
34-
static jfieldID key_st_ino; /* id for FileKey.st_ino */
35-
36-
37-
JNIEXPORT void JNICALL
38-
Java_sun_nio_ch_FileKey_initIDs(JNIEnv *env, jclass clazz)
39-
{
40-
CHECK_NULL(key_st_dev = (*env)->GetFieldID(env, clazz, "st_dev", "J"));
41-
CHECK_NULL(key_st_ino = (*env)->GetFieldID(env, clazz, "st_ino", "J"));
42-
}
43-
44-
4533
JNIEXPORT void JNICALL
46-
Java_sun_nio_ch_FileKey_init(JNIEnv *env, jobject this, jobject fdo)
34+
Java_sun_nio_ch_FileKey_init(JNIEnv* env, jclass clazz, jobject fdo,
35+
jlongArray finfo)
4736
{
4837
struct stat fbuf;
4938
int res;
39+
jlong deviceAndInode[2];
5040

51-
RESTARTABLE(fstat(fdval(env, fdo), &fbuf), res);
41+
int fd = fdval(env, fdo);
42+
RESTARTABLE(fstat(fd, &fbuf), res);
5243
if (res < 0) {
5344
JNU_ThrowIOExceptionWithLastError(env, "fstat failed");
5445
} else {
55-
(*env)->SetLongField(env, this, key_st_dev, (jlong)fbuf.st_dev);
56-
(*env)->SetLongField(env, this, key_st_ino, (jlong)fbuf.st_ino);
46+
deviceAndInode[0] = (jlong)fbuf.st_dev;
47+
deviceAndInode[1] = (jlong)fbuf.st_ino;
48+
(*env)->SetLongArrayRegion(env, finfo, 0, 2, deviceAndInode);
5749
}
5850
}

src/java.base/windows/classes/sun/nio/ch/FileKey.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,21 @@
3333
*/
3434
public class FileKey {
3535

36-
private int dwVolumeSerialNumber;
37-
private int nFileIndexHigh;
38-
private int nFileIndexLow;
36+
private final int dwVolumeSerialNumber;
37+
private final int nFileIndexHigh;
38+
private final int nFileIndexLow;
3939

40-
private FileKey() { }
40+
private FileKey(int dwVolumeSerialNumber, int nFileIndexHigh,
41+
int nFileIndexLow) {
42+
this.dwVolumeSerialNumber = dwVolumeSerialNumber;
43+
this.nFileIndexHigh = nFileIndexHigh;
44+
this.nFileIndexLow = nFileIndexLow;
45+
}
4146

4247
public static FileKey create(FileDescriptor fd) throws IOException {
43-
FileKey fk = new FileKey();
44-
fk.init(fd);
45-
return fk;
48+
int finfo[] = new int[3];
49+
init(fd, finfo);
50+
return new FileKey(finfo[0], finfo[1], finfo[2]);
4651
}
4752

4853
@Override
@@ -60,11 +65,10 @@ public boolean equals(Object obj) {
6065
&& this.nFileIndexLow == other.nFileIndexLow;
6166
}
6267

63-
private native void init(FileDescriptor fd) throws IOException;
64-
private static native void initIDs();
68+
private static native void init(FileDescriptor fd, int[] finfo)
69+
throws IOException;
6570

6671
static {
6772
IOUtil.load();
68-
initIDs();
6973
}
7074
}

src/java.base/windows/native/libnio/ch/FileKey.c

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -30,32 +30,20 @@
3030
#include "nio_util.h"
3131
#include "sun_nio_ch_FileKey.h"
3232

33-
static jfieldID key_volumeSN; /* id for FileKey.dwVolumeSerialNumber */
34-
static jfieldID key_indexHigh; /* id for FileKey.nFileIndexHigh */
35-
static jfieldID key_indexLow; /* id for FileKey.nFileIndexLow */
36-
37-
38-
JNIEXPORT void JNICALL
39-
Java_sun_nio_ch_FileKey_initIDs(JNIEnv *env, jclass clazz)
40-
{
41-
CHECK_NULL(key_volumeSN = (*env)->GetFieldID(env, clazz, "dwVolumeSerialNumber", "I"));
42-
CHECK_NULL(key_indexHigh = (*env)->GetFieldID(env, clazz, "nFileIndexHigh", "I"));
43-
CHECK_NULL(key_indexLow = (*env)->GetFieldID(env, clazz, "nFileIndexLow", "I"));
44-
}
45-
46-
4733
JNIEXPORT void JNICALL
48-
Java_sun_nio_ch_FileKey_init(JNIEnv *env, jobject this, jobject fdo)
34+
Java_sun_nio_ch_FileKey_init(JNIEnv *env, jclass clazz, jobject fdo, jintArray finfo)
4935
{
50-
HANDLE fileHandle = (HANDLE)(handleval(env, fdo));
36+
HANDLE fileHandle = (HANDLE)handleval(env, fdo);
5137
BOOL result;
5238
BY_HANDLE_FILE_INFORMATION fileInfo;
39+
jint info[3];
5340

5441
result = GetFileInformationByHandle(fileHandle, &fileInfo);
5542
if (result) {
56-
(*env)->SetIntField(env, this, key_volumeSN, fileInfo.dwVolumeSerialNumber);
57-
(*env)->SetIntField(env, this, key_indexHigh, fileInfo.nFileIndexHigh);
58-
(*env)->SetIntField(env, this, key_indexLow, fileInfo.nFileIndexLow);
43+
info[0] = (jint)fileInfo.dwVolumeSerialNumber;
44+
info[1] = (jint)fileInfo.nFileIndexHigh;
45+
info[2] = (jint)fileInfo.nFileIndexLow;
46+
(*env)->SetIntArrayRegion(env, finfo, 0, 3, info);
5947
} else {
6048
JNU_ThrowIOExceptionWithLastError(env, "GetFileInformationByHandle failed");
6149
}

0 commit comments

Comments
 (0)