Example usage for io.netty.buffer ByteBuf writeByte

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

Introduction

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

Prototype

public abstract ByteBuf writeByte(int value);

Source Link

Document

Sets the specified byte at the current writerIndex and increases the writerIndex by 1 in this buffer.

Usage

From source file:com.digitalpetri.modbus.codec.ModbusResponseEncoder.java

License:Apache License

private ByteBuf encodeWriteSingleCoil(WriteSingleCoilResponse response, ByteBuf buffer) {
    buffer.writeByte(response.getFunctionCode().getCode());
    buffer.writeShort(response.getAddress());
    buffer.writeShort(response.getValue());

    return buffer;
}

From source file:com.digitalpetri.modbus.codec.ModbusResponseEncoder.java

License:Apache License

private ByteBuf encodeWriteSingleRegister(WriteSingleRegisterResponse response, ByteBuf buffer) {
    buffer.writeByte(response.getFunctionCode().getCode());
    buffer.writeShort(response.getAddress());
    buffer.writeShort(response.getValue());

    return buffer;
}

From source file:com.digitalpetri.modbus.codec.ModbusResponseEncoder.java

License:Apache License

private ByteBuf encodeWriteMultipleCoils(WriteMultipleCoilsResponse response, ByteBuf buffer) {
    buffer.writeByte(response.getFunctionCode().getCode());
    buffer.writeShort(response.getAddress());
    buffer.writeShort(response.getQuantity());

    return buffer;
}

From source file:com.digitalpetri.modbus.codec.ModbusResponseEncoder.java

License:Apache License

private ByteBuf encodeWriteMultipleRegisters(WriteMultipleRegistersResponse response, ByteBuf buffer) {
    buffer.writeByte(response.getFunctionCode().getCode());
    buffer.writeShort(response.getAddress());
    buffer.writeShort(response.getQuantity());

    return buffer;
}

From source file:com.digitalpetri.modbus.codec.ModbusResponseEncoder.java

License:Apache License

private ByteBuf encodeMaskWriteRegister(MaskWriteRegisterResponse response, ByteBuf buffer) {
    buffer.writeByte(response.getFunctionCode().getCode());
    buffer.writeShort(response.getAddress());
    buffer.writeShort(response.getAndMask());
    buffer.writeShort(response.getOrMask());

    return buffer;
}

From source file:com.digitalpetri.opcua.stack.core.channel.ChunkEncoder.java

License:Apache License

private void writePadding(int cipherTextBlockSize, int paddingSize, ByteBuf buffer) {
    if (cipherTextBlockSize > 256) {
        buffer.writeShort(paddingSize);//from  w ww  .  ja  v a 2s  .com
    } else {
        buffer.writeByte(paddingSize);
    }

    for (int i = 0; i < paddingSize; i++) {
        buffer.writeByte(paddingSize);
    }

    if (cipherTextBlockSize > 256) {
        // Replace the last byte with the MSB of the 2-byte padding length
        int paddingLengthMSB = paddingSize >> 8;
        buffer.writerIndex(buffer.writerIndex() - 1);
        buffer.writeByte(paddingLengthMSB);
    }
}

From source file:com.digitalpetri.opcua.stack.core.channel.headers.SecureMessageHeader.java

License:Apache License

public static void encode(SecureMessageHeader header, ByteBuf buffer) throws UaException {
    buffer.writeMedium(MessageType.toMediumInt(header.getMessageType()));
    buffer.writeByte(header.getChunkType());
    buffer.writeInt((int) header.getMessageSize());
    buffer.writeInt((int) header.getSecureChannelId());
}

From source file:com.digitalpetri.opcua.stack.core.channel.messages.TcpMessageEncoder.java

License:Apache License

/**
 * Encode a simple UA TCP message./*w w  w  . j  ava2 s . c o m*/
 *
 * @param messageType    {@link MessageType#Hello}, {@link MessageType#Acknowledge}, or {@link MessageType#Error}.
 * @param messageEncoder a function that encodes the message payload.
 * @param buffer         the {@link ByteBuf} to encode into.
 */
private static ByteBuf encode(MessageType messageType, Consumer<ByteBuf> messageEncoder, ByteBuf buffer)
        throws UaException {
    buffer.writeMedium(MessageType.toMediumInt(messageType));
    buffer.writeByte('F');

    int lengthIndex = buffer.writerIndex();
    buffer.writeInt(0);

    int indexBefore = buffer.writerIndex();
    messageEncoder.accept(buffer);
    int indexAfter = buffer.writerIndex();
    int bytesWritten = indexAfter - indexBefore;

    buffer.writerIndex(lengthIndex);
    buffer.writeInt(8 + bytesWritten);
    buffer.writerIndex(indexAfter);

    return buffer;
}

From source file:com.doctor.netty5.example.factorial_algorithm.NumberEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, Number msg, ByteBuf out) throws Exception {

    if (msg == null) {
        throw new RuntimeException("not null");
    }//w w  w  .  ja  v a2  s  .  c o m
    BigInteger integer = null;
    if (msg instanceof BigInteger) {
        integer = (BigInteger) msg;
    } else {
        integer = new BigInteger(String.valueOf(msg));
    }

    byte[] byteArray = integer.toByteArray();
    // ?F +  +  -> 1 + 4 + ?
    out.writeByte((byte) 'F');
    out.writeInt(byteArray.length);
    out.writeBytes(byteArray);
}

From source file:com.dwarf.netty.guide.factorial.NumberEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, Number msg, ByteBuf out) {
    // Convert to a BigInteger first for easier implementation.
    BigInteger v;//  w  ww .jav  a  2s  .  c  o m
    if (msg instanceof BigInteger) {
        v = (BigInteger) msg;
    } else {
        v = new BigInteger(String.valueOf(msg));
    }

    // Convert the number into a byte array.
    byte[] data = v.toByteArray();
    int dataLength = data.length;

    // Write a message.
    out.writeByte((byte) 'F'); // magic number
    out.writeInt(dataLength); // data length
    out.writeBytes(data); // data
}