1、ByteBuf
// 1.创建一个非池化的ByteBuf,大小为10个字节
ByteBuf buf = Unpooled.buffer(8);
// 2.写入一段内容 -63, 49, -103, -102:-11.1, 65 116 : 16756 -13107:-52 -51
byte[] bytes = {-63, 49, -103, -102, 65, 116, -52, -51};
buf.writeBytes(bytes);
System.out.println("写入的bytes为====================>" + Arrays.toString(bytes));
System.out.println("写入一段内容后ByteBuf为===========>" + buf.toString());
System.out.println("2.ByteBuf中的内容为===============>" + Arrays.toString(buf.array()) + "\n");
byte[] bytes1;
// 从指定的索引读取
buf.readerIndex(6);
bytes1 = new byte[2];
// 读取的内容放到byte1数组中,长度为数组的大小
buf.readBytes(bytes1);
int i = DataUtils.byteArrayToInt(bytes1);
System.out.println(i);
buf.readerIndex(0);
bytes1 = new byte[4];
buf.readBytes(bytes1);
float v = DataUtils.toFloatLittleEndianBySwap(bytes1);
System.out.println(v);
System.out.println("读取一段内容后ByteBuf为===========>" + buf.toString());
System.out.println("3.ByteBuf中的内容为===============>" + Arrays.toString(buf.array()) + "\n");
1.1、从指定的索引读取
// 从指定的索引读取
buf.readerIndex(6);
byte bytes1 = new byte[2];
// 读取的内容放到byte1数组中,长度为数组的大小
buf.readBytes(bytes1);