11

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.

user3155961
  • 111
  • 1
  • 3
  • Thank you guys for your response! There must be a way to perform zero copy data transfers between sockets using java. – user3155961 Jan 04 '14 at 04:30

1 Answers1

-1

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.

Community
  • 1
  • 1
WoooHaaaa
  • 19,732
  • 32
  • 90
  • 138
  • That's two copies, not zero. – user207421 Jan 03 '14 at 06:04
  • 3
    IIRC, `Zero Copy` means to reduce the copy between kernel space and user space, is it right ? – WoooHaaaa Jan 03 '14 at 06:13
  • No, it means zero copies. Zero means zero, not two. It can be accomplished by the transferTo() function, as the OP has described himself. Not via DirectByteBuffers. – user207421 Jan 03 '14 at 11:27
  • @EJP, I don't quite sure, but I think `FileChannel`'s `transferTo` still copy file data into physical memory in the first step, and them write it to another `FileChannel`, so actually there's no exactly `Zero Copy` exist. – WoooHaaaa Jan 04 '14 at 04:51
  • I agree. You're not quite sure. You're guessing. The Javadoc doesn't agree with you. ["This method is potentially much more efficient than a simple loop that reads from this channel and writes to the target channel. Many operating systems can transfer bytes directly from the filesystem cache to the target channel without actually copying them.'](http://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html#transferTo(long,%20long,%20java.nio.channels.WritableByteChannel)) – user207421 Jan 04 '14 at 09:00
  • @EJP I think you may not get what I'm saying, `FileChannel` can uses file system cache, but if the source file has never been used before, OS still have to copy them into physical memory(Same as read into Direct Buffer). – WoooHaaaa Jan 04 '14 at 11:57
  • 1
    but that is not a copy between *kernel space memory and user space memory*, so that's irrelevant. That is a copy between disk and memory. – Robin Green Jan 04 '14 at 12:12
  • @RobinGreen A copy between disk and memory can't be avoid, even use `FileChannel`'s `transferTo`. Unless the file data has already been cached(which shouldn't be consider all the time) – WoooHaaaa Jan 04 '14 at 12:14
  • 2
    please stop trying to deny that zero copy I/O exists, just do some Googling and you will see – Robin Green Jan 04 '14 at 12:15
  • 1
    @RobinGreen according to http://www.ibm.com/developerworks/library/j-zerocopy/ there is copy from file to buffer and from socket buffer to socket. Is there a way to do copy from file directly to socket without buffer? If so can you share a link on that? Thanks – vishnu viswanath Mar 03 '15 at 05:28