最近在研究retrofit的具體實現,retrofit用到了okhttp,okhttp又用到了Okio,so我就想從最底層的Okio開始往上研究。而且一直沒有認真看過java的io這個塊,也正好趁此機會學習下io相關的知識。
本篇是關于Okio,接下來應該還會寫關于okhttp、retrofit各一篇。網上關于這三個框架的文章很多,為什么還要自己在寫呢,因為別人的文章看的再多,終究沒有自己親自動手寫一篇,從頭梳理一遍來的痛快,總是看別人的文章,看后總沒有一種通透的感覺,對整個框架似懂非懂的,而且過兩天就忘的差不多了,所以很多東西自己寫一下很有必要。
本文基于okio-1.9.0分析的,不同版本可能會有差異。如有錯誤,歡迎留言指正。
先看一張圖片(這個不是嚴格的UML圖,算是草稿圖吧) okio的源碼其實不多,看了下總共20多個類,大部分類都不是很長。這些類中最核心的類就幾個,這幾個類就構成了okio的骨架。下面是幾個核心的類: Okio:提供生成Sink和Source的方法 Sink : 接口類,功能上對應OutputStream Source :接口類,功能上對應InputStream BufferedSink:接口類繼承自Sink,內部有一個Buffer的Sink BufferedSource:接口類繼承自Source,內部有一個Buffer的Source Buffer:BufferedSink和BufferedSource的最終實現實現類, 實現緩存功能,內部有一個Segment鏈表 Segment:里面有個byte數組,通過pos,limit控制讀寫的位置(從byte[]哪里開始讀,哪里開始寫入),next, PRev實現導航到前面或后面的Segment(實現Segment鏈表結構)
下面是簡單的使用方法
socket = new Socket("127.0.0.1", 8080);InputStream inputStream = socket.getInputStream();OutputStream outputStream = socket.getOutputStream();BufferedSource source = Okio.buffer(Okio.source(inputStream));BufferedSink sink = Okio.buffer(Okio.sink(outputStream));平時我們使用socket,有時是直接調用InputStream,OutputStream的方法讀寫數據,使用Okio后,可以通過InputStream,OutputStream構造出Source,Sink,然后調用Source和Sink提供的方法讀寫數據。Source和Sink提供了大量的方法讀寫數據,可以支持字節流和字符流,同時使用Buffer提高了讀寫效率。 寫到這里發現沒有寫出okio到底好在哪里,以及怎樣實現的。這個自己暫時還沒有看的很懂,以后看懂了再來寫這部分。
下面是一個通過socket實現的通訊例子,分為client和server Server.java
import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;import java.nio.charset.Charset;import okio.BufferedSink;import okio.BufferedSource;import okio.Okio;/** * Created by lee on 2017/1/24. */public class Server { public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(8080); while (true) { Socket socket = serverSocket.accept(); handleClientSocket(socket); } } catch (IOException e) { e.printStackTrace(); } finally { if (serverSocket != null) { try { serverSocket.close(); } catch (Exception e) { e.printStackTrace(); } } } } private static void handleClientSocket(Socket socket) { Thread thread = new Thread(new Runnable() { @Override public void run() { try { while (true) { BufferedSource source = Okio.buffer(Okio.source(socket)); BufferedSink sink = Okio.buffer(Okio.sink(socket)); int length = source.readInt(); String message = source.readString(length, Charset.forName("utf-8")); System.out.println("length is: " + length + " , message is : " + message); if("error exit".equals(message)){ break; } String responseMsg = getResponseAccordMsg(message); if (responseMsg != null) { int respLength = responseMsg.getBytes().length; sink.writeInt(respLength); sink.writeString(responseMsg, Charset.forName("utf-8")); sink.flush(); } if("error exit".equals(responseMsg)){ break; } } } catch (IOException e) { e.printStackTrace(); }finally { if(socket!=null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }); thread.start(); } private static String getResponseAccordMsg(String msg) { String result = ""; if (msg != null && msg.length() > 0) { if (msg.equals("hello")) { result = "hello"; } else if (msg.equals("nice to meet you")) { result = "nice to meet you too"; } else if (msg.equals("see you")) { result = "see you next time"; } } if (result.length() == 0) { result = "error exit"; } return result; }}client的代碼 OkioClient.java
import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.Socket;import java.nio.charset.Charset;import okio.BufferedSink;import okio.BufferedSource;import okio.Okio;/** * Created by lee on 2017/1/24. */public class OkioClient { public static void main(String[] args) { Socket socket = null; try { socket = new Socket("127.0.0.1", 8080); InputStream inputStream = socket.getInputStream(); OutputStream outputStream = socket.getOutputStream(); BufferedSource source = Okio.buffer(Okio.source(inputStream)); BufferedSink sink = Okio.buffer(Okio.sink(outputStream)); writeMsg(sink, "hello"); while (true) { int length = source.readInt(); String message = source.readString(length, Charset.forName("utf-8")); System.out.println("length is: "+length+" , message is : "+message); if ("error exit".equals(message)) { break; } String respMsg = getResponseAccordMsg(message); writeMsg(sink, respMsg); if ("error exit".equals(respMsg)) { break; } } } catch (IOException e) { e.printStackTrace(); } finally { if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } private static void writeMsg(BufferedSink sink, String msg) { try { int msgLength = msg.getBytes().length; sink.writeInt(msgLength); sink.writeString(msg, Charset.forName("utf-8")); sink.flush(); } catch (IOException e) { e.printStackTrace(); } } private static String getResponseAccordMsg(String msg) { String result = ""; if (msg != null && msg.length() > 0) { if (msg.equals("hello")) { result = "nice to meet you"; } else if (msg.equals("nice to meet you too")) { result = "see you"; } } if (result.length() == 0) { result = "error exit"; } return result; }}新聞熱點
疑難解答