Skip to content

Commit

Permalink
Simplification of JoinClassLoader
Browse files Browse the repository at this point in the history
We do a lot of fancy buffer sizing which is already done (probably better) by the underlying stream implementation. Just use that.
  • Loading branch information
dmlloyd committed Oct 7, 2024
1 parent 0545457 commit f4b3b2f
Showing 1 changed file with 1 addition and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.ServiceLoader;
Expand Down Expand Up @@ -315,7 +313,7 @@ protected Class<?> findClass(String name) throws ClassNotFoundException {
private ByteBuffer loadResource(URL url) throws IOException {
return ClassPathUtils.readStream(url, stream -> {
try {
return ByteBuffer.wrap(read(stream, Math.max(MAX_BUFFER_SIZE, stream.available())));
return ByteBuffer.wrap(stream.readAllBytes());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down Expand Up @@ -344,38 +342,5 @@ protected Enumeration<URL> findResources(String name) throws IOException {
}
return vector.elements();
}

private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;
private static final int BUFFER_SIZE = 8192;

private static byte[] read(InputStream source, int initialSize) throws IOException {
int capacity = initialSize;
byte[] buf = new byte[capacity];
int nread = 0;
int n;
for (;;) {
// read to EOF which may read more or less than initialSize (eg: file
// is truncated while we are reading)
while ((n = source.read(buf, nread, capacity - nread)) > 0)
nread += n;

// if last call to source.read() returned -1, we are done
// 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;
}
buf = Arrays.copyOf(buf, capacity);
buf[nread++] = (byte) n;
}
return (capacity == nread) ? buf : Arrays.copyOf(buf, nread);
}
}
}

0 comments on commit f4b3b2f

Please sign in to comment.