8377910: Minor cleanup of java/io/FileDescriptor/Sharing.java

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2026-02-17 18:07:04 +00:00
parent 63f00fff92
commit 7489f75dbd

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2026, 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
@ -28,7 +28,14 @@
* @run main/othervm Sharing
*/
import java.io.*;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.Writer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.concurrent.CountDownLatch;
@ -71,7 +78,7 @@ public class Sharing {
// encourage gc
System.gc();
// read from fis2 - when fis1 is gc'ed and finalizer is run, read will fail
System.out.print(".");
System.err.print(".");
ret = fis2.read();
}
}
@ -93,7 +100,7 @@ public class Sharing {
* read from fis3 - when raf is gc'ed and finalizer is run,
* fd should still be valid.
*/
System.out.print(".");
System.err.print(".");
ret = fis3.read();
}
} finally {
@ -290,7 +297,7 @@ public class Sharing {
FileInputStream fis = new FileInputStream(raf.getFD());
fis.close();
if (raf.getFD().valid()) {
throw new RuntimeException("FD should not be valid.");
throw new RuntimeException("FD should not be valid.");
}
// Test the suppressed exception handling - FileInputStream
@ -308,7 +315,7 @@ public class Sharing {
ioe.printStackTrace();
if (ioe.getSuppressed().length != 2) {
throw new RuntimeException("[FIS]Incorrect number of suppressed " +
"exceptions received : " + ioe.getSuppressed().length);
"exceptions received : " + ioe.getSuppressed().length);
}
}
if (raf.getFD().valid()) {
@ -332,7 +339,7 @@ public class Sharing {
ioe.printStackTrace();
if (ioe.getSuppressed().length != 2) {
throw new RuntimeException("[FOS]Incorrect number of suppressed " +
"exceptions received : " + ioe.getSuppressed().length);
"exceptions received : " + ioe.getSuppressed().length);
}
}
if (raf.getFD().valid()) {
@ -347,10 +354,8 @@ public class Sharing {
* FileOutputStreams referencing the same native file descriptor.
*/
private static class OpenClose extends Thread {
private FileDescriptor fd = null;
private CountDownLatch done;
FileInputStream[] fisArray = new FileInputStream[numFiles];
FileOutputStream[] fosArray = new FileOutputStream[numFiles];
private final FileDescriptor fd;
private final CountDownLatch done;
OpenClose(FileDescriptor filedescriptor, CountDownLatch done) {
this.fd = filedescriptor;
@ -358,29 +363,32 @@ public class Sharing {
}
public void run() {
try {
for(int i=0;i<numFiles;i++) {
fisArray[i] = new FileInputStream(fd);
fosArray[i] = new FileOutputStream(fd);
}
FileInputStream[] fisArray = new FileInputStream[numFiles];
FileOutputStream[] fosArray = new FileOutputStream[numFiles];
// Now close out
for(int i=0;i<numFiles;i++) {
if(fisArray[i] != null) fisArray[i].close();
if(fosArray[i] != null) fosArray[i].close();
}
try {
for(int i=0;i<numFiles;i++) {
fisArray[i] = new FileInputStream(fd);
fosArray[i] = new FileOutputStream(fd);
}
} catch(IOException ioe) {
System.out.println("OpenClose encountered IO issue :" + ioe);
fail = true;
} finally {
if (fd.valid()) { // fd should not be valid after first close() call
System.out.println("OpenClose: FileDescriptor shouldn't be valid");
fail = true;
}
done.countDown();
}
}
// Now close out
for(int i=0;i<numFiles;i++) {
fisArray[i].close();
fosArray[i].close();
}
} catch(IOException ioe) {
System.err.println("OpenClose encountered IO issue :" + ioe);
fail = true;
} finally {
if (fd.valid()) { // fd should not be valid after first close() call
System.err.println("OpenClose: FileDescriptor shouldn't be valid");
fail = true;
}
done.countDown();
}
}
}
private static class BadFileInputStream extends FileInputStream {