@Override public int forEachByte(int index, int length, ByteBufProcessor processor) { final int writerIndex = buffer.writerIndex(); if (index >= writerIndex) { throw REPLAY; } if (index <= writerIndex - length) { return buffer.forEachByte(index, length, processor); } int ret = buffer.forEachByte(index, writerIndex - index, processor); if (ret < 0) { throw REPLAY; } else { return ret; } }
private String decodeString(ByteBuf in) throws ProtocolException { final StringBuilder buffer = new StringBuilder(); final MutableBoolean reachCRLF = new MutableBoolean(false); setReaderIndex(in, in.forEachByte(new ByteBufProcessor() { @Override public boolean process(byte value) throws Exception { if (value == '\n') { if ((byte) buffer.charAt(buffer.length() - 1) != '\r') { throw new ProtocolException("Response is not ended by CRLF"); } else { buffer.setLength(buffer.length() - 1); reachCRLF.setTrue(); return false; } } else { buffer.append((char) value); return true; } } })); return reachCRLF.booleanValue() ? buffer.toString() : null; }
@Override public int forEachByte(int index, int length, ByteBufProcessor processor) { final int writerIndex = buffer.writerIndex(); if (index >= writerIndex) { throw REPLAY; } if (index + length <= writerIndex) { return buffer.forEachByte(index, length, processor); } int ret = buffer.forEachByte(index, writerIndex - index, processor); if (ret < 0) { throw REPLAY; } else { return ret; } }
@Override public Iterable<String> split(final ByteBuf buffer, final Charset charset, final boolean includeRemainingData) { return () -> new AbstractIterator<String>() { @Override protected String computeNext() { ByteBuf fullLine = null; try { if (!buffer.isReadable()) { return endOfData(); } final int i = buffer.forEachByte(ByteBufProcessor.FIND_CRLF); if (i == -1) { if (includeRemainingData) { final ByteBuf remaining = buffer.readBytes(buffer.readableBytes()); return remaining.toString(charset); } else { return endOfData(); } } fullLine = buffer.readBytes(i); // Strip the \r/\n bytes from the buffer. final byte readByte = buffer.readByte(); // the \r or \n byte if (readByte == '\r') { buffer.readByte(); // the \n byte if previous was \r } return fullLine.toString(charset); } finally { buffer.discardReadBytes(); if (fullLine != null) { fullLine.release(); } } } }; }
@Override protected ByteBuf process(final StreamCipher cipher, ByteBuf data) { final ByteBuf slice = data.slice(); slice.writerIndex(0); data.forEachByte(data.readerIndex(), data.readableBytes(), new ByteBufProcessor() { @Override public boolean process(byte b) throws Exception { slice.writeByte(cipher.returnByte(b)); return true; } }); return data; }
private String readNullTerminatedString(Charset charset) { int indexOfNull = buffer.forEachByte(ByteBufProcessor.FIND_NUL); if (indexOfNull == -1) { throw new UaSerializationException( StatusCodes.Bad_DecodingError, "null terminator not found"); } int index = buffer.readerIndex(); int length = indexOfNull - index; String str = buffer.toString(index, length, charset); buffer.skipBytes(length + 1); return str; }
@Override public Iterable<String> split(final ByteBuf buffer, final Charset charset, final boolean includeRemainingData) { return new Iterable<String>() { @Override public Iterator<String> iterator() { return new AbstractIterator<String>() { @Override protected String computeNext() { try { if (!buffer.isReadable()) { return endOfData(); } final int i = buffer.forEachByte(ByteBufProcessor.FIND_CRLF); if (i == -1) { if (includeRemainingData) { final ByteBuf remaining = buffer.readBytes(buffer.readableBytes()); return remaining.toString(charset); } else { return endOfData(); } } final ByteBuf fullLine = buffer.readBytes(i); // Strip the \r/\n bytes from the buffer. final byte readByte = buffer.readByte(); // the \r or \n byte if (readByte == '\r') { buffer.readByte(); // the \n byte if previous was \r } return fullLine.toString(charset); } finally { buffer.discardReadBytes(); } } }; } }; }
@Override public int forEachByte(ByteBufProcessor processor) { int ret = buffer.forEachByte(processor); if (ret < 0) { throw REPLAY; } else { return ret; } }
@Override public int forEachByteDesc(ByteBufProcessor processor) { if (terminated) { return buffer.forEachByteDesc(processor); } else { reject(); return 0; } }
@Override public int forEachByteDesc(int index, int length, ByteBufProcessor processor) { if (index + length > buffer.writerIndex()) { throw REPLAY; } return buffer.forEachByteDesc(index, length, processor); }
@Test public void testMultiCharFieldSplitProcessor1() throws IOException { String data = "abc||||de||"; final ByteBuf buf = releaseLater( Unpooled.copiedBuffer(data, CharsetUtil.ISO_8859_1)); final int len = buf.readableBytes(); ByteBufProcessor processor = new MultiBytesFieldSplitProcessor("||".getBytes()); assertEquals(4, buf.forEachByte(0, len, processor)); assertEquals(6, buf.forEachByte(5, len - 5, processor)); assertEquals(10, buf.forEachByte(7, len - 7, processor)); assertEquals(-1, buf.forEachByte(11, len - 11, processor)); }
@Test public void testMultiCharFieldSplitProcessor2() throws IOException { String data = "abcㅎㅎdeㅎ"; final ByteBuf buf = releaseLater( Unpooled.copiedBuffer(data, CharsetUtil.UTF_8)); final int len = buf.readableBytes(); ByteBufProcessor processor = new MultiBytesFieldSplitProcessor("ㅎ".getBytes()); assertEquals(5, buf.forEachByte(0, len, processor)); assertEquals(8, buf.forEachByte(6, len - 6, processor)); assertEquals(13, buf.forEachByte(9, len - 9, processor)); assertEquals(-1, buf.forEachByte(14, len - 14, processor)); }
@Override public int forEachByte(ByteBufProcessor processor) { int ret = buffer.forEachByte(processor); if (!terminated && ret < 0) { throw REPLAY; } else { return ret; } }
/** * Parse out the info portion from the header part of the query response. * * This includes the total rows, but also debug info if attached. */ private void parseViewInfo() { int rowsStart = -1; for (int i = responseContent.readerIndex(); i < responseContent.writerIndex() - 2; i++) { byte curr = responseContent.getByte(i); byte f1 = responseContent.getByte(i + 1); byte f2 = responseContent.getByte(i + 2); if (curr == '"' && f1 == 'r' && f2 == 'o') { rowsStart = i; break; } } if (rowsStart == -1) { return; } ByteBuf info = responseContent.readBytes(rowsStart - responseContent.readerIndex()); int closingPointer = info.forEachByteDesc(new ByteBufProcessor() { @Override public boolean process(byte value) throws Exception { return value != ','; } }); if (closingPointer > 0) { info.setByte(closingPointer, '}'); viewInfoObservable.onNext(info); } else { //JVMCBC-360 don't forget to release the now unused info ByteBuf info.release(); viewInfoObservable.onNext(Unpooled.EMPTY_BUFFER); } viewInfoObservable.onCompleted(); viewParsingState = QUERY_STATE_ROWS; }
/** * Parse the signature section in the N1QL response. */ private void parseQuerySignature(boolean lastChunk) { ByteBufProcessor processor = null; //signature can be any valid JSON item, which get tricky to detect //let's try to find out what's the boundary character int openPos = responseContent.forEachByte(new WhitespaceSkipper()) - responseContent.readerIndex(); if (openPos < 0) { //only whitespace left in the buffer, need more data return; } char openChar = (char) responseContent.getByte(responseContent.readerIndex() + openPos); if (openChar == '{') { processor = new ClosingPositionBufProcessor('{', '}', true); } else if (openChar == '[') { processor = new ClosingPositionBufProcessor('[', ']', true); } else if (openChar == '"') { processor = new StringClosingPositionBufProcessor(); } //else this should be a scalar, skip processor int closePos; if (processor != null) { closePos = responseContent.forEachByte(processor) - responseContent.readerIndex(); } else { closePos = findNextChar(responseContent, ',') - 1; } if (closePos > 0) { responseContent.skipBytes(openPos); int length = closePos - openPos + 1; ByteBuf signature = responseContent.readSlice(length); querySignatureObservable.onNext(signature.copy()); } else { //wait for more data return; } //note: the signature section could be absent, so we'll make sure to complete the observable // when receiving status since this is in every well-formed response. sectionDone(); queryParsingState = transitionToNextToken(lastChunk); }
/** * Parse the signature section in the Analytics response. */ private void parseQuerySignature(boolean lastChunk) { ByteBufProcessor processor = null; //signature can be any valid JSON item, which get tricky to detect //let's try to find out what's the boundary character int openPos = responseContent.forEachByte(new WhitespaceSkipper()) - responseContent.readerIndex(); if (openPos < 0) { //only whitespace left in the buffer, need more data return; } char openChar = (char) responseContent.getByte(responseContent.readerIndex() + openPos); if (openChar == '{') { processor = new ClosingPositionBufProcessor('{', '}', true); } else if (openChar == '[') { processor = new ClosingPositionBufProcessor('[', ']', true); } else if (openChar == '"') { processor = new StringClosingPositionBufProcessor(); } //else this should be a scalar, skip processor int closePos; if (processor != null) { closePos = responseContent.forEachByte(processor) - responseContent.readerIndex(); } else { closePos = findNextChar(responseContent, ',') - 1; } if (closePos > 0) { responseContent.skipBytes(openPos); int length = closePos - openPos + 1; ByteBuf signature = responseContent.readSlice(length); querySignatureObservable.onNext(signature.copy()); } else { //wait for more data return; } //note: the signature section could be absent, so we'll make sure to complete the observable // when receiving status since this is in every well-formed response. sectionDone(); queryParsingState = transitionToNextToken(lastChunk); }
/** * Reads the optional EOL (and other control characters) that are permitted * between the end of one frame and the start of the next frame. When a * non-control character is detected, the decoder state will be advanced. * * @param in the input buffer to read from * * @return the next decoder state or null if no checkpoint should be set */ private DecoderState readControlChars(ByteBuf in) { DecoderState nextState = DecoderState.READ_CONTROL_CHARS; int index = in.forEachByte(new ByteBufProcessor() { @Override public boolean process(byte b) throws Exception { switch (b) { // This is a little more lax than the spec which allows for only // EOL character(s) between frames. case ' ': case CARRIAGE_RETURN_CHAR: case LINE_FEED_CHAR: case NULL_CHAR: // ignore the character return true; default: return false; } } }); if (index != -1) { // A non-control character was found so we skip up to that index and // move to the next state. in.readerIndex(index); nextState = DecoderState.READ_COMMAND; } else { // Discard all available bytes because we couldn't find a // non-control character. in.readerIndex(in.writerIndex()); } return nextState; }
@Override public int forEachByte(ByteBufProcessor byteBufProcessor) { return byteBuf.forEachByte(byteBufProcessor); }
@Override public int forEachByte(int i, int i1, ByteBufProcessor byteBufProcessor) { return byteBuf.forEachByte(i, i1, byteBufProcessor); }
@Override public int forEachByteDesc(ByteBufProcessor byteBufProcessor) { return byteBuf.forEachByteDesc(byteBufProcessor); }
@Override public int forEachByteDesc(int i, int i1, ByteBufProcessor byteBufProcessor) { return byteBuf.forEachByteDesc(i, i1, byteBufProcessor); }
public int forEachByte(ByteBufProcessor p_forEachByte_1_) { return this.buf.forEachByte(p_forEachByte_1_); }
public int forEachByte(int p_forEachByte_1_, int p_forEachByte_2_, ByteBufProcessor p_forEachByte_3_) { return this.buf.forEachByte(p_forEachByte_1_, p_forEachByte_2_, p_forEachByte_3_); }
public int forEachByteDesc(ByteBufProcessor p_forEachByteDesc_1_) { return this.buf.forEachByteDesc(p_forEachByteDesc_1_); }
public int forEachByteDesc(int p_forEachByteDesc_1_, int p_forEachByteDesc_2_, ByteBufProcessor p_forEachByteDesc_3_) { return this.buf.forEachByteDesc(p_forEachByteDesc_1_, p_forEachByteDesc_2_, p_forEachByteDesc_3_); }