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 d
I don't know much about SocketChannel, but I think ByteBuffer.allocateDirect()
is a good choice for you.
Just read socket A's data into ByteBuffer.allocateDirect()
and let socket B read from it, simple and easy.
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 SocketA -> SocketB
, the os need to load data into physical memory before send them out.
According to the article you referred, FileChannel's transferTo do it this way :
Use 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.