Skip to content

Commit d29d4d4

Browse files
committed
Upgrade to ASM master
Closes gh-27023
1 parent 432fdad commit d29d4d4

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

spring-core/src/main/java/org/springframework/asm/ClassReader.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ public class ClassReader {
8888
*/
8989
static final int EXPAND_ASM_INSNS = 256;
9090

91+
/** The maximum size of array to allocate. */
92+
private static final int MAX_BUFFER_SIZE = 1024 * 1024;
93+
9194
/** The size of the temporary byte array used to read class input streams chunk by chunk. */
9295
private static final int INPUT_STREAM_DATA_CHUNK_SIZE = 4096;
9396

@@ -310,13 +313,19 @@ private static byte[] readStream(final InputStream inputStream, final boolean cl
310313
if (inputStream == null) {
311314
throw new IOException("Class not found");
312315
}
316+
int bufferSize = calculateBufferSize(inputStream);
313317
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
314-
byte[] data = new byte[INPUT_STREAM_DATA_CHUNK_SIZE];
318+
byte[] data = new byte[bufferSize];
315319
int bytesRead;
316-
while ((bytesRead = inputStream.read(data, 0, data.length)) != -1) {
320+
int readCount = 0;
321+
while ((bytesRead = inputStream.read(data, 0, bufferSize)) != -1) {
317322
outputStream.write(data, 0, bytesRead);
323+
readCount++;
318324
}
319325
outputStream.flush();
326+
if (readCount == 1) {
327+
return data;
328+
}
320329
return outputStream.toByteArray();
321330
} finally {
322331
if (close) {
@@ -325,6 +334,20 @@ private static byte[] readStream(final InputStream inputStream, final boolean cl
325334
}
326335
}
327336

337+
private static int calculateBufferSize(final InputStream inputStream) throws IOException {
338+
int expectedLength = inputStream.available();
339+
/*
340+
* Some implementations can return 0 while holding available data
341+
* (e.g. new FileInputStream("/proc/a_file"))
342+
* Also in some pathological cases a very small number might be returned,
343+
* and in this case we use default size
344+
*/
345+
if (expectedLength < 256) {
346+
return INPUT_STREAM_DATA_CHUNK_SIZE;
347+
}
348+
return Math.min(expectedLength, MAX_BUFFER_SIZE);
349+
}
350+
328351
// -----------------------------------------------------------------------------------------------
329352
// Accessors
330353
// -----------------------------------------------------------------------------------------------
@@ -3456,25 +3479,24 @@ final int getFirstAttributeOffset() {
34563479
private int[] readBootstrapMethodsAttribute(final int maxStringLength) {
34573480
char[] charBuffer = new char[maxStringLength];
34583481
int currentAttributeOffset = getFirstAttributeOffset();
3459-
int[] currentBootstrapMethodOffsets = null;
34603482
for (int i = readUnsignedShort(currentAttributeOffset - 2); i > 0; --i) {
34613483
// Read the attribute_info's attribute_name and attribute_length fields.
34623484
String attributeName = readUTF8(currentAttributeOffset, charBuffer);
34633485
int attributeLength = readInt(currentAttributeOffset + 2);
34643486
currentAttributeOffset += 6;
34653487
if (Constants.BOOTSTRAP_METHODS.equals(attributeName)) {
34663488
// Read the num_bootstrap_methods field and create an array of this size.
3467-
currentBootstrapMethodOffsets = new int[readUnsignedShort(currentAttributeOffset)];
3489+
int[] result = new int[readUnsignedShort(currentAttributeOffset)];
34683490
// Compute and store the offset of each 'bootstrap_methods' array field entry.
34693491
int currentBootstrapMethodOffset = currentAttributeOffset + 2;
3470-
for (int j = 0; j < currentBootstrapMethodOffsets.length; ++j) {
3471-
currentBootstrapMethodOffsets[j] = currentBootstrapMethodOffset;
3492+
for (int j = 0; j < result.length; ++j) {
3493+
result[j] = currentBootstrapMethodOffset;
34723494
// Skip the bootstrap_method_ref and num_bootstrap_arguments fields (2 bytes each),
34733495
// as well as the bootstrap_arguments array field (of size num_bootstrap_arguments * 2).
34743496
currentBootstrapMethodOffset +=
34753497
4 + readUnsignedShort(currentBootstrapMethodOffset + 2) * 2;
34763498
}
3477-
return currentBootstrapMethodOffsets;
3499+
return result;
34783500
}
34793501
currentAttributeOffset += attributeLength;
34803502
}

spring-core/src/main/java/org/springframework/asm/ClassWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public class ClassWriter extends ClassVisitor {
7979

8080
/**
8181
* The access_flags field of the JVMS ClassFile structure. This field can contain ASM specific
82-
* access flags, such as {@link Opcodes#ACC_DEPRECATED} or {}@link Opcodes#ACC_RECORD}, which are
82+
* access flags, such as {@link Opcodes#ACC_DEPRECATED} or {@link Opcodes#ACC_RECORD}, which are
8383
* removed when generating the ClassFile structure.
8484
*/
8585
private int accessFlags;

0 commit comments

Comments
 (0)