Skip to content
Permalink
Browse files
8268435: (ch) ChannelInputStream could override readAllBytes
Reviewed-by: alanb, lancea
  • Loading branch information
Brian Burkhalter committed Oct 1, 2021
1 parent bb4500d commit 0786d8b7b367e3aa3ffa54a3e339572938378dca
Showing 3 changed files with 412 additions and 11 deletions.
@@ -339,7 +339,7 @@ public byte[] readNBytes(int len) throws IOException {
int n;
do {
n = read(buf, nread, remaining);
if (n > 0 ) {
if (n > 0) {
nread += n;
remaining -= n;
} else if (n == 0) {
@@ -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 @@
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 @@ protected int read(ByteBuffer bb)
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 int available() throws IOException {

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) {

1 comment on commit 0786d8b

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 0786d8b Oct 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.