classlib: fix ByteBuffer.slice

Probably fixes #607
This commit is contained in:
Alexey Andreev 2022-08-16 10:05:56 +02:00
parent 2d73403b9a
commit 38d3bf1c63
3 changed files with 29 additions and 1 deletions

View File

@ -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

View File

@ -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();

View File

@ -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();