Skip to content

Commit

Permalink
Stream improvements (closes #25)
Browse files Browse the repository at this point in the history
  • Loading branch information
devgianlu committed Nov 23, 2018
1 parent 413cc1d commit d905e78
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions core/src/main/java/org/librespot/spotify/BytesArrayList.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down

0 comments on commit d905e78

Please sign in to comment.