@Test public void testDirectBufferFor() throws Exception { fillRandom(); ByteBuffer buf = Uns.directBufferFor(((DirectBuffer) directBuffer).address(), 0, directBuffer.capacity(), false); for (int i = 0; i < CAPACITY; i++) { byte b = buf.get(); byte d = directBuffer.get(); assertEquals(b, d); assertEquals(buf.position(), directBuffer.position()); assertEquals(buf.limit(), directBuffer.limit()); assertEquals(buf.remaining(), directBuffer.remaining()); assertEquals(buf.capacity(), directBuffer.capacity()); } buf.clear(); directBuffer.clear(); while (buf.remaining() >= 8) { long b = buf.getLong(); long d = directBuffer.getLong(); assertEquals(b, d); assertEquals(buf.position(), directBuffer.position()); assertEquals(buf.remaining(), directBuffer.remaining()); } while (buf.remaining() >= 4) { int b = buf.getInt(); int d = directBuffer.getInt(); assertEquals(b, d); assertEquals(buf.position(), directBuffer.position()); assertEquals(buf.remaining(), directBuffer.remaining()); } for (int i = 0; i < CAPACITY; i++) { byte b = buf.get(i); byte d = directBuffer.get(i); assertEquals(b, d); if (i >= CAPACITY - 1) continue; char bufChar = buf.getChar(i); char dirChar = directBuffer.getChar(i); short bufShort = buf.getShort(i); short dirShort = directBuffer.getShort(i); assertEquals(bufChar, dirChar); assertEquals(bufShort, dirShort); if (i >= CAPACITY - 3) continue; int bufInt = buf.getInt(i); int dirInt = directBuffer.getInt(i); float bufFloat = buf.getFloat(i); float dirFloat = directBuffer.getFloat(i); assertEquals(bufInt, dirInt); assertEquals(bufFloat, dirFloat); if (i >= CAPACITY - 7) continue; long bufLong = buf.getLong(i); long dirLong = directBuffer.getLong(i); double bufDouble = buf.getDouble(i); double dirDouble = directBuffer.getDouble(i); assertEquals(bufLong, dirLong); assertEquals(bufDouble, dirDouble); } }
static ByteBuffer directBufferFor(long address, long offset, long len, boolean readOnly) { if (len > Integer.MAX_VALUE || len < 0L) throw new IllegalArgumentException(); try { ByteBuffer bb = (ByteBuffer) unsafe.allocateInstance(readOnly ? DIRECT_BYTE_BUFFER_CLASS_R : DIRECT_BYTE_BUFFER_CLASS); unsafe.putLong(bb, DIRECT_BYTE_BUFFER_ADDRESS_OFFSET, address + offset); unsafe.putInt(bb, DIRECT_BYTE_BUFFER_CAPACITY_OFFSET, (int) len); unsafe.putInt(bb, DIRECT_BYTE_BUFFER_LIMIT_OFFSET, (int) len); bb.order(ByteOrder.BIG_ENDIAN); return bb; } catch (Error e) { throw e; } catch (Throwable t) { throw new RuntimeException(t); } }
Uns { static ByteBuffer directBufferFor(long address, long offset, long len, boolean readOnly) { if (len > Integer.MAX_VALUE || len < 0L) throw new IllegalArgumentException(); try { ByteBuffer bb = (ByteBuffer) unsafe.allocateInstance(readOnly ? DIRECT_BYTE_BUFFER_CLASS_R : DIRECT_BYTE_BUFFER_CLASS); unsafe.putLong(bb, DIRECT_BYTE_BUFFER_ADDRESS_OFFSET, address + offset); unsafe.putInt(bb, DIRECT_BYTE_BUFFER_CAPACITY_OFFSET, (int) len); unsafe.putInt(bb, DIRECT_BYTE_BUFFER_LIMIT_OFFSET, (int) len); bb.order(ByteOrder.BIG_ENDIAN); return bb; } catch (Error e) { throw e; } catch (Throwable t) { throw new RuntimeException(t); } } }
Uns { static ByteBuffer directBufferFor(long address, long offset, long len, boolean readOnly) { if (len > Integer.MAX_VALUE || len < 0L) throw new IllegalArgumentException(); try { ByteBuffer bb = (ByteBuffer) unsafe.allocateInstance(readOnly ? DIRECT_BYTE_BUFFER_CLASS_R : DIRECT_BYTE_BUFFER_CLASS); unsafe.putLong(bb, DIRECT_BYTE_BUFFER_ADDRESS_OFFSET, address + offset); unsafe.putInt(bb, DIRECT_BYTE_BUFFER_CAPACITY_OFFSET, (int) len); unsafe.putInt(bb, DIRECT_BYTE_BUFFER_LIMIT_OFFSET, (int) len); bb.order(ByteOrder.BIG_ENDIAN); return bb; } catch (Error e) { throw e; } catch (Throwable t) { throw new RuntimeException(t); } } private  Uns(); }
Uns { static ByteBuffer directBufferFor(long address, long offset, long len, boolean readOnly) { if (len > Integer.MAX_VALUE || len < 0L) throw new IllegalArgumentException(); try { ByteBuffer bb = (ByteBuffer) unsafe.allocateInstance(readOnly ? DIRECT_BYTE_BUFFER_CLASS_R : DIRECT_BYTE_BUFFER_CLASS); unsafe.putLong(bb, DIRECT_BYTE_BUFFER_ADDRESS_OFFSET, address + offset); unsafe.putInt(bb, DIRECT_BYTE_BUFFER_CAPACITY_OFFSET, (int) len); unsafe.putInt(bb, DIRECT_BYTE_BUFFER_LIMIT_OFFSET, (int) len); bb.order(ByteOrder.BIG_ENDIAN); return bb; } catch (Error e) { throw e; } catch (Throwable t) { throw new RuntimeException(t); } } private  Uns();  }
Uns { static ByteBuffer directBufferFor(long address, long offset, long len, boolean readOnly) { if (len > Integer.MAX_VALUE || len < 0L) throw new IllegalArgumentException(); try { ByteBuffer bb = (ByteBuffer) unsafe.allocateInstance(readOnly ? DIRECT_BYTE_BUFFER_CLASS_R : DIRECT_BYTE_BUFFER_CLASS); unsafe.putLong(bb, DIRECT_BYTE_BUFFER_ADDRESS_OFFSET, address + offset); unsafe.putInt(bb, DIRECT_BYTE_BUFFER_CAPACITY_OFFSET, (int) len); unsafe.putInt(bb, DIRECT_BYTE_BUFFER_LIMIT_OFFSET, (int) len); bb.order(ByteOrder.BIG_ENDIAN); return bb; } catch (Error e) { throw e; } catch (Throwable t) { throw new RuntimeException(t); } } private  Uns();   }