Example usage for io.netty.buffer ByteBuf getByte

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

Introduction

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

Prototype

public abstract byte getByte(int index);

Source Link

Document

Gets a byte at the specified absolute index in this buffer.

Usage

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;
            }/* ww w  . j  a  va 2s .co  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.DataTypeCodec.java

License:Apache License

private static Object defaultDecodeText(int index, int len, ByteBuf buff) {
    // decode unknown text values as text or as an array if it begins with `{`
    if (len > 1 && buff.getByte(index) == '{') {
        return textDecodeArray(STRING_ARRAY_FACTORY, DataType.TEXT, index, len, buff);
    }//from w ww  .  ja va  2s. c  o m
    return textdecodeTEXT(index, len, buff);
}

From source file:io.reactiverse.pgclient.impl.codec.DataTypeCodec.java

License:Apache License

private static Boolean textDecodeBOOL(int index, int len, ByteBuf buff) {
    if (buff.getByte(index) == 't') {
        return Boolean.TRUE;
    } else {//from   w  w w .ja  v  a  2s  .  c o  m
        return Boolean.FALSE;
    }
}

From source file:io.reactiverse.pgclient.impl.codec.DataTypeCodec.java

License:Apache License

private static Path textDecodePath(int index, int len, ByteBuf buff) {
    // Path representation: (p1,p2...pn) or [p1,p2...pn]
    byte first = buff.getByte(index);
    byte last = buff.getByte(index + len - 1);
    boolean isOpen;
    if (first == '(' && last == ')') {
        isOpen = false;/*from  www  .ja  v  a2  s.  c  om*/
    } else if (first == '[' && last == ']') {
        isOpen = true;
    } else {
        throw new DecoderException("Decoding Path is in wrong syntax");
    }
    List<Point> points = textDecodeMultiplePoints(index + 1, len - 2, buff);
    return new Path(isOpen, points);
}

From source file:io.reactiverse.pgclient.impl.codec.DataTypeCodec.java

License:Apache License

private static Path binaryDecodePath(int index, int len, ByteBuf buff) {
    byte first = buff.getByte(index);
    boolean isOpen;
    if (first == 0) {
        isOpen = true;//from w  w w . j a  v  a  2  s.  co m
    } else if (first == 1) {
        isOpen = false;
    } else {
        throw new DecoderException("Decoding Path exception");
    }
    int idx = ++index;
    int numberOfPoints = binaryDecodeINT4(idx, 4, buff);
    idx += 4;
    List<Point> points = new ArrayList<>();
    // maybe we need some check?
    for (int i = 0; i < numberOfPoints; i++) {
        points.add(binaryDecodePoint(idx, 16, buff));
        idx += 16;
    }
    return new Path(isOpen, points);
}

From source file:io.reactiverse.pgclient.impl.codec.DataTypeCodec.java

License:Apache License

/**
 * Decode the specified {@code buff} formatted as a decimal string starting at the readable index
 * with the specified {@code length} to a long.
 *
 * @param index the hex string index//from  w ww  .ja va2  s.com
 * @param len the hex string length
 * @param buff the byte buff to read from
 * @return the decoded value as a long
 */
private static long decodeDecStringToLong(int index, int len, ByteBuf buff) {
    long value = 0;
    if (len > 0) {
        int to = index + len;
        boolean neg = false;
        if (buff.getByte(index) == '-') {
            neg = true;
            index++;
        }
        while (index < to) {
            byte ch = buff.getByte(index++);
            byte nibble = (byte) (ch - '0');
            value = value * 10 + nibble;
        }
        if (neg) {
            value = -value;
        }
    }
    return value;
}

From source file:io.reactiverse.pgclient.impl.codec.DataTypeCodec.java

License:Apache License

/**
 * Decode the specified {@code buff} formatted as an hex string starting at the buffer readable index
 * with the specified {@code length} to a byte array.
 *
 * @param len the hex string length/*from   www.  j  a  v a 2 s  .  c o m*/
 * @param buff the byte buff to read from
 * @return the decoded value as a byte array
 */
private static byte[] decodeHexStringToBytes(int index, int len, ByteBuf buff) {
    len = len >> 1;
    byte[] bytes = new byte[len];
    for (int i = 0; i < len; i++) {
        byte b0 = decodeHexChar(buff.getByte(index++));
        byte b1 = decodeHexChar(buff.getByte(index++));
        bytes[i] = (byte) (b0 * 16 + b1);
    }
    return bytes;
}

From source file:io.reactiverse.pgclient.impl.codec.DataTypeCodec.java

License:Apache License

private static <T> T[] textDecodeArray(IntFunction<T[]> supplier, DataType type, int index, int len,
        ByteBuf buff) {
    List<T> list = new ArrayList<>();
    int from = index + 1; // Set index after '{'
    int to = index + len - 1; // Set index before '}'
    while (from < to) {
        // Escaped content ?
        boolean escaped = buff.getByte(from) == '"';
        int idx;/*  w  ww. j  ava2  s  . co m*/
        if (escaped) {
            idx = buff.forEachByte(from, to - from, new UTF8StringEndDetector());
            idx = buff.indexOf(idx, to, (byte) ','); // SEE iF WE CAN GET RID oF IT
        } else {
            idx = buff.indexOf(from, to, (byte) ',');
        }
        if (idx == -1) {
            idx = to;
        }
        T elt = textDecodeArrayElement(type, from, idx - from, buff);
        list.add(elt);
        from = idx + 1;
    }
    return list.toArray(supplier.apply(list.size()));
}

From source file:io.reactiverse.pgclient.impl.codec.DataTypeCodec.java

License:Apache License

private static <T> T textDecodeArrayElement(DataType type, int index, int len, ByteBuf buff) {
    if (len == 4 && Character.toUpperCase(buff.getByte(index)) == 'N'
            && Character.toUpperCase(buff.getByte(index + 1)) == 'U'
            && Character.toUpperCase(buff.getByte(index + 2)) == 'L'
            && Character.toUpperCase(buff.getByte(index + 3)) == 'L') {
        return null;
    } else {//w w  w. j a  v a2s  . com
        boolean escaped = buff.getByte(index) == '"';
        if (escaped) {
            // Some escaping - improve that later...
            String s = buff.toString(index + 1, len - 2, StandardCharsets.UTF_8);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                if (c == '\\') {
                    c = s.charAt(++i);
                }
                sb.append(c);
            }
            buff = Unpooled.copiedBuffer(sb, StandardCharsets.UTF_8);
            index = 0;
            len = buff.readableBytes();
        }
        return (T) decodeText(type, index, len, buff);
    }
}

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

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    // This must be a single byte buffer - after that follow the SSL handshake
    ByteBuf byteBuf = (ByteBuf) msg;
    byte b = byteBuf.getByte(0);
    byteBuf.release();//  w  ww .j  a  v a 2 s.  c o  m
    switch (b) {
    case MessageType.SSL_YES: {
        conn.socket().upgradeToSsl(v -> {
            ctx.pipeline().remove(this);
            upgradeFuture.complete();
        });
        break;
    }
    case MessageType.SSL_NO: {
        upgradeFuture.fail(new Exception("Postgres Server does not handle SSL connection"));
        break;
    }
    default:
        upgradeFuture.fail(new Exception("Invalid SSL connection message"));
        break;
    }
}