国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Okio使用分析

2019-11-09 18:42:44
字體:
來源:轉載
供稿:網友

  最近在研究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; }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新巴尔虎左旗| 长沙市| 晴隆县| 清流县| 大方县| 尉氏县| 焉耆| 邢台市| 轮台县| 临海市| 三穗县| 宣化县| 聂荣县| 中牟县| 平泉县| 泸州市| 嵩明县| 杨浦区| 东源县| 喀什市| 通化市| 明光市| 海南省| 刚察县| 灌南县| 江华| 崇礼县| 酉阳| 潼南县| 秦安县| 呼伦贝尔市| 普兰店市| 无锡市| 基隆市| 榆中县| 团风县| 察哈| 柳河县| 诸暨市| 郓城县| 嵊泗县|