List of usage examples for java.nio ByteBuffer isDirect
public abstract boolean isDirect();
From source file:Main.java
public static void main(String[] argv) throws Exception { ByteBuffer bbuf = ByteBuffer.wrap(new byte[10]); boolean isDirect = bbuf.isDirect(); // false bbuf = ByteBuffer.allocate(10); isDirect = bbuf.isDirect(); // false bbuf = ByteBuffer.allocateDirect(10); isDirect = bbuf.isDirect(); // true }
From source file:Main.java
public static void main(String[] argv) throws Exception { ByteBuffer bbuf = ByteBuffer.allocate(10); int capacity = bbuf.capacity(); // 10 System.out.println(capacity); bbuf.putShort(2, (short) 123); System.out.println(bbuf.isDirect()); }
From source file:Main.java
public static void main(String[] argv) throws Exception { ByteBuffer bbuf = ByteBuffer.allocate(10); int capacity = bbuf.capacity(); // 10 System.out.println(capacity); bbuf.putShort(2, (short) 123); System.out.println(Arrays.toString(bbuf.array())); System.out.println(bbuf.isDirect()); }
From source file:Main.java
public static ByteBuffer cloneByteBuffer(final ByteBuffer original) { // Create clone with same capacity as original. final ByteBuffer clone = (original.isDirect()) ? ByteBuffer.allocateDirect(original.capacity()) : ByteBuffer.allocate(original.capacity()); // Create a read-only copy of the original. // This allows reading from the original without modifying it. final ByteBuffer readOnlyCopy = original.asReadOnlyBuffer(); // Flip and read from the original. readOnlyCopy.flip();//from ww w.jav a 2s.co m clone.put(readOnlyCopy); clone.position(original.position()); clone.limit(original.limit()); clone.order(original.order()); return clone; }
From source file:Main.java
public static final void checkBuffer(final ByteBuffer buffer) { if (buffer == null) { throw new IllegalArgumentException("buffer == null"); }/* ww w . ja v a2 s . co m*/ if (!buffer.isDirect()) { throw new IllegalArgumentException("must use DirectByteBuffer"); } }
From source file:com.icloud.framework.core.nio.ByteBufferUtil.java
public static ByteBuffer clone(ByteBuffer o) { assert o != null; if (o.remaining() == 0) return ByteBuffer.wrap(ArrayUtils.EMPTY_BYTE_ARRAY); ByteBuffer clone = ByteBuffer.allocate(o.remaining()); if (o.isDirect()) { for (int i = o.position(); i < o.limit(); i++) { clone.put(o.get(i));//from ww w . j a v a 2 s.com } clone.flip(); } else { System.arraycopy(o.array(), o.arrayOffset() + o.position(), clone.array(), 0, o.remaining()); } return clone; }
From source file:Main.java
public static ByteBuffer clone(final ByteBuffer buf) { if (buf == null) { return null; }//from w w w .j a va 2 s .c om buf.rewind(); final ByteBuffer copy; if (buf.isDirect()) { copy = createByteBuffer(buf.limit()); } else { copy = createByteBufferOnHeap(buf.limit()); } copy.put(buf); return copy; }
From source file:com.gamesalutes.utils.ByteUtils.java
/** * Extends the size of <code>buf</code> to at least meet <code>minCap</code>. * If <code>buf</code> is too small, then a new buffer is allocated and * any existing contents in <code>buf</code> will be transfered. The position * of the new buffer will be that of the old buffer if it was not <code>null</code>, and * the previous mark will be discarded if one was set. * //from www . j a v a2s.com * @param buf the input <code>ByteBuffer</code> * @param minCap the minimum capacity * @return a <code>ByteBuffer</code> that can meet <code>minCap</code> */ public static ByteBuffer growBuffer(ByteBuffer buf, int minCap) { int myLimit = buf != null ? buf.limit() : 0; // limit can accomidate capacity requirements if (buf != null && myLimit >= minCap) return buf; int myCap = buf != null ? buf.capacity() : 0; // capacity can accomidate but limit is too small if (buf != null && myCap >= minCap) { buf.limit(myCap); return buf; } else //if(myCap < minCap) { ByteBuffer newBuffer = null; if (myCap == 0) myCap = 1; while (myCap < minCap) myCap <<= 1; if (buf != null && buf.isDirect()) newBuffer = ByteBuffer.allocateDirect(myCap); else newBuffer = ByteBuffer.allocate(myCap); // copy contents of original buffer if (buf != null) { int pos = buf.position(); buf.clear(); newBuffer.put(buf); newBuffer.position(pos); } return newBuffer; } }
From source file:io.horizondb.io.buffers.DirectBuffer.java
/** * Creates a new <code>DirectBuffer</code> that wrap the specified <code>ByteBuffer</code>. * //from w w w . j a va 2 s .c o m * @param buffer the <code>ByteBuffer</code>. */ DirectBuffer(ByteBuffer buffer) { notNull(buffer, "the buffer parameter must not be null"); isTrue(buffer.isDirect(), "the buffer must be direct"); this.buffer = buffer; subRegion(0, buffer.capacity()); writerIndex(buffer.capacity()); }
From source file:com.intel.chimera.codec.OpensslCipher.java
/** * Continues a multiple-part encryption or decryption operation. The data * is encrypted or decrypted, depending on how this cipher was initialized. * <p/>//from ww w .ja v a 2s . c o m * * All <code>input.remaining()</code> bytes starting at * <code>input.position()</code> are processed. The result is stored in * the output buffer. * <p/> * * Upon return, the input buffer's position will be equal to its limit; * its limit will not have changed. The output buffer's position will have * advanced by n, when n is the value returned by this method; the output * buffer's limit will not have changed. * <p/> * * If <code>output.remaining()</code> bytes are insufficient to hold the * result, a <code>ShortBufferException</code> is thrown. * * @param input the input ByteBuffer * @param output the output ByteBuffer * @return int number of bytes stored in <code>output</code> * @throws ShortBufferException if there is insufficient space in the * output buffer */ public int update(ByteBuffer input, ByteBuffer output) throws ShortBufferException { checkState(); Preconditions.checkArgument(input.isDirect() && output.isDirect(), "Direct buffers are required."); int len = OpensslCipherNative.update(context, input, input.position(), input.remaining(), output, output.position(), output.remaining()); input.position(input.limit()); output.position(output.position() + len); return len; }