Java Code Examples for io.netty.buffer.ByteBuf.writeBytes()

The following are Jave code examples for showing how to use writeBytes() of the io.netty.buffer.ByteBuf class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: Mods   File: TF2Message.java   Source Code and License Vote up 6 votes
@Override
public void toBytes(ByteBuf buf) {
	buf.writeInt(entityID);
	buf.writeByte(type);
	if (type == 0)
		buf.writeInt(intValue);
	else if (type == 1)
		buf.writeFloat(floatValue);
	else if (type == 2)
		buf.writeByte(byteValue);
	else if (type == 3) {
		byte[] stringValueArray = stringValue.getBytes(StandardCharsets.UTF_8);
		buf.writeByte(stringValueArray.length);
		buf.writeBytes(stringValueArray);
	}
	byte[] stringNameArray = name.getBytes(StandardCharsets.UTF_8);
	buf.writeBytes(stringNameArray);
}
 
Example 2
Project: happylifeplat-transaction   File: KryoCodecServiceImpl.java   Source Code and License Vote up 5 votes
@Override
public void encode(ByteBuf out, Object message) throws IOException {
    try {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        closer.register(byteArrayOutputStream);
        KryoSerialize kryoSerialization = new KryoSerialize(pool);
        kryoSerialization.serialize(byteArrayOutputStream, message);
        byte[] body = byteArrayOutputStream.toByteArray();
        int dataLength = body.length;
        out.writeInt(dataLength);
        out.writeBytes(body);
    } finally {
        closer.close();
    }
}
 
Example 3
Project: neoscada   File: MessageManager.java   Source Code and License Vote up 5 votes
private ByteBuf mirrorUnknown ( final ByteBuf data, final byte typeId, final InformationStructure informationStructure, final byte size, final ASDUHeader header, final Cause newCause )
{
    logger.debug ( "Mirror unknown message -> {}", newCause );
    final ByteBuf reply = Unpooled.buffer ();

    EncodeHelper.encodeHeader ( typeId, informationStructure, this.options, (int)size, header.clone ( newCause ), reply );

    reply.writeBytes ( data ); // copy data

    return reply;
}
 
Example 4
Project: netty_op   File: RightTimeClientHandler.java   Source Code and License Vote up 5 votes
/**
 *@description 客户端成功连接到服务器之后向服务器发送数据
 *@time 创建时间:2017年7月26日下午7:51:31
 *@param ctx
 *@throws Exception
 *@author dzn
 */
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    ByteBuf message = null;
    for(int i = 0; i < 100; i ++){
        message = Unpooled.buffer(req.length);
        message.writeBytes(req);
        ctx.channel().writeAndFlush(message);
    }
}
 
Example 5
Project: util4j   File: NettyConnection.java   Source Code and License Vote up 5 votes
@Override
public void write(byte[] bytes) {
	if(bytes!=null && bytes.length>0 && isActive())
	{
		ByteBuf buf=PooledByteBufAllocator.DEFAULT.buffer();
		buf.writeBytes(bytes);
		write(buf);
	}
}
 
Example 6
Project: sstable-adaptor   File: CBUtil.java   Source Code and License Vote up 5 votes
public static void writeInet(InetSocketAddress inet, ByteBuf cb)
{
    byte[] address = inet.getAddress().getAddress();

    cb.writeByte(address.length);
    cb.writeBytes(address);
    cb.writeInt(inet.getPort());
}
 
Example 7
Project: LibraSock   File: LibraMessage.java   Source Code and License Vote up 5 votes
/**
 * 编码
 * 
 * @param out
 */
public void encode(ByteBuf out) {
	byte[] head = encodeHead();
	byte[] body = encodeBody();
	out.writeShort((short) (head.length + body.length + 2)); // 消息的总长度
	out.writeBytes(head); // 协议头
	out.writeShort((short) body.length); // 消息体长度
	out.writeBytes(body); // 消息体
}
 
Example 8
Project: simulacron   File: ByteBufCodec.java   Source Code and License Vote up 5 votes
@Override
public void writeBytes(ByteBuffer bytes, ByteBuf dest) {
  if (bytes == null) {
    writeInt(-1, dest);
  } else {
    writeInt(bytes.remaining(), dest);
    dest.writeBytes(bytes.duplicate());
  }
}
 
Example 9
Project: fastdfs-spring-boot   File: FileAppendEncoder.java   Source Code and License Vote up 5 votes
@Override
protected ByteBuf metadata(ByteBufAllocator alloc) {
    byte[] pathBytes = fileId.pathBytes();

    int metaSize = 2 * FastdfsConstants.FDFS_PROTO_PKG_LEN_SIZE + pathBytes.length;
    ByteBuf buf = alloc.buffer(metaSize);
    buf.writeLong(pathBytes.length);
    buf.writeLong(size());
    buf.writeBytes(pathBytes);
    return buf;
}
 
Example 10
Project: JRediClients   File: JsonJacksonMapValueCodecTest.java   Source Code and License Vote up 5 votes
@Test
public void shouldDeserializeTheMapCorrectly() throws Exception {
    ByteBuf buf = new PooledByteBufAllocator(true).buffer();
    buf.writeBytes(new ObjectMapper().writeValueAsBytes(map));
    assertThat(mapCodec.getMapValueDecoder().decode(buf, new State(false)))
            .isInstanceOf(Map.class)
            .isEqualTo(map);
}
 
Example 11
Project: angel   File: GetUDFResponse.java   Source Code and License Vote up 5 votes
@Override
public void serialize(ByteBuf buf) {
  super.serialize(buf);
  if (partResult != null) {
    byte[] data = partResult.getClass().getName().getBytes();
    buf.writeInt(data.length);
    buf.writeBytes(data);

    partResult.serialize(buf);
  }
}
 
Example 12
Project: spark_deep   File: Encoders.java   Source Code and License Vote up 4 votes
public static void encode(ByteBuf buf, String s) {
  byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
  buf.writeInt(bytes.length);
  buf.writeBytes(bytes);
}
 
Example 13
Project: NettyStudy   File: ByteBufExamples.java   Source Code and License Vote up 4 votes
public static void main(String[] args) {
   	ByteBuf heapBuf = Unpooled.buffer(10);
   	heapBuf.writeBytes("helloworld".getBytes());
	heapBuffer(heapBuf);
}
 
Example 14
Project: sstable-adaptor   File: CBUtil.java   Source Code and License Vote up 4 votes
public static void writeUUID(UUID uuid, ByteBuf cb)
{
    cb.writeBytes(UUIDGen.decompose(uuid));
}
 
Example 15
Project: Mods   File: TF2Message.java   Source Code and License Vote up 4 votes
@Override
public void toBytes(ByteBuf buf) {
	byte[] stringValueArray = value.getBytes(StandardCharsets.UTF_8);
	buf.writeByte(stringValueArray.length);
	buf.writeBytes(stringValueArray);
}
 
Example 16
Project: simulacron   File: ByteBufCodec.java   Source Code and License Vote up 4 votes
@Override
public void writeInetAddr(InetAddress address, ByteBuf dest) {
  byte[] addr = address.getAddress();
  dest.writeByte(addr.length);
  dest.writeBytes(addr);
}
 
Example 17
Project: simulacron   File: ByteBufCodec.java   Source Code and License Vote up 4 votes
@Override
public void writeString(String s, ByteBuf dest) {
  byte[] data = s.getBytes(CharsetUtil.UTF_8);
  writeUnsignedShort(data.length, dest);
  dest.writeBytes(data);
}
 
Example 18
Project: message-broker   File: ShortString.java   Source Code and License Vote up 4 votes
public void write(ByteBuf buf) {
    buf.writeByte((int) length);
    buf.writeBytes(content);
}
 
Example 19
Project: JavaNRPC   File: NrpcChannel.java   Source Code and License Vote up 4 votes
public byte[] recvRpcMessage(Socket socket) throws IOException, java.net.SocketTimeoutException {
    BufferedInputStream dataInput = new BufferedInputStream(socket.getInputStream());
    do {
        byte[] temp = new byte[Constants.BUFFER_SIZE];
        int n = dataInput.read(temp);
        if(n>0){
            readBuffer.write(temp,0,n);
        }
        if (n < 0 || n < Constants.BUFFER_SIZE) {
            break;
        }
    } while (true);
    byte[] readContent = readBuffer.toByteArray();
    ByteBuf in = Unpooled.buffer(readContent.length);
    in.writeBytes(readContent);
    int readable = in.readableBytes();
    int pos = 0;
    if (readable >= Constants.HEADER_SIZE) {
        // 检查Magic number
        if (in.getByte(pos) == 'N' && in.getByte(pos + 1) == 'R' && in.getByte(pos + 2) == 'P' && in.getByte(pos + 3) == 'C') {
        } else {
            logger.error("Wrong magic number!");
            closeSocket();
            throw new IOException("Wrong magic number!");
        }
        int totalSize = (int)in.getUnsignedInt(pos + 4);
        byte type = in.getByte(pos + 8);
        if (type == 0) {
            logger.debug("one request");
        } else if (type == 1) {
            logger.debug("one response");
        }
        if (readable < totalSize) {
            logger.warn("Not enough data");
            closeSocket();
            throw new IOException("Not enough data!");
        }
        // 开始解码报文
        in.skipBytes(Constants.HEADER_SIZE);
        byte[] dest = new byte[totalSize - Constants.HEADER_SIZE];
        in.readBytes(dest);
        return dest;
    } else {
        logger.warn("Not enough data header");
        closeSocket();
        throw new IOException("Not enough data header!");
    }
}
 
Example 20
Project: curiostack   File: ProtobufRedisCodec.java   Source Code and License Vote up 4 votes
@Override
public void encodeKey(K key, ByteBuf target) {
  target.writeBytes(keyPrefix);
  encodeTo(key, target);
  target.writerIndex(key.getSerializedSize() + keyPrefix.length);
}