Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Commit

Permalink
8259634: MemorySegment::asByteBuffer does not respect spatial bounds
Browse files Browse the repository at this point in the history
Reviewed-by: alanb, chegar
  • Loading branch information
mcimadamore committed Jan 12, 2021
1 parent 8a81cf1 commit b03880e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/nio/Buffer.java
Expand Up @@ -793,7 +793,7 @@ public ByteBuffer newMappedByteBuffer(UnmapperProxy unmapperProxy, long address,

@Override
public ByteBuffer newHeapByteBuffer(byte[] hb, int offset, int capacity, MemorySegmentProxy segment) {
return new HeapByteBuffer(hb, offset, capacity, segment);
return new HeapByteBuffer(hb, -1, 0, capacity, capacity, offset, segment);
}

@Override
Expand Down
30 changes: 29 additions & 1 deletion test/jdk/java/foreign/TestByteBuffer.java
Expand Up @@ -64,13 +64,15 @@
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;

import jdk.internal.foreign.HeapMemorySegmentImpl;
Expand Down Expand Up @@ -635,7 +637,8 @@ public void testIOOnClosedConfinedSegmentBuffer() throws IOException {
}
}

public void testIOOnClosedConfinedSegment() throws IOException {
@Test
public void testIOOnConfinedSegment() throws IOException {
File tmp = File.createTempFile("tmp", "txt");
tmp.deleteOnExit();
try (FileChannel channel = FileChannel.open(tmp.toPath(), StandardOpenOption.WRITE)) {
Expand All @@ -648,6 +651,31 @@ public void testIOOnClosedConfinedSegment() throws IOException {
}
}

@Test(dataProvider="segments")
public void buffersAndArraysFromSlices(Supplier<MemorySegment> segmentSupplier) {
try (MemorySegment segment = segmentSupplier.get()) {
int newSize = 8;
var slice = segment.asSlice(4, newSize);

var bytes = slice.toByteArray();
assertEquals(newSize, bytes.length);

var buffer = slice.asByteBuffer();
// Fails for heap segments, but passes for native segments:
assertEquals(0, buffer.position());
assertEquals(newSize, buffer.limit());
assertEquals(newSize, buffer.capacity());
}
}

@DataProvider(name = "segments")
public static Object[][] segments() throws Throwable {
return new Object[][] {
{ (Supplier<MemorySegment>) () -> MemorySegment.allocateNative(16) },
{ (Supplier<MemorySegment>) () -> MemorySegment.ofArray(new byte[16]) }
};
}

@DataProvider(name = "bufferOps")
public static Object[][] bufferOps() throws Throwable {
List<Object[]> args = new ArrayList<>();
Expand Down

1 comment on commit b03880e

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.