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:com.whizzosoftware.wzwave.frame.GetRoutingInfo.java

License:Open Source License

public GetRoutingInfo(ByteBuf buffer) {
    super(buffer);
    nodeMask = buffer.readBytes(29).array();
}

From source file:com.whizzosoftware.wzwave.frame.SendData.java

License:Open Source License

public SendData(ByteBuf buffer) {
    super(buffer);
    if (dataFrameLength == 4) {
        this.retVal = buffer.readByte();
    } else if (dataFrameLength == 5) {
        this.callbackId = buffer.readByte();
        this.tx = buffer.readByte();
    } else if (dataFrameLength > 5) {
        this.nodeId = buffer.readByte();
        byte dataLength = buffer.readByte();
        this.sendData = buffer.readBytes(dataLength).array();
        this.tx = buffer.readByte();
        this.callbackId = buffer.readByte();
    }/*from w  ww .  j  a v  a 2  s.  c  om*/
}

From source file:com.whizzosoftware.wzwave.frame.Version.java

License:Open Source License

public Version(ByteBuf buffer) {
    super(buffer);
    libraryVersion = new String(buffer.readBytes(12).array()).trim();
    libraryType = buffer.readByte();/*from  w  w w.  j  a  v a 2s . co m*/
}

From source file:com.whizzosoftware.wzwave.node.NodeInfo.java

License:Open Source License

public NodeInfo(byte nodeId, ByteBuf buffer, int nodeInfoLength) {
    this.nodeId = nodeId;
    buffer.readByte();/*w  ww  .j  ava  2s . c  o  m*/
    basicDeviceClass = buffer.readByte();
    genericDeviceClass = buffer.readByte();
    specificDeviceClass = buffer.readByte();
    commandClasses = buffer.readBytes(nodeInfoLength - 3).array();
}

From source file:com.witjit.game.server.communication.bbmessage.MessageInHandler.java

@Override
protected ByteBuf readMessage(ByteBuf mb) {
    ByteBuf buffer = Unpooled.buffer(128);
    mb.readBytes(buffer);
    return buffer;
}

From source file:com.xx_dev.apn.socks.remote.FakeHttpServerEncoder.java

License:Apache License

@Override
protected void encode(final ChannelHandlerContext ctx, final ByteBuf msg, final ByteBuf out) throws Exception {
    int length = msg.readableBytes();

    out.writeBytes(TextUtil.toUTF8Bytes("HTTP/1.1 200 OK \r\n"));
    out.writeBytes(TextUtil.toUTF8Bytes("Content-Type: image/png\r\n"));
    out.writeBytes(TextUtil.toUTF8Bytes("X-C: " + String.format("%1$08x", length) + "\r\n"));
    out.writeBytes(TextUtil.toUTF8Bytes("Content-Length: " + "1234" + "\r\n"));
    out.writeBytes(TextUtil.toUTF8Bytes("Connection: Keep-Alive\r\n"));
    out.writeBytes(TextUtil.toUTF8Bytes("Server: nginx\r\n"));
    out.writeBytes(TextUtil.toUTF8Bytes("\r\n"));

    if (length > 0) {
        trafficLogger.info("D," + ctx.channel().attr(NettyAttributeKey.LINK_USER).get() + "," + length);

        byte[] buf = new byte[length];
        msg.readBytes(buf);

        byte[] res = new byte[length];

        for (int i = 0; i < buf.length; i++) {
            //res[i] = (byte)(buf[i] ^ key);
            res[i] = (byte) (buf[i] ^ (RemoteConfig.ins().getEncryptKey() & 0xFF));
        }//w w  w .  j  a v a  2 s  . c o  m

        out.writeBytes(res);
    }
}

From source file:com.yea.remote.netty.codec.NettyMessageDecoder.java

License:Apache License

@Override
public Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    ByteBuf frame = (ByteBuf) super.decode(ctx, in);
    if (frame == null) {
        return null;
    }/*  ww  w  .  j a  va2  s  . c o  m*/
    ISerializer serializer = serializePool.borrow();
    try {
        Message message = new Message();
        byte[] sessionID = new byte[16];
        Header header = new Header();
        header.setCrcCode(frame.readInt());
        header.setLength(frame.readInt());
        frame.readBytes(sessionID);
        header.setSessionID(sessionID);
        header.setType(frame.readByte());
        header.setPriority(frame.readByte());
        header.setResult(frame.readByte());
        RemoteConstants.CompressionAlgorithm compressionAlgorithm = RemoteConstants.CompressionAlgorithm
                .valueOf(frame.readByte());
        if (compressionAlgorithm != null && compressionAlgorithm.code() > 0) {
            //
            serializer.setCompress(new Compress().setCompressionAlgorithm(compressionAlgorithm.algorithm()));
        }
        long basedate = frame.readLong();

        int attachmentSize = frame.readByte();
        if (attachmentSize > 0) {
            attachmentSize = frame.readByte();//?Date?String?
            if (attachmentSize > 0) {
                byte[] keyArray = null;
                byte[] valueArray = null;
                for (int i = 0; i < attachmentSize; i++) {
                    keyArray = new byte[frame.readShort()];
                    frame.readBytes(keyArray);
                    valueArray = new byte[frame.readShort()];
                    frame.readBytes(valueArray);

                    header.addAttachment(new String(keyArray, "ISO-8859-1"),
                            serializer.deserialize(valueArray));
                }
            }
            attachmentSize = frame.readByte();//Date?
            if (attachmentSize > 0) {
                byte[] keyArray = null;
                byte length = 0;
                for (int i = 0; i < attachmentSize; i++) {
                    keyArray = new byte[frame.readShort()];
                    frame.readBytes(keyArray);
                    length = frame.readByte();
                    if (length == 1) {
                        header.addAttachment(new String(keyArray, "ISO-8859-1"),
                                new Date(basedate - frame.readByte()));
                    } else if (length == 2) {
                        header.addAttachment(new String(keyArray, "ISO-8859-1"),
                                new Date(basedate - frame.readShort()));
                    } else if (length == 4) {
                        header.addAttachment(new String(keyArray, "ISO-8859-1"),
                                new Date(basedate - frame.readInt()));
                    } else {
                        header.addAttachment(new String(keyArray, "ISO-8859-1"),
                                new Date(basedate - frame.readLong()));
                    }
                }
            }
            attachmentSize = frame.readByte();//String?
            if (attachmentSize > 0) {
                byte[] keyArray = null;
                byte[] valueArray = null;
                for (int i = 0; i < attachmentSize; i++) {
                    keyArray = new byte[frame.readShort()];
                    frame.readBytes(keyArray);
                    valueArray = new byte[frame.readShort()];
                    frame.readBytes(valueArray);

                    header.addAttachment(new String(keyArray, "ISO-8859-1"),
                            new String(valueArray, "ISO-8859-1"));
                }
            }
        }
        header.addAttachment(NettyConstants.MessageHeaderAttachment.SEND_DATE.value(), new Date(basedate));
        header.addAttachment(NettyConstants.MessageHeaderAttachment.RECIEVE_DATE.value(), new Date());
        message.setHeader(header);

        if (frame.readableBytes() > 4) {
            int length = frame.readInt();//Body
            byte[] objArray = new byte[length];
            frame.readBytes(objArray);
            try {
                Object body = serializer.deserialize(objArray);
                message.setBody(body);
            } catch (Exception ex) {
                if (RemoteConstants.MessageType.SERVICE_RESP.value() == message.getHeader().getType()) {
                    message.getHeader().setType(RemoteConstants.MessageType.SUSPEND_RESP.value());
                    message.getHeader().setResult(RemoteConstants.MessageResult.FAILURE.value());
                    message.setBody(ex);
                } else if (RemoteConstants.MessageType.SERVICE_REQ.value() == message.getHeader().getType()) {
                    message.getHeader().setType(RemoteConstants.MessageType.SUSPEND_REQ.value());
                    message.getHeader().setResult(RemoteConstants.MessageResult.FAILURE.value());
                    message.setBody(ex);
                } else {
                    throw ex;
                }
            }
        }

        return message;
    } finally {
        serializePool.restore(serializer);
        frame.release();
    }
}

From source file:com.zanclus.ssh.transport.SSHFrameDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    in.markReaderIndex();/*from  w  ww . ja  v  a2  s  .c om*/
    if (in.readableBytes() > HEADER_LEN) {
        byte[] header = new byte[HEADER_LEN];
        in.readBytes(header);
        int packetLen = shift(header[0], 24) | shift(header[1], 16) | shift(header[2], 8) | shift(header[3], 0);
        if (largePacketSupport || packetLen <= 35000) {
            decodePacket(header[4], packetLen, in);
        } else {
            // Packet length cannot be greater than 35000 bytes according to RFC 4253 Section 6.1
            throw new PacketSizeException(
                    String.format("Packet size of '%d' exceeds RFC 4253 recommendations and "
                            + "large packet support was not expressly enabled.", packetLen));
        }
    } else {
        LOG.info("Insufficient bytes to finish decoding Packet. Resetting input buffer.");
        in.resetReaderIndex();
    }
}

From source file:com.zanclus.ssh.transport.SSHFrameDecoder.java

License:Apache License

/**
 * Given an input buffer, payload length, and padding length; parse the input buffer bytes to
 * extract the payload data//from  w ww .j  av a 2 s .co  m
 * @param payloadLen The number of bytes which are expected in the encoded payload
 * @param in The input {@link ByteBuf}
 * @param paddingLen The expected length of the random padding bytes
 * @return An array of {@code byte}s containing the decrypted payload
 * @throws IllegalAccessException Thrown by the {@link SSHFrameDecoder#calculateMAC(byte[])} method
 * @throws InvalidCipherTextException Thrown by the {@link SSHFrameDecoder#decryptPayload(byte[])} method 
 * @throws InstantiationException Thrown by the {@link SSHFrameDecoder#calculateMAC(byte[])} method
 * @throws InvalidMACException Thrown by the {@link SSHFrameDecoder#validateMAC(byte[], byte[])} method
 */
private byte[] decodePayload(int payloadLen, ByteBuf in, int paddingLen)
        throws IllegalAccessException, InvalidCipherTextException, InstantiationException, InvalidMACException {
    // Read the payload
    byte[] payload = new byte[payloadLen];
    in.readBytes(payload);

    byte[] plaintext = decryptPayload(payload);

    // Skip the random padding
    in.skipBytes(paddingLen);

    byte[] computedMAC = calculateMAC(plaintext);

    // Read MAC from ByteBuf
    byte[] recievedMAC = new byte[algorithm.digestLen()];
    in.readBytes(recievedMAC);

    validateMAC(recievedMAC, computedMAC);
    LOG.debug("Message Packet decoded and MAC verified.");
    return plaintext;
}

From source file:com.zaradai.distributor.messaging.netty.InetSocketAddressSerializer.java

License:Apache License

public static InetSocketAddress deserialize(ByteBuf in) throws UnknownHostException {
    int length = in.readInt();
    byte[] bytes = new byte[length];
    in.readBytes(bytes);

    InetAddress inetAddress = InetAddress.getByAddress(bytes);
    // read port and return
    return new InetSocketAddress(inetAddress, in.readInt());
}