< prev index next >
src/java.base/share/classes/java/nio/file/Files.java
Print this page
rev 54801 : [mq]: 8223593-Refactor-code-for-reallocating-storage
@@ -75,10 +75,11 @@
import java.util.Spliterators;
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;
/**
* This class consists exclusively of static methods that operate on files,
@@ -3194,18 +3195,10 @@
try (InputStream in = newInputStream(source)) {
return in.transferTo(out);
}
}
- /**
- * 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();
/**
* Reads all the bytes from an input stream. Uses {@code initialSize} as a hint
@@ -3238,17 +3231,12 @@
// otherwise, try to read one more byte; if that failed we're done too
if (n < 0 || (n = source.read()) < 0)
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.newCapacity(capacity, 1, capacity),
+ BUFFER_SIZE);
buf = Arrays.copyOf(buf, capacity);
buf[nread++] = (byte)n;
}
return (capacity == nread) ? buf : Arrays.copyOf(buf, nread);
}
@@ -3281,11 +3269,11 @@
try (SeekableByteChannel sbc = Files.newByteChannel(path);
InputStream in = Channels.newInputStream(sbc)) {
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);
}
}
< prev index next >