/** * 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) { for (Buffer b : buffers) { int pos = (null == buffer ? 0 : buffer.position()); int len = b.remaining(); ensureCapacity(len); buffer.put(b.byteBuffer()); buffer.position(pos + len); } return this; }
/** * Very efficient method for parsing an {@link Integer} from the given {@literal Buffer}. Much * faster than {@link Integer#parseInt(String)}. * * @param b The {@literal Buffer} to slice. * @return The int value or {@literal null} if the {@literal Buffer} could not be read. */ public static Integer parseInt(Buffer b) { if (b.remaining() == 0) { return null; } b.snapshot(); int len = b.remaining(); int num = 0; int dec = 1; for (int i = (b.position + len); i > b.position; ) { char c = (char) b.buffer.get(--i); num += Character.getNumericValue(c) * dec; dec *= 10; } b.reset(); return num; }
/** * Very efficient method for parsing a {@link Long} from the given {@literal Buffer}. Much faster * than {@link Long#parseLong(String)}. * * @param b The {@literal Buffer} to slice. * @return The long value or {@literal null} if the {@literal Buffer} could not be read. */ public static Long parseLong(Buffer b) { if (b.remaining() == 0) { return null; } ByteBuffer bb = b.buffer; int origPos = bb.position(); int len = bb.remaining(); long num = 0; int dec = 1; for (int i = len; i > 0; ) { char c = (char) bb.get(--i); num += Character.getNumericValue(c) * dec; dec *= 10; } bb.position(origPos); return num; }