Using Java to perform Zero Copy data transfers between two or more sockets

后端 未结 1 1161
被撕碎了的回忆
被撕碎了的回忆 2021-02-08 04:08

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

相关标签:
1条回答
  • 2021-02-08 04:39

    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

    Note

    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.

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

    According to the article you referred, FileChannel's transferTo do it this way :

    enter image description here

    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.

    0 讨论(0)
提交回复
热议问题