Skip to content

Commit 5b00058

Browse files
author
duke
committedMar 31, 2023
Automatic merge of jdk:master into master
2 parents 7b0722a + 8eb4e7e commit 5b00058

File tree

3 files changed

+59
-66
lines changed

3 files changed

+59
-66
lines changed
 

‎src/jdk.compiler/share/classes/com/sun/tools/javac/file/BaseFileManager.java

+20-41
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,12 @@
7575
* java.io.File or java.nio.file.Path.
7676
*/
7777
public abstract class BaseFileManager implements JavaFileManager {
78+
79+
private static final byte[] EMPTY_ARRAY = new byte[0];
80+
7881
@SuppressWarnings("this-escape")
7982
protected BaseFileManager(Charset charset) {
8083
this.charset = charset;
81-
byteBufferCache = new ByteBufferCache();
8284
locations = createLocations();
8385
}
8486

@@ -403,56 +405,33 @@ public CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErro
403405

404406
// <editor-fold defaultstate="collapsed" desc="ByteBuffers">
405407
/**
406-
* Make a byte buffer from an input stream.
408+
* Make a {@link ByteBuffer} from an input stream.
407409
* @param in the stream
408410
* @return a byte buffer containing the contents of the stream
409411
* @throws IOException if an error occurred while reading the stream
410412
*/
411-
public ByteBuffer makeByteBuffer(InputStream in)
412-
throws IOException {
413-
int limit = in.available();
414-
if (limit < 1024) limit = 1024;
415-
ByteBuffer result = byteBufferCache.get(limit);
416-
int position = 0;
417-
while (in.available() != 0) {
418-
if (position >= limit)
419-
// expand buffer
420-
result = ByteBuffer.
421-
allocate(limit <<= 1).
422-
put(result.flip());
423-
int count = in.read(result.array(),
424-
position,
425-
limit - position);
426-
if (count < 0) break;
427-
result.position(position += count);
413+
public ByteBuffer makeByteBuffer(InputStream in) throws IOException {
414+
byte[] array;
415+
synchronized (this) {
416+
if ((array = byteArrayCache) != null)
417+
byteArrayCache = null;
418+
else
419+
array = EMPTY_ARRAY;
428420
}
429-
return result.flip();
430-
}
431-
432-
public void recycleByteBuffer(ByteBuffer bb) {
433-
byteBufferCache.put(bb);
421+
com.sun.tools.javac.util.ByteBuffer buf = new com.sun.tools.javac.util.ByteBuffer(array);
422+
buf.appendStream(in);
423+
return buf.asByteBuffer();
434424
}
435425

436-
/**
437-
* A single-element cache of direct byte buffers.
438-
*/
439-
private static class ByteBufferCache {
440-
private ByteBuffer cached;
441-
ByteBuffer get(int capacity) {
442-
if (capacity < 20480) capacity = 20480;
443-
ByteBuffer result =
444-
(cached != null && cached.capacity() >= capacity)
445-
? cached.clear()
446-
: ByteBuffer.allocate(capacity);
447-
cached = null;
448-
return result;
449-
}
450-
void put(ByteBuffer x) {
451-
cached = x;
426+
public void recycleByteBuffer(ByteBuffer buf) {
427+
if (buf.hasArray()) {
428+
synchronized (this) {
429+
byteArrayCache = buf.array();
430+
}
452431
}
453432
}
454433

455-
private final ByteBufferCache byteBufferCache;
434+
private byte[] byteArrayCache;
456435
// </editor-fold>
457436

458437
// <editor-fold defaultstate="collapsed" desc="Content cache">

‎src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassReader.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -2741,7 +2741,9 @@ public void readClassFile(ClassSymbol c) {
27412741
try {
27422742
bp = 0;
27432743
buf.reset();
2744-
buf.appendStream(c.classfile.openInputStream());
2744+
try (InputStream input = c.classfile.openInputStream()) {
2745+
buf.appendStream(input);
2746+
}
27452747
readClassBuffer(c);
27462748
if (!missingTypeVariables.isEmpty() && !foundTypeVariables.isEmpty()) {
27472749
List<Type> missing = missingTypeVariables;

‎src/jdk.compiler/share/classes/com/sun/tools/javac/util/ByteBuffer.java

+36-24
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,13 @@ public ByteBuffer() {
5656
* of given size.
5757
*/
5858
public ByteBuffer(int initialSize) {
59-
elems = new byte[initialSize];
60-
length = 0;
59+
this(new byte[initialSize]);
60+
}
61+
62+
/** Create a new byte buffer using the given array for storage.
63+
*/
64+
public ByteBuffer(byte[] elems) {
65+
this.elems = elems;
6166
}
6267

6368
/** Append byte to this buffer.
@@ -147,30 +152,28 @@ public void appendName(Name name) {
147152
appendBytes(name.getByteArray(), name.getByteOffset(), name.getByteLength());
148153
}
149154

150-
/** Append the content of a given input stream, and then close the stream.
155+
/** Append the content of the given input stream.
151156
*/
152-
public void appendStream(InputStream is) throws IOException {
153-
try (InputStream input = is) {
154-
while (true) {
155-
156-
// Read another chunk of data, using size hint from available().
157-
// If available() is accurate, the array size should be just right.
158-
int amountToRead = Math.max(input.available(), 64);
159-
elems = ArrayUtils.ensureCapacity(elems, length + amountToRead);
160-
int amountRead = input.read(elems, length, amountToRead);
161-
if (amountRead == -1)
157+
public void appendStream(InputStream input) throws IOException {
158+
while (true) {
159+
160+
// Read another chunk of data, using size hint from available().
161+
// If available() is accurate, the array size should be just right.
162+
int amountToRead = Math.max(input.available(), 64);
163+
elems = ArrayUtils.ensureCapacity(elems, length + amountToRead);
164+
int amountRead = input.read(elems, length, amountToRead);
165+
if (amountRead == -1)
166+
break;
167+
length += amountRead;
168+
169+
// Check for the common case where input.available() returned the
170+
// entire remaining input; in that case, avoid an extra array extension.
171+
// Note we are guaranteed that elems.length >= length + 1 at this point.
172+
if (amountRead == amountToRead) {
173+
int byt = input.read();
174+
if (byt == -1)
162175
break;
163-
length += amountRead;
164-
165-
// Check for the common case where input.available() returned the
166-
// entire remaining input; in that case, avoid an extra array extension.
167-
// Note we are guaranteed that elems.length >= length + 1 at this point.
168-
if (amountRead == amountToRead) {
169-
int byt = input.read();
170-
if (byt == -1)
171-
break;
172-
elems[length++] = (byte)byt;
173-
}
176+
elems[length++] = (byte)byt;
174177
}
175178
}
176179
}
@@ -292,6 +295,15 @@ public void verifyRange(int off, int len) throws UnderflowException {
292295
throw new UnderflowException(length);
293296
}
294297

298+
/** Create a {@link java.nio.ByteBuffer} view of this instance.
299+
*
300+
* <p>
301+
* If this instance is modified, the returned buffer may no longer reflect it.
302+
*/
303+
public java.nio.ByteBuffer asByteBuffer() {
304+
return java.nio.ByteBuffer.wrap(elems, 0, length);
305+
}
306+
295307
// UnderflowException
296308

297309
/** Thrown when trying to read past the end of the buffer.

0 commit comments

Comments
 (0)
Please sign in to comment.