diff --git a/classlib/src/main/java/org/teavm/classlib/java/nio/TByteBufferImpl.java b/classlib/src/main/java/org/teavm/classlib/java/nio/TByteBufferImpl.java index cd7589c03..aab2cfaf1 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/nio/TByteBufferImpl.java +++ b/classlib/src/main/java/org/teavm/classlib/java/nio/TByteBufferImpl.java @@ -32,7 +32,8 @@ class TByteBufferImpl extends TByteBuffer { @Override public TByteBuffer slice() { - return new TByteBufferImpl(position, limit - position, array, 0, limit - position, direct, readOnly); + return new TByteBufferImpl(position + start, limit - position, array, 0, limit - position, + direct, readOnly); } @Override diff --git a/tests/src/test/java/org/teavm/classlib/java/nio/ByteBufferTest.java b/tests/src/test/java/org/teavm/classlib/java/nio/ByteBufferTest.java index 3f705b8a4..96cc08ff4 100644 --- a/tests/src/test/java/org/teavm/classlib/java/nio/ByteBufferTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/nio/ByteBufferTest.java @@ -17,6 +17,7 @@ package org.teavm.classlib.java.nio; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.sameInstance; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; import java.nio.BufferOverflowException; @@ -153,6 +154,18 @@ public class ByteBufferTest { assertThat(slice.get(1), is((byte) 25)); } + @Test + public void sliceOfSlice() { + ByteBuffer buffer = ByteBuffer.allocate(100); + buffer.put(new byte[10]); + ByteBuffer slice1 = buffer.slice(); + slice1.put(new byte[15]); + ByteBuffer slice2 = slice1.slice(); + + assertEquals(25, slice2.arrayOffset()); + assertEquals(75, slice2.capacity()); + } + @Test public void slicePropertiesSameWithOriginal() { ByteBuffer buffer = ByteBuffer.allocate(100).asReadOnlyBuffer().slice(); diff --git a/tests/src/test/java/org/teavm/classlib/java/nio/IntBufferTest.java b/tests/src/test/java/org/teavm/classlib/java/nio/IntBufferTest.java index c09382297..b8e95c8b8 100644 --- a/tests/src/test/java/org/teavm/classlib/java/nio/IntBufferTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/nio/IntBufferTest.java @@ -17,6 +17,7 @@ package org.teavm.classlib.java.nio; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.sameInstance; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; import static org.junit.Assert.fail; import java.nio.BufferOverflowException; @@ -132,6 +133,19 @@ public class IntBufferTest { assertThat(slice.get(1), is(25)); } + + @Test + public void sliceOfSlice() { + var buffer = IntBuffer.allocate(100); + buffer.put(new int[10]); + var slice1 = buffer.slice(); + slice1.put(new int[15]); + var slice2 = slice1.slice(); + + assertEquals(25, slice2.arrayOffset()); + assertEquals(75, slice2.capacity()); + } + @Test public void slicePropertiesSameWithOriginal() { IntBuffer buffer = IntBuffer.allocate(100).asReadOnlyBuffer().slice();