Okio库的使用

Okio库是一个由square公司开发的,其官方简介为,Okio complements java.io and java.nio to make it much easier to access, store, and process your data.。它补充了java.io和java.nio的不足以更方便的访问、存储及处理数据。

1.最新版本及Gradle引用

        compile 'com.squareup.okio:okio:1.9.0'

        官方API地址:OkioAPI

2.核心

        Okio库的核心是两个接口Sink和Source,这两个接口都继承了Closeable接口;而Sink可以简单的看做OutputStream(将流写入文件),Source可以简单的看做InputStream(从文件读取至流)。而这两个接口都是支持读写超时设置的。其中,Sink声明了write()、flush()、close()、timeout()等方法,Source中声明了read()、close()、timeout(),这些方法包含了对文件的读写及资源的释放。它们各自有一个支持缓冲区的子类接口,BufferedSink和BufferedSource,这两个子接口有一个共同的实现类Buffer,对缓冲区操作。

        Sink和Source它门还各自有一个支持gzip压缩的实现类GzipSink和GzipSource;一个具有委托功能的抽象类ForwardingSink和ForwardingSource;还有一个实现类便是InflaterSource和DeflaterSink,这两个类主要用于压缩,为GzipSink和GzipSource服务


3.BufferedSink及BufferedSource

        BufferedSink中定义了一系列写入缓存区的方法,当然其一定定义了与Buffer相关的方法,具体方法相见官方API。

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">BufferedSink     write(byte[] source) 将字符数组source 写入  
  2. BufferedSink     write(byte[] source, int offset, int byteCount)  将字符数组的从offset开始的byteCount个字符写入  
  3. BufferedSink     write(ByteString byteString)  将字符串写入  
  4. BufferedSink     write(Source source, long byteCount) 从Source写入byteCount个长度的  
  5. long                 writeAll(Source source) 将Source中的所有数据写入  
  6. BufferedSink     writeByte(int b) 写入一个byte整型  
  7. BufferedSink     writeDecimalLong(long v) 写入一个十进制的长整型  
  8. BufferedSink     writeHexadecimalUnsignedLong(long v) 写入一个十六进制无符号的长整型  
  9. BufferedSink     writeInt(int i) 写入一个整型  
  10. BufferedSink     writeIntLe(int i)  
  11. BufferedSink     writeLong(long v) 写入一个长整型  
  12. BufferedSink     writeLongLe(long v)  
  13. BufferedSink     writeShort(int s) 写入一个短整型  
  14. BufferedSink     writeShortLe(int s)  
  15. BufferedSink     writeString(String string, Charset charset) 写入一个String,并以charset格式编码  
  16. BufferedSink     writeString(String string, int beginIndex, int endIndex, Charset charset) 将String中从beginIndex到endIndex写入,并以charset格式编码  
  17. BufferedSink     writeUtf8(String string)  将String 以Utf - 8编码形式写入  
  18. BufferedSink     writeUtf8(String string, int beginIndex, int endIndex) 将String中从beginIndex到endIndex写入,并以Utf - 8格式编码  
  19. BufferedSink     writeUtf8CodePoint(int codePoint) 以Utf - 8编码形式写入的节点长度          
  20.     </span>  
        BufferedSource定义的方法和BufferedSink极为相似,只不过一个是写一个是读,基本上都是一一对应的。

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">int         read(byte[] sink) 将缓冲区中读取字符数组sink 至sink  
  2. int         read(byte[] sink, int offset, int byteCount)  将缓冲区中从offst开始读取byteCount个字符 至sink  
  3. long        readAll(Sink sink) 读取所有的Sink  
  4. byte        readByte()  从缓冲区中读取一个字符  
  5. byte[]      readByteArray()  从缓冲区中读取一个字符数组  
  6. byte[]      readByteArray(long byteCount) 从缓冲区中读取一个长度为byteCount的字符数组  
  7. ByteString  readByteString() 将缓冲区全部读取为字符串  
  8. ByteString  readByteString(long byteCount) 将缓冲区读取长度为byteCount的字符串  
  9. long            readDecimalLong()  读取十进制数长度  
  10. void            readFully(Buffer sink, long byteCount) 读取byteCount个字符至sink  
  11. void            readFully(byte[] sink)   读取所有字符至sink   
  12. long            readHexadecimalUnsignedLong() 读取十六进制数长度  
  13. int         readInt() 从缓冲区中读取一个整数  
  14. int         readIntLe()    
  15. long            readLong() 从缓冲区中读取Long 整数  
  16. long        readLongLe()   
  17. short       readShort() 从缓冲区中读取一个短整形  
  18. short       readShortLe()  
  19. String      readString(Charset charset) 从缓冲区中读取一个String  
  20. String      readString(long byteCount, Charset charset) 读取一个长度为byteCount的String,并以charset形式编码  
  21. String      readUtf8() 读取编码格式为Utf-8的String  
  22. String      readUtf8(long byteCount) 读取编码格式为Utf-8且长度为byteCount的String  
  23. int         readUtf8CodePoint() 读取一个Utf-8编码节点,长度在1-4之间  
  24. String      readUtf8Line() 读取一行Utf-8 编码的String,碰到换行时停止  
  25. String      readUtf8LineStrict()</span>  

4.Okio类

       Okio类作为OkIo库暴露给外部使用的类,其内部有大量的静态方法,包括通过一个Source获得BufferedSource,通过一个Sink获得一个BufferedSink。

    static Sink     appendingSink(File file) 将Sink追加 file

    static BufferedSink     buffer(Sink sink) 通过一个Sink获得BufferedSink
    static BufferedSource     buffer(Source source) 通过一个Source获得BufferedSource
    
    static Sink     sink(File file)  通过一个文件file获得Sink
    static Sink     sink(OutputStream out)通过一个输出流out获得Sink
    static Sink     sink(java.nio.file.Path path, java.nio.file.OpenOption... options)
    static Sink     sink(Socket socket)通过一个套接字socket获得Sink
    
    static Source     source(File file) 通过一个文件file获得Source
    static Source     source(InputStream in) 通过一个输入流in获得Source
    static Source     source(java.nio.file.Path path, java.nio.file.OpenOption... options)
    static Source     source(Socket socket) 通过一个套接字socket获得source

5.具体使用

       现在对Okio库的整体框架有了基本了解,那么就该实际操作了。之初就已经说过OKio操作十分的简单,具体步骤如下:

       1.调用Okio类的静态方法获取Source(Sink)

       2.调用Okio类库的静态方法,通过刚才获取的Source(Sink)获取BufferedSink(BufferedSink)

       3.对缓冲区根据实际需求做相应操作

       4.若是Source,须将调用flush()

       5.最后close掉,避免内存泄漏


     例如:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">        String fileName = "tea.txt";  
  2.         Source source;  
  3.         BufferedSource bufferedSource = null;  
  4.   
  5.         try {  
  6.             String path = Environment.getExternalStorageDirectory().getPath();  
  7.             File file = new File(path, fileName);  
  8.             source = Okio.source(file);  
  9.             bufferedSource = Okio.buffer(source);  
  10.   
  11.             String read = bufferedSource.readString(Charset.forName("GBK"));  
  12.             Logger.d(read);  
  13.   
  14.         } catch (IOException e) {  
  15.             e.printStackTrace();  
  16.         } finally {  
  17.             try {  
  18.                 if (null != bufferedSource) {  
  19.                     bufferedSource.close();  
  20.                 }  
  21.             } catch (IOException e) {  
  22.                 e.printStackTrace();  
  23.             }  
  24.         }</span>  

    
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">        String fileName = "tea.txt";  
  2.         boolean isCreate = false;  
  3.         Sink sink;  
  4.         BufferedSink bufferedSink = null;  
  5.   
  6.         String path = Environment.getExternalStorageDirectory().getPath();  
  7.         try {  
  8.   
  9.             File file = new File(path, fileName);  
  10.             if (!file.exists()) {  
  11.                 isCreate = file.createNewFile();  
  12.             } else {  
  13.                 isCreate = true;  
  14.             }  
  15.   
  16.             if (isCreate) {  
  17.                 sink = Okio.sink(file);  
  18.                 bufferedSink = Okio.buffer(sink);  
  19.                 bufferedSink.writeInt(90002);  
  20.                 bufferedSink.writeString("aaa12352345234523452233asdfasdasdfas大家可能觉得我举的例子有些太简单了,好吧,我来说一个难的。让byte变量b等于-1。",  
  21.                         Charset.forName("GBK"));  
  22.   
  23.                 bufferedSink.flush();  
  24.   
  25.             }  
  26.         } catch (IOException e) {  
  27.             e.printStackTrace();  
  28.         } finally {  
  29.             try {  
  30.                 if (null != bufferedSink) {  
  31.                     bufferedSink.close();  
  32.                 }  
  33.             } catch (IOException e) {  
  34.                 e.printStackTrace();  
  35.             }  
  36.   
  37.         }</span>  

6.工具类 - ByteString

       ByteString作为一个工具类,功能十分强大,它可以把byte转为String,这个String可以是utf8的值,也可以是base64后的值,也可以是md5的值,也可以是sha256的值

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">String  base64()  
  2. String  base64Url()  
  3. String  utf8()  
  4. ByteString  sha1()  
  5. ByteString  sha256()  
  6.   
  7. static ByteString   decodeBase64(String base64)  
  8. static ByteString   decodeHex(String hex)  
  9. static ByteString   encodeUtf8(String s)</span>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值