io.netty.buffer.ByteBufProcessor Java Examples

The following examples show how to use io.netty.buffer.ByteBufProcessor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReplayingDecoderBuffer.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
@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;
    }
}
 
Example #2
Source File: StompFrameDecoder.java    From hazelcastmq with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #3
Source File: AnalyticsHandler.java    From couchbase-jvm-core with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #4
Source File: QueryHandler.java    From couchbase-jvm-core with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #5
Source File: ViewHandler.java    From couchbase-jvm-core with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #6
Source File: TestSplitProcessor.java    From tajo with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #7
Source File: TestSplitProcessor.java    From tajo with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #8
Source File: ReplayingDecoderBuffer.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
public int forEachByteDesc(int index, int length, ByteBufProcessor processor) {
    if (index + length > buffer.writerIndex()) {
        throw REPLAY;
    }

    return buffer.forEachByteDesc(index, length, processor);
}
 
Example #9
Source File: ReplayingDecoderBuffer.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
public int forEachByteDesc(ByteBufProcessor processor) {
    if (terminated) {
        return buffer.forEachByteDesc(processor);
    } else {
        reject();
        return 0;
    }
}
 
Example #10
Source File: ReplayingDecoderBuffer.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
@Override
public int forEachByte(ByteBufProcessor processor) {
    int ret = buffer.forEachByte(processor);
    if (ret < 0) {
        throw REPLAY;
    } else {
        return ret;
    }
}
 
Example #11
Source File: ProtocolBuffer.java    From CloudNet with Apache License 2.0 4 votes vote down vote up
public int forEachByteDesc(int i, int i1, ByteBufProcessor byteBufProcessor) {
    return byteBuf.forEachByteDesc(i, i1, byteBufProcessor);
}
 
Example #12
Source File: ProtocolBuffer.java    From CloudNet with Apache License 2.0 4 votes vote down vote up
public int forEachByteDesc(ByteBufProcessor byteBufProcessor) {
    return byteBuf.forEachByteDesc(byteBufProcessor);
}
 
Example #13
Source File: ProtocolBuffer.java    From CloudNet with Apache License 2.0 4 votes vote down vote up
public int forEachByte(int i, int i1, ByteBufProcessor byteBufProcessor) {
    return byteBuf.forEachByte(i, i1, byteBufProcessor);
}
 
Example #14
Source File: ProtocolBuffer.java    From CloudNet with Apache License 2.0 4 votes vote down vote up
public int forEachByte(ByteBufProcessor byteBufProcessor) {
    return byteBuf.forEachByte(byteBufProcessor);
}
 
Example #15
Source File: ByteBufJsonParser.java    From couchbase-jvm-core with Apache License 2.0 4 votes vote down vote up
/**
 * Handle the logic for reading down a JSON Value.
 *
 * @param level the current level.
 * @throws EOFException if more data is needed.
 */
private void readValue(final JsonLevel level) throws EOFException {
    int readerIndex = content.readerIndex();
    ByteBufProcessor processor = null;
    Mode mode = level.peekMode();
    switch (mode) {
        case JSON_ARRAY_VALUE:
            arProcessor.reset();
            processor = arProcessor;
            break;
        case JSON_OBJECT_VALUE:
            obProcessor.reset();
            processor = obProcessor;
            break;
        case JSON_STRING_VALUE:
        case JSON_STRING_HASH_KEY:
            processor = stProcessor;
            break;
        case JSON_NULL_VALUE:
            nullProcessor.reset();
            processor = nullProcessor;
            break;
        case JSON_BOOLEAN_TRUE_VALUE:
            processor = trueProcessor;
            break;
        case JSON_BOOLEAN_FALSE_VALUE:
            processor = falseProcessor;
            break;
        case JSON_NUMBER_VALUE:
            processor = numProcessor;
            break;
    }
    int length;
    boolean shouldSaveValue = tree.isTerminalPath(level.jsonPointer()) || mode == Mode.JSON_STRING_HASH_KEY;

    int lastValidIndex = content.forEachByte(processor);
    if (lastValidIndex == -1) {
        if (mode == Mode.JSON_NUMBER_VALUE && content.readableBytes() > 2) {
            length = 1;
            level.setCurrentValue(this.content.copy(readerIndex - 1, length), length);
            //no need to skip here
            this.content.discardReadBytes();
            level.emitJsonPointerValue();
        } else {
            throw NEED_MORE_DATA;
        }
    } else {
        if (mode == Mode.JSON_OBJECT_VALUE ||
                mode == Mode.JSON_ARRAY_VALUE ||
                mode == Mode.JSON_STRING_VALUE ||
                mode == Mode.JSON_STRING_HASH_KEY ||
                mode == Mode.JSON_NULL_VALUE ||
                mode == Mode.JSON_BOOLEAN_TRUE_VALUE ||
                mode == Mode.JSON_BOOLEAN_FALSE_VALUE) {

            length = lastValidIndex - readerIndex + 1;
            if (shouldSaveValue) {
                level.setCurrentValue(this.content.copy(readerIndex - 1, length + 1), length);
                level.emitJsonPointerValue();
            }
            this.content.skipBytes(length);
            this.content.discardReadBytes();
        } else {
            //special handling for number as they don't need structural tokens intact
            //and the processor returns only on an unacceptable value rather than a finite state automaton
            length = lastValidIndex - readerIndex;
            if (length > 0) {
                if (shouldSaveValue) {
                    level.setCurrentValue(this.content.copy(readerIndex - 1, length + 1), length);
                    level.emitJsonPointerValue();
                }
                this.content.skipBytes(length);
                this.content.discardReadBytes();
            } else {
                length = 1;
                if (shouldSaveValue) {
                    level.setCurrentValue(this.content.copy(readerIndex - 1, length), length);
                    this.content.discardReadBytes();
                    level.emitJsonPointerValue();
                }
            }
        }
    }
    if (mode != Mode.JSON_STRING_HASH_KEY) {
        level.removeLastTokenFromJsonPointer();
    }
    level.popMode();
}
 
Example #16
Source File: DeadBuf.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public int forEachByteDesc(int arg0, int arg1, ByteBufProcessor arg2) {
  return 0;
}
 
Example #17
Source File: DeadBuf.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public int forEachByteDesc(ByteBufProcessor arg0) {
  return 0;
}
 
Example #18
Source File: DeadBuf.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public int forEachByte(int arg0, int arg1, ByteBufProcessor arg2) {
  return 0;
}
 
Example #19
Source File: DeadBuf.java    From Bats with Apache License 2.0 4 votes vote down vote up
@Override
public int forEachByte(ByteBufProcessor arg0) {
  return 0;
}