在 Java 中使用单个系统调用通过 header 通过套接字发送文件
Send file over socket with a header with a single system call in Java
我有一个巨大的文件,试图发送该文件的一部分,其中包含 header 和预告片。 (a header + 此文件的一部分 + 尾随数据)
明显的解决方案是 SocketChannel.write(),它包括多个系统调用。所以,我愿意接受建议。
一个选项是 FileChannel.transferTo() 但它不允许在之前或之后附加任何数据,因此它至少包括 3 个系统调用。
在Java中还有其他有效的方法吗?可能类似于 FreeBSD 的 sendFile 实现:
https://www.freebsd.org/cgi/man.cgi?query=sendfile&sektion=2
您可以将 MappedByteBuffer 与 scatter/gather IO 一起使用。
发送性能将更接近 transferTo(),因为 mmap 也使用类似于 sendFile() 调用的内核缓冲区。
此处为半伪代码,带有一次写入调用:
MappedByteBuffer fileBuf = FileChannel.map(....);
ByteBuffer header = ...;
ByteBuffer trailer = ...;
ByteBuffer slice = fileBuf.slice(); // Set limit and position before slice
socketChannel.write(new ByteBuffer[]{header, slice, trailer}, 0, 3);
我有一个巨大的文件,试图发送该文件的一部分,其中包含 header 和预告片。 (a header + 此文件的一部分 + 尾随数据)
明显的解决方案是 SocketChannel.write(),它包括多个系统调用。所以,我愿意接受建议。
一个选项是 FileChannel.transferTo() 但它不允许在之前或之后附加任何数据,因此它至少包括 3 个系统调用。
在Java中还有其他有效的方法吗?可能类似于 FreeBSD 的 sendFile 实现: https://www.freebsd.org/cgi/man.cgi?query=sendfile&sektion=2
您可以将 MappedByteBuffer 与 scatter/gather IO 一起使用。
发送性能将更接近 transferTo(),因为 mmap 也使用类似于 sendFile() 调用的内核缓冲区。
此处为半伪代码,带有一次写入调用:
MappedByteBuffer fileBuf = FileChannel.map(....);
ByteBuffer header = ...;
ByteBuffer trailer = ...;
ByteBuffer slice = fileBuf.slice(); // Set limit and position before slice
socketChannel.write(new ByteBuffer[]{header, slice, trailer}, 0, 3);