| 13 | import sun.misc.Unsafe; |
| 14 | |
| 15 | class DirectByteBuffer extends ByteBuffer { |
| 16 | private static final Unsafe unsafe = Unsafe.getUnsafe(); |
| 17 | private static final int baseOffset = unsafe.arrayBaseOffset(byte[].class); |
| 18 | |
| 19 | protected final long address; |
| 20 | |
| 21 | protected DirectByteBuffer(long address, int capacity, boolean readOnly) { |
| 22 | super(readOnly); |
| 23 | |
| 24 | this.address = address; |
| 25 | this.capacity = capacity; |
| 26 | this.limit = capacity; |
| 27 | this.position = 0; |
| 28 | } |
| 29 | |
| 30 | protected DirectByteBuffer(long address, int capacity) { |
| 31 | this(address, capacity, false); |
| 32 | } |
| 33 | |
| 34 | public ByteBuffer asReadOnlyBuffer() { |
| 35 | ByteBuffer b = new DirectByteBuffer(address, capacity, true); |
| 36 | b.position(position()); |
| 37 | b.limit(limit()); |
| 38 | return b; |
| 39 | } |
| 40 | |
| 41 | public ByteBuffer slice() { |
| 42 | return new DirectByteBuffer(address + position, remaining(), true); |
| 43 | } |
| 44 | |
| 45 | protected void doPut(int position, byte val) { |
| 46 | unsafe.putByte(address + position, val); |
| 47 | } |
| 48 | |
| 49 | public ByteBuffer put(ByteBuffer src) { |
| 50 | if (src instanceof DirectByteBuffer) { |
| 51 | checkPut(position, src.remaining(), false); |
| 52 | |
| 53 | DirectByteBuffer b = (DirectByteBuffer) src; |
| 54 | |
| 55 | unsafe.copyMemory |
| 56 | (b.address + b.position, address + position, b.remaining()); |
| 57 | |
| 58 | position += b.remaining(); |
| 59 | b.position += b.remaining(); |
| 60 | |
| 61 | return this; |
| 62 | } else { |
| 63 | return super.put(src); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | public ByteBuffer put(byte[] src, int offset, int length) { |
| 68 | if (offset < 0 || offset + length > src.length) { |
| 69 | throw new ArrayIndexOutOfBoundsException(); |
| 70 | } |
| 71 | |
| 72 | checkPut(position, length, false); |
nothing calls this directly
no test coverage detected