8357268: Use JavaNioAccess.getBufferAddress rather than DirectBuffer.address()

Reviewed-by: alanb, valeriep
This commit is contained in:
Per Minborg 2025-05-27 19:11:12 +00:00
parent c1f066e17e
commit d4b923d175
14 changed files with 78 additions and 61 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2013, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -27,6 +27,7 @@ package com.sun.crypto.provider;
import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle; import java.lang.invoke.VarHandle;
import java.nio.Buffer;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.security.*; import java.security.*;
@ -910,26 +911,26 @@ abstract class GaloisCounterMode extends CipherSpi {
*/ */
ByteBuffer overlapDetection(ByteBuffer src, ByteBuffer dst) { ByteBuffer overlapDetection(ByteBuffer src, ByteBuffer dst) {
if (src.isDirect() && dst.isDirect()) { if (src.isDirect() && dst.isDirect()) {
// The use of DirectBuffer::address below need not be guarded as // The use of addresses below need not be guarded as
// no access is made to actual memory. // no access is made to actual memory.
DirectBuffer dsrc = (DirectBuffer) src; DirectBuffer dsrc = (DirectBuffer) src;
DirectBuffer ddst = (DirectBuffer) dst; DirectBuffer ddst = (DirectBuffer) dst;
// Get the current memory address for the given ByteBuffers // Get the current memory address for the given ByteBuffers
long srcaddr = dsrc.address(); long srcaddr = NIO_ACCESS.getBufferAddress(src);
long dstaddr = ddst.address(); long dstaddr = NIO_ACCESS.getBufferAddress(dst);
// Find the lowest attachment that is the base memory address // Find the lowest attachment that is the base memory address
// of the shared memory for the src object // of the shared memory for the src object
while (dsrc.attachment() != null) { while (dsrc.attachment() != null) {
srcaddr = ((DirectBuffer) dsrc.attachment()).address(); srcaddr = NIO_ACCESS.getBufferAddress((Buffer) dsrc.attachment());
dsrc = (DirectBuffer) dsrc.attachment(); dsrc = (DirectBuffer) dsrc.attachment();
} }
// Find the lowest attachment that is the base memory address // Find the lowest attachment that is the base memory address
// of the shared memory for the dst object // of the shared memory for the dst object
while (ddst.attachment() != null) { while (ddst.attachment() != null) {
dstaddr = ((DirectBuffer) ddst.attachment()).address(); dstaddr = NIO_ACCESS.getBufferAddress((Buffer) ddst.attachment());
ddst = (DirectBuffer) ddst.attachment(); ddst = (DirectBuffer) ddst.attachment();
} }
@ -947,8 +948,8 @@ abstract class GaloisCounterMode extends CipherSpi {
// side, we are not in overlap. // side, we are not in overlap.
// NOTE: inPlaceArray does not apply here as direct buffers run // NOTE: inPlaceArray does not apply here as direct buffers run
// through a byte[] to get to the combined intrinsic // through a byte[] to get to the combined intrinsic
if (((DirectBuffer) src).address() - srcaddr + src.position() >= if (NIO_ACCESS.getBufferAddress(src) - srcaddr + src.position() >=
((DirectBuffer) dst).address() - dstaddr + dst.position()) { NIO_ACCESS.getBufferAddress(dst) - dstaddr + dst.position()) {
return dst; return dst;
} }
@ -1602,7 +1603,7 @@ abstract class GaloisCounterMode extends CipherSpi {
NIO_ACCESS.acquireSession(dst); NIO_ACCESS.acquireSession(dst);
try { try {
Unsafe.getUnsafe().setMemory( Unsafe.getUnsafe().setMemory(
((DirectBuffer)dst).address(), NIO_ACCESS.getBufferAddress(dst),
len + dst.position(), (byte) 0); len + dst.position(), (byte) 0);
} finally { } finally {
NIO_ACCESS.releaseSession(dst); NIO_ACCESS.releaseSession(dst);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -484,7 +484,7 @@ public final class IOUtil {
NIO_ACCESS.acquireSession(bb); NIO_ACCESS.acquireSession(bb);
} }
private static void releaseScope(ByteBuffer bb) { static void releaseScope(ByteBuffer bb) {
try { try {
NIO_ACCESS.releaseSession(bb); NIO_ACCESS.releaseSession(bb);
} catch (Exception e) { } catch (Exception e) {

View File

@ -166,10 +166,10 @@ abstract class UnixUserDefinedFileAttributeView
assert (pos <= lim); assert (pos <= lim);
int rem = (pos <= lim ? lim - pos : 0); int rem = (pos <= lim ? lim - pos : 0);
if (dst instanceof sun.nio.ch.DirectBuffer ddst) { if (dst.isDirect()) {
NIO_ACCESS.acquireSession(dst); NIO_ACCESS.acquireSession(dst);
try { try {
long address = ddst.address() + pos; long address = NIO_ACCESS.getBufferAddress(dst) + pos;
int n = read(name, address, rem); int n = read(name, address, rem);
dst.position(pos + n); dst.position(pos + n);
return n; return n;
@ -225,10 +225,10 @@ abstract class UnixUserDefinedFileAttributeView
assert (pos <= lim); assert (pos <= lim);
int rem = (pos <= lim ? lim - pos : 0); int rem = (pos <= lim ? lim - pos : 0);
if (src instanceof sun.nio.ch.DirectBuffer buf) { if (src.isDirect()) {
NIO_ACCESS.acquireSession(src); NIO_ACCESS.acquireSession(src);
try { try {
long address = buf.address() + pos; long address = NIO_ACCESS.getBufferAddress(src) + pos;
write(name, address, rem); write(name, address, rem);
src.position(pos + rem); src.position(pos + rem);
return rem; return rem;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -22,6 +22,7 @@
* or visit www.oracle.com if you need additional information or have any * or visit www.oracle.com if you need additional information or have any
* questions. * questions.
*/ */
package sun.security.pkcs11; package sun.security.pkcs11;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -743,8 +744,8 @@ final class P11AEADCipher extends CipherSpi {
inOfs = 0; inOfs = 0;
inLen = in.length; inLen = in.length;
} else { } else {
if (inBuffer instanceof DirectBuffer dInBuffer) { if (inBuffer instanceof DirectBuffer) {
inAddr = dInBuffer.address(); inAddr = NIO_ACCESS.getBufferAddress(inBuffer);
inOfs = inBuffer.position(); inOfs = inBuffer.position();
} else { } else {
if (inBuffer.hasArray()) { if (inBuffer.hasArray()) {
@ -759,8 +760,8 @@ final class P11AEADCipher extends CipherSpi {
long outAddr = 0; long outAddr = 0;
byte[] outArray = null; byte[] outArray = null;
int outOfs = 0; int outOfs = 0;
if (outBuffer instanceof DirectBuffer dOutBuffer) { if (outBuffer instanceof DirectBuffer) {
outAddr = dOutBuffer.address(); outAddr = NIO_ACCESS.getBufferAddress(outBuffer);
outOfs = outBuffer.position(); outOfs = outBuffer.position();
} else { } else {
if (outBuffer.hasArray()) { if (outBuffer.hasArray()) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -22,6 +22,7 @@
* or visit www.oracle.com if you need additional information or have any * or visit www.oracle.com if you need additional information or have any
* questions. * questions.
*/ */
package sun.security.pkcs11; package sun.security.pkcs11;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
@ -742,8 +743,8 @@ final class P11Cipher extends CipherSpi {
int inOfs = 0; int inOfs = 0;
byte[] inArray = null; byte[] inArray = null;
if (inBuffer instanceof DirectBuffer dInBuffer) { if (inBuffer instanceof DirectBuffer) {
inAddr = dInBuffer.address(); inAddr = NIO_ACCESS.getBufferAddress(inBuffer);
inOfs = origPos; inOfs = origPos;
} else if (inBuffer.hasArray()) { } else if (inBuffer.hasArray()) {
inArray = inBuffer.array(); inArray = inBuffer.array();
@ -753,8 +754,8 @@ final class P11Cipher extends CipherSpi {
long outAddr = 0; long outAddr = 0;
int outOfs = 0; int outOfs = 0;
byte[] outArray = null; byte[] outArray = null;
if (outBuffer instanceof DirectBuffer dOutBuffer) { if (outBuffer instanceof DirectBuffer) {
outAddr = dOutBuffer.address(); outAddr = NIO_ACCESS.getBufferAddress(outBuffer);
outOfs = outBuffer.position(); outOfs = outBuffer.position();
} else { } else {
if (outBuffer.hasArray()) { if (outBuffer.hasArray()) {
@ -1012,8 +1013,8 @@ final class P11Cipher extends CipherSpi {
long outAddr = 0; long outAddr = 0;
byte[] outArray = null; byte[] outArray = null;
int outOfs = 0; int outOfs = 0;
if (outBuffer instanceof DirectBuffer dOutBuffer) { if (outBuffer instanceof DirectBuffer) {
outAddr = dOutBuffer.address(); outAddr = NIO_ACCESS.getBufferAddress(outBuffer);
outOfs = outBuffer.position(); outOfs = outBuffer.position();
} else { } else {
if (outBuffer.hasArray()) { if (outBuffer.hasArray()) {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -271,7 +271,7 @@ final class P11Digest extends MessageDigestSpi implements Cloneable,
return; return;
} }
if (!(byteBuffer instanceof DirectBuffer dByteBuffer)) { if (!(byteBuffer instanceof DirectBuffer)) {
super.engineUpdate(byteBuffer); super.engineUpdate(byteBuffer);
return; return;
} }
@ -289,7 +289,8 @@ final class P11Digest extends MessageDigestSpi implements Cloneable,
} }
NIO_ACCESS.acquireSession(byteBuffer); NIO_ACCESS.acquireSession(byteBuffer);
try { try {
token.p11.C_DigestUpdate(session.id(), dByteBuffer.address() + ofs, null, 0, len); final long address = NIO_ACCESS.getBufferAddress(byteBuffer);
token.p11.C_DigestUpdate(session.id(), address + ofs, null, 0, len);
} finally { } finally {
NIO_ACCESS.releaseSession(byteBuffer); NIO_ACCESS.releaseSession(byteBuffer);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -22,6 +22,7 @@
* or visit www.oracle.com if you need additional information or have any * or visit www.oracle.com if you need additional information or have any
* questions. * questions.
*/ */
package sun.security.pkcs11; package sun.security.pkcs11;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -577,8 +578,8 @@ final class P11KeyWrapCipher extends CipherSpi {
inOfs = 0; inOfs = 0;
inLen = in.length; inLen = in.length;
} else { } else {
if (inBuffer instanceof DirectBuffer dInBuffer) { if (inBuffer instanceof DirectBuffer) {
inAddr = dInBuffer.address(); inAddr = NIO_ACCESS.getBufferAddress(inBuffer);
inOfs = inBuffer.position(); inOfs = inBuffer.position();
} else { } else {
if (inBuffer.hasArray()) { if (inBuffer.hasArray()) {
@ -593,8 +594,8 @@ final class P11KeyWrapCipher extends CipherSpi {
long outAddr = 0; long outAddr = 0;
byte[] outArray = null; byte[] outArray = null;
int outOfs = 0; int outOfs = 0;
if (outBuffer instanceof DirectBuffer dOutBuffer) { if (outBuffer instanceof DirectBuffer) {
outAddr = dOutBuffer.address(); outAddr = NIO_ACCESS.getBufferAddress(outBuffer);
outOfs = outBuffer.position(); outOfs = outBuffer.position();
} else { } else {
if (outBuffer.hasArray()) { if (outBuffer.hasArray()) {

View File

@ -279,14 +279,15 @@ final class P11Mac extends MacSpi {
if (len <= 0) { if (len <= 0) {
return; return;
} }
if (!(byteBuffer instanceof DirectBuffer dByteBuffer)) { if (!(byteBuffer instanceof DirectBuffer)) {
super.engineUpdate(byteBuffer); super.engineUpdate(byteBuffer);
return; return;
} }
int ofs = byteBuffer.position(); int ofs = byteBuffer.position();
NIO_ACCESS.acquireSession(byteBuffer); NIO_ACCESS.acquireSession(byteBuffer);
try { try {
token.p11.C_SignUpdate(session.id(), dByteBuffer.address() + ofs, null, 0, len); final long address = NIO_ACCESS.getBufferAddress(byteBuffer);
token.p11.C_SignUpdate(session.id(), address + ofs, null, 0, len);
} finally { } finally {
NIO_ACCESS.releaseSession(byteBuffer); NIO_ACCESS.releaseSession(byteBuffer);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2019, 2024, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -614,7 +614,7 @@ final class P11PSSSignature extends SignatureSpi {
isActive = true; isActive = true;
switch (type) { switch (type) {
case T_UPDATE -> { case T_UPDATE -> {
if (!(byteBuffer instanceof DirectBuffer dByteBuffer)) { if (!(byteBuffer instanceof DirectBuffer)) {
// cannot do better than default impl // cannot do better than default impl
super.engineUpdate(byteBuffer); super.engineUpdate(byteBuffer);
return; return;
@ -622,7 +622,7 @@ final class P11PSSSignature extends SignatureSpi {
int ofs = byteBuffer.position(); int ofs = byteBuffer.position();
NIO_ACCESS.acquireSession(byteBuffer); NIO_ACCESS.acquireSession(byteBuffer);
try { try {
long addr = dByteBuffer.address(); long addr = NIO_ACCESS.getBufferAddress(byteBuffer);
if (mode == M_SIGN) { if (mode == M_SIGN) {
if (DEBUG) System.out.println(this + ": Calling C_SignUpdate"); if (DEBUG) System.out.println(this + ": Calling C_SignUpdate");
token.p11.C_SignUpdate token.p11.C_SignUpdate

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -579,7 +579,7 @@ final class P11Signature extends SignatureSpi {
} }
switch (type) { switch (type) {
case T_UPDATE -> { case T_UPDATE -> {
if (!(byteBuffer instanceof DirectBuffer dByteBuffer)) { if (!(byteBuffer instanceof DirectBuffer)) {
// cannot do better than default impl // cannot do better than default impl
super.engineUpdate(byteBuffer); super.engineUpdate(byteBuffer);
return; return;
@ -587,7 +587,7 @@ final class P11Signature extends SignatureSpi {
int ofs = byteBuffer.position(); int ofs = byteBuffer.position();
NIO_ACCESS.acquireSession(byteBuffer); NIO_ACCESS.acquireSession(byteBuffer);
try { try {
long addr = dByteBuffer.address(); long addr = NIO_ACCESS.getBufferAddress(byteBuffer);
if (mode == M_SIGN) { if (mode == M_SIGN) {
token.p11.C_SignUpdate token.p11.C_SignUpdate
(session.id(), addr + ofs, null, 0, len); (session.id(), addr + ofs, null, 0, len);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2009, 2024, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2009, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -22,6 +22,7 @@
* or visit www.oracle.com if you need additional information or have any * or visit www.oracle.com if you need additional information or have any
* questions. * questions.
*/ */
package sun.nio.ch.sctp; package sun.nio.ch.sctp;
import java.net.InetAddress; import java.net.InetAddress;
@ -829,7 +830,7 @@ public class SctpChannelImpl extends SctpChannel
{ {
NIO_ACCESS.acquireSession(bb); NIO_ACCESS.acquireSession(bb);
try { try {
int n = receive0(fd, resultContainer, ((DirectBuffer)bb).address() + pos, rem, peek); int n = receive0(fd, resultContainer, NIO_ACCESS.getBufferAddress(bb) + pos, rem, peek);
if (n > 0) if (n > 0)
bb.position(pos + n); bb.position(pos + n);
@ -1012,7 +1013,7 @@ public class SctpChannelImpl extends SctpChannel
NIO_ACCESS.acquireSession(bb); NIO_ACCESS.acquireSession(bb);
try { try {
int written = send0(fd, ((DirectBuffer)bb).address() + pos, rem, addr, int written = send0(fd, NIO_ACCESS.getBufferAddress(bb) + pos, rem, addr,
port, -1 /*121*/, streamNumber, unordered, ppid); port, -1 /*121*/, streamNumber, unordered, ppid);
if (written > 0) if (written > 0)
bb.position(pos + written); bb.position(pos + written);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2009, 2024, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2009, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -22,6 +22,7 @@
* or visit www.oracle.com if you need additional information or have any * or visit www.oracle.com if you need additional information or have any
* questions. * questions.
*/ */
package sun.nio.ch.sctp; package sun.nio.ch.sctp;
import java.net.InetAddress; import java.net.InetAddress;
@ -561,7 +562,7 @@ public class SctpMultiChannelImpl extends SctpMultiChannel
throws IOException { throws IOException {
NIO_ACCESS.acquireSession(bb); NIO_ACCESS.acquireSession(bb);
try { try {
int n = receive0(fd, resultContainer, ((DirectBuffer)bb).address() + pos, rem); int n = receive0(fd, resultContainer, NIO_ACCESS.getBufferAddress(bb) + pos, rem);
if (n > 0) if (n > 0)
bb.position(pos + n); bb.position(pos + n);
return n; return n;
@ -870,7 +871,7 @@ public class SctpMultiChannelImpl extends SctpMultiChannel
NIO_ACCESS.acquireSession(bb); NIO_ACCESS.acquireSession(bb);
try { try {
int written = send0(fd, ((DirectBuffer)bb).address() + pos, rem, addr, int written = send0(fd, NIO_ACCESS.getBufferAddress(bb) + pos, rem, addr,
port, assocId, streamNumber, unordered, ppid); port, assocId, streamNumber, unordered, ppid);
if (written > 0) if (written > 0)
bb.position(pos + written); bb.position(pos + written);

View File

@ -29,6 +29,7 @@
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.lang.foreign.Arena;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousCloseException; import java.nio.channels.AsynchronousCloseException;
import java.nio.channels.AsynchronousFileChannel; import java.nio.channels.AsynchronousFileChannel;
@ -58,6 +59,7 @@ import static java.nio.file.StandardOpenOption.*;
public class Basic { public class Basic {
// Must be indeterministic
private static final Random rand = new Random(); private static final Random rand = new Random();
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
@ -563,15 +565,16 @@ public class Basic {
static ByteBuffer genBuffer() { static ByteBuffer genBuffer() {
int size = 1024 + rand.nextInt(16000); int size = 1024 + rand.nextInt(16000);
byte[] buf = new byte[size]; byte[] buf = new byte[size];
boolean useDirect = rand.nextBoolean(); return switch (rand.nextInt(3)) {
if (useDirect) { case 0 -> ByteBuffer.allocateDirect(buf.length)
ByteBuffer bb = ByteBuffer.allocateDirect(buf.length); .put(buf)
bb.put(buf); .flip();
bb.flip(); case 1 -> ByteBuffer.wrap(buf);
return bb; case 2 -> Arena.ofAuto().allocate(buf.length).asByteBuffer()
} else { .put(buf)
return ByteBuffer.wrap(buf); .flip();
} default -> throw new InternalError("Should not reach here");
};
} }
// writes all remaining bytes in the buffer to the given channel at the // writes all remaining bytes in the buffer to the given channel at the

View File

@ -31,6 +31,7 @@
*/ */
import java.io.IOException; import java.io.IOException;
import java.lang.foreign.Arena;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -46,7 +47,8 @@ import jdk.test.lib.Platform;
public class Basic { public class Basic {
private static Random rand = new Random(); // Must be indeterministic
private static final Random rand = new Random();
private static final String ATTR_NAME = "mime_type"; private static final String ATTR_NAME = "mime_type";
private static final String ATTR_VALUE = "text/plain"; private static final String ATTR_VALUE = "text/plain";
@ -88,8 +90,12 @@ public class Basic {
static void test(Path file, LinkOption... options) throws IOException { static void test(Path file, LinkOption... options) throws IOException {
final UserDefinedFileAttributeView view = final UserDefinedFileAttributeView view =
Files.getFileAttributeView(file, UserDefinedFileAttributeView.class, options); Files.getFileAttributeView(file, UserDefinedFileAttributeView.class, options);
ByteBuffer buf = rand.nextBoolean() ? final ByteBuffer buf = switch (rand.nextInt(3)) {
ByteBuffer.allocate(100) : ByteBuffer.allocateDirect(100); case 0 -> ByteBuffer.allocate(100);
case 1 -> ByteBuffer.allocateDirect(100);
case 2 -> Arena.ofAuto().allocate(100).asByteBuffer();
default -> throw new InternalError("Should not reach here");
};
// Test: write // Test: write
buf.put(ATTR_VALUE.getBytes()).flip(); buf.put(ATTR_VALUE.getBytes()).flip();