1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-12-23 08:18:52 -05:00

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.Certificate;
import java.security.cert.CertificateException; import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory; import java.security.cert.CertificateFactory;
import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.JarInputStream; import java.util.jar.JarInputStream;
@ -36,7 +35,7 @@ public class ScriptBundle implements ScriptProvider {
continue; continue;
// completely read and verify current jar entry // 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); buffer.transferFully(jar);
jar.closeEntry(); jar.closeEntry();
@ -45,7 +44,7 @@ public class ScriptBundle implements ScriptProvider {
Certificate[] certificates = f.getCertificates(); Certificate[] certificates = f.getCertificates();
if (certificates == null || stream(f.getCertificates()).noneMatch(certificate::equals)) { 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(); return UTF_8.decode(buffer.getByteBuffer()).toString();

View File

@ -14,7 +14,7 @@ public class ByteBufferOutputStream extends OutputStream {
private final float loadFactor; private final float loadFactor;
public ByteBufferOutputStream(long initialCapacity) { public ByteBufferOutputStream(long initialCapacity) {
this((int) initialCapacity); this((int) initialCapacity, 1.0f);
} }
public ByteBufferOutputStream(int initialCapacity) { public ByteBufferOutputStream(int initialCapacity) {
@ -23,10 +23,10 @@ public class ByteBufferOutputStream extends OutputStream {
public ByteBufferOutputStream(int initialCapacity, float loadFactor) { public ByteBufferOutputStream(int initialCapacity, float loadFactor) {
if (initialCapacity < 0) 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)) 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.buffer = ByteBuffer.allocate(initialCapacity + 1);
this.loadFactor = loadFactor; this.loadFactor = loadFactor;