java_NIO之四总结

Java NIO FileChannel
Writing Data to a FileChannel
String newData = "New String to write to file..." + System.currentTimeMillis();

ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();
buf.put(newData.getBytes());

buf.flip();

while(buf.hasRemaining()) {
    channel.write(buf);
}

long pos channel.position();

channel.position(pos +123);
channel.truncate(1024);
channel.force(true);

Java NIO SocketChannel
Opening a SocketChannel
SocketChannel socketChannel = SocketChannel.open();
socketChannel.connect(new InetSocketAddress("http://jenkov.com", 80));
Reading from a SocketChannel

ByteBuffer buf = ByteBuffer.allocate(48);

int bytesRead = socketChannel.read(buf);
Writing to a SocketChannel
String newData = "New String to write to file..." + System.currentTimeMillis();

ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();
buf.put(newData.getBytes());

buf.flip();

while(buf.hasRemaining()) {
    channel.write(buf);
}
Non-blocking Mode with Selectors
//非阻塞模式可以更好的应用于选择器,通过注册一个或者多个选择器在socketChannel上
The non-blocking mode of SocketChannel's works much better with Selector's. By registering one or more SocketChannel's with a Selector,
you can ask the Selector for channels that are ready for reading, writing etc.对于通道可以使用选择器更好的工作 为了读或者写

ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

serverSocketChannel.socket().bind(new InetSocketAddress(9999));

while(true){
    SocketChannel socketChannel =
            serverSocketChannel.accept();

    //do something with socketChannel...
}

Non-blocking Mode//异步模式

//一个ServerSocketChannel能够被设为异步模式,在异步模式中的 accept()方法将会立即返回,也许会返回null值,假如没有链接到达,因此你必须检查返回的socketChannel是否为空
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();

serverSocketChannel.socket().bind(new InetSocketAddress(9999));
serverSocketChannel.configureBlocking(false);

while(true){
    SocketChannel socketChannel =
            serverSocketChannel.accept();

    if(socketChannel != null){
        //do something with socketChannel...
        }
}

Java NIO DatagramChannel//





Get all my free tips & tutorials!
Connect with me, or sign up for my news letter or RSS feed, and get all my tips that help you become a more skilled and efficient developer.
Newsletter
     
Java NIO DatagramChannel

By Jakob Jenkov
 Connect with me:
Rate article:
Share article:
Tweet

Table of Contents

    Opening a DatagramChannel
    Receiving Data
    Sending Data
    Connecting to a Specific Address
//DatagramChannel是一个通道,他可以发送和接收UDP的数据包,UDP是一个若连接,你不能仅仅是使用默认的writing和read的方式, 替代方法是使用Send和receive方法接收和发送数据包,

DatagramChannel channel = DatagramChannel.open();
channel.socket().bind(new InetSocketAddress(9999));

Receiving Data//接收数据
ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();

channel.receive(buf);
 //Sending Data  发送数据
 String newData = "New String to write to file..."
                    + System.currentTimeMillis();
    
ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();
buf.put(newData.getBytes());
buf.flip();

int bytesSent = channel.send(buf, new InetSocketAddress("jenkov.com", 80));

Connecting to a Specific Address

channel.connect(new InetSocketAddress("jenkov.com", 80));    

When connected you can also use the read() and write() method, as if you were using a traditional channel.
You just don't have any guarantees about delivery of the sent data. Here are a few examples
int bytesRead = channel.read(buf);    

int bytesWritten = channel.write(buf);




1     Java NIO Tutorial
2     Java NIO Overview
3     Java NIO Channel
4     Java NIO Buffer
5     Java NIO Scatter / Gather
6     Java NIO Channel to Channel Transfers
7     Java NIO Selector
8     Java NIO FileChannel
9     Java NIO SocketChannel
10     Java NIO ServerSocketChannel
11     Java NIO DatagramChannel
12     Java NIO Pipe
13     Java NIO vs. IO





Java NIO Pipe
//
Pipe就像是一个方法数据连接到两个线程,A Pipe有一个源通道,和一个水槽,你些数据到水槽, 这些数据可以被另一个线程访问在源通道中访问到,
Here is an illustration of the Pipe principle
 


Get all my free tips & tutorials!
Connect with me, or sign up for my news letter or RSS feed, and get all my tips that help you become a more skilled and efficient developer.
Newsletter
     
Java NIO Pipe

By Jakob Jenkov
 Connect with me:
Rate article:
Share article:
Tweet

Table of Contents

    Creating a Pipe
    Writing to a Pipe
    Reading from a Pipe

A Java NIO Pipe is a one-way data connection between two threads. A Pipe has a source channel and a sink channel. You write data to the sink channel. This data can then be read from the source channel.

Here is an illustration of the Pipe principle:
Java NIO: Pipe Internals

Creating a Pipe

Pipe pipe = Pipe.open();

Write模式
String newData = "New String to write to file..." + System.currentTimeMillis();

ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();
buf.put(newData.getBytes());

buf.flip();

while(buf.hasRemaining()) {
    sinkChannel.write(buf);
}

Reading from a Pipe
Pipe.SourceChannel sourceChannel = pipe.source();

将数据读到缓冲区中
ByteBuffer buf = ByteBuffer.allocate(48);

int bytesRead = inChannel.read(buf);java_NIO之四总结

你可能感兴趣的:(java_NIO之四总结)