Java NIO Channel to Channel Transfers
2017-07-25 16:09
405 查看
Java Nio
By Jakob Jenkov Connect with me:
Rate article:Share article:Tweet
Table of ContentstransferFrom()
transferTo()
In Java NIO you can transfer data directly from one channel to another, if one of the channels is a
Additionally, some
The issue with
Next: Java NIO Selector
Java NIO Channel to Channel Transfers
By Jakob Jenkov Connect with me:
Rate article:Share article:Tweet
Table of ContentstransferFrom()
transferTo()
In Java NIO you can transfer data directly from one channel to another, if one of the channels is a
FileChannel. The
FileChannelclass has a
transferTo()and a
transferFrom()method which does this for you.
transferFrom()
TheFileChannel.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(fromChannel, position, count);The 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()
ThetransferTo()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.
Next: Java NIO Selector
相关文章推荐
- 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】6、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——与传统IO文件复制的效率比较(channel.transferTo、Files.copy 、channel.write)
- Java NIO ServerSocketChannel
- Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace.
- android Unable to execute dex: java.nio.BufferOverflowException错误
- Java NIO SocketChannel
- android错误:Unable to execute dex: java.nio.BufferOverflowException.Check
- Java NIO 学习(五)--DataGramChannel
- Unable to execute dex: java.nio.BufferOverflowException.解决办法
- 【Java.NIO】API —— Channel接口 —— SocketChannel类
- java的nio之:java的nio系列教程之channel的概念