mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 06:45:07 +02:00
8223593: Refactor code for reallocating storage
Reviewed-by: prappo, plevart, rriggs, smarks
This commit is contained in:
parent
54d0b2a8d6
commit
218204b1a3
11 changed files with 129 additions and 247 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 1994, 2016, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -26,6 +26,7 @@
|
|||
package java.io;
|
||||
|
||||
import jdk.internal.misc.Unsafe;
|
||||
import jdk.internal.util.ArraysSupport;
|
||||
|
||||
/**
|
||||
* A <code>BufferedInputStream</code> adds
|
||||
|
@ -53,14 +54,6 @@ class BufferedInputStream extends FilterInputStream {
|
|||
|
||||
private static int DEFAULT_BUFFER_SIZE = 8192;
|
||||
|
||||
/**
|
||||
* 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 int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;
|
||||
|
||||
/**
|
||||
* As this class is used early during bootstrap, it's motivated to use
|
||||
* Unsafe.compareAndSetObject instead of AtomicReferenceFieldUpdater
|
||||
|
@ -220,7 +213,7 @@ class BufferedInputStream extends FilterInputStream {
|
|||
byte[] buffer = getBufIfOpen();
|
||||
if (markpos < 0)
|
||||
pos = 0; /* no mark: throw away the buffer */
|
||||
else if (pos >= buffer.length) /* no room left in buffer */
|
||||
else if (pos >= buffer.length) { /* no room left in buffer */
|
||||
if (markpos > 0) { /* can throw away early part of the buffer */
|
||||
int sz = pos - markpos;
|
||||
System.arraycopy(buffer, markpos, buffer, 0, sz);
|
||||
|
@ -229,11 +222,10 @@ class BufferedInputStream extends FilterInputStream {
|
|||
} else if (buffer.length >= marklimit) {
|
||||
markpos = -1; /* buffer got too big, invalidate mark */
|
||||
pos = 0; /* drop buffer contents */
|
||||
} else if (buffer.length >= MAX_BUFFER_SIZE) {
|
||||
throw new OutOfMemoryError("Required array size too large");
|
||||
} else { /* grow buffer */
|
||||
int nsz = (pos <= MAX_BUFFER_SIZE - pos) ?
|
||||
pos * 2 : MAX_BUFFER_SIZE;
|
||||
int nsz = ArraysSupport.newLength(pos,
|
||||
1, /* minimum growth */
|
||||
pos /* preferred growth */);
|
||||
if (nsz > marklimit)
|
||||
nsz = marklimit;
|
||||
byte[] nbuf = new byte[nsz];
|
||||
|
@ -248,6 +240,7 @@ class BufferedInputStream extends FilterInputStream {
|
|||
}
|
||||
buffer = nbuf;
|
||||
}
|
||||
}
|
||||
count = pos;
|
||||
int n = getInIfOpen().read(buffer, pos, buffer.length - pos);
|
||||
if (n > 0)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue