使用Java在两个或多个套接字之间执行零拷贝数据传输 [英] Using Java to perform Zero Copy data transfers between two or more sockets
问题描述
有没有人知道在两个或多个套接字之间执行零拷贝数据传输的好的java库/ API包?我知道Java的NIO API可以分别使用java.nio.channels.FileChannel.transferTo和java.nio.channels.FileChannel.transferFrom方法执行从磁盘到套接字的零拷贝数据传输,反之亦然。但是,似乎没有支持Java套接字到套接字零复制传输。另外,任何可以执行系统调用拼接的java库/ API(可以将文件描述符中的数据传输到管道,反之亦然)将是一个优点,最好是在Linux平台上。
感谢您的回应。另外,我已经阅读了大部分有关零拷贝的博客,以及其他信息丰富的网站,如 http://www.ibm.com/developerworks/library/j-zerocopy/ ;不过,上面的问题似乎还没有得到解决。
我对SocketChannel了解不多,但是我认为 ByteBuffer.allocateDirect()
是一个不错的选择。
以下是区别:
1。旧的方式 SocketA - > BufferA(内核空间) - > BufferB(用户空间) 2。零拷贝方式 SocketA - > DirectBuffer(可以从内核和用户空间访问) 根据你提到的文章,FileChannel的transferTo这样做: 使用 Does any one know of any good java libraries/API packages that performs zero copy data transfers between two or more sockets? I know that Java's NIO API can perform zero copy data transfers from disk to socket and vice versa using java.nio.channels.FileChannel.transferTo and java.nio.channels.FileChannel.transferFrom methods respectively. However, there doesn't appear to be support for java socket to socket zero copy transfers. In addition any java libraries/API that can perform the system call splice (which can transfer data from a file descriptor to a pipe and vice versa) would be a plus, preferably on a linux platform. Thank you for response. In addition, I have read most of the previous blogs about zero copy as well as other informative sites such as http://www.ibm.com/developerworks/library/j-zerocopy/; However, it appears the above issue has not been addressed. I don't know much about SocketChannel, but I think Just read socket A's data into Here's the differences: 1. Old Way SocketA --> BufferA(Kernel Space) --> BufferB(User Space) BufferB(User Space) --> BufferC(Kernel Space) --> SocketB 2. Zero Copy Way SocketA --> DirectBuffer(Could be accessed from Kernel and User Space) DirectBuffer --> SocketB IMHO, I don't think we can make it directly According to the article you referred, FileChannel's transferTo do it this way : Use 这篇关于使用Java在两个或多个套接字之间执行零拷贝数据传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
BufferB(用户空间) - > BufferC(内核空间) - > SocketB
<注意:我们不认为我们可以直接把它做成直接的, SocketA - > SocketB
,操作系统需要加载数据到物理内存之前发送出去。
ByteBuffer.allocateDirect()
您不需要在内核和用户空间之间切换上下文。唯一的缓冲区映射到物理内存,而读取和发送使用相同的块。ByteBuffer.allocateDirect()
is a good choice for you.ByteBuffer.allocateDirect()
and let socket B read from it, simple and easy.Note
SocketA -> SocketB
, the os need to load data into physical memory before send them out.========= EDIT =========
ByteBuffer.allocateDirect()
you don't need to switch context between Kernel and User space. The only buffer is mapped to physical memory while reading and sending use the same blocks.