From 95c6c085f6858789f5c69155d6e14faf8d316856 Mon Sep 17 00:00:00 2001 From: Maschell Date: Wed, 10 Apr 2019 18:11:54 +0200 Subject: [PATCH] Optimize the getBytesFromStream function StreamUtils --- src/de/mas/wiiu/jnus/utils/StreamUtils.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/de/mas/wiiu/jnus/utils/StreamUtils.java b/src/de/mas/wiiu/jnus/utils/StreamUtils.java index a509554..512953f 100644 --- a/src/de/mas/wiiu/jnus/utils/StreamUtils.java +++ b/src/de/mas/wiiu/jnus/utils/StreamUtils.java @@ -41,13 +41,17 @@ public final class StreamUtils { } else { buffer = new byte[0x8000]; } - int totalRead = 0; + int toRead = size; + int curReadChunk = buffer.length; do { - int read = in.read(buffer); + if (toRead < curReadChunk) { + curReadChunk = toRead; + } + int read = in.read(buffer, 0, curReadChunk); if (read < 0) break; - System.arraycopy(buffer, 0, result, totalRead, read); - totalRead += read; - } while (totalRead < size); + System.arraycopy(buffer, 0, result, size - toRead, read); + toRead -= read; + } while (toRead > 0); in.close(); return result; } @@ -61,7 +65,6 @@ public final class StreamUtils { do { try { bytesRead = inputStream.read(overflowbuf, overflowbuffer.getLengthOfDataInBuffer(), overflowbuffer.getSpaceLeft()); - } catch (IOException e) { log.info(e.getMessage()); if (!e.getMessage().equals("Write end dead")) {