From 9fcdb46505137a125769d518c4278758416fcae9 Mon Sep 17 00:00:00 2001 From: gohai Date: Mon, 24 Apr 2017 22:26:47 +0200 Subject: [PATCH] Improve loadBytes performance Before, loading a 54 MB file took 2.7 seconds on my Macbook Air. With this change applied, it only takes 0.4 seconds. --- core/src/processing/core/PApplet.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/core/src/processing/core/PApplet.java b/core/src/processing/core/PApplet.java index 54a930388..be6f184d7 100644 --- a/core/src/processing/core/PApplet.java +++ b/core/src/processing/core/PApplet.java @@ -7174,14 +7174,15 @@ public class PApplet implements PConstants { */ static public byte[] loadBytes(InputStream input) { try { - BufferedInputStream bis = new BufferedInputStream(input); ByteArrayOutputStream out = new ByteArrayOutputStream(); + byte[] buf = new byte[4096]; - int c = bis.read(); - while (c != -1) { - out.write(c); - c = bis.read(); + int bytesRead = input.read(buf); + while (bytesRead != -1) { + out.write(buf, 0, bytesRead); + bytesRead = input.read(buf); } + out.flush(); return out.toByteArray(); } catch (IOException e) {