Java NIO(6-Channel to Channel Transfers)
2013-10-27 19:06
495 查看
In Java NIO you can transfer data directly from one channel to another, if one of the channels is a
a
The
The two first parameters, position and count, tell where in the destination file to start writing (
If the source channel has fewer than
Additionally, some
if the
The
Notice how similar the example is to the previous. The only real difference is the which
The issue with
may only transfer bytes from the
FileChannel. The
FileChannelclass has a
transferTo()and
a
transferFrom()method which does this for you.
transferFrom()
The FileChannel.transferFrom()method transfers data from a source channel into the
FileChannel. Here is a simple example:
RandomAccessFile fromFile = new RandomAccessFile("fromFile.txt", "rw"); FileChannel fromChannel = fromFile.getChannel(); RandomAccessFile toFile = new RandomAccessFile("toFile.txt", "rw"); FileChannel toChannel = toFile.getChannel(); long position = 0; long count = fromChannel.size(); toChannel.transferFrom(position, count, fromChannel);
The two first parameters, position and count, tell where in the destination file to start writing (
position), and how many bytes to transfer maximally (
count).
If the source channel has fewer than
countbytes, less is transfered.
Additionally, some
SocketChannelimplementations may transfer only the data the
SocketChannelhas ready in its internal buffer here and now - even
if the
SocketChannelmay later have more data available. Thus, it may not transfer the entire data requested (
count) from the
SocketChannelinto
FileChannel.
transferTo()
The transferTo()method transfer from a
FileChannelinto some other channel. Here is a simple example:
RandomAccessFile fromFile = new RandomAccessFile("fromFile.txt", "rw"); FileChannel fromChannel = fromFile.getChannel(); RandomAccessFile toFile = new RandomAccessFile("toFile.txt", "rw"); FileChannel toChannel = toFile.getChannel(); long position = 0; long count = fromChannel.size(); fromChannel.transferTo(position, count, toChannel);
Notice how similar the example is to the previous. The only real difference is the which
FileChannelobject the method is called on. The rest is the same.
The issue with
SocketChannelis also present with the
transferTo()method. The
SocketChannelimplementation
may only transfer bytes from the
FileChanneluntil the send buffer is full, and then stop.
相关文章推荐
- Java NIO Channel to Channel Transfers
- Java NIO Channel to Channel Transfers
- 【JAVA】【NIO】6、Java NIO Channel to Channel Transfers
- Java NIO——与传统IO文件复制的效率比较(channel.transferTo、Files.copy 、channel.write)
- Java NIO Channel to Channel Transfers
- Java NIO Channel to Channel Transfers通道传输接口
- Java NIO Channel to Channel Transfers
- Java NIO Channel to Channel Transfers
- Java NIO Channel to Channel Transfers
- Java NIO Channel to Channel Transfers
- Java NIO (五) Channel to Channel Transfers
- 黑马程序员_O‘Reilly java nio学习笔记之通道_内存映射文件&&Channel-to-Channel 传输
- Java NIO Channel to Channel Transfers
- Java NIO系列教程(九) ServerSocketChannel
- Java NIO系列教程(八) SocketChannel
- Java NIO系列教程(二) Channel
- UDK: Lightmap UV Layout Techniques & How to Create a Second UV Channel in Maya
- Java NIO系列教程(八) SocketChannel
- Java NIO系列教程(二) Channel
- Java NIO系列教程(九) ServerSocketChannel