简体   繁体   中英

How to implement seek() function in BufferedSink (or BufferedSource) in OKHttp?

How to implement seek() function in BufferSink (or BufferedSource) in OKHttp?

We all know that in Java, the RandomAccessFile class has a method seek(long) , which enable us to start reading/writing a file from a specific position, and the bytes before the postion will be discarded. Is there any similar methods in OKHttp?

I have noticed that there is a method in BufferedSink:

write(byteString: ByteString, offset: Int, byteCount: Int)

But unfortunately the parameter "offset" aceepts only type int, not type long, which has some limit when transmitting large files.

The API you're looking for is BufferedSource.skip() .

In Okio 3.0 (coming soon) we're adding a new Cursor class that'll make skip() faster if the underlying source is a File .

https://github.com/square/okio/issues/889

I am using Okio.buffer to read an image file from the assets folder like this:

 BufferedSource img = Okio.buffer(Okio.source(getAssets().open("image.jpg")));

 byte[] image = img.readByteArray();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM