作用:与java io流的作用一样,能够进行数据的访问、存储以及处理,那为什么要用okio呢,因为相当于java io流来说,okio会使这些操作变得更加简单。
okio是建立在ByteString和Buffers的基础上进行一系列操作的;
ByteString的应用(常用于处理数据)
首先我们来看该如何去创建一个ByteString对象,创建ByteString可以使用以下几个静态方法:
of(byte... data) 需要输入的参数是一些字节;
encodeUtf8(String s) 需要输入的参数是一个字符串;
decodeHex(String hex) 需要输入的参数是十六进制串;
decodeBase64(String base64) 需要输入的参数是已经编好的Base64编码;
read(InputStream in, int byteCount) 需要输入的参数是一个inputStream对象以及读取字节长度;
以上方法都能返回一个ByteString对象,那么得到这个对象以后能干什么呢?ByteString有以下几个方法可以使用:
base64() 返回一个Base64的编码值;
hex() 返回一个十六进制的编码值;
md5() 返回一个MD5的校验值;
sha1() 返回一个SHA1的校验值;
write(OutputStream) 写入输出流中;
ByteString的具体用法:
注:使用okio之前需要导入okio的jar包
例1 获取字符串的Base64编码值和MD5校验值:
public class OkioTest {
public static void main(String[] args) {
String str = "This is a string";
//得到一个ByteString对象
ByteString byteString = ByteString.encodeUtf8(str);
String base64 = byteString.base64();
System.out.println("该字符串的base64编码值是:" + base64);
ByteString md5 = byteString.md5();
System.out.println("该字符串的MD5校验值是:" + md5);
}
}
例2 对Base64编码值进行解码:
public class OkioTest {
public static void main(String[] args) {
//这是字符串“abc”的base64编码值
String base64 = "YWJj";
ByteString byteString = ByteString.decodeBase64(base64);
System.out.println(byteString.utf8());
/*
运行结果便是“abc”
*/
}
}
例3 文件传输:
public class OkioTest {
public static void main(String[] args) throws IOException {
//放一张图片在项目下,然后创建一个输入流对象
FileInputStream inputStream = new FileInputStream("img.png");
/*
通过ByteString的read方法创建一个ByteString对象
read方法的第二个参数表示想要读取多少字节的长度,available()返回输入对象的字节长,表示有多长读多长;
*/
ByteString byteString = ByteString.read(inputStream,inputStream.available());
//创建一个输出流对象
FileOutputStream outputStream = new FileOutputStream("img2.png");
//调用write方法将多读取到的字节写入输出流中
byteString.write(outputStream);
}
}
buffer的应用(常用于访问和存储数据)
相对于Java io来说,okio中的Source和Sink这两个接口便分别充当着输入流和输出流这两个角色,Source对应InputStream,Sink对应OutputStream,再结合Buffer提供的相关方法来进行数据的访问和存储;
Source的相关方法:
read(Buffer sink,long byteCount) 将Source文件里的数据读给Buffer
timeout() 超时处理
close() 关闭流
Sink的相关方法:
write(Buffer source, long byteCount) 将Buffer里的数据写入Sink文件中
flush() 清空缓存区
timeout() 超时处理
close() 关闭流
例:
//写数据
File file = new File("/Users/aliouswang/Documents/olympic/JavaArt//text.temp");
Sink sink = Okio.sink(file);
Buffer buffer = new Buffer();
buffer.writeString("Hello okio!", Charset.forName("UTF-8"));
buffer.writeInt(998);
buffer.writeByte(1);
buffer.writeLong(System.currentTimeMillis());
buffer.writeUtf8("Hello end!");
sink.write(buffer, buffer.size());
sink.flush();
sink.close();
//读数据
Source source = Okio.source(file);
buffer.clear();
source.read(buffer, 1024);
String string = buffer.readString("Hello okio!".length(), Charset.forName("UTF-8"));
int intValue = buffer.readInt();
byte byteValue = buffer.readByte();
long longValue = buffer.readLong();
String utf8 = buffer.readUtf8();
System.out.println("str:" + string + ";\nint:" + intValue + ";\nbyte:" + byteValue + ";" +
"\nlong:" + longValue + "\nutf8:" + utf8);
source.close();
// 打印结果:
str:Hello okio!;
int:998;
byte:1;
long:1555325659665
utf8:Hello end!
由于每次在进行读写文件的时候都需要创建一个Buffer对象,这样很麻烦,于是乎便有了BufferedSource、BufferedSink。BufferedSource 和 BufferedSink 也都是接口,分别实现了Source和Sink接口。这使得访问和存储数据变得更加便捷;
我们可以通过BufferedSource和BufferedSink对上面读写文件的例子进行修改:
File file = new File("/Users/aliouswang/Documents/java/JavaArt/text.temp");
Sink sink = Okio.sink(file);
BufferedSink bufferedSink = Okio.buffer(sink);
bufferedSink.writeString("Hello okio!", Charset.forName("UTF-8"));
bufferedSink.writeInt(998);
bufferedSink.writeByte(1);
bufferedSink.writeLong(System.currentTimeMillis());
bufferedSink.writeUtf8("Hello end!");
bufferedSink.close();
Source source = Okio.source(file);
BufferedSource bufferedSource = Okio.buffer(source);
String string = bufferedSource.readString("Hello okio!".length(), Charset.forName("UTF-8"));
int intValue = bufferedSource.readInt();
byte byteValue = bufferedSource.readByte();
long longValue = bufferedSource.readLong();
String utf8 = bufferedSource.readUtf8();
System.out.println("str:" + string + ";\nint:" + intValue + ";\nbyte:" + byteValue + ";" +
"\nlong:" + longValue + "\nutf8:" + utf8);
source.close();
是不是很方便呢?BufferedSource和BufferedSink能够满足我们对io的日常绝大部分使用场景了!
想要更加深入理解Okio请参考
深入理解Okio之旅 - 简书 (jianshu.com)