• Java NIO(二)


    http://wiki.jikexueyuan.com/project/java-nio-zh/java-nio-scatter-gather.html

    05. Java NIO Scatter / Gather

    Scattering read指的是从通道读取的操作能把数据写入多个buffer,也就是sctters代表了数据从一个channel到多个buffer的过程。

    gathering write则正好相反,表示的是从多个buffer把数据写入到一个channel中。

    Scatter/gather在有些场景下会非常有用,比如需要处理多份分开传输的数据。举例来说,假设一个消息包含了header和body,我们可能会把header和body保存在不同独立buffer中,这种分开处理header与body的做法会使开发更简明。

    ByteBuffer header = ByteBuffer.allocate(128);
    ByteBuffer body   = ByteBuffer.allocate(1024);
    
    ByteBuffer[] bufferArray = { header, body };
    
    channel.read(bufferArray);//必须写满一个buffer后才会向后移动到下一个buffer,适合header大小固定的读取

    Gathering Writes

    ByteBuffer header = ByteBuffer.allocate(128);
    ByteBuffer body   = ByteBuffer.allocate(1024);
    
    //write data into buffers
    
    ByteBuffer[] bufferArray = { header, body };
    
    channel.write(bufferArray);//按顺序将数组内的内容写进channel,写入buffer中position到limit之间的数据,适用于可变大小的message

    06. Java NIO Channel to Channel Transfers通道传输接口

    在Java NIO中如果一个channel是FileChannel类型的,利用FileChannel包含的transferTo和transferFrom两个方法,可以直接把数据传输到另一个channel。

    FileChannel.transferFrom方法把数据从通道源传输到FileChannel:

    RandomAccessFile fromFile = new RandomAccessFile("fromFile.txt", "rw");
    FileChannel      fromChannel = fromFile.getChannel();
    
    RandomAccessFile toFile = new RandomAccessFile("toFile.txt", "rw");
    FileChannel      toChannel = toFile.getChannel();
    
    long position = 0;
    long count    = fromChannel.size();
    
    toChannel.transferFrom(fromChannel, position, count);

    transferFrom的参数position和count表示目标文件的写入位置和最多写入的数据量。如果通道源的数据小于count那么就传实际有的数据量。 另外,有些SocketChannel的实现在传输时只会传输哪些处于就绪状态的数据,即使SocketChannel后续会有更多可用数据。因此,这个传输过程可能不会传输整个的数据。

    transferTo方法把FileChannel数据传输到另一个channel

    RandomAccessFile fromFile = new RandomAccessFile("fromFile.txt", "rw");
    FileChannel      fromChannel = fromFile.getChannel();
    
    RandomAccessFile toFile = new RandomAccessFile("toFile.txt", "rw");
    FileChannel      toChannel = toFile.getChannel();
    
    long position = 0;
    long count    = fromChannel.size();
    
    fromChannel.transferTo(position, count, toChannel);

    这段代码和之前介绍transfer时的代码非常相似,区别只在于调用方法的是哪个FileChannel.

    SocketChannel的问题也存在与transferTo.SocketChannel的实现可能只在发送的buffer填充满后才发送,并结束。

  • 相关阅读:
    CentOS 7 最小化安装建议安装包
    何为“精通Java”
    初识设计模式、软件设计的六大原则
    Git——常用场景解析
    元素水平或垂直居中问题
    书写静态页面的那些事儿。。。
    Position定位相关知识了解
    溢出处理、盒子模型、背景图片、float(浮动)
    CSS颜色、单位、文本样式
    CSS样式表及选择器相关内容(二)-伪类与伪元素选择器
  • 原文地址:https://www.cnblogs.com/jerrice/p/7118371.html
Copyright © 2020-2023  润新知