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

699 Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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

0
On

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();