博客
关于我
java 零拷贝 分块传输大文件(解除8M限制)
阅读量:264 次
发布时间:2019-03-01

本文共 3378 字,大约阅读时间需要 11 分钟。

NIO-Based Zero Copy File Transfer Implementation

Server Side Implementation

package com.atguigu.nio.zerocopy;import java.io.IOException;import java.net.InetSocketAddress;import java.net.ServerSocket;import java.nio.ByteBuffer;import java.nio.channels.ServerSocketChannel;import java.nio.channels.SocketChannel;public class NewIOServer {    public static void main(String[] args) throws Exception {        InetSocketAddress address = new InetSocketAddress(7002);        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();        ServerSocket serverSocket = serverSocketChannel.socket();        serverSocket.bind(address);        ByteBuffer byteBuffer = ByteBuffer.allocate(4096);        while (true) {            SocketChannel socketChannel = serverSocketChannel.accept();            int readCount = 0;            while (-1 != readCount) {                try {                    readCount = socketChannel.read(byteBuffer);                } catch (Exception e) {                    break;                }                byteBuffer.rewind();            }        }    }}

Client Side Implementation

package com.atguigu.nio.zerocopy;import java.io.FileInputStream;import java.net.InetSocketAddress;import java.nio.channels.FileChannel;import java.nio.channels.SocketChannel;public class NewIOClient {    public static void main(String[] args) throws Exception {        SocketChannel socketChannel = SocketChannel.open();        socketChannel.connect(new InetSocketAddress("localhost", 7002));                String filename = "./test.avi";        FileChannel fileChannel = new FileInputStream(filename).getChannel();                long startTime = System.currentTimeMillis();        long totalSize = fileChannel.size();        long max_pkg = 8 * 1024 * 1024;        long cur_pos = 0;        long transSize = 0;                while (totalSize > max_pkg) {            long tmp = fileChannel.transferTo(cur_pos, max_pkg, socketChannel);            totalSize -= tmp;            cur_pos += tmp;            transSize += tmp;        }                if (totalSize > 0) {            long tmp = fileChannel.transferTo(cur_pos, totalSize, socketChannel);            transSize += tmp;        }                System.out.println("Total bytes transferred: " + transSize +                          " Elapsed time: " + (System.currentTimeMillis() - startTime));        fileChannel.close();    }}

Explanation

Server Side

  • Port Binding: The server binds to port 7002 using InetSocketAddress.
  • Reading Data: It uses a ByteBuffer of size 4096 to read data from connected clients.
  • Zero Copy Mechanism: The data is read directly from the socket channel into the buffer, leveraging zero copy functionality for efficient I/O operations.

Client Side

  • Connection Establishment: The client connects to the server at port 7002.
  • File Reading: It reads the file using FileInputStream and converts it to a FileChannel for NIO operations.
  • Zero Copy Transfer: The client transfers the file data to the server in chunks to handle large files efficiently. The transfer size is dynamically adjusted based on the maximum packet size (8MB in this implementation).
  • Performance Metrics: The total transfer time and bytes transferred are logged for verification purposes.

This implementation demonstrates the practical application of NIO's zero copy mechanism for high-performance file transfer scenarios.

转载地址:http://cqaa.baihongyu.com/

你可能感兴趣的文章
Openstack 之 网络设置静态IP地址
查看>>
OpenStack 存储服务详解
查看>>
openstack 导出镜像
查看>>
OpenStack 搭建私有云主机实战(附OpenStack实验环境)
查看>>
OpenStack 综合服务详解
查看>>
OpenStack 网络服务Neutron详解
查看>>
Openstack 网络管理企业级实战
查看>>
OpenStack 计算服务Nova详解
查看>>
Openstack(两控制节点+四计算节点)-1
查看>>
openstack--memecache
查看>>
openstack-keystone安装权限报错问题
查看>>
openstack【Kilo】汇总:包括20英文文档、各个组件新增功能及Kilo版部署
查看>>
openstack下service和endpoint
查看>>
Openstack企业级云计算实战第二、三期培训即将开始
查看>>
OpenStack创建虚拟机实例实战
查看>>
OpenStack安装部署实战
查看>>
OpenStack实践系列⑨云硬盘服务Cinder
查看>>
OpenStack架构
查看>>
OpenStack版本升级与故障排查实战
查看>>
Openstack的HA解决方案【替换原有的dashboard】
查看>>