Avoid potential "initialCapacity must not be negative" exceptions (JarEntry seems to behave strangely on some platforms)

@see https://www.filebot.net/forums/viewtopic.php?f=10&t=4509&p=25071#p25071
This commit is contained in:
Reinhard Pointner 2016-12-14 13:11:04 +08:00
parent 77512d0e4f
commit 6342efc743
2 changed files with 5 additions and 6 deletions

View File

@ -10,7 +10,6 @@ import java.io.InputStream;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.util.Arrays;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
@ -36,7 +35,7 @@ public class ScriptBundle implements ScriptProvider {
continue;
// completely read and verify current jar entry
ByteBufferOutputStream buffer = new ByteBufferOutputStream(f.getSize());
ByteBufferOutputStream buffer = new ByteBufferOutputStream(f.getSize() > 0 ? f.getSize() : 8192);
buffer.transferFully(jar);
jar.closeEntry();
@ -45,7 +44,7 @@ public class ScriptBundle implements ScriptProvider {
Certificate[] certificates = f.getCertificates();
if (certificates == null || stream(f.getCertificates()).noneMatch(certificate::equals)) {
throw new SecurityException(String.format("BAD certificate: %s", Arrays.toString(certificates)));
throw new SecurityException("BAD certificate: " + asList(certificates));
}
return UTF_8.decode(buffer.getByteBuffer()).toString();

View File

@ -14,7 +14,7 @@ public class ByteBufferOutputStream extends OutputStream {
private final float loadFactor;
public ByteBufferOutputStream(long initialCapacity) {
this((int) initialCapacity);
this((int) initialCapacity, 1.0f);
}
public ByteBufferOutputStream(int initialCapacity) {
@ -23,10 +23,10 @@ public class ByteBufferOutputStream extends OutputStream {
public ByteBufferOutputStream(int initialCapacity, float loadFactor) {
if (initialCapacity < 0)
throw new IllegalArgumentException("initialCapacity must not be negative");
throw new IllegalArgumentException("initialCapacity must not be negative: " + initialCapacity);
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("loadFactor must be greater than 0");
throw new IllegalArgumentException("loadFactor must be greater than zero: " + loadFactor);
this.buffer = ByteBuffer.allocate(initialCapacity + 1);
this.loadFactor = loadFactor;