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:com.whizzosoftware.foscam.camera.protocol.OrderDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> list) throws Exception {
    Byte b;/*  w  w  w .  java 2 s  . c  o m*/
    if (buf.isReadable()) {
        do {
            b = buf.readByte();
        } while (b != 'M' && buf.isReadable());

        if (buf.readableBytes() > 0) {
            logger.trace("Found possible start of order");
            b = buf.readByte();
            if (b == 'O' && buf.isReadable()) {
                b = buf.readByte();
                if (b == '_' && buf.isReadable()) {
                    b = buf.readByte();
                    if (b == 'I' && buf.isReadable()) {
                        Integer operationCode = popINT16(buf);
                        if (operationCode != null) {
                            if (popBytes(buf, 9)) {
                                Integer length = popINT32(buf);
                                if (length != null && popBytes(buf, 4)) {
                                    if (buf.readableBytes() >= length) {
                                        byte[] text = new byte[length];
                                        buf.readBytes(text);
                                        switch (operationCode) {
                                        case 1:
                                            try {
                                                list.add(new SearchResponse(text, 0, length));
                                            } catch (UnknownHostException e) {
                                                logger.error("Error processing search response", e);
                                            }
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

From source file:com.whizzosoftware.foscam.camera.protocol.OrderDecoder.java

License:Open Source License

/**
 * Pops four bytes from the buffer and returns them as a little-endian integer.
 *
 * @param buf the buffer to read/* ww  w  . j av  a 2  s .com*/
 *
 * @return an Integer (or null if there aren't at least four bytes available in the buffer)
 */
private Integer popINT32(ByteBuf buf) {
    if (buf.readableBytes() >= 4) {
        byte b[] = new byte[4];
        b[0] = buf.readByte();
        b[1] = buf.readByte();
        b[2] = buf.readByte();
        b[3] = buf.readByte();
        return java.nio.ByteBuffer.wrap(b).order(java.nio.ByteOrder.LITTLE_ENDIAN).getInt();
    } else {
        return null;
    }
}

From source file:com.whizzosoftware.hobson.davisvantage.api.codec.VantageSerialFrameDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf buffer, List<Object> list)
        throws Exception {
    int ix = buffer.readerIndex();
    int readableBytes = buffer.readableBytes();
    logger.trace("Readable bytes remaining to read: {}", readableBytes);
    if (readableBytes > 0) {
        if (buffer.readableBytes() >= 6 && buffer.getByte(ix) == 'T' && buffer.getByte(ix + 1) == 'E'
                && buffer.getByte(ix + 2) == 'S' && buffer.getByte(ix + 3) == 'T'
                && buffer.getByte(ix + 4) == '\n' && buffer.getByte(ix + 5) == '\r') {
            buffer.readBytes(6);/*from  ww w . ja  v  a 2s  .c  o  m*/
            logger.trace("Got TEST");
            list.add(new Test());
        } else if (buffer.getByte(ix) == '\r' || buffer.getByte(ix) == '\n') {
            logger.trace("Discarding whitespace: {}", (int) buffer.getByte(ix));
            buffer.readByte();
        } else if (buffer.getByte(ix) == 0x06) {
            buffer.readByte();
            list.add(new ACK());
        } else if (buffer.readableBytes() >= 4 && buffer.getByte(ix) == 'O' && buffer.getByte(ix + 1) == 'K'
                && buffer.getByte(ix + 2) == '\n' && buffer.getByte(ix + 3) == '\r') {
            logger.trace("Got OK");
            buffer.readBytes(4);
            list.add(new OK());
        } else if (buffer.getByte(ix) == 'L' && buffer.getByte(ix + 1) == 'O'
                && buffer.getByte(ix + 2) == 'O') {
            if (buffer.readableBytes() >= 99) {
                logger.trace("Got LOOP");
                byte[] bytes = new byte[99];
                buffer.readBytes(bytes, 0, 99);
                list.add(new LoopResponse(bytes));
            }
        } else if ((buffer.getByte(ix) == 'A' && buffer.getByte(ix + 1) == 'p' && buffer.getByte(ix + 2) == 'r')
                || (buffer.getByte(ix) == 'A' && buffer.getByte(ix + 1) == 'u' && buffer.getByte(ix + 2) == 'g')
                || (buffer.getByte(ix) == 'D' && buffer.getByte(ix + 1) == 'e' && buffer.getByte(ix + 2) == 'c')
                || (buffer.getByte(ix) == 'F' && buffer.getByte(ix + 1) == 'e' && buffer.getByte(ix + 2) == 'b')
                || (buffer.getByte(ix) == 'J' && buffer.getByte(ix + 1) == 'a' && buffer.getByte(ix + 2) == 'n')
                || (buffer.getByte(ix) == 'J' && buffer.getByte(ix + 1) == 'u' && buffer.getByte(ix + 2) == 'l')
                || (buffer.getByte(ix) == 'J' && buffer.getByte(ix + 1) == 'u' && buffer.getByte(ix + 2) == 'n')
                || (buffer.getByte(ix) == 'M' && buffer.getByte(ix + 1) == 'a' && buffer.getByte(ix + 2) == 'r')
                || (buffer.getByte(ix) == 'M' && buffer.getByte(ix + 1) == 'a' && buffer.getByte(ix + 2) == 'y')
                || (buffer.getByte(ix) == 'N' && buffer.getByte(ix + 1) == 'o' && buffer.getByte(ix + 2) == 'v')
                || (buffer.getByte(ix) == 'O' && buffer.getByte(ix + 1) == 'c' && buffer.getByte(ix + 2) == 't')
                || (buffer.getByte(ix) == 'D' && buffer.getByte(ix + 1) == 'e'
                        && buffer.getByte(ix + 2) == 'c')) {
            if (buffer.readableBytes() >= 13) {
                byte[] bytes = new byte[13];
                buffer.readBytes(bytes, 0, 13);
                list.add(new VersionResponse(new String(bytes, 0, 11)));
            }
        } else {
            byte b = buffer.readByte();
            logger.trace("Discarding unknown byte: {}", Hex.encodeHexString(new byte[] { b }));
        }
    }
}

From source file:com.whizzosoftware.hobson.lifx.api.codec.LIFXFrameDecoder.java

License:Open Source License

protected Object readDeviceStateService(InetSocketAddress sender, ByteBuf buf) {
    return new DeviceStateService(sender, readHeader(buf), buf.readByte(), read32BitLE(buf));
}

From source file:com.whizzosoftware.hobson.lifx.api.codec.LIFXFrameDecoder.java

License:Open Source License

protected MessageHeader readHeader(ByteBuf buf) {
    // read message length
    int length = read16BitLE(buf);
    // read origin/tagged/addressable/protocol
    buf.readByte();
    buf.readByte();/*from   w  w  w. ja  va 2  s .c o m*/
    // read source
    int source = read32BitLE(buf);
    // read target
    long target = read64BitLE(buf);
    // read reserved
    buf.readByte();
    buf.readByte();
    buf.readByte();
    buf.readByte();
    buf.readByte();
    buf.readByte();
    // read ack_required/res_required
    buf.readByte();
    // read sequence
    byte sequence = buf.readByte();
    // read reserved
    buf.readByte();
    buf.readByte();
    buf.readByte();
    buf.readByte();
    buf.readByte();
    buf.readByte();
    buf.readByte();
    buf.readByte();
    // read type
    int type = read16BitLE(buf);
    // read reserved
    buf.readByte();
    buf.readByte();

    // TODO: set correct booleans
    return new MessageHeader(length, false, false, source, target, true, true, sequence, type);
}

From source file:com.whizzosoftware.hobson.lifx.api.codec.LIFXFrameDecoder.java

License:Open Source License

protected int read16BitLE(ByteBuf buf) {
    return ((buf.readByte() & 0xFF) + ((buf.readByte() << 8)) & 0xFFFF);
}

From source file:com.whizzosoftware.hobson.lifx.api.codec.LIFXFrameDecoder.java

License:Open Source License

protected int read32BitLE(ByteBuf buf) {
    return (buf.readByte() + (buf.readByte() >> 8) + (buf.readByte() >> 16) + (buf.readByte() >> 24));
}

From source file:com.whizzosoftware.hobson.lifx.api.codec.LIFXFrameDecoder.java

License:Open Source License

protected long read64BitLE(ByteBuf buf) {
    return (buf.readByte() + (buf.readByte() >> 8) + (buf.readByte() >> 16) + (buf.readByte() >> 24)
            + (buf.readByte() >> 32) + (buf.readByte() >> 40) + (buf.readByte() >> 48)
            + (buf.readByte() >> 56));
}

From source file:com.whizzosoftware.hobson.lifx.api.codec.LIFXFrameDecoder.java

License:Open Source License

protected String readString(ByteBuf buf, int len) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < len; i++) {
        byte b = buf.readByte();
        if (b > 0) {
            sb.append((char) b);
        }/*w w  w  .j  av  a2s .c o m*/
    }
    return sb.toString();
}

From source file:com.whizzosoftware.hobson.radiora.api.codec.RadioRaFrameDecoder.java

License:Open Source License

@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception {
    logger.trace("decode: {}", buffer.toString(CharsetUtil.UTF_8));

    ByteBuf frame = (ByteBuf) super.decode(ctx, buffer);

    if (frame != null) {
        // if we receive a single byte frame, it should be a '!'
        if (frame.readableBytes() == 1) {
            byte b = frame.readByte();
            if (b == '!') {
                return null;
            } else {
                throw new CorruptedFrameException("Unexpected single byte frame");
            }//  ww  w .j  a v a 2  s  .  c  o m
            // otherwise, we assume it's a command frame
        } else {
            int ix = frame.indexOf(frame.readerIndex(), frame.writerIndex(), (byte) ',');
            if (ix > 0) {
                String cmdName = frame.slice(0, ix).toString(CharsetUtil.UTF_8);

                switch (cmdName) {
                case ZoneMap.TYPE:
                    if (frame.readableBytes() >= ix + 33) {
                        return new ZoneMap(frame.slice(ix + 1, 32).toString(CharsetUtil.UTF_8));
                    } else {
                        throw new CorruptedFrameException("Received invalid zone map size");
                    }

                case LocalZoneChange.TYPE:
                    if (frame.readableBytes() >= ix + 3) {
                        String sZoneNum = frame.slice(ix + 1, 2).toString(CharsetUtil.UTF_8);
                        if (frame.readableBytes() >= ix + 7) {
                            String state = frame.slice(ix + 4, 3).toString(CharsetUtil.UTF_8).trim();
                            try {
                                return new LocalZoneChange(Integer.parseInt(sZoneNum),
                                        LocalZoneChange.State.valueOf(state));
                            } catch (IllegalArgumentException iae) {
                                throw new CorruptedFrameException("Invalid LZC state string");
                            }
                        } else {
                            throw new CorruptedFrameException("Invalid LZC size (state)");
                        }
                    } else {
                        throw new CorruptedFrameException("Invalid LZC size (zoneNum)");
                    }

                case LEDMap.TYPE:
                    if (frame.readableBytes() >= ix + 16) {
                        return new LEDMap(frame.slice(ix + 1, 15).toString(CharsetUtil.UTF_8));
                    } else {
                        throw new CorruptedFrameException("Invalid LED map size");
                    }

                default:
                    throw new DecoderException("Unrecognized command: " + cmdName);
                }
            } else {
                throw new CorruptedFrameException("Invalid frame format (no comma)");
            }
        }
    } else {
        return null;
    }
}