Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
8259634: MemorySegment::asByteBuffer does not respect spatial bounds
Reviewed-by: alanb, chegar
  • Loading branch information
mcimadamore committed Jan 12, 2021
1 parent 8a81cf1 commit b03880e33b0d9442a608c9ec911afbe863221bf2
Showing with 30 additions and 2 deletions.
  1. +1 −1 src/java.base/share/classes/java/nio/Buffer.java
  2. +29 −1 test/jdk/java/foreign/TestByteBuffer.java
@@ -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
@@ -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;
@@ -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)) {
@@ -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<>();

1 comment on commit b03880e

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on b03880e Jan 12, 2021

Please sign in to comment.