Example usage for io.netty.buffer ByteBuf readBytes

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

Introduction

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

Prototype

public abstract ByteBuf readBytes(ByteBuffer dst);

Source Link

Document

Transfers this buffer's data to the specified destination starting at the current readerIndex until the destination's position reaches its limit, and increases the readerIndex by the number of the transferred bytes.

Usage

From source file:eu.xworlds.util.raknet.protocol.OpenConnectionRequest1.java

License:Open Source License

@Override
protected void parseMessage(ByteBuf buf) {
    this.magic = new byte[MAGIC_BYTES];
    buf.readBytes(this.magic);
    this.procotolVersion = buf.readByte();
    this.mtuPayload = buf.readBytes(buf.readableBytes()).array();
}

From source file:eu.xworlds.util.raknet.protocol.OpenConnectionRequest2.java

License:Open Source License

@Override
protected void parseMessage(ByteBuf buf) {
    this.magic = new byte[MAGIC_BYTES];
    buf.readBytes(this.magic);
    // TODO how to get the security flag?
    if (this.useSecurity) {
        this.cookie = buf.readInt();
        this.clientWroteChallenge = buf.readBoolean();
        if (this.clientWroteChallenge) {
            this.clientChallenge = new byte[EASYHANDSHAKE_CHALLENGE_BYTES];
            buf.readBytes(this.clientChallenge);
        }/*from  w  ww . ja  va 2 s  .c o m*/
    }
    this.bindingAddress = readIPv4Address(buf);
    this.mtuSize = buf.readUnsignedShort();
    this.guid = readGuid(buf);
}

From source file:eu.xworlds.util.raknet.protocol.OutOfBandInternal.java

License:Open Source License

@Override
protected void parseMessage(ByteBuf buf) {
    this.guid = readGuid(buf);
    this.magic = new byte[MAGIC_BYTES];
    buf.readBytes(this.magic);
    if (buf.readableBytes() > 0) {
        this.oobData = buf.readBytes(buf.readableBytes()).array();
    }//from w  w w.  j  a v  a 2 s .  c  o  m
}

From source file:eu.xworlds.util.raknet.protocol.UnconnectedPing.java

License:Open Source License

@Override
protected void parseMessage(ByteBuf buf) {
    this.time = readTime(buf);
    this.magic = new byte[MAGIC_BYTES];
    buf.readBytes(this.magic.length);
}

From source file:fourteen.proprietaryprotocol.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;
    }//from  w  ww .  j  a  v  a2  s . c om

    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, marshallingDecoder.decode(frame));
        }
        keyArray = null;
        key = null;
        header.setAttachment(attch);
    }
    if (frame.readableBytes() > 4) {
        message.setObject(marshallingDecoder.decode(frame));
    }
    message.setHeader(header);
    return message;
}

From source file:gedi.remote.codec.DefaultDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

    if (in.readableBytes() < Integer.BYTES)
        return;/*from  w  w w .  j  av  a 2s.com*/

    in.markReaderIndex();
    int size = in.readInt();

    if (in.readableBytes() < size) {
        in.resetReaderIndex();
        return;
    }

    // everything has arrived, decode
    char[] classname = new char[in.readInt()];
    for (int i = 0; i < classname.length; i++)
        classname[i] = (char) (in.readByte() & 255);

    String clsName = String.valueOf(classname);

    if (clsName.length() == 1) {

        switch (clsName) {
        case "A":
            char[] re = new char[in.readInt()];
            for (int i = 0; i < re.length; i++)
                re[i] = (char) (in.readByte() & 255);
            out.add(String.valueOf(re));
            break;
        case "B":
            out.add(in.readByte());
            break;
        case "S":
            out.add(in.readShort());
            break;
        case "I":
            out.add(in.readInt());
            break;
        case "L":
            out.add(in.readLong());
            break;
        case "F":
            out.add(in.readFloat());
            break;
        case "D":
            out.add(in.readDouble());
            break;
        }

    } else {

        if (!ClassPathCache.getInstance().existsClass(clsName)) {
            in.resetReaderIndex();
            return;
        }

        Class<?> cls = Class.forName(clsName);
        BinarySerializable re = (BinarySerializable) cls.newInstance();

        BinaryBlob buff = new BinaryBlob(size - Integer.BYTES - classname.length);
        in.readBytes(buff.getBuffer());
        buff.getBuffer().flip();

        re.deserialize(buff);

        out.add(re);
    }
}

From source file:growthcraft.api.core.stream.StreamUtils.java

License:Open Source License

/**
 * Reads an ASCII string from the stream, the first int should be the length
 * of the string./*from w  ww .ja  v  a  2  s. co m*/
 *
 * @param stream - stream to read from
 * @return string
 */
public static String readStringASCII(ByteBuf stream) throws UnsupportedEncodingException {
    final int len = stream.readInt();
    final byte[] bytes = stream.readBytes(len).array();
    return new String(bytes, "US-ASCII");
}

From source file:handling.netty.MaplePacketDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> message) throws Exception {
    final MapleClient client = (MapleClient) ctx.channel().attr(MapleClient.CLIENT_KEY).get();
    DecoderState decoderState = (DecoderState) ctx.channel().attr(DECODER_STATE_KEY).get();
    if (decoderState == null) {
        decoderState = new DecoderState();
        ctx.channel().attr(DECODER_STATE_KEY).set(decoderState);
    }/*from   w w  w. j  ava  2  s . c  o m*/
    if (in.readableBytes() >= 4 && decoderState.packetlength == -1) {
        int packetHeader = in.readInt();
        if (!client.getReceiveCrypto().checkPacket(packetHeader)) {
            ctx.channel().disconnect();
            return;
        }
        decoderState.packetlength = MapleAESOFB.getPacketLength(packetHeader);
    } else if (in.readableBytes() < 4 && decoderState.packetlength == -1) {
        return;
    }
    if (in.readableBytes() >= decoderState.packetlength) {
        byte decryptedPacket[] = new byte[decoderState.packetlength];
        in.readBytes(decryptedPacket);
        decoderState.packetlength = -1;
        client.getReceiveCrypto().crypt(decryptedPacket);
        //MapleCustomEncryption.decryptData(decryptedPacket);
        message.add(decryptedPacket);
    }
}

From source file:hellfirepvp.astralsorcery.common.network.packet.server.PktSyncConfig.java

License:Open Source License

@Override
public void fromBytes(ByteBuf buf) {
    int count = buf.readByte();
    fields = new ArrayList<>(count);
    for (int i = 0; i < count; i++) {
        fields.add(new SyncTuple(null, null)); //Empty init
    }/*from  w  w w.j  a  va 2s  .  c om*/

    for (int i = 0; i < count; i++) {
        byte[] data = new byte[buf.readShort()];
        buf.readBytes(data);

        ByteArrayInputStream in = new ByteArrayInputStream(data);
        SyncTuple tuple = null;
        String key = null;
        try {
            key = new DataInputStream(in).readUTF();
            Object value = new ObjectInputStream(in).readObject();
            tuple = new SyncTuple(key, value);
        } catch (Exception ignored) {
        }

        if (tuple == null) {
            fields = null;
            if (key != null) {
                AstralSorcery.log.info("Could not read config from server with key: " + key);
            }
            break;
        }
        fields.set(i, tuple);
    }
}

From source file:herddb.utils.ByteBufUtils.java

License:Apache License

public static final byte[] readArray(ByteBuf buffer) {
    final int len = readVInt(buffer);
    final byte[] array = new byte[len];
    buffer.readBytes(array);
    return array;
}