Example usage for io.netty.buffer ByteBuf readByte

List of usage examples for io.netty.buffer ByteBuf readByte

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf readByte.

Prototype

public abstract byte readByte();

Source Link

Document

Gets a byte at the current readerIndex and increases the readerIndex by 1 in this buffer.

Usage

From source file:io.nodyn.http.HTTPParser.java

License:Apache License

protected boolean readStatusLine() {
    ByteBuf line = readLine();

    if (line == null) {
        return false;
    }//  w ww.j  av  a  2s .co m

    int space = line.indexOf(line.readerIndex(), line.readerIndex() + line.readableBytes(), (byte) ' ');

    if (space < 0) {
        setError(Error.INVALID_VERSION);
        return false;
    }

    int len = space - line.readerIndex();

    ByteBuf versionBuf = line.readSlice(len);

    if (!readVersion(versionBuf)) {
        setError(Error.INVALID_VERSION);
        return false;
    }

    // skip space
    line.readByte();

    space = line.indexOf(line.readerIndex(), line.readerIndex() + line.readableBytes(), (byte) ' ');

    if (space < 0) {
        setError(Error.INVALID_STATUS);
        return false;
    }

    len = space - line.readerIndex();

    ByteBuf statusBuf = line.readSlice(len);

    int status = -1;

    try {
        status = Integer.parseInt(statusBuf.toString(UTF8));
    } catch (NumberFormatException e) {
        setError(Error.INVALID_STATUS);
        return false;
    }

    if (status > 999 || status < 100) {
        setError(Error.INVALID_STATUS);
        return false;
    }

    this.statusCode = status;

    // skip space
    line.readByte();

    ByteBuf messageBuf = line.readSlice(line.readableBytes());

    this.statusMessage = messageBuf.toString(UTF8).trim();

    return true;
}

From source file:io.nodyn.http.HTTPParser.java

License:Apache License

protected boolean readHeader(ByteBuf line, List<String> target, boolean analyze) {
    int colonLoc = line.indexOf(line.readerIndex(), line.readerIndex() + line.readableBytes(), (byte) ':');

    if (colonLoc < 0) {
        // maybe it's a continued header
        if (line.readableBytes() > 1) {
            char c = (char) line.getByte(0);
            if (c == ' ' || c == '\t') {
                // it IS a continued header value
                int lastIndex = this.headers.size() - 1;
                String val = this.headers.get(lastIndex);
                val = val + " " + line.toString(ASCII).trim();
                this.headers.set(lastIndex, val);
                return true;
            }/*w w w.ja  v a 2 s . c o  m*/
        }
        return false;
    }

    int len = colonLoc - line.readerIndex();
    ByteBuf keyBuf = line.readSlice(len);

    // skip colon
    line.readByte();

    ByteBuf valueBuf = line.readSlice(line.readableBytes());

    String key = keyBuf.toString(UTF8).trim();
    String value = valueBuf.toString(UTF8).trim();

    target.add(key);
    target.add(value);

    if (analyze) {
        return analyzeHeader(key.toLowerCase(), value);
    }

    return true;
}

From source file:io.reactiverse.pgclient.impl.codec.decoder.MessageDecoder.java

License:Apache License

private void decodeReadyForQuery(ByteBuf in) {
    byte id = in.readByte();
    TxStatus txStatus;/*from  ww  w . j a va 2  s.  com*/
    if (id == I) {
        txStatus = TxStatus.IDLE;
    } else if (id == T) {
        txStatus = TxStatus.ACTIVE;
    } else {
        txStatus = TxStatus.FAILED;
    }
    inflight.peek().handleReadyForQuery(txStatus);
}

From source file:io.reactiverse.pgclient.impl.codec.decoder.MessageDecoder.java

License:Apache License

private void decodeErrorOrNotice(Response response, ByteBuf in) {

    byte type;/*from ww  w  . ja  v  a  2 s .  c o m*/

    while ((type = in.readByte()) != 0) {

        switch (type) {

        case ErrorOrNoticeType.SEVERITY:
            response.setSeverity(Util.readCStringUTF8(in));
            break;

        case ErrorOrNoticeType.CODE:
            response.setCode(Util.readCStringUTF8(in));
            break;

        case ErrorOrNoticeType.MESSAGE:
            response.setMessage(Util.readCStringUTF8(in));
            break;

        case ErrorOrNoticeType.DETAIL:
            response.setDetail(Util.readCStringUTF8(in));
            break;

        case ErrorOrNoticeType.HINT:
            response.setHint(Util.readCStringUTF8(in));
            break;

        case ErrorOrNoticeType.INTERNAL_POSITION:
            response.setInternalPosition(Util.readCStringUTF8(in));
            break;

        case ErrorOrNoticeType.INTERNAL_QUERY:
            response.setInternalQuery(Util.readCStringUTF8(in));
            break;

        case ErrorOrNoticeType.POSITION:
            response.setPosition(Util.readCStringUTF8(in));
            break;

        case ErrorOrNoticeType.WHERE:
            response.setWhere(Util.readCStringUTF8(in));
            break;

        case ErrorOrNoticeType.FILE:
            response.setFile(Util.readCStringUTF8(in));
            break;

        case ErrorOrNoticeType.LINE:
            response.setLine(Util.readCStringUTF8(in));
            break;

        case ErrorOrNoticeType.ROUTINE:
            response.setRoutine(Util.readCStringUTF8(in));
            break;

        case ErrorOrNoticeType.SCHEMA:
            response.setSchema(Util.readCStringUTF8(in));
            break;

        case ErrorOrNoticeType.TABLE:
            response.setTable(Util.readCStringUTF8(in));
            break;

        case ErrorOrNoticeType.COLUMN:
            response.setColumn(Util.readCStringUTF8(in));
            break;

        case ErrorOrNoticeType.DATA_TYPE:
            response.setDataType(Util.readCStringUTF8(in));
            break;

        case ErrorOrNoticeType.CONSTRAINT:
            response.setConstraint(Util.readCStringUTF8(in));
            break;

        default:
            Util.readCStringUTF8(in);
            break;
        }
    }
}

From source file:io.reactiverse.pgclient.impl.codec.util.Util.java

License:Apache License

public static String readCString(ByteBuf src, Charset charset) {
    int len = src.bytesBefore(ZERO);
    String s = src.readCharSequence(len, charset).toString();
    src.readByte();
    return s;//  ww  w  . j  a  v a2s  . c o  m
}

From source file:io.reactiverse.pgclient.impl.codec.util.Util.java

License:Apache License

public static String readCStringUTF8(ByteBuf src) {
    int len = src.bytesBefore(ZERO);
    String s = src.readCharSequence(len, UTF_8).toString();
    src.readByte();
    return s;/*from   ww w. ja  v  a2 s. co  m*/
}

From source file:io.reactivex.netty.protocol.text.sse.ServerSentEventDecoder.java

License:Apache License

private String readFullLine(ByteBuf in) {
    StringBuilder line = new StringBuilder();

    for (;;) {//from   w w  w  . j  a  v  a2  s. c o  m
        char c = (char) in.readByte();
        if (isLineDelimiter(c)) {
            // If colon is at the very end of a line, having an empty string will help us avoid IndexOutOfBoundException
            // without checking the colon is at the end of the line.
            line.append("");

            checkpoint(State.END_OF_LINE);
            break;
        }

        line.append(c);
    }
    return line.toString();
}

From source file:io.reactivex.netty.protocol.text.sse.ServerSentEventDecoder.java

License:Apache License

private int skipLineDelimiters(ByteBuf in) {
    int skipped = 0;
    while (in.writerIndex() - in.readerIndex() > 0) {
        // the above check is needed to ensure that last event is delivered
        // otherwise, an exception (Netty's Signal object) will be thrown
        // from ReplayingDecoderBuffer.readByte()
        char c = (char) in.readByte();
        if (isLineDelimiter(c)) {
            skipped += 1;//from  ww  w  .  j ava 2  s  . c o  m
            continue;
        }

        // Leave the reader index at the first letter of the next line, if any
        in.readerIndex(in.readerIndex() - 1);
        checkpoint(State.NEW_LINE);
        break;
    }

    return skipped;
}

From source file:io.reactivex.netty.protocol.text.StringLineDecoder.java

License:Apache License

private String readFullLine(ByteBuf in) {
    StringBuilder line = new StringBuilder();

    for (;;) {/*  w w w .  j  a v a2s .c o  m*/
        char c = (char) in.readByte();
        if (isLineDelimiter(c)) {
            checkpoint(State.END_OF_LINE);
            return line.toString();
        }

        line.append(c);
    }
}