NIO-Buffer-Channel

Channel

import java.nio.channels.Channel;
public interface Channel extends Closeable {
	public boolean isOpen();
	public void close() throws IOException;
}
public static void exampleForChannel() throws IOException {
		ByteBuffer byteBuffer = ByteBuffer.allocate(10);
		Channel channel;
		
		FileChannel fileChannel = null;//文件数据读写
		fileChannel.read(byteBuffer);//从通道读取数据写入buffer
		fileChannel.write(byteBuffer);//把buffer数据写入通道中
		
		ServerSocketChannel serverSocketChannel;//TCP数据读写
		SocketChannel socketChannel;//TCP数据读写
		DatagramChannel datagramChannel; //UDP数据读写
}
public static void exampleForChannel_3() throws IOException {
		String filePath = "D:\\temp\\c\\0325\\1616577053009.txt";
		FileInputStream fileInputStream = new FileInputStream(filePath);
		FileChannel fileChannel = fileInputStream.getChannel();	
		
		String filePath2 = "D:\\temp\\c\\0325\\"+System.currentTimeMillis()+".txt";
		File file = new File(filePath2);
		if(!file.exists()) {
			file.createNewFile();
		}
		FileOutputStream fileOutputStream = new FileOutputStream(filePath2);
		FileChannel fileChannel2 = fileOutputStream.getChannel();
		
		ByteBuffer byteBuffer = ByteBuffer.allocate(5);
		while(true) {
			//复位
			byteBuffer.clear();
			int read = fileChannel.read(byteBuffer);
			if(read == -1) {
				break;
			}
			byteBuffer.flip();
			fileChannel2.write(byteBuffer);
		}		
		fileInputStream.close();
		fileOutputStream.close();
		System.out.println("文件拷贝完毕=======");
	}
public static void exampleForChannel_4() throws IOException {
		String filePath = "D:\\temp\\c\\0325\\12.jpg";
		FileInputStream fileInputStream = new FileInputStream(filePath);
		FileChannel source = fileInputStream.getChannel();	
		
		String filePath2 = "D:\\temp\\c\\0325\\"+System.currentTimeMillis()+".jpg";
		File file = new File(filePath2);
		/*if(!file.exists()) {
			file.createNewFile();
		}*/
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		FileChannel destion = fileOutputStream.getChannel();
		
		destion.transferFrom(source, 0, source.size());
		
		source.close();
		destion.close();
		fileInputStream.close();
		fileOutputStream.close();
		System.out.println("文件拷贝完毕=======");
	}

Buffer

import java.nio.ByteBuffer; //字节数据
import java.nio.CharBuffer; //字符数据
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer; 

	public static void exampleForBuffer() {
		//大小为5的Buffer
		IntBuffer intBuffer = IntBuffer.allocate(5);
		for(int i = 0;i<intBuffer.capacity();i++) {
			intBuffer.put(2*i);
		}
		//读写切换
		intBuffer.flip();
		intBuffer.position(1);
		intBuffer.limit(3);
		while(intBuffer.hasRemaining()) {
			//内部维护了索引Index
			System.out.println(intBuffer.get());
		}
	}
public abstract class IntBuffer extends Buffer implements Comparable<IntBuffer>{
	final int[] hb;
}
public abstract class Buffer {
	private int mark = -1; //标记
    private int position = 0; //当前位置索引
    private int limit; //当前缓冲区的终点
    private int capacity; //容量,不可变
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值