Java Code Examples for java.nio.Buffer#isDirect()
The following examples show how to use
java.nio.Buffer#isDirect() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PtNDManager.java From djl with Apache License 2.0 | 5 votes |
/** {@inheritDoc} */ @Override public PtNDArray create(Buffer data, Shape shape, DataType dataType) { if (data.isDirect() && data instanceof ByteBuffer) { return JniUtils.createNdFromByteBuffer( this, (ByteBuffer) data, shape, dataType, SparseFormat.DENSE, device); } int size = data.remaining(); // int8, uint8, boolean use ByteBuffer, so need to explicitly input DataType DataType inputType = DataType.fromBuffer(data); int numOfBytes = inputType.getNumOfBytes(); ByteBuffer buf = allocateDirect(size * numOfBytes); switch (inputType) { case FLOAT32: buf.asFloatBuffer().put((FloatBuffer) data); break; case FLOAT64: buf.asDoubleBuffer().put((DoubleBuffer) data); break; case UINT8: case INT8: case BOOLEAN: buf.put((ByteBuffer) data); break; case INT32: buf.asIntBuffer().put((IntBuffer) data); break; case INT64: buf.asLongBuffer().put((LongBuffer) data); break; case FLOAT16: default: throw new AssertionError("Show never happen"); } return JniUtils.createNdFromByteBuffer( this, buf, shape, dataType, SparseFormat.DENSE, device); }
Example 2
Source File: MxNDArray.java From djl with Apache License 2.0 | 5 votes |
/** {@inheritDoc} */ @Override public void set(Buffer data) { int size = data.remaining(); // int8, uint8, boolean use ByteBuffer, so need to explicitly input DataType DataType inputType = DataType.fromBuffer(data); validate(inputType, size); if (data.isDirect()) { JnaUtils.syncCopyFromCPU(getHandle(), data, size); return; } int numOfBytes = inputType.getNumOfBytes(); ByteBuffer buf = manager.allocateDirect(size * numOfBytes); switch (inputType) { case FLOAT32: buf.asFloatBuffer().put((FloatBuffer) data); break; case FLOAT64: buf.asDoubleBuffer().put((DoubleBuffer) data); break; case UINT8: case INT8: case BOOLEAN: buf.put((ByteBuffer) data); break; case INT32: buf.asIntBuffer().put((IntBuffer) data); break; case INT64: buf.asLongBuffer().put((LongBuffer) data); break; case FLOAT16: default: throw new AssertionError("Show never happen"); } JnaUtils.syncCopyFromCPU(getHandle(), buf, size); }
Example 3
Source File: DirectBufferUtils.java From gflogger with Apache License 2.0 | 5 votes |
/** * @param toBeReleased The direct buffer that will be "cleaned". Utilizes reflection. */ public static void releaseBuffer( final Buffer toBeReleased ) { if ( toBeReleased == null || !toBeReleased.isDirect() ) { return; } try { if ( freeMethod != null ) { freeMethod.invoke( toBeReleased ); } else { final Object cleaner = cleanerMethod.invoke( toBeReleased ); if ( cleaner != null ) { cleanMethod.invoke( cleaner ); } else { // Try the alternate approach of getting the viewed buffer first final Object viewedBuffer = viewedBufferMethod.invoke( toBeReleased ); if ( viewedBuffer != null ) { releaseBuffer( ( Buffer ) viewedBuffer ); } else { LogLog.warn( "Can't release direct buffer:" + toBeReleased ); } } } } catch ( Exception ex ) { LogLog.warn( "Can't release direct buffer: " + toBeReleased, ex ); } }
Example 4
Source File: SteamInterface.java From steamworks4j with MIT License | 4 votes |
void checkBuffer(Buffer buffer) throws SteamException { if (!buffer.isDirect()) { throw new SteamException("Direct buffer required."); } }
Example 5
Source File: BufferUtils.java From Ultraino with MIT License | 2 votes |
/** * Test whether the specified buffer is direct. * * @param buf the buffer to test (not null, unaffected) * @return true if direct, otherwise false */ private static boolean isDirect(Buffer buf) { return buf.isDirect(); }
Example 6
Source File: BufferUtils.java From jmonkeyengine with BSD 3-Clause "New" or "Revised" License | 2 votes |
/** * Test whether the specified buffer is direct. * * @param buf the buffer to test (not null, unaffected) * @return true if direct, otherwise false */ private static boolean isDirect(Buffer buf) { return buf.isDirect(); }