private String readTypeName(Buffer buffer) { int len = buffer.readInt(); if (buffer.remaining() <= len) { throw new IllegalArgumentException( "Incomplete buffer. Must contain " + len + " bytes, " + "but only " + buffer.remaining() + " were found."); } byte[] bytes = new byte[len]; buffer.read(bytes); return new String(bytes); }
private boolean tryEmit(Buffer buffer) { IN next; Iterator<Buffer.View> views = codec.iterateDecode(buffer, decoderContext); if (!views.hasNext()) { combine(buffer); return false; } Buffer.View cursor; while (views.hasNext()) { cursor = views.next(); if (cursor != null) { next = codec.decodeNext(cursor.get(), decoderContext); if (next != null && BackpressureUtils.getAndSub(PENDING_UPDATER, this, 1L) > 0) { subscriber.onNext(next); } else { combine(buffer.slice(cursor.getStart(), buffer.limit())); return next != null; } } else { combine(buffer); return false; } } if (buffer.remaining() > 0) { combine(buffer); return false; } return true; }
/** * 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; }
/** * 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; }
@Override public Buffer.View next() { int limit = buffer.limit(); int endchunk = canDecodeNext(buffer, context); if (endchunk == -1) { return null; } Buffer.View view = buffer.createView(buffer.position(), endchunk); if (buffer.remaining() > 0) { buffer.position(Math.min(limit, view.getEnd())); buffer.limit(limit); } return view; }
/** * 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; }
protected int canDecodeNext(Buffer buffer, Object context) { return delimiter == null ? (buffer.remaining() > 0 ? buffer.limit() : -1) : buffer.indexOf(delimiter); }
@Override protected int canDecodeNext(Buffer buffer, Object context) { return buffer.remaining() > 0 ? buffer.limit() : -1; }