1

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.

vainquit
  • 491
  • 6
  • 13
  • 1
    HTTP streams are not going to be random access without some additional support on the server side. As you're trying to seek past two gig you may want to consider an alternate protocol such as [RTMP](https://en.wikipedia.org/wiki/Real-Time_Messaging_Protocol) or others. – stdunbar Feb 14 '21 at 18:13

2 Answers2

2

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

Jesse Wilson
  • 39,078
  • 8
  • 121
  • 128
-1

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