8223593: Refactor code for reallocating storage

Reviewed-by: prappo, plevart, rriggs, smarks
This commit is contained in:
Ivan Gerasimov 2019-05-21 18:40:29 -07:00
parent 54d0b2a8d6
commit 218204b1a3
11 changed files with 129 additions and 247 deletions

View file

@ -77,6 +77,7 @@ import java.util.function.BiPredicate;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import jdk.internal.util.ArraysSupport;
import sun.nio.ch.FileChannelImpl;
import sun.nio.fs.AbstractFileSystemProvider;
@ -3196,14 +3197,6 @@ public final class Files {
}
}
/**
* The maximum size of array to allocate.
* Some VMs reserve some header words in an array.
* Attempts to allocate larger arrays may result in
* OutOfMemoryError: Requested array size exceeds VM limit
*/
private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;
private static final jdk.internal.access.JavaLangAccess JLA =
jdk.internal.access.SharedSecrets.getJavaLangAccess();
@ -3240,13 +3233,10 @@ public final class Files {
break;
// one more byte was read; need to allocate a larger buffer
if (capacity <= MAX_BUFFER_SIZE - capacity) {
capacity = Math.max(capacity << 1, BUFFER_SIZE);
} else {
if (capacity == MAX_BUFFER_SIZE)
throw new OutOfMemoryError("Required array size too large");
capacity = MAX_BUFFER_SIZE;
}
capacity = Math.max(ArraysSupport.newLength(capacity,
1, /* minimum growth */
capacity /* preferred growth */),
BUFFER_SIZE);
buf = Arrays.copyOf(buf, capacity);
buf[nread++] = (byte)n;
}
@ -3283,7 +3273,7 @@ public final class Files {
if (sbc instanceof FileChannelImpl)
((FileChannelImpl) sbc).setUninterruptible();
long size = sbc.size();
if (size > (long) MAX_BUFFER_SIZE)
if (size > (long) Integer.MAX_VALUE)
throw new OutOfMemoryError("Required array size too large");
return read(in, (int)size);
}