Java NIO 随笔

3 篇文章 0 订阅


从C那边转过来学习java 的NIO,相对来说还是容易些。也更愿意把C底层的socket和Java NIO做些比较。


Java NIO的核心部件:

Channels对应于socket 的文件句柄

Selectors对应于socket的select函数

Buffers对应于socket的内存,通常即读写socket的void 数组。


Channel 包含:

DatagramChannel (TCP)

SocketChannel (UDP)

FileChannel(File)

ServerSocketChannel


Buffer方法:

flip()方法,从写模式转换到读模式

clear()方法,清除所有数据。 compact清除已经读过的数据。

Buffer的三个属性值:

capacity buf申请的内存大小

position 当前位置

limit 写时等于capacity,读时为写的数据大小

rewind方法使得position 为0

mark,reset,打记号


Buffer 类型:

ByteBuffer
MappedByteBuffer
CharBuffer
DoubleBuffer
FloatBuffer
IntBuffer
LongBuffer
ShortBuffer


Buffer分配:

allocate方法


Buffer读写方法:

int bytesRead = inChannel.read(buf); //read into buffer.
buf.put(127);

int bytesWritten = inChannel.write(buf);
byte aByte = buf.get();


Channel分散读,聚集写:

ByteBuffer header = ByteBuffer.allocate(128);
ByteBuffer body = ByteBuffer.allocate(1024);
ByteBuffer[] bufferArray = {header,body};
channel.read(bufferArray);

ByteBuffer header = ByteBuffer.allocate(128);
ByteBuffer body = ByteBuffer.allocate(1024);
ByteBuffer[] bufferArray = {header,body};
channel.write(bufferArray);


demo:

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class ChannelTest {
	
	public static void main(String args[]) throws IOException
	{	
		RandomAccessFile aFile = new RandomAccessFile("/home/violation.network.1446106703.ing", "r");
		FileChannel inChannel = aFile.getChannel();
		ByteBuffer buf = ByteBuffer.allocate(48);
		int bytesRead = inChannel.read(buf);
		while (bytesRead != -1) 
		{
			System.out.println("Read " + bytesRead);
			buf.flip();
			while(buf.hasRemaining()){
				System.out.print((char) buf.get());
			}
			buf.clear();
			bytesRead = inChannel.read(buf);
		}
		aFile.close();
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值