1、ByteBuf

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

1.1、从指定的索引读取

  1. // 从指定的索引读取
  2. buf.readerIndex(6);
  3. byte bytes1 = new byte[2];
  4. // 读取的内容放到byte1数组中,长度为数组的大小
  5. buf.readBytes(bytes1);