MemorySegment seg = createSegment(1024); ByteBuffer buf1 = seg.wrap(13, 47); assertEquals(13, buf1.position()); assertEquals(60, buf1.limit()); assertEquals(47, buf1.remaining()); ByteBuffer buf2 = seg.wrap(500, 267); assertEquals(500, buf2.position()); assertEquals(767, buf2.limit()); assertEquals(267, buf2.remaining()); ByteBuffer buf3 = seg.wrap(0, 1024); assertEquals(0, buf3.position()); assertEquals(1024, buf3.limit()); seg.wrap(-1, 20); fail("should throw an exception"); seg.wrap(10, -20); fail("should throw an exception"); seg.wrap(10, 1024); fail("should throw an exception"); seg.wrap(13, 47); fail("should fail with an exception");
ByteBuffer wrapAsByteBuffer(int length) { if (length <= (limit - position)) { return segment.wrap(position, length); } else { throw new IllegalArgumentException("require more than remaining bytes, required length: " + length + ", bytes left: " + (limit - position)); } }
public ByteBuffer getNioBuffer() { synchronized (recycleLock) { ensureNotRecycled(); // the memory segment returns a distinct buffer every time return memorySegment.wrap(0, currentSize); } }
@Override public ByteBuf setBytes(int index, ByteBuffer src) { // adapted from UnpooledDirectByteBuf: checkIndex(index, src.remaining()); ByteBuffer tmpBuf = memorySegment.wrap(index, src.remaining()); tmpBuf.put(src); return this; }
@Override public int setBytes(int index, FileChannel in, long position, int length) throws IOException { // adapted from UnpooledDirectByteBuf: checkIndex(index, length); ByteBuffer tmpBuf = memorySegment.wrap(index, length); try { return in.read(tmpBuf, position); } catch (ClosedChannelException ignored) { return -1; } }
@Override public int getBytes(int index, FileChannel out, long position, int length) throws IOException { // adapted from UnpooledDirectByteBuf: checkIndex(index, length); if (length == 0) { return 0; } ByteBuffer tmpBuf = memorySegment.wrap(index, length); return out.write(tmpBuf, position); }
@Override public int getBytes(int index, FileChannel out, long position, int length) throws IOException { // adapted from UnpooledDirectByteBuf: checkIndex(index, length); if (length == 0) { return 0; } ByteBuffer tmpBuf = memorySegment.wrap(index, length); return out.write(tmpBuf, position); }
@Override public int setBytes(int index, FileChannel in, long position, int length) throws IOException { // adapted from UnpooledDirectByteBuf: checkIndex(index, length); ByteBuffer tmpBuf = memorySegment.wrap(index, length); try { return in.read(tmpBuf, position); } catch (ClosedChannelException ignored) { return -1; } }
@Override public ByteBuffer nioBuffer(int index, int length) { checkIndex(index, length); return memorySegment.wrap(index, length).slice(); }
@Override public ByteBuf setBytes(int index, ByteBuffer src) { // adapted from UnpooledDirectByteBuf: checkIndex(index, src.remaining()); ByteBuffer tmpBuf = memorySegment.wrap(index, src.remaining()); tmpBuf.put(src); return this; }
@Override public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) { // adapted from UnpooledDirectByteBuf: checkSrcIndex(index, length, srcIndex, src.length); ByteBuffer tmpBuf = memorySegment.wrap(index, length); tmpBuf.put(src, srcIndex, length); return this; }
@Override public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) { // adapted from UnpooledDirectByteBuf: checkSrcIndex(index, length, srcIndex, src.length); ByteBuffer tmpBuf = memorySegment.wrap(index, length); tmpBuf.put(src, srcIndex, length); return this; }
@Override public ByteBuffer nioBuffer(int index, int length) { checkIndex(index, length); return memorySegment.wrap(index, length).slice(); }
@Override public void write() throws IOException { try { FileUtils.writeCompletely(this.channel.fileChannel, this.segment.wrap(0, this.segment.size())); } catch (NullPointerException npex) { throw new IOException("Memory segment has been released."); } }
@Override public int getBytes(int index, GatheringByteChannel out, int length) throws IOException { // adapted from UnpooledDirectByteBuf: checkIndex(index, length); if (length == 0) { return 0; } ByteBuffer tmpBuf = memorySegment.wrap(index, length); return out.write(tmpBuf); }
@Override public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException { // adapted from UnpooledDirectByteBuf: checkIndex(index, length); ByteBuffer tmpBuf = memorySegment.wrap(index, length); try { return in.read(tmpBuf); } catch (ClosedChannelException ignored) { return -1; } }
@Override public int getBytes(int index, GatheringByteChannel out, int length) throws IOException { // adapted from UnpooledDirectByteBuf: checkIndex(index, length); if (length == 0) { return 0; } ByteBuffer tmpBuf = memorySegment.wrap(index, length); return out.write(tmpBuf); }
@Override public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException { // adapted from UnpooledDirectByteBuf: checkIndex(index, length); ByteBuffer tmpBuf = memorySegment.wrap(index, length); try { return in.read(tmpBuf); } catch (ClosedChannelException ignored) { return -1; } }
@Override public int getBytes(int index, GatheringByteChannel out, int length) throws IOException { // adapted from UnpooledDirectByteBuf: checkIndex(index, length); if (length == 0) { return 0; } ByteBuffer tmpBuf = memorySegment.wrap(index, length); return out.write(tmpBuf); }
@Override public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException { // adapted from UnpooledDirectByteBuf: checkIndex(index, length); ByteBuffer tmpBuf = memorySegment.wrap(index, length); try { return in.read(tmpBuf); } catch (ClosedChannelException ignored) { return -1; } }