Using Java to Transfer Zero Copy Data Between Two or More Sockets

Does anyone know of any good java libraries / API packages that do zero data transfers with copies between two or more sockets? I know that the Java NIO API can perform zero transfers of data from disk to socket and vice versa using the java.nio.channels.FileChannel.transferTo and java.nio.channels.FileChannel.transferFrom methods respectively. However, it seems that java socket support for zero copy sockets is not supported. In addition, any java libraries / APIs that can splice system calls (which can transfer data from a file descriptor to a pipe and vice versa) would be a plus, preferably on the linux platform.

Thanks for the answer.

In addition, I read most of the previous zero-copy blogs, as well as other information sites, such as http://www.ibm.com/developerworks/library/j-zerocopy/ ; However, it seems that the above problem has not been resolved.

+7
java file-io zero-copy
source share
1 answer

I don't know much about SocketChannel, but I think ByteBuffer.allocateDirect() is a good choice for you.

Just read the socket. Data in ByteBuffer.allocateDirect() and let socket B read it simple and easy.

Here are the differences:

1. The old way

SocketA -> BufferA (kernel space) -> BufferB (user space)

BufferB (user space) -> BufferC (kernel space) -> SocketB

2. Zero copy path

SocketA -> DirectBuffer (access to it from the kernel and user space)

DirectBuffer โ†’ SocketB

Note

IMHO, I donโ€™t think we can do this directly SocketA -> SocketB , os need to load data into physical memory before sending it.

========= EDIT ==========

According to the article you mentioned, FileChannel transferTo do this as follows:

enter image description here

Use ByteBuffer.allocateDirect() , you do not need to switch the context between the kernel and user space. A single buffer is mapped to physical memory, while reading and sending use the same blocks.

-one
source share

All Articles