Okio库的使用

Okio库的使用Okio库是一个由square公司开发的,其官方简介为,Okiocomplementsjava.ioandjava.niotomakeitmucheasiertoaccess,store,andprocessyourdata.。它补充了java.io和java.nio的不足以更方便的访问、存储及处理数据。1.最新版本及Gradle引用     comp

大家好,又见面了,我是你们的朋友全栈君。

        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,对缓冲区操作。

Okio库的使用

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

Okio库的使用

3.BufferedSink及BufferedSource

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

BufferedSink     write(byte[] source) 将字符数组source 写入
BufferedSink     write(byte[] source, int offset, int byteCount)  将字符数组的从offset开始的byteCount个字符写入
BufferedSink     write(ByteString byteString)  将字符串写入
BufferedSink     write(Source source, long byteCount) 从Source写入byteCount个长度的
long                 writeAll(Source source) 将Source中的所有数据写入
BufferedSink     writeByte(int b) 写入一个byte整型
BufferedSink     writeDecimalLong(long v) 写入一个十进制的长整型
BufferedSink     writeHexadecimalUnsignedLong(long v) 写入一个十六进制无符号的长整型
BufferedSink     writeInt(int i) 写入一个整型
BufferedSink     writeIntLe(int i)
BufferedSink     writeLong(long v) 写入一个长整型
BufferedSink     writeLongLe(long v)
BufferedSink     writeShort(int s) 写入一个短整型
BufferedSink     writeShortLe(int s)
BufferedSink     writeString(String string, Charset charset) 写入一个String,并以charset格式编码
BufferedSink     writeString(String string, int beginIndex, int endIndex, Charset charset) 将String中从beginIndex到endIndex写入,并以charset格式编码
BufferedSink     writeUtf8(String string)  将String 以Utf - 8编码形式写入
BufferedSink     writeUtf8(String string, int beginIndex, int endIndex) 将String中从beginIndex到endIndex写入,并以Utf - 8格式编码
BufferedSink     writeUtf8CodePoint(int codePoint) 以Utf - 8编码形式写入的节点长度        

        BufferedSource定义的方法和BufferedSink极为相似,只不过一个是写一个是读,基本上都是一一对应的。

int 		read(byte[] sink) 将缓冲区中读取字符数组sink 至sink
int 		read(byte[] sink, int offset, int byteCount)  将缓冲区中从offst开始读取byteCount个字符 至sink
long 		readAll(Sink sink) 读取所有的Sink
byte 		readByte()  从缓冲区中读取一个字符
byte[] 		readByteArray()  从缓冲区中读取一个字符数组
byte[] 		readByteArray(long byteCount) 从缓冲区中读取一个长度为byteCount的字符数组
ByteString 	readByteString() 将缓冲区全部读取为字符串
ByteString 	readByteString(long byteCount) 将缓冲区读取长度为byteCount的字符串
long 	        readDecimalLong()  读取十进制数长度
void 	        readFully(Buffer sink, long byteCount) 读取byteCount个字符至sink
void 	        readFully(byte[] sink)   读取所有字符至sink 
long 	        readHexadecimalUnsignedLong() 读取十六进制数长度
int 		readInt() 从缓冲区中读取一个整数
int 		readIntLe()  
long 	        readLong() 从缓冲区中读取Long 整数
long 		readLongLe() 
short 		readShort() 从缓冲区中读取一个短整形
short 		readShortLe()
String 		readString(Charset charset) 从缓冲区中读取一个String
String 		readString(long byteCount, Charset charset) 读取一个长度为byteCount的String,并以charset形式编码
String 		readUtf8() 读取编码格式为Utf-8的String
String 		readUtf8(long byteCount) 读取编码格式为Utf-8且长度为byteCount的String
int 		readUtf8CodePoint() 读取一个Utf-8编码节点,长度在1-4之间
String 		readUtf8Line() 读取一行Utf-8 编码的String,碰到换行时停止
String 		readUtf8LineStrict()

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掉,避免内存泄漏


     例如:

        String fileName = "tea.txt";
        Source source;
        BufferedSource bufferedSource = null;

        try {
            String path = Environment.getExternalStorageDirectory().getPath();
            File file = new File(path, fileName);
            source = Okio.source(file);
            bufferedSource = Okio.buffer(source);

            String read = bufferedSource.readString(Charset.forName("GBK"));
            Logger.d(read);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != bufferedSource) {
                    bufferedSource.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    

        String fileName = "tea.txt";
        boolean isCreate = false;
        Sink sink;
        BufferedSink bufferedSink = null;

        String path = Environment.getExternalStorageDirectory().getPath();
        try {

            File file = new File(path, fileName);
            if (!file.exists()) {
                isCreate = file.createNewFile();
            } else {
                isCreate = true;
            }

            if (isCreate) {
                sink = Okio.sink(file);
                bufferedSink = Okio.buffer(sink);
                bufferedSink.writeInt(90002);
                bufferedSink.writeString("aaa12352345234523452233asdfasdasdfas大家可能觉得我举的例子有些太简单了,好吧,我来说一个难的。让byte变量b等于-1。",
                        Charset.forName("GBK"));

                bufferedSink.flush();

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != bufferedSink) {
                    bufferedSink.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

6.工具类 – ByteString

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

String 	base64()
String 	base64Url()
String 	utf8()
ByteString 	sha1()
ByteString 	sha256()

static ByteString 	decodeBase64(String base64)
static ByteString 	decodeHex(String hex)
static ByteString 	encodeUtf8(String s)

参考资料

        1.OkioAPI

        2.Android 善用Okio简化处理I/O操作

        3.Okio GitHub地址

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/132561.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 基于LDC1000的自动循迹小车

    基于LDC1000的自动循迹小车大三上学期课程设计的题目选了做小车,需要使用的是TI公司的LDC1000或者LDC1314,题目如下:首先选择做这道题是因为之前做小车比较熟悉,仔细分析一下其实也就是缺个传感器,其他的该有的都有了只需要稍作修改,于是乎开始了这一段有意思的旅途。将整个系统分为采集、处理、控制三部分。第一部分为采集模块,采用LDC1000采集赛道信息并转化为数字信号传输给数据处理模块。

    2022年6月7日
    39
  • C#实现一个局域网文件传输工具

    C#实现一个局域网文件传输工具工作需要,经常会在工作的台式机和笔记本之间传文件或者需要拷贝文本,两个机器都位于局域网内,传文件或者文本的方式有很多种,之前是通过共享文件夹来进行文件的拷贝,或者通过SVN进行同步。文本传递比较简单,可以通过两台机器上装QQ登两个号码,或者在共享目录下建一个TXT,或者发电子邮件等等。不过上面这些方法总觉得不直接,所以想基于P2P做一个小的局域网文件和文字传输小工具。WinForm的工程,

    2022年6月3日
    38
  • 《微机原理与接口技术》期末复习笔记「建议收藏」

    《微机原理与接口技术》期末复习笔记「建议收藏」微机原理与接口技术第01章微机原理概述基本知识CPU:中央处理单元(CentralProcessorUnit),处理器,能够分析和执行指令的部件,能分析和执行指令的芯片就是CPUMPU:微处理器,MicroProcessorUnit,CPU集成芯片,比传统CPU功能性能简化,集成度高,价格低廉,性价比高微型计算机(微机:MicroComputer):MPU+存储器+…

    2022年9月27日
    5
  • mysql8.0压缩包安装教程(zarchiver怎么安装游戏)

    首先声明,这里是Mysql8.0.27版本、64位操作系统解压版mysql的下载及安装配置及修改初始密码。如果是其他版本的mysql安装,可以自行查找其他方法,记住搜索的时候最好加上是什么版本的,因为不同版本的mysql安装的时候有些命令是不同的,例如,mysql5.7的就搜索mysql5.7安装。话不多说,接下来就是我的mysql8.0.27版本的教程了。1.下载打开下载地址:https://dev.mysql.com/downloads/mysql/,找到箭头中的文件https:/

    2022年4月14日
    194
  • bitblt函数_统计参数的含义

    bitblt函数_统计参数的含义【转载请注明出处: http://blog.csdn.net/lzl124631x】接口BOOLBitBlt( _In_ HDChdcDest, _In_ intnXDest, _In_ intnYDest, _In_ intnWidth, _In_ intnHeight, _In_ HDChdcSrc, _I

    2022年10月19日
    2
  • asp.net(c#)的货币格式化

    asp.net(c#)的货币格式化

    2021年8月7日
    58

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号