Channel就是一个通道,用于传输数据,两端分别是缓冲区和实体(文件或者套接字),通道的特点(也是NIO的特点):通道中的数据总是要先读到一个缓冲区,或者总是要从一个缓冲区中读入。
Channel的分类
1) FileChannel:从文件中读写数据
2) SocketChannel:通过TCP协议读写网络中的数据
3) ServerSocketChannel:在服务器端可以监听新进来的TCP连接,像WEB服务器那样,对每一个新进来的请求创建一个SocketChannel
4) DatagramChannel:通过UDP协议读写网络中的数据
上面众多的分类,是对应了不同的实体,这些通道包括了文件IO、TCP和UDP网络IO。
下面来看看Channel的源码:
public interface Channel extends Closeable {
public boolean isOpen();
public void close() throws IOException;
}
从这里我们可以看到,Channel接口只提供了关闭通道和检测通道是否打开这两个方法,剩下方法的都是由子接口和实现类来定义提供。
我们选择其中几个来看看这些接口的源码:
public interface WritableByteChannel
extends Channel
{
public int write(ByteBuffer src) throws IOException;
}
public interface ReadableByteChannel extends Channel
{
public int read(ByteBuffer dst) throws IOException;
}
public interface ByteChannel
extends ReadableByteChannel, WritableByteChannel
{
}
前面我提到过:通道可以只读、只写或者同时读写,因为Channel类可以只实现只读接口ReadableByteChannel或者只实现只写接口WritableByteChannel,而我们常用的Channel类FileChannel、SocketChannel、DatagramChannel是双向通信的, 因为实现了ByteChannel接口。
Channel的获取
IO在广义上可以分为:文件IO和网络IO。文件IO对应的通道为FileChannel,而网络IO对应的通道则有三个:SocketChannel、ServerSoketChannel和DatagramChannel。
一、文件通道
FileChannel对象不能直接创建,只能通过FileInputStream、OutputStream、RandomAccessFile对象的getChannel()来获取,如:
FileInputStream fis = new FileInputStream("c:/in.txt");
FileChannel fic = fis.getChannel();
FileChannel无法设置为非阻塞模式,它总是运行在阻塞模式下。
1)使用通道读取文件
public class NIOFileReadTest
{
public static void main(String[] args) throws IOException
{
RandomAccessFile raf = new RandomAccessFile("D:/in.txt","rw");
FileChannel fis = raf.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
fis.read(buffer);
buffer.flip();
while(buffer.hasRemaining())
{
System.out.print((char)buffer.get());
}
buffer.clear();
fis.close();
}
}
执行结果:
FileChannel
ByteBuffer
SelectorPicked
2)使用通道写入文件
public class NIOFileWriteTest
{
public static void main(String[] args) throws Exception
{
FileOutputStream fos = new FileOutputStream("d:/out.txt");
FileChannel fc = fos.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(10);
buffer.clear();
String str = "Channel";
buffer.put(str.getBytes());
buffer.flip();
while(buffer.hasRemaining())
{
fc.write(buffer);
}
fc.close();
fos.close();
}
}
在这里总是要记住channel是要关闭的。
ByteBuffer中的方法我在下一章再详细介绍,这里只要注意这点即可:通道只能使用ByteBuffer,不管是读还是写,通道都要对接缓冲区。
3)通道的常用方法
position();返回通道的文件位置
position(long newPosition):设置通道的文件位置
将上面读文件的程序修改下,来观察这几个方法:
public class NIOFileReadTest
{
public static void main(String[] args) throws IOException
{
RandomAccessFile raf = new RandomAccessFile("D:/in.txt","rw");
FileChannel fis = raf.getChannel();
System.out.println("此通道文件的总长度:" +fis.size());
//当前通道的文件位置
long position = fis.position();
System.out.println("通道当前的位置:" + position);
//设置新的通道文件位置,从这个位置开始读取
fis.position(position + 8);
ByteBuffer buffer = ByteBuffer.allocate(50);
fis.read(buffer);
buffer.flip();
while(buffer.hasRemaining())
{
System.out.print((char)buffer.get());
}
buffer.clear();
fis.close();
}
}
执行结果:
此通道文件的总长度:33
通道当前的位置:0
nel
ByteBuffer
Selector
FileChannel是线程安全的,可以多个线程在同一个实例上并发操作,但是其中有些方法(改变文件通道位置或者文件大小的方法)必须是单线程操作。
二、网络通道
SocketChannel是一个连接到TCP套接字的通道,获取的方式有两种:
1、打开一个SocketChannel并连接到互联网上某台服务器。
2、一个新连接到达ServerSocketChannel时,会创建一个SocketChannel。
上面这两种模式跟IO的Socket、ServerSocket类似,下面分别来看看客户端和服务器端:
一、SocketChannel
从通道中读取数据
public class SocketChannelTest
{
public static void main(String[] args) throws Exception
{
//获取socket通道
SocketChannel sc = SocketChannel.open();
//设置为非阻塞模式
sc.configureBlocking(false);
//建立连接,非阻塞模式下,该方法可能在连接建立之前就返回了
sc.connect(new InetSocketAddress("wap.cmread.com",80));
//判断连接是否建立
while(!sc.finishConnect())
{
System.out.println("连接未建立");
Thread.sleep(5);
}
ByteBuffer buffer = ByteBuffer.allocate(48);
int byteRead = sc.read(buffer);
System.out.println(byteRead);
sc.close();
buffer.clear();
}
}
执行结果;
连接未建立
连接未建立
0
1、第6、7行是获取一个socket通道,并且设置为非阻塞模式。
2、由于是非阻塞模式,通道在调用方法connect/read/writer这三个方法时,会出现这些情况:连接未建立,connect方法就返回了;尚未读取任何数据时,read方法就返回;尚未写出任何内容时,writer就返回。
3、在12行的循环代码中,是判断连接是否建立,从执行结果来看,循环执行了两次连接才建立(在循环里线程还有休眠)。
4、由于只是建立了连接,通道里面其实没有任何的数据。
5、第18行调用read方法,由于是非阻塞模式,所以在并未读取任何数据的情况下就返回0(尽管通道里面没有数据)。
将数据写入通道
public class SocketChannelTest
{
public static void main(String[] args) throws Exception
{
SocketChannel sc = SocketChannel.open();
String str = "non-blocking socket channel";
ByteBuffer buffer = ByteBuffer.allocate(100);
buffer.put(str.getBytes());
buffer.flip();
while(buffer.hasRemaining())
{
sc.write(buffer);
}
sc.close();
buffer.clear();
}
}
1、SocketChannel.write()方法的调用是在一个while循环中的。Write()方法无法保证能写多少字节到SocketChannel。所以,我们重复调用write()直到Buffer没有要写的字节为止。
二、ServerSocketChannel
ServerSocketChannel是一个可以监听新进来的TCP连接的通道。
public class ServerSocketChannelTest
{
public static void main(String[] args) throws Exception
{
ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket().bind(new InetSocketAddress(80));
ssc.configureBlocking(false);
while(true)
{
SocketChannel sc = ssc.accept();
if(null != sc)
{
//do something;
}
}
}
}
1、第5、6、7行,获取一个ServerSocketChannel,并且监听80端口,设置为非阻塞模式。
2、通过accept方法监听新接入进来的连接,这个方法会返回一个包含新进来的连接的SocketChannel(服务器端的通道的获取方式)。如果是阻塞模式,该方法会一直阻塞直到有新的连接进来。如果是非阻塞模式,则accept方法会立刻返回,返回值是null。
3、第11行,是因为在非阻塞模式下,需要检查SocketChannel是否为null。
三、socket通道与socket
ServerSocketChannel ssc = ServerSocketChannel.open();
ServerSocket socket = ssc.socket();
ServerSocketChannel ssc1 = socket.getChannel();
1、从这代码片段可以大概看到这样一种关系:所有socket通道(SocketChannel/ServerSocketChanne/DatagramSocketChannel)在被实例化之后,都是伴随生成对应的socket对象,就是前面IO章节介绍的java.net类(Socket/ServerSocket/DatagramSocket)。通过通道类的socket方法来获取。
2、java.net类(Socket/ServerSocket/DatagramSocket)现在可以通过getChannel方法来获取对应的通道。前提是这些socket对象不是使用传统方式(直接实例化)创建的。否则它就没有关联的socket通道,调用getChannel方法返回总是null。
Java NIO理解(一)概述
Java NIO理解(二)Channel
Java NIO理解(三)Buffer
Java NIO理解(四)Selector