• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

netty1NIO 基础:三大组件和ByteBuffer

武飞扬头像
不死鸟.亚历山大.狼崽子
帮助1

1 三大组件

1.1 Channel & Buffer

channel 有一点类似于 stream,它就是读写数据的双向通道,可以从 channel 将数据读入 buffer,也可以将 buffer 的数据写入 channel,而之前的 stream 要么是输入,要么是输出,channel 比 stream 更为底层

学新通

常见的 Channel 有

  • FileChannel
  • DatagramChannel
  • SocketChannel
  • ServerSocketChannel

buffer 则用来缓冲读写数据,常见的 buffer 有

  • ByteBuffer

        MappedByteBuffer

        DirectByteBuffer

        HeapByteBuffer

  • ShortBuffer
  • IntBuffer
  • LongBuffer
  • FloatBuffer
  • DoubleBuffer
  • CharBuffer

1.2 Selector

selector 单从字面意思不好理解,需要结合服务器的设计演化来理解它的用途

多线程版设计

学新通

多线程版缺点

  • 内存占用高
  • 线程上下文切换成本高
  • 只适合连接数少的场景

线程池版设计

学新通

线程池版缺点

  • 阻塞模式下,线程仅能处理一个 socket 连接
  • 仅适合短连接场景

selector 版设计

selector 的作用就是配合一个线程来管理多个 channel,获取这些 channel 上发生的事件,这些 channel 工作在非阻塞模式下,不会让线程吊死在一个 channel 上。适合连接数特别多,但流量低的场景(low traffic)

学新通

调用 selector 的 select() 会阻塞直到 channel 发生了读写就绪事件,这些事件发生,select 方法就会返回这些事件交给 thread 来处理

2 ByteBuffer

学新通

有一普通文本文件 data.txt,内容为

1234567890abcd

使用 FileChannel 来读取文件内容

  1.  
    package org.example.demo1;
  2.  
     
  3.  
    import lombok.extern.slf4j.Slf4j;
  4.  
     
  5.  
    import java.io.FileInputStream;
  6.  
    import java.io.FileNotFoundException;
  7.  
    import java.io.IOException;
  8.  
    import java.io.RandomAccessFile;
  9.  
    import java.nio.ByteBuffer;
  10.  
    import java.nio.channels.FileChannel;
  11.  
     
  12.  
    @Slf4j
  13.  
    public class ChannelDemo1 {
  14.  
    public static void main(String[] args) {
  15.  
    try (FileChannel channel = new FileInputStream("data.txt").getChannel()) {
  16.  
    ByteBuffer buffer = ByteBuffer.allocate(10);
  17.  
    do {
  18.  
    // 向 buffer 写入
  19.  
    int len = channel.read(buffer);
  20.  
    log.debug("读到字节数:{}", len);
  21.  
    if (len == -1) {
  22.  
    break;
  23.  
    }
  24.  
    // 切换 buffer 读模式
  25.  
    buffer.flip();
  26.  
    while(buffer.hasRemaining()) {
  27.  
    byte b = buffer.get();
  28.  
    log.debug("实际字节{}", (char)b);
  29.  
    }
  30.  
    // 切换 buffer 写模式
  31.  
    buffer.clear();
  32.  
    } while (true);
  33.  
    } catch (IOException e) {
  34.  
    e.printStackTrace();
  35.  
    }
  36.  
    }
  37.  
     
  38.  
    }
学新通

输出

  1.  
    15:03:39.467 [main] DEBUG org.example.demo1.ChannelDemo1 - 读到字节数:10
  2.  
    15:03:39.475 [main] DEBUG org.example.demo1.ChannelDemo1 - 实际字节1
  3.  
    15:03:39.475 [main] DEBUG org.example.demo1.ChannelDemo1 - 实际字节2
  4.  
    15:03:39.476 [main] DEBUG org.example.demo1.ChannelDemo1 - 实际字节3
  5.  
    15:03:39.476 [main] DEBUG org.example.demo1.ChannelDemo1 - 实际字节4
  6.  
    15:03:39.476 [main] DEBUG org.example.demo1.ChannelDemo1 - 实际字节5
  7.  
    15:03:39.476 [main] DEBUG org.example.demo1.ChannelDemo1 - 实际字节6
  8.  
    15:03:39.476 [main] DEBUG org.example.demo1.ChannelDemo1 - 实际字节7
  9.  
    15:03:39.476 [main] DEBUG org.example.demo1.ChannelDemo1 - 实际字节8
  10.  
    15:03:39.476 [main] DEBUG org.example.demo1.ChannelDemo1 - 实际字节9
  11.  
    15:03:39.476 [main] DEBUG org.example.demo1.ChannelDemo1 - 实际字节0
  12.  
    15:03:39.476 [main] DEBUG org.example.demo1.ChannelDemo1 - 读到字节数:4
  13.  
    15:03:39.477 [main] DEBUG org.example.demo1.ChannelDemo1 - 实际字节a
  14.  
    15:03:39.477 [main] DEBUG org.example.demo1.ChannelDemo1 - 实际字节b
  15.  
    15:03:39.477 [main] DEBUG org.example.demo1.ChannelDemo1 - 实际字节c
  16.  
    15:03:39.477 [main] DEBUG org.example.demo1.ChannelDemo1 - 实际字节d
  17.  
    15:03:39.477 [main] DEBUG org.example.demo1.ChannelDemo1 - 读到字节数:-1
学新通

2.1 ByteBuffer 正确使用姿势

  • 向 buffer 写入数据,例如调用 channel.read(buffer)
  • 调用 flip() 切换至读模式
  • 从 buffer 读取数据,例如调用 buffer.get()
  • 调用 clear() 或 compact() 切换至写模式
  • 重复 1~4 步骤

2.2 ByteBuffer 结构

ByteBuffer 有以下重要属性

  • capacity
  • position
  • limit

一开始

学新通

写模式下,position 是写入位置,limit 等于容量,下图表示写入了 4 个字节后的状态

学新通

flip 动作发生后,position 切换为读取位置,limit 切换为读取限制

学新通

读取 4 个字节后,状态

学新通

clear 动作发生后,状态

学新通

compact 方法,是把未读完的部分向前压缩,然后切换至写模式 

学新通

调试工具类

  1.  
    package org.example.utils;
  2.  
     
  3.  
    import io.netty.util.internal.StringUtil;
  4.  
     
  5.  
    import java.nio.ByteBuffer;
  6.  
     
  7.  
    import static io.netty.util.internal.MathUtil.isOutOfBounds;
  8.  
    import static io.netty.util.internal.StringUtil.NEWLINE;
  9.  
     
  10.  
    public class ByteBufferUtil {
  11.  
    private static final char[] BYTE2CHAR = new char[256];
  12.  
    private static final char[] HEXDUMP_TABLE = new char[256 * 4];
  13.  
    private static final String[] HEXPADDING = new String[16];
  14.  
    private static final String[] HEXDUMP_ROWPREFIXES = new String[65536 >>> 4];
  15.  
    private static final String[] BYTE2HEX = new String[256];
  16.  
    private static final String[] BYTEPADDING = new String[16];
  17.  
     
  18.  
    static {
  19.  
    final char[] DIGITS = "0123456789abcdef".toCharArray();
  20.  
    for (int i = 0; i < 256; i ) {
  21.  
    HEXDUMP_TABLE[i << 1] = DIGITS[i >>> 4 & 0x0F];
  22.  
    HEXDUMP_TABLE[(i << 1) 1] = DIGITS[i & 0x0F];
  23.  
    }
  24.  
     
  25.  
    int i;
  26.  
     
  27.  
    // Generate the lookup table for hex dump paddings
  28.  
    for (i = 0; i < HEXPADDING.length; i ) {
  29.  
    int padding = HEXPADDING.length - i;
  30.  
    StringBuilder buf = new StringBuilder(padding * 3);
  31.  
    for (int j = 0; j < padding; j ) {
  32.  
    buf.append(" ");
  33.  
    }
  34.  
    HEXPADDING[i] = buf.toString();
  35.  
    }
  36.  
     
  37.  
    // Generate the lookup table for the start-offset header in each row (up to 64KiB).
  38.  
    for (i = 0; i < HEXDUMP_ROWPREFIXES.length; i ) {
  39.  
    StringBuilder buf = new StringBuilder(12);
  40.  
    buf.append(NEWLINE);
  41.  
    buf.append(Long.toHexString(i << 4 & 0xFFFFFFFFL | 0x100000000L));
  42.  
    buf.setCharAt(buf.length() - 9, '|');
  43.  
    buf.append('|');
  44.  
    HEXDUMP_ROWPREFIXES[i] = buf.toString();
  45.  
    }
  46.  
     
  47.  
    // Generate the lookup table for byte-to-hex-dump conversion
  48.  
    for (i = 0; i < BYTE2HEX.length; i ) {
  49.  
    BYTE2HEX[i] = ' ' StringUtil.byteToHexStringPadded(i);
  50.  
    }
  51.  
     
  52.  
    // Generate the lookup table for byte dump paddings
  53.  
    for (i = 0; i < BYTEPADDING.length; i ) {
  54.  
    int padding = BYTEPADDING.length - i;
  55.  
    StringBuilder buf = new StringBuilder(padding);
  56.  
    for (int j = 0; j < padding; j ) {
  57.  
    buf.append(' ');
  58.  
    }
  59.  
    BYTEPADDING[i] = buf.toString();
  60.  
    }
  61.  
     
  62.  
    // Generate the lookup table for byte-to-char conversion
  63.  
    for (i = 0; i < BYTE2CHAR.length; i ) {
  64.  
    if (i <= 0x1f || i >= 0x7f) {
  65.  
    BYTE2CHAR[i] = '.';
  66.  
    } else {
  67.  
    BYTE2CHAR[i] = (char) i;
  68.  
    }
  69.  
    }
  70.  
    }
  71.  
     
  72.  
    /**
  73.  
    * 打印所有内容
  74.  
    * @param buffer
  75.  
    */
  76.  
    public static void debugAll(ByteBuffer buffer) {
  77.  
    int oldlimit = buffer.limit();
  78.  
    buffer.limit(buffer.capacity());
  79.  
    StringBuilder origin = new StringBuilder(256);
  80.  
    appendPrettyHexDump(origin, buffer, 0, buffer.capacity());
  81.  
    System.out.println(" -------- -------------------- all ------------------------ ---------------- ");
  82.  
    System.out.printf("position: [%d], limit: [%d]\n", buffer.position(), oldlimit);
  83.  
    System.out.println(origin);
  84.  
    buffer.limit(oldlimit);
  85.  
    }
  86.  
     
  87.  
    /**
  88.  
    * 打印可读取内容
  89.  
    * @param buffer
  90.  
    */
  91.  
    public static void debugRead(ByteBuffer buffer) {
  92.  
    StringBuilder builder = new StringBuilder(256);
  93.  
    appendPrettyHexDump(builder, buffer, buffer.position(), buffer.limit() - buffer.position());
  94.  
    System.out.println(" -------- -------------------- read ----------------------- ---------------- ");
  95.  
    System.out.printf("position: [%d], limit: [%d]\n", buffer.position(), buffer.limit());
  96.  
    System.out.println(builder);
  97.  
    }
  98.  
     
  99.  
    private static void appendPrettyHexDump(StringBuilder dump, ByteBuffer buf, int offset, int length) {
  100.  
    if (isOutOfBounds(offset, length, buf.capacity())) {
  101.  
    throw new IndexOutOfBoundsException(
  102.  
    "expected: " "0 <= offset(" offset ") <= offset length(" length
  103.  
    ") <= " "buf.capacity(" buf.capacity() ')');
  104.  
    }
  105.  
    if (length == 0) {
  106.  
    return;
  107.  
    }
  108.  
    dump.append(
  109.  
    " ------------------------------------------------- "
  110.  
    NEWLINE " | 0 1 2 3 4 5 6 7 8 9 a b c d e f |"
  111.  
    NEWLINE " -------- ------------------------------------------------- ---------------- ");
  112.  
     
  113.  
    final int startIndex = offset;
  114.  
    final int fullRows = length >>> 4;
  115.  
    final int remainder = length & 0xF;
  116.  
     
  117.  
    // Dump the rows which have 16 bytes.
  118.  
    for (int row = 0; row < fullRows; row ) {
  119.  
    int rowStartIndex = (row << 4) startIndex;
  120.  
     
  121.  
    // Per-row prefix.
  122.  
    appendHexDumpRowPrefix(dump, row, rowStartIndex);
  123.  
     
  124.  
    // Hex dump
  125.  
    int rowEndIndex = rowStartIndex 16;
  126.  
    for (int j = rowStartIndex; j < rowEndIndex; j ) {
  127.  
    dump.append(BYTE2HEX[getUnsignedByte(buf, j)]);
  128.  
    }
  129.  
    dump.append(" |");
  130.  
     
  131.  
    // ASCII dump
  132.  
    for (int j = rowStartIndex; j < rowEndIndex; j ) {
  133.  
    dump.append(BYTE2CHAR[getUnsignedByte(buf, j)]);
  134.  
    }
  135.  
    dump.append('|');
  136.  
    }
  137.  
     
  138.  
    // Dump the last row which has less than 16 bytes.
  139.  
    if (remainder != 0) {
  140.  
    int rowStartIndex = (fullRows << 4) startIndex;
  141.  
    appendHexDumpRowPrefix(dump, fullRows, rowStartIndex);
  142.  
     
  143.  
    // Hex dump
  144.  
    int rowEndIndex = rowStartIndex remainder;
  145.  
    for (int j = rowStartIndex; j < rowEndIndex; j ) {
  146.  
    dump.append(BYTE2HEX[getUnsignedByte(buf, j)]);
  147.  
    }
  148.  
    dump.append(HEXPADDING[remainder]);
  149.  
    dump.append(" |");
  150.  
     
  151.  
    // Ascii dump
  152.  
    for (int j = rowStartIndex; j < rowEndIndex; j ) {
  153.  
    dump.append(BYTE2CHAR[getUnsignedByte(buf, j)]);
  154.  
    }
  155.  
    dump.append(BYTEPADDING[remainder]);
  156.  
    dump.append('|');
  157.  
    }
  158.  
     
  159.  
    dump.append(NEWLINE
  160.  
    " -------- ------------------------------------------------- ---------------- ");
  161.  
    }
  162.  
     
  163.  
    private static void appendHexDumpRowPrefix(StringBuilder dump, int row, int rowStartIndex) {
  164.  
    if (row < HEXDUMP_ROWPREFIXES.length) {
  165.  
    dump.append(HEXDUMP_ROWPREFIXES[row]);
  166.  
    } else {
  167.  
    dump.append(NEWLINE);
  168.  
    dump.append(Long.toHexString(rowStartIndex & 0xFFFFFFFFL | 0x100000000L));
  169.  
    dump.setCharAt(dump.length() - 9, '|');
  170.  
    dump.append('|');
  171.  
    }
  172.  
    }
  173.  
     
  174.  
    public static short getUnsignedByte(ByteBuffer buffer, int index) {
  175.  
    return (short) (buffer.get(index) & 0xFF);
  176.  
    }
  177.  
    }
学新通

测试如下:

  1.  
    package org.example.demo1;
  2.  
     
  3.  
    import java.nio.ByteBuffer;
  4.  
     
  5.  
    import static org.example.utils.ByteBufferUtil.debugAll;
  6.  
     
  7.  
    public class TestByteBufferReadWrite {
  8.  
    public static void main(String[] args){
  9.  
    ByteBuffer byteBuffer = ByteBuffer.allocate(10);
  10.  
    byteBuffer.put((byte) 0x61);// a
  11.  
    debugAll(byteBuffer);
  12.  
    byteBuffer.put(new byte[]{0x62,0x63,0x64});
  13.  
    debugAll(byteBuffer);
  14.  
    byteBuffer.get();
  15.  
    debugAll(byteBuffer);
  16.  
    //切换为读的状态
  17.  
    byteBuffer.flip();
  18.  
    byteBuffer.get();
  19.  
    debugAll(byteBuffer);
  20.  
    byteBuffer.compact();
  21.  
    debugAll(byteBuffer);
  22.  
    }
  23.  
    }
学新通

运行结果如下:

  1.  
    18:12:55.063 [main] DEBUG io.netty.util.internal.logging.InternalLoggerFactory - Using SLF4J as the default logging framework
  2.  
    -------- -------------------- all ------------------------ ----------------
  3.  
    position: [1], limit: [10]
  4.  
    -------------------------------------------------
  5.  
    | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
  6.  
    -------- ------------------------------------------------- ----------------
  7.  
    |00000000| 61 00 00 00 00 00 00 00 00 00 |a......... |
  8.  
    -------- ------------------------------------------------- ----------------
  9.  
    -------- -------------------- all ------------------------ ----------------
  10.  
    position: [4], limit: [10]
  11.  
    -------------------------------------------------
  12.  
    | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
  13.  
    -------- ------------------------------------------------- ----------------
  14.  
    |00000000| 61 62 63 64 00 00 00 00 00 00 |abcd...... |
  15.  
    -------- ------------------------------------------------- ----------------
  16.  
    -------- -------------------- all ------------------------ ----------------
  17.  
    position: [5], limit: [10]
  18.  
    -------------------------------------------------
  19.  
    | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
  20.  
    -------- ------------------------------------------------- ----------------
  21.  
    |00000000| 61 62 63 64 00 00 00 00 00 00 |abcd...... |
  22.  
    -------- ------------------------------------------------- ----------------
  23.  
    -------- -------------------- all ------------------------ ----------------
  24.  
    position: [1], limit: [5]
  25.  
    -------------------------------------------------
  26.  
    | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
  27.  
    -------- ------------------------------------------------- ----------------
  28.  
    |00000000| 61 62 63 64 00 00 00 00 00 00 |abcd...... |
  29.  
    -------- ------------------------------------------------- ----------------
  30.  
    -------- -------------------- all ------------------------ ----------------
  31.  
    position: [4], limit: [10]
  32.  
    -------------------------------------------------
  33.  
    | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
  34.  
    -------- ------------------------------------------------- ----------------
  35.  
    |00000000| 62 63 64 00 00 00 00 00 00 00 |bcd....... |
  36.  
    -------- ------------------------------------------------- ----------------
  37.  
     
  38.  
    Process finished with exit code 0
学新通

2.3 ByteBuffer 常见方法

分配空间

可以使用 allocate 方法为 ByteBuffer 分配空间,其它 buffer 类也有该方法

Bytebuffer buf = ByteBuffer.allocate(16);

例子:

  1.  
    package org.example.demo1;
  2.  
     
  3.  
    import java.nio.ByteBuffer;
  4.  
     
  5.  
    public class TestByteBufferAllocate {
  6.  
    public static void main(String[] args){
  7.  
    System.out.println(ByteBuffer.allocate(16).getClass());
  8.  
    System.out.println(ByteBuffer.allocateDirect(16).getClass());
  9.  
     
  10.  
    }
  11.  
    }

运行结果如下: 

学新通

注意:
        class java.nio.HeapByteBuffer    -java 堆内存,读写效率低,受到GC的影响
        class java.nio.DirectByteBuffer   -直接内存,读写效率高(少一次拷贝),不会受GC影响,分配的效率低

向 buffer 写入数据

有两种办法

  • 调用 channel 的 read 方法
  • 调用 buffer 自己的 put 方法
int readBytes = channel.read(buf);

buf.put((byte)127);

从 buffer 读取数据

同样有两种办法

  • 调用channel的write方法
  • 调用buffer自己的get方法
int writeBytes = channel.write(buf);

byte b = buf.get();

get 方法会让 position 读指针向后走,如果想重复读取数据

  • 可以调用 rewind 方法将 position 重新置为 0
  1.  
    package org.example.demo1;
  2.  
     
  3.  
    import java.nio.ByteBuffer;
  4.  
     
  5.  
    import static org.example.utils.ByteBufferUtil.debugAll;
  6.  
     
  7.  
    public class TestByteBufferRead {
  8.  
    public static void main(String[] args){
  9.  
    ByteBuffer buffer = ByteBuffer.allocate(10);
  10.  
    buffer.put(new byte[]{'a','b','c','d'});
  11.  
    buffer.flip();
  12.  
     
  13.  
    //rewind 从头开始读
  14.  
    buffer.get(new byte[4]);
  15.  
    debugAll(buffer);
  16.  
    System.out.println("===============================rewind================================");
  17.  
    buffer.rewind();
  18.  
    System.out.println((char)buffer.get());
  19.  
     
  20.  
    }
  21.  
    }
学新通

调用结果:

  1.  
    -------- -------------------- all ------------------------ ----------------
  2.  
    position: [4], limit: [4]
  3.  
    -------------------------------------------------
  4.  
    | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
  5.  
    -------- ------------------------------------------------- ----------------
  6.  
    |00000000| 61 62 63 64 00 00 00 00 00 00 |abcd...... |
  7.  
    -------- ------------------------------------------------- ----------------
  8.  
    ===============================rewind================================
  9.  
    a
  • 或者调用 get(int i) 方法获取索引 i 的内容,它不会移动读指针
  1.  
    package org.example.demo1;
  2.  
     
  3.  
    import java.nio.ByteBuffer;
  4.  
     
  5.  
    import static org.example.utils.ByteBufferUtil.debugAll;
  6.  
     
  7.  
    public class TestByteBufferRead {
  8.  
    public static void main(String[] args){
  9.  
    ByteBuffer buffer = ByteBuffer.allocate(10);
  10.  
    buffer.put(new byte[]{'a','b','c','d'});
  11.  
    buffer.flip();
  12.  
     
  13.  
    //get(i) 不会改变读索引的位置
  14.  
    System.out.println((char) buffer.get(3));
  15.  
    debugAll(buffer);
  16.  
     
  17.  
    }
  18.  
    }
学新通

调用结果:

学新通

mark 和 reset

mark 是在读取时,做一个标记,即使 position 改变,只要调用 reset 就能回到 mark 的位置

  1.  
    package org.example.demo1;
  2.  
     
  3.  
    import java.nio.ByteBuffer;
  4.  
     
  5.  
    import static org.example.utils.ByteBufferUtil.debugAll;
  6.  
     
  7.  
    public class TestByteBufferRead {
  8.  
    public static void main(String[] args){
  9.  
    ByteBuffer buffer = ByteBuffer.allocate(10);
  10.  
    buffer.put(new byte[]{'a','b','c','d'});
  11.  
    buffer.flip();
  12.  
     
  13.  
    //mark & reset
  14.  
    //mark 做一个标记,记录position位置,reset 是将position重置到mark的位置
  15.  
    System.out.println((char) buffer.get());
  16.  
    System.out.println((char) buffer.get());
  17.  
    buffer.mark();//加标记,索引2的位置
  18.  
    System.out.println((char) buffer.get());
  19.  
    System.out.println((char) buffer.get());
  20.  
    buffer.reset();//将position重置到索引2
  21.  
    System.out.println((char) buffer.get());
  22.  
    System.out.println((char) buffer.get());
  23.  
     
  24.  
    }
  25.  
    }
学新通

测试结果:

  1.  
    a
  2.  
    b
  3.  
    c
  4.  
    d
  5.  
    c
  6.  
    d

注意

rewind 和 flip 都会清除 mark 位置

字符串与ByteBuffer互转

  1.  
    package org.example.demo1;
  2.  
     
  3.  
    import java.nio.ByteBuffer;
  4.  
    import java.nio.CharBuffer;
  5.  
    import java.nio.charset.Charset;
  6.  
    import java.nio.charset.StandardCharsets;
  7.  
     
  8.  
    import static org.example.utils.ByteBufferUtil.debugAll;
  9.  
     
  10.  
    public class TestByteBufferString {
  11.  
    public static void main(String[] args){
  12.  
    ByteBuffer buffer = ByteBuffer.allocate(16);
  13.  
    buffer.put("hello".getBytes());
  14.  
    debugAll(buffer);
  15.  
     
  16.  
    buffer.flip();
  17.  
    CharBuffer charBuffer = StandardCharsets.UTF_8.decode(buffer);
  18.  
    String charBufferstr = charBuffer.toString();
  19.  
    System.out.println(charBufferstr);
  20.  
     
  21.  
    //2.Charset
  22.  
    ByteBuffer buffer2 = StandardCharsets.UTF_8.encode("hello");
  23.  
    debugAll(buffer2);
  24.  
     
  25.  
     
  26.  
    CharBuffer charBuffer1 = StandardCharsets.UTF_8.decode(buffer2);
  27.  
    String buffer1 = charBuffer1.toString();
  28.  
    System.out.println(buffer1);
  29.  
     
  30.  
    //3.wrap
  31.  
    ByteBuffer buffer3 = ByteBuffer.wrap("hello".getBytes());
  32.  
    debugAll(buffer3);
  33.  
     
  34.  
     
  35.  
    CharBuffer charBuffer3 = StandardCharsets.UTF_8.decode(buffer3);
  36.  
    String bufferstr3 = charBuffer3.toString();
  37.  
    System.out.println(bufferstr3);
  38.  
     
  39.  
     
  40.  
    }
  41.  
    }
学新通

输出:

  1.  
    -------- -------------------- all ------------------------ ----------------
  2.  
    position: [5], limit: [16]
  3.  
    -------------------------------------------------
  4.  
    | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
  5.  
    -------- ------------------------------------------------- ----------------
  6.  
    |00000000| 68 65 6c 6c 6f 00 00 00 00 00 00 00 00 00 00 00 |hello...........|
  7.  
    -------- ------------------------------------------------- ----------------
  8.  
    hello
  9.  
    -------- -------------------- all ------------------------ ----------------
  10.  
    position: [0], limit: [5]
  11.  
    -------------------------------------------------
  12.  
    | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
  13.  
    -------- ------------------------------------------------- ----------------
  14.  
    |00000000| 68 65 6c 6c 6f |hello |
  15.  
    -------- ------------------------------------------------- ----------------
  16.  
    -------- -------------------- all ------------------------ ----------------
  17.  
    position: [0], limit: [5]
  18.  
    -------------------------------------------------
  19.  
    | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
  20.  
    -------- ------------------------------------------------- ----------------
  21.  
    |00000000| 68 65 6c 6c 6f |hello |
  22.  
    -------- ------------------------------------------------- ----------------
  23.  
    hello
学新通

Buffer的线程安全

Buffer是非线程安全的

2.4 Scattering Reads

分散读取,有一个文本文件parts.txt

onetwothree

使用如下方式读取,可以将数据填充至多个 buffer

  1.  
    package org.example.demo1;
  2.  
     
  3.  
    import java.io.IOException;
  4.  
    import java.io.RandomAccessFile;
  5.  
    import java.nio.ByteBuffer;
  6.  
    import java.nio.channels.FileChannel;
  7.  
     
  8.  
    import static org.example.utils.ByteBufferUtil.debugAll;
  9.  
     
  10.  
    public class TestByteBufferReads {
  11.  
    public static void main(String[] args){
  12.  
    try (RandomAccessFile file = new RandomAccessFile("parts.txt", "r")) {
  13.  
    FileChannel channel = file.getChannel();
  14.  
    ByteBuffer a = ByteBuffer.allocate(3);
  15.  
    ByteBuffer b = ByteBuffer.allocate(3);
  16.  
    ByteBuffer c = ByteBuffer.allocate(5);
  17.  
    channel.read(new ByteBuffer[]{a, b, c});
  18.  
    a.flip();
  19.  
    b.flip();
  20.  
    c.flip();
  21.  
    debugAll(a);
  22.  
    debugAll(b);
  23.  
    debugAll(c);
  24.  
    } catch (IOException e) {
  25.  
    e.printStackTrace();
  26.  
    }
  27.  
    }
  28.  
    }
学新通

结果:

  1.  
    -------- -------------------- all ------------------------ ----------------
  2.  
    position: [0], limit: [3]
  3.  
    -------------------------------------------------
  4.  
    | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
  5.  
    -------- ------------------------------------------------- ----------------
  6.  
    |00000000| 6f 6e 65 |one |
  7.  
    -------- ------------------------------------------------- ----------------
  8.  
    -------- -------------------- all ------------------------ ----------------
  9.  
    position: [0], limit: [3]
  10.  
    -------------------------------------------------
  11.  
    | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
  12.  
    -------- ------------------------------------------------- ----------------
  13.  
    |00000000| 74 77 6f |two |
  14.  
    -------- ------------------------------------------------- ----------------
  15.  
    -------- -------------------- all ------------------------ ----------------
  16.  
    position: [0], limit: [5]
  17.  
    -------------------------------------------------
  18.  
    | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
  19.  
    -------- ------------------------------------------------- ----------------
  20.  
    |00000000| 74 68 72 65 65 |three |
  21.  
    -------- ------------------------------------------------- ----------------
学新通

2.5 Gathering Writes

使用如下方式写入,可以将多个 buffer 的数据填充至 channel

  1.  
    package org.example.demo1;
  2.  
     
  3.  
    import java.io.IOException;
  4.  
    import java.io.RandomAccessFile;
  5.  
    import java.nio.ByteBuffer;
  6.  
    import java.nio.channels.FileChannel;
  7.  
    import java.nio.charset.StandardCharsets;
  8.  
     
  9.  
    public class TestGatheringWrites {
  10.  
    public static void main(String[] args){
  11.  
    ByteBuffer b1 = StandardCharsets.UTF_8.encode("hello");
  12.  
    ByteBuffer b2 = StandardCharsets.UTF_8.encode("world");
  13.  
    ByteBuffer b3 = StandardCharsets.UTF_8.encode("你好");
  14.  
     
  15.  
    try(FileChannel channel = new RandomAccessFile("words2.txt","rw").getChannel()){
  16.  
    channel.write(new ByteBuffer[]{b1,b2,b3});
  17.  
    }catch (IOException ex){
  18.  
     
  19.  
    }
  20.  
    }
  21.  
    }
学新通

输出结果:

学新通

2.6 黏包半包现象

网络上有多条数据发送给服务端,数据之间使用 \n 进行分隔 但由于某种原因这些数据在接收时,被进行了重新组合,例如原始数据有3条为

  • Hello,world\n
  • I'm zhangsan\n
  • How are you?\n

变成了下面的两个 byteBuffer (黏包,半包)

  • Hello,world\nI'm zhangsan\nHo
  • w are you?\n

现在要求你编写程序,将错乱的数据恢复成原始的按 \n 分隔的数据

  1.  
    public static void main(String[] args) {
  2.  
    ByteBuffer source = ByteBuffer.allocate(32);
  3.  
    // 11 24
  4.  
    source.put("Hello,world\nI'm zhangsan\nHo".getBytes());
  5.  
    split(source);
  6.  
     
  7.  
    source.put("w are you?\nhaha!\n".getBytes());
  8.  
    split(source);
  9.  
    }
  10.  
     
  11.  
    private static void split(ByteBuffer source) {
  12.  
    source.flip();
  13.  
    int oldLimit = source.limit();
  14.  
    for (int i = 0; i < oldLimit; i ) {
  15.  
    if (source.get(i) == '\n') {
  16.  
    System.out.println(i);
  17.  
    ByteBuffer target = ByteBuffer.allocate(i 1 - source.position());
  18.  
    // 0 ~ limit
  19.  
    source.limit(i 1);
  20.  
    target.put(source); //source 读,向 target 写
  21.  
    debugAll(target);
  22.  
    source.limit(oldLimit);
  23.  
    }
  24.  
    }
  25.  
    source.compact();
  26.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfhgkck
系列文章
更多 icon
同类精品
更多 icon
继续加载