From 23dc3b02468836f4c9b4303f2c7c0a7305461ce1 Mon Sep 17 00:00:00 2001 From: Brian Burkhalter Date: Fri, 23 Aug 2024 16:32:14 +0000 Subject: [PATCH] 8324048: (fc) Make FileKey fields final Reviewed-by: djelinski, alanb, jpai --- .../unix/classes/sun/nio/ch/FileKey.java | 23 ++++++++------- src/java.base/unix/native/libnio/ch/FileKey.c | 24 ++++++---------- .../windows/classes/sun/nio/ch/FileKey.java | 24 +++++++++------- .../windows/native/libnio/ch/FileKey.c | 28 ++++++------------- 4 files changed, 43 insertions(+), 56 deletions(-) diff --git a/src/java.base/unix/classes/sun/nio/ch/FileKey.java b/src/java.base/unix/classes/sun/nio/ch/FileKey.java index e60e63f073d..119abe4a3b5 100644 --- a/src/java.base/unix/classes/sun/nio/ch/FileKey.java +++ b/src/java.base/unix/classes/sun/nio/ch/FileKey.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -33,15 +33,18 @@ import java.io.IOException; */ public class FileKey { - private long st_dev; // ID of device - private long st_ino; // Inode number + private final long st_dev; // ID of device + private final long st_ino; // Inode number - private FileKey() { } + private FileKey(long st_dev, long st_ino) { + this.st_dev = st_dev; + this.st_ino = st_ino; + } public static FileKey create(FileDescriptor fd) throws IOException { - FileKey fk = new FileKey(); - fk.init(fd); - return fk; + long finfo[] = new long[2]; + init(fd, finfo); + return new FileKey(finfo[0], finfo[1]); } @Override @@ -59,10 +62,10 @@ public class FileKey { && (this.st_ino == other.st_ino); } - private native void init(FileDescriptor fd) throws IOException; - private static native void initIDs(); + private static native void init(FileDescriptor fd, long[] finfo) + throws IOException; static { - initIDs(); + IOUtil.load(); } } diff --git a/src/java.base/unix/native/libnio/ch/FileKey.c b/src/java.base/unix/native/libnio/ch/FileKey.c index c3817003b5b..59a866bcaff 100644 --- a/src/java.base/unix/native/libnio/ch/FileKey.c +++ b/src/java.base/unix/native/libnio/ch/FileKey.c @@ -30,29 +30,21 @@ #include "nio_util.h" #include "sun_nio_ch_FileKey.h" -static jfieldID key_st_dev; /* id for FileKey.st_dev */ -static jfieldID key_st_ino; /* id for FileKey.st_ino */ - - JNIEXPORT void JNICALL -Java_sun_nio_ch_FileKey_initIDs(JNIEnv *env, jclass clazz) -{ - CHECK_NULL(key_st_dev = (*env)->GetFieldID(env, clazz, "st_dev", "J")); - CHECK_NULL(key_st_ino = (*env)->GetFieldID(env, clazz, "st_ino", "J")); -} - - -JNIEXPORT void JNICALL -Java_sun_nio_ch_FileKey_init(JNIEnv *env, jobject this, jobject fdo) +Java_sun_nio_ch_FileKey_init(JNIEnv* env, jclass clazz, jobject fdo, + jlongArray finfo) { struct stat fbuf; int res; + jlong deviceAndInode[2]; - RESTARTABLE(fstat(fdval(env, fdo), &fbuf), res); + int fd = fdval(env, fdo); + RESTARTABLE(fstat(fd, &fbuf), res); if (res < 0) { JNU_ThrowIOExceptionWithLastError(env, "fstat failed"); } else { - (*env)->SetLongField(env, this, key_st_dev, (jlong)fbuf.st_dev); - (*env)->SetLongField(env, this, key_st_ino, (jlong)fbuf.st_ino); + deviceAndInode[0] = (jlong)fbuf.st_dev; + deviceAndInode[1] = (jlong)fbuf.st_ino; + (*env)->SetLongArrayRegion(env, finfo, 0, 2, deviceAndInode); } } diff --git a/src/java.base/windows/classes/sun/nio/ch/FileKey.java b/src/java.base/windows/classes/sun/nio/ch/FileKey.java index 8669517f0c5..a900c382e68 100644 --- a/src/java.base/windows/classes/sun/nio/ch/FileKey.java +++ b/src/java.base/windows/classes/sun/nio/ch/FileKey.java @@ -33,16 +33,21 @@ import java.io.IOException; */ public class FileKey { - private int dwVolumeSerialNumber; - private int nFileIndexHigh; - private int nFileIndexLow; + private final int dwVolumeSerialNumber; + private final int nFileIndexHigh; + private final int nFileIndexLow; - private FileKey() { } + private FileKey(int dwVolumeSerialNumber, int nFileIndexHigh, + int nFileIndexLow) { + this.dwVolumeSerialNumber = dwVolumeSerialNumber; + this.nFileIndexHigh = nFileIndexHigh; + this.nFileIndexLow = nFileIndexLow; + } public static FileKey create(FileDescriptor fd) throws IOException { - FileKey fk = new FileKey(); - fk.init(fd); - return fk; + int finfo[] = new int[3]; + init(fd, finfo); + return new FileKey(finfo[0], finfo[1], finfo[2]); } @Override @@ -60,11 +65,10 @@ public class FileKey { && this.nFileIndexLow == other.nFileIndexLow; } - private native void init(FileDescriptor fd) throws IOException; - private static native void initIDs(); + private static native void init(FileDescriptor fd, int[] finfo) + throws IOException; static { IOUtil.load(); - initIDs(); } } diff --git a/src/java.base/windows/native/libnio/ch/FileKey.c b/src/java.base/windows/native/libnio/ch/FileKey.c index 9cd3459aef8..e552ed6b7ae 100644 --- a/src/java.base/windows/native/libnio/ch/FileKey.c +++ b/src/java.base/windows/native/libnio/ch/FileKey.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -30,32 +30,20 @@ #include "nio_util.h" #include "sun_nio_ch_FileKey.h" -static jfieldID key_volumeSN; /* id for FileKey.dwVolumeSerialNumber */ -static jfieldID key_indexHigh; /* id for FileKey.nFileIndexHigh */ -static jfieldID key_indexLow; /* id for FileKey.nFileIndexLow */ - - JNIEXPORT void JNICALL -Java_sun_nio_ch_FileKey_initIDs(JNIEnv *env, jclass clazz) +Java_sun_nio_ch_FileKey_init(JNIEnv *env, jclass clazz, jobject fdo, jintArray finfo) { - CHECK_NULL(key_volumeSN = (*env)->GetFieldID(env, clazz, "dwVolumeSerialNumber", "I")); - CHECK_NULL(key_indexHigh = (*env)->GetFieldID(env, clazz, "nFileIndexHigh", "I")); - CHECK_NULL(key_indexLow = (*env)->GetFieldID(env, clazz, "nFileIndexLow", "I")); -} - - -JNIEXPORT void JNICALL -Java_sun_nio_ch_FileKey_init(JNIEnv *env, jobject this, jobject fdo) -{ - HANDLE fileHandle = (HANDLE)(handleval(env, fdo)); + HANDLE fileHandle = (HANDLE)handleval(env, fdo); BOOL result; BY_HANDLE_FILE_INFORMATION fileInfo; + jint info[3]; result = GetFileInformationByHandle(fileHandle, &fileInfo); if (result) { - (*env)->SetIntField(env, this, key_volumeSN, fileInfo.dwVolumeSerialNumber); - (*env)->SetIntField(env, this, key_indexHigh, fileInfo.nFileIndexHigh); - (*env)->SetIntField(env, this, key_indexLow, fileInfo.nFileIndexLow); + info[0] = (jint)fileInfo.dwVolumeSerialNumber; + info[1] = (jint)fileInfo.nFileIndexHigh; + info[2] = (jint)fileInfo.nFileIndexLow; + (*env)->SetIntArrayRegion(env, finfo, 0, 3, info); } else { JNU_ThrowIOExceptionWithLastError(env, "GetFileInformationByHandle failed"); }