HiFabby的項目代碼
最近在接觸人像扣圖項目時, 需要了解不少OpenGL的代碼, 經常看到ByteBuffer類的使用.
同樣, 在官方給出的OpenGL教程中, 也可以看到使用到了NIO.
eg:
import java.nio.ByteBuffer;
private short drawOrder[] = {0, 1, 2, 0, 2, 3};
private ShortBuffer drawListBuffer;
// initialize byte buffer for the draw list
ByteBuffer dlb = ByteBuffer.allocateDirect(drawOrder.length * 2);
dlb.order(ByteOrder.nativeOrder());
drawListBuffer = dlb.asShortBuffer();
drawListBuffer.put(drawOrder);
drawListBuffer.position(0);
ByteBuffer類之前沒接觸過, 所以這篇文章把NIO的知識點補充起來.
IO 分兩類
BIO: 傳統的IO方式, 把數據寫入OutputStream或是從InputStream中讀取數據都是阻塞式的.
NIO: Noblocking IO. 非阻塞的IO. 也可以叫做 New IO.
NIO和傳統的I/O比較大的區別在于傳輸方式非阻塞,一種基于事件驅動的模式,將會使方法執行完后立即返回,傳統I/O主要使用了流Stream的方式,而在New I/O中,使用了字節緩存ByteBuffer來承載數據。
NIO的分類
Android NIO主要分為三大類,ByteBuffer、FileChannel和SocketChannel.
- java.nio.ByteBuffer
在涉及到OpenGl的代碼中會很常見.
使用講解:
直接使用ByteBuffer類的靜態方法static ByteBuffer allocate(int capacity) 或 static ByteBuffer allocateDirect(int capacity) 這兩個方法來分配內存空間.
往ByteBuffer中添加元素, 使用put() API.
final ByteBuffer order(ByteOrder byteOrder) 設置字節順序,ByteOrder類的值有兩個定義,比如LITTLE_ENDIAN、BIG_ENDIAN,如果使用當前平臺則為ByteOrder.nativeOrder()在Android中則為 BIG_ENDIAN.
public final Buffer position(int newPosition) Sets this buffer's position
類型轉化:, ByteBuffer可以很好的和字節數組byte[]轉換類型,通過執行ByteBuffer類的final byte[] array() 方法就可以將ByteBuffer轉為byte[]。從byte[]來構造ByteBuffer可以使用ByteBuffer.wrap(byte[] data)方法.
了解到這些基本用法后, 上面的代碼片段就可以看懂了.
- FileChannel
FileChannel位于java.nio.channels.FileChannel包中. 實現對文件的操作.
"Channel" 即是管道, 是NIO引入的概念.
自己寫了一個工具類, 通過NIO的方式實現copy文件的功能.
public class NIOTestUtil {
public static void copyFileByNIOTest() throws IOException {
String infile = "/sdcard/landcruiser.jpg";
String outfile = "/sdcard/landcruiser_nio_copy.zip";
FileInputStream fin = new FileInputStream( infile );
FileOutputStream fout = new FileOutputStream( outfile );
FileChannel fcin = fin.getChannel();
FileChannel fcout = fout.getChannel();
ByteBuffer buffer = ByteBuffer.allocate( 1024 ); //分配1KB作為緩沖區
while (true) {
buffer.clear(); //每次使用必須置空緩沖區
int r = fcin.read( buffer );
if (r==-1) {
break;
}
buffer.flip(); //寫入前使用flip這個方法
fcout.write( buffer );
}
}
//使用傳統 BIO 的方式
public static void copyFileByBIOTest() throws IOException {
String infile = "/sdcard/landcruiser.jpg";
String outfile = "/sdcard/landcruiser_bio_copy.zip";
File source = new File(infile);
File dest = new File(outfile);
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(source);
output = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
} finally {
input.close();
output.close();
}
}
}
- SocketChannel 用來處理網絡IO操作.
在Java的New I/O中,處理Socket類對應的東西,我們可以看做是SocketChannel,套接字通道關聯了一個Socket類,這一點使用SocketChannel類的socket() 方法可以返回一個傳統IO的Socket類。SocketChannel 對象在Server中一般通過Socket類的getChannel()方法獲得。
在使用SocketChannel的過程中, 還涉及到 Selector 選擇器這個概念, 用來在NIO中注冊各種事件.
目前接觸過的項目中還沒見過使用NIO進行網絡通信的例子. 對于SocketChannel更詳細的用法就先不總結了, 以后如果用到的話, 再總結這部分知識點.
總結
基礎知識要不斷的擴展, 這樣接觸到新項目時, 起碼可以降低閱讀代碼的障礙, 能快速的理解新項目的技術實現思路.