Java NIO Channel to Channel Transfers
2016-10-26 23:48
393 查看
In Java NIO you can transfer data directly from one channel to another, if one of the channels is a
a
The
The parameters position and count, tell where in the destination file to start writing (
less is transfered.
Additionally, some
later have more data available. Thus, it may not transfer the entire data requested (
The
Notice how similar the example is to the previous. The only real difference is the which
The issue with
the send buffer is full, and then stop.
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(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()
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】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 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——与传统IO文件复制的效率比较(channel.transferTo、Files.copy 、channel.write)
- Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace.
- Java的NIO之不同channel之间传输数据
- 解读java nio之channel
- JAVA-4NIO之Channel之间的数据传输方法
- 解决Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace.
- Java NIO Channel
- 安卓编译报错:Dex Loader] Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace.
- Error处理:Conversion to Dalvik format failed: Unable to execute dex: java.nio.BufferOverflowException
- Zookeeper昨天还可以启动今天提示: Cannot open channel to 1 at election address /192.168.253.130:3888 java.net.Co