/** * Append the given {@link ByteBuffer} to this {@literal Buffer}. * * @param buffers The {@link ByteBuffer ByteBuffers} to append. * @return {@literal this} */ public Buffer append(ByteBuffer... buffers) { for (ByteBuffer bb : buffers) { if (bb != null) { ensureCapacity(bb.remaining()); buffer.put(bb); } } return this; }
private void shift(int right) { ByteBuffer currentBuffer; if (null == buffer) { ensureCapacity(right); currentBuffer = buffer; } else { currentBuffer = buffer.slice(); } int len = buffer.remaining(); int pos = buffer.position(); ensureCapacity(right + len); buffer.position(pos + right); buffer.put(currentBuffer); buffer.position(pos); snapshot(); }
/** * Append the given {@link Buffer} to this {@literal Buffer}. * * @param buffers The {@link Buffer Buffers} to append. * @return {@literal this} */ public Buffer append(Buffer... buffers) { if (buffers == null) return this; for (Buffer b : buffers) { if (b == null) continue; int pos = position(); int len = b.remaining(); ensureCapacity(len); if (b.byteBuffer() != null) { buffer.put(b.byteBuffer()); buffer.position(pos + len); } } return this; }
/** * Create an {@literal Buffer} that has an internal {@link ByteBuffer} allocated to the given size * and optional make this buffer fixed-length. * * @param atLeast Allocate this many bytes immediately. * @param fixed {@literal true} to make this buffer fixed-length, {@literal false} otherwise. */ public Buffer(int atLeast, boolean fixed) { if (fixed) { if (atLeast <= ReactiveState.MAX_IO_BUFFER_SIZE) { this.buffer = ByteBuffer.allocate(atLeast); } else { throw new IllegalArgumentException( "Requested buffer size exceeds maximum allowed (" + ReactiveState.MAX_IO_BUFFER_SIZE + ")"); } } else { ensureCapacity(atLeast); } this.dynamic = !fixed; }
/** * Append the given {@code byte[]} to this {@literal Buffer}, starting at the given index and * continuing for the given length. * * @param b the bytes to append * @param start the index of where to start copying bytes * @param len the len of the bytes to copy * @return {@literal this} */ public Buffer append(byte[] b, int start, int len) { ensureCapacity(b.length); buffer.put(b, start, len); return this; }
/** * Append the given {@code byte[]} to this {@literal Buffer}. * * @param b The {@code byte[]} to append. * @return {@literal this} */ public Buffer append(byte[] b) { ensureCapacity(b.length); buffer.put(b); return this; }
/** * Append the given {@code byte} to this {@literal Buffer}. * * @param b The {@code byte} to append. * @return {@literal this} */ public Buffer append(byte b) { ensureCapacity(1); buffer.put(b); return this; }
/** * Append the given {@code char} to this {@literal Buffer}. * * @param c The {@code char} to append. * @return {@literal this} */ public Buffer append(char c) { ensureCapacity(2); buffer.putChar(c); return this; }
/** * Append the given {@code long} to this {@literal Buffer}. * * @param l The {@code long} to append. * @return {@literal this} */ public Buffer append(long l) { ensureCapacity(8); buffer.putLong(l); return this; }
/** * Append the given {@code int} to this {@literal Buffer}. * * @param i The {@code int} to append. * @return {@literal this} */ public Buffer append(int i) { ensureCapacity(4); buffer.putInt(i); return this; }
/** * Append the given {@code short} to this {@literal Buffer}. * * @param s The {@code short} to append. * @return {@literal this} */ public Buffer append(short s) { ensureCapacity(2); buffer.putShort(s); return this; }
/** * Append the given String to this {@literal Buffer}. * * @param s The String to append. * @return {@literal this} */ public Buffer append(String s) { ensureCapacity(s.length()); buffer.put(s.getBytes()); return this; }