8341666: FileInputStream doesn't support readAllBytes() or readNBytes(int) on pseudo devices

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2024-10-28 16:38:36 +00:00
parent 52382e285f
commit 1341b81321
7 changed files with 267 additions and 5 deletions

View file

@ -29,6 +29,7 @@ import java.nio.channels.FileChannel;
import java.util.Arrays;
import jdk.internal.util.ArraysSupport;
import jdk.internal.event.FileReadEvent;
import jdk.internal.vm.annotation.Stable;
import sun.nio.ch.FileChannelImpl;
/**
@ -81,6 +82,10 @@ public class FileInputStream extends InputStream
private volatile boolean closed;
// This field indicates whether the file is a regular file as some
// operations need the current position which requires seeking
private @Stable Boolean isRegularFile;
/**
* Creates a {@code FileInputStream} to read from an existing file
* named by the path name {@code name}.
@ -331,6 +336,9 @@ public class FileInputStream extends InputStream
@Override
public byte[] readAllBytes() throws IOException {
if (!isRegularFile())
return super.readAllBytes();
long length = length();
long position = position();
long size = length - position;
@ -382,6 +390,9 @@ public class FileInputStream extends InputStream
if (len == 0)
return new byte[0];
if (!isRegularFile())
return super.readNBytes(len);
long length = length();
long position = position();
long size = length - position;
@ -418,7 +429,7 @@ public class FileInputStream extends InputStream
@Override
public long transferTo(OutputStream out) throws IOException {
long transferred = 0L;
if (out instanceof FileOutputStream fos) {
if (out instanceof FileOutputStream fos && isRegularFile()) {
FileChannel fc = getChannel();
long pos = fc.position();
transferred = fc.transferTo(pos, Long.MAX_VALUE, fos.getChannel());
@ -471,7 +482,10 @@ public class FileInputStream extends InputStream
*/
@Override
public long skip(long n) throws IOException {
return skip0(n);
if (isRegularFile())
return skip0(n);
return super.skip(n);
}
private native long skip0(long n) throws IOException;
@ -603,6 +617,18 @@ public class FileInputStream extends InputStream
return fc;
}
/**
* Determine whether the file is a regular file.
*/
private boolean isRegularFile() {
Boolean isRegularFile = this.isRegularFile;
if (isRegularFile == null) {
this.isRegularFile = isRegularFile = isRegularFile0(fd);
}
return isRegularFile;
}
private native boolean isRegularFile0(FileDescriptor fd);
private static native void initIDs();
static {