nio 实现从写入流复制到输入流

话就不多说了,直接上代码:


package com.yao.nio.channel;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

public class ChannelCopy {

	public static void main(String[] args) throws IOException {
		ReadableByteChannel src = Channels.newChannel(System.in);
		WritableByteChannel desc = Channels.newChannel(System.out);
		
		copy1(src,desc);
		
		src.close();
		desc.close();
	}
	
	private static void copy1(ReadableByteChannel src,WritableByteChannel desc) throws IOException{
		ByteBuffer buffer = ByteBuffer.allocate(16*1024);
		while(src.read(buffer) != -1){
			buffer.flip();
			desc.write(buffer);
			buffer.compact();
		}
		//可能有阻塞的没有写入通道
		buffer.flip();
		while (buffer.hasRemaining()) {
			desc.write(buffer);
		}
	}
	
	//和方法一功能一样
	private static void copy2(ReadableByteChannel src,WritableByteChannel desc) throws IOException{
		ByteBuffer buffer = ByteBuffer.allocate(16*1024);
		while (src.read(buffer) != -1) {
			buffer.flip();
			while(buffer.hasRemaining()){
				desc.write(buffer);
			}
			buffer.clear();
		}
	}

}

其中copy1和copy2的功能是一样的,留意一下它们的差别。因为数据在传输的过程中调用 write 出现数据阻塞的时候,可能会出现有些数据还没有完全写到输出通道。所以要用 hasRemaining() 判断一下再继续写。

注意到copy1用到的是 buffer.compact() 压缩方法,如果缓冲区中所有数据都读出后它的实现是和 buffer.clear()一样的。不过如果还有留下的数据 compact() 可以继续把没有读入的继续到下次读。


你可能感兴趣的:(nio)