博客
关于我
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/

你可能感兴趣的文章
QGIS中怎样设置图层样式并导出地图样式
查看>>
PowerDesigner使用笔记
查看>>
QGIS中怎样实现数据坐标系转换
查看>>
PowerDesigner学习--基本步骤
查看>>
PowerDesigner导出Report通用报表
查看>>
PowerDesigner教程系列(二)概念数据模型
查看>>
Powerdesigner显示表的comment和列的comment的方法
查看>>
PowerDesigner最基础的使用方法入门学习
查看>>
PowerDesigner版本控制器设置权限
查看>>
PowerDesigner生成数据模型并导出报告
查看>>
QGIS中导入dwg文件并使用GetWKT插件获取绘制元素WKT字符串以及QuickWKT插件实现WKT显示在图层
查看>>
PowerDesigner逆向工程从SqlServer数据库生成PDM(图文教程)
查看>>
PowerEdge T630服务器安装机器学习环境(Ubuntu18.04、Nvidia 1080Ti驱动、CUDA及CUDNN安装)
查看>>
PowerPC-object与elf中的符号引用
查看>>
QFileSystemModel
查看>>
Powershell DSC 5.0 - 参数,证书加密账号,以及安装顺序
查看>>
PowerShell 批量签入SharePoint Document Library中的文件
查看>>
Powershell 自定义对象小技巧
查看>>
pytorch从预训练权重加载完全相同的层
查看>>
PowerShell~发布你的mvc网站
查看>>