From 6342efc743494692d7b367b10a5b7aa5041e7c70 Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Wed, 14 Dec 2016 13:11:04 +0800 Subject: [PATCH] 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 --- source/net/filebot/cli/ScriptBundle.java | 5 ++--- source/net/filebot/util/ByteBufferOutputStream.java | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/source/net/filebot/cli/ScriptBundle.java b/source/net/filebot/cli/ScriptBundle.java index b7ba12e8..b3278476 100644 --- a/source/net/filebot/cli/ScriptBundle.java +++ b/source/net/filebot/cli/ScriptBundle.java @@ -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(); diff --git a/source/net/filebot/util/ByteBufferOutputStream.java b/source/net/filebot/util/ByteBufferOutputStream.java index 18d1d4e7..5b691bef 100644 --- a/source/net/filebot/util/ByteBufferOutputStream.java +++ b/source/net/filebot/util/ByteBufferOutputStream.java @@ -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;