How does zero copy work when receiving data from socket and pushing it to a file?

kant kodali kanth909 at gmail.com
Tue Oct 20 13:16:22 UTC 2020


Hi All,

I am wondering how zero copy works when receiving data from socket, do some
error checking and sending it to a local file? for example I see that the
following code can do a zero copy from socket to a local file however lets
say, before sending it to local file I want my program to verify certain
things on the data that is coming from the socket(like say message
validation etc) now, at this point I lost zero copy. isn't it? because I am
under an assumption that the moment a program is trying to access the data
received from the socker the OS needs to copy it to the user space buffer?
No?

String destination = "/tmp/dest";
int port = 9999;
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.bind(new InetSocketAddress(9999));
SocketChannel socketChannel = serverSocketChannel.accept();
FileChannel fileChannel = new FileOutputStream(destination).getChannel();
fileChannel.transferFrom(socketChannel, 0, 32);
socketChannel.close();
serverSocketChannel.close();

If I look at this from other direction which means reading data from a
local file, doing some verification and then sending to a socket. I feel I
can do the verifications on data while still doing zero copy by calling
fileChannel.map(mmap system call) and then using fileChannel.transferTo

so seems to me that

reading data from local file -> do some verification on data -> send it to
socket (Zero copy possible)

reading data from socket -> do some verification on data -> send it to
local file (Zero copy not possible)

is that correct?

Thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/nio-dev/attachments/20201020/b814552a/attachment.htm>


More information about the nio-dev mailing list