From d905e789e0aae72cd2d469abeb089e8103853909 Mon Sep 17 00:00:00 2001 From: Gianlu Date: Fri, 23 Nov 2018 18:33:40 +0100 Subject: [PATCH] Stream improvements (closes #25) --- .../org/librespot/spotify/BytesArrayList.java | 38 +++++++++++++++++-- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/librespot/spotify/BytesArrayList.java b/core/src/main/java/org/librespot/spotify/BytesArrayList.java index 690debbf..62e8f4a4 100644 --- a/core/src/main/java/org/librespot/spotify/BytesArrayList.java +++ b/core/src/main/java/org/librespot/spotify/BytesArrayList.java @@ -83,18 +83,48 @@ private InternalStream() { } @Override - public synchronized int read() { + public int read(@NotNull byte[] b, int off, int len) { + if (off < 0 || len < 0 || len > b.length - off) { + throw new IndexOutOfBoundsException(); + } else if (len == 0) { + return 0; + } + if (sub >= elementData.length) return -1; - if (offset >= elementData[sub].length) { - offset = 0; - sub++; + int i = 0; + while (true) { + if (sub >= elementData.length) + return i; + + int copy = Math.min(len - i, elementData[sub].length - offset); + System.arraycopy(elementData[sub], offset, b, off + i, copy); + i += copy; + offset += copy; + + if (i == len) + return i; + + if (offset >= elementData[sub].length) { + offset = 0; + if (++sub >= elementData.length) + return i; + } } + } + @Override + public synchronized int read() { if (sub >= elementData.length) return -1; + if (offset >= elementData[sub].length) { + offset = 0; + if (++sub >= elementData.length) + return -1; + } + return elementData[sub][offset++] & 0xff; } }