@Test public void copyEmptyDirectBufferToNativeMemory() { final ByteBuffer buf = ByteBuffer.allocateDirect(0); assertEquals(0, buf.remaining()); assertThrows( IllegalArgumentException.class, () -> BufferUtils.copyToNativeMemory(buf)); }
static Pointer copyToNativeMemory(final ByteBuffer buffer) { if (buffer == null || buffer.remaining() <= 0) { throw new IllegalArgumentException("buffer must not be null or empty"); } final int length = buffer.remaining(); if (buffer.isDirect()) { final int offset = buffer.position(); return Native.getDirectBufferPointer(buffer).share(offset, length); } else { final Memory mem = new Memory(length); int index; byte[] bytes; if (buffer.hasArray()) { index = buffer.arrayOffset() + buffer.position(); bytes = buffer.array(); } else { index = 0; bytes = new byte[length]; buffer.duplicate().get(bytes); } mem.write(0, bytes, index, length); return mem.share(0, length); } }
BufferUtils { static Pointer copyToNativeMemory(final ByteBuffer buffer) { if (buffer == null || buffer.remaining() <= 0) { throw new IllegalArgumentException("buffer must not be null or empty"); } final int length = buffer.remaining(); if (buffer.isDirect()) { final int offset = buffer.position(); return Native.getDirectBufferPointer(buffer).share(offset, length); } else { final Memory mem = new Memory(length); int index; byte[] bytes; if (buffer.hasArray()) { index = buffer.arrayOffset() + buffer.position(); bytes = buffer.array(); } else { index = 0; bytes = new byte[length]; buffer.duplicate().get(bytes); } mem.write(0, bytes, index, length); return mem.share(0, length); } } }
BufferUtils { static Pointer copyToNativeMemory(final ByteBuffer buffer) { if (buffer == null || buffer.remaining() <= 0) { throw new IllegalArgumentException("buffer must not be null or empty"); } final int length = buffer.remaining(); if (buffer.isDirect()) { final int offset = buffer.position(); return Native.getDirectBufferPointer(buffer).share(offset, length); } else { final Memory mem = new Memory(length); int index; byte[] bytes; if (buffer.hasArray()) { index = buffer.arrayOffset() + buffer.position(); bytes = buffer.array(); } else { index = 0; bytes = new byte[length]; buffer.duplicate().get(bytes); } mem.write(0, bytes, index, length); return mem.share(0, length); } }  }
BufferUtils { static Pointer copyToNativeMemory(final ByteBuffer buffer) { if (buffer == null || buffer.remaining() <= 0) { throw new IllegalArgumentException("buffer must not be null or empty"); } final int length = buffer.remaining(); if (buffer.isDirect()) { final int offset = buffer.position(); return Native.getDirectBufferPointer(buffer).share(offset, length); } else { final Memory mem = new Memory(length); int index; byte[] bytes; if (buffer.hasArray()) { index = buffer.arrayOffset() + buffer.position(); bytes = buffer.array(); } else { index = 0; bytes = new byte[length]; buffer.duplicate().get(bytes); } mem.write(0, bytes, index, length); return mem.share(0, length); } }   }
BufferUtils { static Pointer copyToNativeMemory(final ByteBuffer buffer) { if (buffer == null || buffer.remaining() <= 0) { throw new IllegalArgumentException("buffer must not be null or empty"); } final int length = buffer.remaining(); if (buffer.isDirect()) { final int offset = buffer.position(); return Native.getDirectBufferPointer(buffer).share(offset, length); } else { final Memory mem = new Memory(length); int index; byte[] bytes; if (buffer.hasArray()) { index = buffer.arrayOffset() + buffer.position(); bytes = buffer.array(); } else { index = 0; bytes = new byte[length]; buffer.duplicate().get(bytes); } mem.write(0, bytes, index, length); return mem.share(0, length); } }    }