@Override public void write(byte[] bytes, int off, int len) throws IOException { DefaultDataBuffer.this.write(bytes, off, len); } }
static DefaultDataBuffer fromFilledByteBuffer(DefaultDataBufferFactory dataBufferFactory, ByteBuffer byteBuffer) { DefaultDataBuffer dataBuffer = new DefaultDataBuffer(dataBufferFactory, byteBuffer); dataBuffer.writePosition(byteBuffer.remaining()); return dataBuffer; }
throw new IllegalArgumentException(String.format("'newCapacity' %d must be higher than 0", newCapacity)); int readPosition = readPosition(); int writePosition = writePosition(); int oldCapacity = capacity(); ByteBuffer newBuffer = allocate(newCapacity, oldBuffer.isDirect()); ((Buffer) oldBuffer).position(0).limit(oldBuffer.capacity()); ((Buffer) newBuffer).position(0).limit(oldBuffer.capacity()); newBuffer.put(oldBuffer); newBuffer.clear(); setNativeBuffer(newBuffer); ByteBuffer newBuffer = allocate(newCapacity, oldBuffer.isDirect()); if (readPosition < newCapacity) { if (writePosition > newCapacity) { writePosition = newCapacity; writePosition(writePosition); readPosition(newCapacity); writePosition(newCapacity); setNativeBuffer(newBuffer);
@Override public DataBuffer ensureCapacity(int length) { if (length > writableByteCount()) { int newCapacity = calculateCapacity(this.writePosition + length); capacity(newCapacity); } return this; }
@Override public ByteBuffer asByteBuffer() { return asByteBuffer(this.readPosition, readableByteCount()); }
@Override public DefaultDataBuffer writePosition(int writePosition) { assertIndex(writePosition >= this.readPosition, "'writePosition' %d must be >= %d", writePosition, this.readPosition); assertIndex(writePosition <= this.capacity, "'writePosition' %d must be <= %d", writePosition, this.capacity); this.writePosition = writePosition; return this; }
@Override public DefaultDataBuffer write(byte b) { ensureCapacity(1); int pos = this.writePosition; this.byteBuffer.put(pos, b); this.writePosition = pos + 1; return this; }
@Override public ByteBuffer asByteBuffer(int index, int length) { checkIndex(index, length); ByteBuffer duplicate = this.byteBuffer.duplicate(); // Explicit access via Buffer base type for compatibility // with covariant return type on JDK 9's ByteBuffer... Buffer buffer = duplicate; buffer.position(index); buffer.limit(index + length); return duplicate.slice(); }
@Override public DefaultDataBuffer allocateBuffer(int initialCapacity) { ByteBuffer byteBuffer = (this.preferDirect ? ByteBuffer.allocateDirect(initialCapacity) : ByteBuffer.allocate(initialCapacity)); return DefaultDataBuffer.fromEmptyByteBuffer(this, byteBuffer); }
static DefaultDataBuffer fromEmptyByteBuffer(DefaultDataBufferFactory dataBufferFactory, ByteBuffer byteBuffer) { return new DefaultDataBuffer(dataBufferFactory, byteBuffer); }
@Override public DataBuffer ensureCapacity(int length) { if (length > writableByteCount()) { int newCapacity = calculateCapacity(this.writePosition + length); capacity(newCapacity); } return this; }
@Override public DefaultDataBuffer readPosition(int readPosition) { assertIndex(readPosition >= 0, "'readPosition' %d must be >= 0", readPosition); assertIndex(readPosition <= this.writePosition, "'readPosition' %d must be <= %d", readPosition, this.writePosition); this.readPosition = readPosition; return this; }
@Override public ByteBuffer asByteBuffer() { return asByteBuffer(this.readPosition, readableByteCount()); }
@Override public DefaultDataBuffer write(ByteBuffer... buffers) { if (!ObjectUtils.isEmpty(buffers)) { int capacity = Arrays.stream(buffers).mapToInt(ByteBuffer::remaining).sum(); ensureCapacity(capacity); Arrays.stream(buffers).forEach(this::write); } return this; }
@Override public DefaultDataBuffer slice(int index, int length) { checkIndex(index, length); int oldPosition = this.byteBuffer.position(); // Explicit access via Buffer base type for compatibility // with covariant return type on JDK 9's ByteBuffer... Buffer buffer = this.byteBuffer; try { buffer.position(index); ByteBuffer slice = this.byteBuffer.slice(); // Explicit cast for compatibility with covariant return type on JDK 9's ByteBuffer ((Buffer) slice).limit(length); return new SlicedDefaultDataBuffer(slice, this.dataBufferFactory, length); } finally { buffer.position(oldPosition); } }
@Override public DefaultDataBuffer allocateBuffer(int initialCapacity) { ByteBuffer byteBuffer = (this.preferDirect ? ByteBuffer.allocateDirect(initialCapacity) : ByteBuffer.allocate(initialCapacity)); return DefaultDataBuffer.fromEmptyByteBuffer(this, byteBuffer); }
static DefaultDataBuffer fromEmptyByteBuffer(DefaultDataBufferFactory dataBufferFactory, ByteBuffer byteBuffer) { return new DefaultDataBuffer(dataBufferFactory, byteBuffer); }
throw new IllegalArgumentException(String.format("'newCapacity' %d must be higher than 0", newCapacity)); int readPosition = readPosition(); int writePosition = writePosition(); int oldCapacity = capacity(); ByteBuffer newBuffer = allocate(newCapacity, oldBuffer.isDirect()); ((Buffer) oldBuffer).position(0).limit(oldBuffer.capacity()); ((Buffer) newBuffer).position(0).limit(oldBuffer.capacity()); newBuffer.put(oldBuffer); newBuffer.clear(); setNativeBuffer(newBuffer); ByteBuffer newBuffer = allocate(newCapacity, oldBuffer.isDirect()); if (readPosition < newCapacity) { if (writePosition > newCapacity) { writePosition = newCapacity; writePosition(writePosition); readPosition(newCapacity); writePosition(newCapacity); setNativeBuffer(newBuffer);
@Override public void write(int b) throws IOException { DefaultDataBuffer.this.write((byte) b); }
private void ensureCapacity(int length) { if (length <= writableByteCount()) { return; } int newCapacity = calculateCapacity(this.writePosition + length); capacity(newCapacity); }