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

你可能感兴趣的文章
PostgreSQL学习总结(2)—— PostgreSQL 语法
查看>>
PostgreSQL学习总结(3)—— PostgreSQL 数据类型
查看>>
Qt开发——圆面积计算器
查看>>
PostgreSQL学习总结(5)—— PostgreSQL table 创建与删除
查看>>
PostgreSQL学习总结(6)—— PostgreSQL 模式(SCHEMA)详解
查看>>
PostgreSQL学习总结(7)—— PostgreSQL 语句 INSERT INTO、SELECT、UPDATE、DELETE 等学习
查看>>
PostgreSQL学习总结(8)—— PostgreSQL 基于数据库和基于模式(schema)的多租户分析
查看>>
PostgreSQL学习总结(9)—— PostgreSQL 运算符与表达式
查看>>
PostGreSql学习笔记001---PostgreSQL10.4安装(Windows)_支持PostGreGis_PostJDBC
查看>>
PostGreSql学习笔记002---Navicat Premium中管理PostGreSql 错误:字段rolcatupdate 不存在
查看>>
PostgreSQL学习笔记:PostgreSQL vs MySQL
查看>>
PostgreSQL实现shape数据转geojson数据(地图工具篇.18)
查看>>
PostgreSQL导入shape数据(地图工具篇.10)
查看>>
PostGreSql工作笔记003---在Navicat中创建数据库时报错rolcatupdate不存在_具体原因看其他博文_这里使用pgAdmin4创建管理postgre
查看>>
PostGreSql工作笔记004---PostGreSql修改密码_windows和linux下修改
查看>>
Postgresql常用命令行操作_以及Navicat操作PostGis时的问题_自动截取长度_WKB structure does not match exp---PostgreSQL工作笔记005
查看>>
PostgreSQL忘记密码
查看>>
PostgreSQL数据库pg_dump命令行不输入密码的方法
查看>>
PostgreSQL新手入门
查看>>
postgresql树状结构查询示例
查看>>