8268435: (ch) ChannelInputStream could override readAllBytes

Reviewed-by: alanb, lancea
This commit is contained in:
Brian Burkhalter 2021-10-01 20:15:44 +00:00
parent bb4500d0d2
commit 0786d8b7b3
3 changed files with 412 additions and 11 deletions

View file

@ -339,7 +339,7 @@ public class FileInputStream extends InputStream
int n;
do {
n = read(buf, nread, remaining);
if (n > 0 ) {
if (n > 0) {
nread += n;
remaining -= n;
} else if (n == 0) {

View file

@ -25,11 +25,18 @@
package sun.nio.ch;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.channels.spi.*;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.IllegalBlockingModeException;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.SeekableByteChannel;
import java.nio.channels.SelectableChannel;
import java.util.Arrays;
import java.util.Objects;
import jdk.internal.util.ArraysSupport;
/**
* This class is defined here rather than in java.nio.channels.Channels
@ -43,13 +50,13 @@ import java.util.Objects;
public class ChannelInputStream
extends InputStream
{
private static final int DEFAULT_BUFFER_SIZE = 8192;
public static int read(ReadableByteChannel ch, ByteBuffer bb,
boolean block)
throws IOException
{
if (ch instanceof SelectableChannel) {
SelectableChannel sc = (SelectableChannel)ch;
if (ch instanceof SelectableChannel sc) {
synchronized (sc.blockingLock()) {
boolean bm = sc.isBlocking();
if (!bm)
@ -107,10 +114,95 @@ public class ChannelInputStream
return ChannelInputStream.read(ch, bb, true);
}
@Override
public byte[] readAllBytes() throws IOException {
if (!(ch instanceof SeekableByteChannel sbc))
return super.readAllBytes();
long length = sbc.size();
long position = sbc.position();
long size = length - position;
if (length <= 0 || size <= 0)
return super.readAllBytes();
if (size > (long) Integer.MAX_VALUE) {
String msg =
String.format("Required array size too large: %d = %d - %d",
size, length, position);
throw new OutOfMemoryError(msg);
}
int capacity = (int)size;
byte[] buf = new byte[capacity];
int nread = 0;
int n;
for (;;) {
// read to EOF which may read more or less than initial size, e.g.,
// file is truncated while we are reading
while ((n = read(buf, nread, capacity - nread)) > 0)
nread += n;
// if last call to read() returned -1, we are done; otherwise,
// try to read one more byte and if that fails we're done too
if (n < 0 || (n = read()) < 0)
break;
// one more byte was read; need to allocate a larger buffer
capacity = Math.max(ArraysSupport.newLength(capacity,
1, // min growth
capacity), // pref growth
DEFAULT_BUFFER_SIZE);
buf = Arrays.copyOf(buf, capacity);
buf[nread++] = (byte)n;
}
return (capacity == nread) ? buf : Arrays.copyOf(buf, nread);
}
@Override
public byte[] readNBytes(int len) throws IOException {
if (len < 0)
throw new IllegalArgumentException("len < 0");
if (len == 0)
return new byte[0];
if (!(ch instanceof SeekableByteChannel sbc))
return super.readAllBytes();
long length = sbc.size();
long position = sbc.position();
long size = length - position;
if (length <= 0 || size <= 0)
return super.readNBytes(len);
int capacity = (int)Math.min(len, size);
byte[] buf = new byte[capacity];
int remaining = capacity;
int nread = 0;
int n;
do {
n = read(buf, nread, remaining);
if (n > 0) {
nread += n;
remaining -= n;
} else if (n == 0) {
// Block until a byte is read or EOF is detected
byte b = (byte)read();
if (b == -1 )
break;
buf[nread++] = b;
remaining--;
}
} while (n >= 0 && remaining > 0);
return (capacity == nread) ? buf : Arrays.copyOf(buf, nread);
}
public int available() throws IOException {
// special case where the channel is to a file
if (ch instanceof SeekableByteChannel) {
SeekableByteChannel sbc = (SeekableByteChannel)ch;
if (ch instanceof SeekableByteChannel sbc) {
long rem = Math.max(0, sbc.size() - sbc.position());
return (rem > Integer.MAX_VALUE) ? Integer.MAX_VALUE : (int)rem;
}
@ -119,8 +211,7 @@ public class ChannelInputStream
public synchronized long skip(long n) throws IOException {
// special case where the channel is to a file
if (ch instanceof SeekableByteChannel) {
SeekableByteChannel sbc = (SeekableByteChannel)ch;
if (ch instanceof SeekableByteChannel sbc) {
long pos = sbc.position();
long newPos;
if (n > 0) {