TCP/IP學習筆記二:NIO的網絡編程Buffer簡單使用
標籤(空格分隔):網絡編程 NIO Buffer
NIO的有三種模型:ByteBuffer (position/limit/capacity) / Channel / Selector 通道選擇器
先介紹前兩種的簡單示例,NIO的Buffer的簡單使用、文件的讀寫和文件的複製
如下實例中其實也是阻塞I/O(並非真正的NIO)。對於真正的非阻塞I/O實例在下篇文章中展示。
NIO的Buffer的使用:
package com.nio.buffer;
import java.nio.ByteBuffer;
/**
* ByteBuffer實例
* @author MOTUI
*
*/
public class TestBuffer {
public static void main(String[] args) {
//創建Buffer
ByteBuffer buffer = ByteBuffer.allocate(5);
System.out.println("初始化狀態:"+buffer.position()+","+buffer.limit()+","+buffer.capacity());
//寫入兩個字節
buffer.put((byte)'a');
buffer.put((byte)'b');
System.out.println("存入兩個字節狀態:"+buffer.position()+","+buffer.limit()+","+buffer.capacity());
buffer.flip();
System.out.println("重置狀態:"+buffer.position()+","+buffer.limit()+","+buffer.capacity());
System.out.println("打印兩個字節:");
//讀取寫入的字節
while(buffer.hasRemaining()){
System.out.print(buffer.get()+" ");
}
//換行
System.out.println();
//重置
buffer.clear();
//存入一個字節
buffer.put((byte)'c');
//buffer.flip();
System.out.println("打印未重置:");
//讀取
while(buffer.hasRemaining()){
System.out.print(buffer.get()+" ");
}
//換行
System.out.println();
//設置參數
buffer.position(0);
buffer.limit(1);
System.out.println("打印重置後:");
//讀取寫入的新字節
while(buffer.hasRemaining()){
System.out.print(buffer.get()+" ");
}
}
}
運行結果爲:
初始化狀態:0,5,5
存入兩個字節狀態:2,5,5
重置狀態:0,2,5
打印兩個字節:
97 98
打印未重置:
98 0 0 0
打印重置後:
99
對於Buffer的圖解:
文件的寫操作:
package com.nio.buffer;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
* 文件寫入
* @author MOTUI
*
*/
public class TestWriteFile {
public static void main(String[] args) throws Exception {
//輸出流
FileOutputStream fos = new FileOutputStream("write.txt");
//獲得管道
FileChannel writeChannel = fos.getChannel();
//創建Buffer
ByteBuffer buffer = ByteBuffer.allocate(1024);
String byteStr = "你好,他好,大家好!";
//寫入數據
buffer.put(byteStr.getBytes());
buffer.flip();
//通過管道寫入數據
writeChannel.write(buffer);
//關閉流
if (writeChannel.isOpen()) writeChannel.close();
fos.close();
}
}
文件的讀操作:
package com.nio.buffer;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
* 文件讀取
* @author MOTUI
*
*/
public class TestRead {
public static void main(String[] args) throws Exception {
//輸入流
FileInputStream fis = new FileInputStream("write.txt");
//獲得管道
FileChannel readChanner = fis.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
//存儲讀取的字節
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len = 0;
while (true) {
//重置buffer
buffer.clear();
//讀取數據
len = readChanner.read(buffer);
buffer.flip();
//判斷是否讀到末尾
if (len == -1) break;
while (buffer.hasRemaining()) {
//寫入數據
baos.write(buffer.get());
}
}
//關閉流
if (readChanner.isOpen()) readChanner.close();
fis.close();
//打印
System.out.println(baos.toString());
}
}
運行結果:
你好,他好,大家好!
文件的拷貝:
package com.nio.buffer;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
* 文件拷貝
* @author MOTUI
*
*/
public class TestCopy {
public static void main(String[] args) throws Exception {
//讀入文件流
FileInputStream fis = new FileInputStream("ssss.png");
//寫入文件流
FileOutputStream fos = new FileOutputStream("aaaa.png");
//獲得讀管道
FileChannel readChannel = fis.getChannel();
//獲取寫管道
FileChannel writeChannel = fos.getChannel();
//創建Buffer
ByteBuffer buffer = ByteBuffer.allocate(1024);
int len = 0;
while (true) {
//清除Buffer
buffer.clear();
//讀數據
len = readChannel.read(buffer);
//判斷是否讀到末尾
if(len == -1) break;
buffer.flip();
//寫入數據
if (buffer.hasRemaining()) {
writeChannel.write(buffer);
}
}
//關閉流
if(writeChannel.isOpen()) writeChannel.close();
if(readChannel.isOpen()) readChannel.close();
fis.close();
fos.close();
}
}
總結:
對於NIO的Buffer的操作較簡單,掌握基本使用即可。