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.ixortalk.nifi.processors.gettcp.TcpClientHandler.java

License:Apache License

/**
 *
 * When data becomes available on the socket, it will trigger the method below.
 * We'll read from the buffer until we find the delimiter.
 * When we find the delimiter, we'll offer it to the queue so that it can be subsequently polled by the processor.
 * The delimiter will not be offered as part of the message (TODO: make this optional)
 *
 * In case we fail to convert a byte from the buffer we log an error, but continue processing.
 *
 * @param channelHandlerContext//from w w  w .  jav  a  2  s  . c  o m
 * @param o
 * @throws Exception
 */
protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
    ByteBuf in = (ByteBuf) o;

    try {
        while (in.isReadable()) {
            byte b = in.readByte();

            if (log.isTraceEnabled()) {
                log.trace("Read byte " + ByteUtils.byteToHex(b));
            }

            if (b == delimiter) {
                if (log.isTraceEnabled()) {
                    log.trace("Found delimiter, offering message : " + message.toString());
                }

                socketMessagesReceived.offer(message.toString());
                message = new StringBuffer();
            } else {
                try {
                    message.append(new String(new byte[] { b }, "UTF-8"));
                } catch (Exception e) {
                    log.error("Error occured while converting byte to string.", e);
                }
            }
        }
    } catch (Exception e) {
        log.error("Error occured while reading channel", e);
    }
}

From source file:com.jadarstudios.rankcapes.forge.network.packet.C0PacketPlayerCapesUpdate.java

License:MIT License

@Override
public void read(ByteBuf data) throws IndexOutOfBoundsException {
    byte typeByte = data.readByte();
    this.type = CapeUpdateType.values()[typeByte];

    this.players = this.readString(data);
}

From source file:com.kaijin.AdvPowerMan.tileentities.TEAdjustableTransformer.java

License:Open Source License

@SideOnly(Side.CLIENT)
@Override/*from w ww  . ja v  a 2  s.  c  o m*/
public void receiveDescriptionData(int packetID, ByteBuf stream) {
    // try
    // {
    for (int i = 0; i < 6; i++) {
        sideSettings[i] = stream.readByte();
    }
    /*
     * } catch (IOException e) { logDescPacketError(e); return; }
     */
    worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}

From source file:com.kingmed.dp.lisclient.demo.DiscardServerHandler.java

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf in = (ByteBuf) msg;
    try {//from   ww  w .ja  v  a 2  s. c o m
        while (in.isReadable()) {
            System.out.println((char) in.readByte());
            System.out.flush();
        }
    } finally {
        ReferenceCountUtil.release(msg);
    }
}

From source file:com.kradac.karview.netty.EchoServerHandler.java

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    this.c = ctx.channel();
    ByteBuf buf = (ByteBuf) msg;
    String auxdata = "";
    while (buf.isReadable()) {
        byte aux = buf.readByte();
        char charVal = (char) aux;
        int valEnt = charVal;
        if (valEnt < 10) {
            auxdata += valEnt;/*  w w w . ja v  a2 s  .  co  m*/
        } else if (valEnt == 10 || valEnt == 13) {
            auxdata += "@";
        } else {
            auxdata += charVal;
        }
    }
    this.data = auxdata;
    buf.clear();
    System.out.println("Trama que llega: " + this.data);
    if (this.data.indexOf("0@80") == 0) {
        System.out.println("Trama SKP+ +param: [" + auxdata + "]");
        u.sendToFile(3, "skp+", this.data);
        tramaMinuto(this.data.substring(5));
    } else if (this.data.indexOf("0@8000001") == 0) {
        System.out.println("Trama Conexin SKP+: [" + auxdata + "]");
        u.sendToFile(2, "skp+", this.data);
        tramaConecxion(u.clearDataConnection(this.data));
        tipoEquipoGPRS = 1;
    } else if (this.data.indexOf("0@80") == 0) {
        System.out.println("Trama Conexin SKP: [" + auxdata + "]");
        u.sendToFile(2, "skp", this.data);
        tramaConecxion(u.clearDataConnection(this.data));
        tipoEquipoGPRS = 2;
    } else if (this.data.indexOf("0150") == 0) {
        u.sendToFile(5, "skp", data);
        if (registered) {
            System.out.println(
                    "Respuesta Comando [5] [" + data + "], Equipo: [" + v.getIdEquipo().getEquipo() + "]");
            processResponseComand(this.data.substring(5));
        } else {
            System.out.println("Respuesta Comando [5] [" + data + "], Equipo: [Desconocido]");
            System.out.println(
                    "Dato Enviado a Tabla de Invalidos por no estar registrado en el sistema [Desconocido].");
            dijc.create(new DatoInvalidos(3, new Date(), e.getEquipo(), this.data));
        }
        //            System.out.println("Respuesta Cmd: [" + auxdata + "]");
        //            processResponseComand(this.data.substring(5));
    } else if (this.data.indexOf("0420") == 0) {
        System.out.println("Trama SKP: [" + data + "]");
        u.sendToFile(3, "skp", this.data);
        procesarSKP(this.data.substring(5));
    } else if (this.data.indexOf("0@8 488") == 0) { // 0@8 488 
        System.out.println("Trama SKP+ -param: [" + auxdata + "]");
        u.sendToFile(3, "skp", this.data);
        tramaMinuto(this.data.substring(9));
    } else {
        if (this.data.indexOf("0@80") == 0) {
            System.out.println("Aqui");
        } else if (this.data.indexOf("0@80") == 0) {

        } else if (this.data.indexOf("0 @80") == 0) {
            System.err.println("Trama sin Procesar: [" + auxdata + "]");
            if (registered) {
                dijc.create(new DatoInvalidos(1, new Date(), e.getEquipo(), this.data));
                System.out.println("entra aqui 1");
            } else {
                System.out.println("entra aqui 2");
                dijc.create(new DatoInvalidos(1, new Date(), "", this.data));
            }
        }
    }
}

From source file:com.kradac.karview.netty.MyDecoder.java

@Override
protected void decode(ChannelHandlerContext chc, ByteBuf bb, List<Object> list) throws Exception {
    if (bb.readableBytes() < 2) {
        return;/*from   w  w w  .  j  ava 2s .c o  m*/
    }
    bb.markReaderIndex();
    int length = bb.readChar();
    if (length > 150) {
        String data = "";
        while (bb.isReadable()) {
            data += (char) bb.readByte();
        }
        bb.clear();
        if (data.contains("OK") || data.contains("handshake")) {
            if (data.contains("handshake")) {
                chc.channel().write("0%%at");
            }
            if (data.contains("OK")) {
                System.out.println("Respuesta de Comando AT [" + data + "]");
            }
        } else {
            System.err.println("Datos incorrectos enviados al Servidor [" + data + "]");
            chc.channel().disconnect();
        }
    }
    if (bb.readableBytes() < length - 2) {
        bb.resetReaderIndex();
        return;
    }
    in.writeBytes(bb);//Escribimos los bytes
    in.discardReadBytes();//
    in.retain();
    list.add(in);
    bb.clear();//vaciamos el byteBuf
}

From source file:com.l2jmobius.gameserver.network.client.Crypt.java

License:Open Source License

@Override
public void encrypt(ByteBuf buf) {
    if (!_isEnabled) {
        _isEnabled = true;/*from  ww  w  .  j ava 2s  .  c  o  m*/
        onPacketSent(buf);
        return;
    }

    onPacketSent(buf);

    int a = 0;
    while (buf.isReadable()) {
        final int b = buf.readByte() & 0xFF;
        a = b ^ _outKey[(buf.readerIndex() - 1) & 15] ^ a;
        buf.setByte(buf.readerIndex() - 1, a);
    }

    shiftKey(_outKey, buf.writerIndex());
}

From source file:com.l2jmobius.gameserver.network.client.Crypt.java

License:Open Source License

@Override
public void decrypt(ByteBuf buf) {
    if (!_isEnabled) {
        onPacketReceive(buf);/*from  w w  w. j  a v  a  2 s .c o m*/
        return;
    }

    int a = 0;
    while (buf.isReadable()) {
        final int b = buf.readByte() & 0xFF;
        buf.setByte(buf.readerIndex() - 1, b ^ _inKey[(buf.readerIndex() - 1) & 15] ^ a);
        a = b;
    }

    shiftKey(_inKey, buf.writerIndex());

    onPacketReceive(buf);
}

From source file:com.ldp.nettydemo.netty.codec.NettyMessageDecoder.java

License:Apache License

@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    ByteBuf frame = (ByteBuf) super.decode(ctx, in);
    if (frame == null) {
        return null;
    }//w w  w .java  2  s  .  c  o  m

    NettyMessage message = new NettyMessage();
    Header header = new Header();
    header.setCrcCode(frame.readInt());
    header.setLength(frame.readInt());
    header.setSessionID(frame.readLong());
    header.setType(frame.readByte());
    header.setPriority(frame.readByte());

    int size = frame.readInt();
    if (size > 0) {
        Map<String, Object> attch = new HashMap<String, Object>(size);
        int keySize = 0;
        byte[] keyArray = null;
        String key = null;
        for (int i = 0; i < size; i++) {
            keySize = frame.readInt();
            keyArray = new byte[keySize];
            frame.readBytes(keyArray);
            key = new String(keyArray, "UTF-8");
            attch.put(key, ByteObjConverter.ByteToObject(new ByteBufToBytes().read(frame)));
            //      attch.put(key, marshallingDecoder.decode(frame));
        }
        keyArray = null;
        key = null;
        header.setAttachment(attch);
    }
    if (frame.readableBytes() > 4) {
        message.setBody(ByteObjConverter.ByteToObject(new ByteBufToBytes().read(frame)));
        //       message.setBody(marshallingDecoder.decode(frame));
    }
    message.setHeader(header);
    return message;
}

From source file:com.lothrazar.cyclicmagic.playerupgrade.PacketSyncExtendedInventory.java

License:Open Source License

@Override
public void fromBytes(ByteBuf buffer) {
    slot = buffer.readByte();
    playerId = buffer.readInt();
    itemStack = ByteBufUtils.readItemStack(buffer);
}