If you are working with Java 21 you could use the Foreign API.
public static ByteBuffer asByteBuffer(Buffer buf) { return MemorySegment.ofBuffer(buf).asByteBuffer(); }