Questions tagged [okio]

Okio is a library that complements java.io and java.nio to make it easier to access, store, and process data.

Okio is a library that complements java.io and java.nio to make it easier to access, store, and process data. It is built around two types:

ByteString is an immutable sequence of bytes. For character data, String is fundamental.

ByteString is String's long-lost brother, making it easy to treat binary data as a value. This class is ergonomic: it knows how to encode and decode itself as hex, base64, and UTF-8.

Buffer is a mutable sequence of bytes. Like ArrayList, you don't need to size your buffer in advance. You read and write buffers as a queue: write data to the end and read it from the front. There's no obligation to manage positions, limits, or capacities.

Internally, ByteString and Buffer do some clever things to save CPU and memory. If you encode a UTF-8 string as a ByteString, it caches a reference to that string so that if you decode it later, there's no work to do.

Buffer is implemented as a linked list of segments. When you move data from one buffer to another, it reassigns ownership of the segments rather than copying the data across. This approach is particularly helpful for multithreaded programs: a thread that talks to the network can exchange data with a worker thread without any copying or ceremony.

84 questions
39
votes
5 answers

Tracking progress of multipart file upload using OKHTTP

I am trying to implement a a progress bar to indicate the progress of a multipart file upload. I have read from a comment on this answer - https://stackoverflow.com/a/24285633/1022454 that I have to wrap the sink passed to the RequestBody and…
Jonathon Fry
  • 3,042
  • 3
  • 23
  • 30
21
votes
3 answers

How to use Retrofit and SimpleXML together in downloading and parsing an XML file from a site?

I just started working with Retrofit. I am working on a project that uses SimpleXML. Can somebody provide me an example in which one fetches an XML from a site e.g. http://www.w3schools.com/xml/simple.xml" and reads it out?
greenspand
  • 749
  • 2
  • 8
  • 15
16
votes
6 answers

kotlin/TypeCastException when trying to create OkHttpClient object

When i try to create a new OkHttpClient object an Exception get thrown I'm using OkHttp 3.11.0 and OkIO 2.0.0-RC1. Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/TypeCastException at…
Mouamle Hasan
  • 264
  • 1
  • 4
  • 14
15
votes
2 answers

Download progress with RxJava, OkHttp and Okio in Android

In our app I download an image file with this code. I need to show download progress(downloaded bytes in percentage) on UI. How I can get download progress in this code? I searched for solution, but still can't manage to do it on my…
Rafael
  • 6,091
  • 5
  • 54
  • 79
13
votes
2 answers

Fatal Exception: java.lang.OutOfMemoryError using okhttp3 okio for download files

I have a App that downlaod content from the web. music, videos, pdfs.... like a download manager. But now its is crashing everytime is it downloading content: E/LVN/advanced_memory_manager.c: ---------------------------------- AMM report…
Thiago
  • 12,778
  • 14
  • 93
  • 110
13
votes
2 answers

Handling Authentication in Okhttp

I'm using OkHttp 2.3 with basic authentication requests, according to OKHttp docs, it automatically retries unauthenticated requests, but whenever I provide invalid credentials, the request takes too much time and I get this exception in the…
Silvia H
  • 8,097
  • 7
  • 30
  • 33
10
votes
2 answers

How to fix: "Unresolved reference: buffer" or "Using 'buffer(Source): BufferedSource' is an error. moved to extension function"?

I had 3 line code to get body from OkHttp3 source: val responseBody = response.peekBody(response.body()!!.contentLength()) val source = GzipSource(responseBody.source()) val body = Okio.buffer(source).readUtf8() //issue is that line on another…
LunaVulpo
  • 3,043
  • 5
  • 30
  • 58
10
votes
3 answers

Getting "source exhausted prematurely" when inflating gzip HTTP response body

I get this following error when I'm trying to make a HTTP call with okhttp: W/System.err: java.io.EOFException: source exhausted prematurely W/System.err: at okio.InflaterSource.read(InflaterSource.java:83) W/System.err: at…
bleepmeh
  • 967
  • 5
  • 17
  • 36
9
votes
2 answers

Why is Okio more efficient than BufferedInputStream and BufferedOutputStream?

I tried to find out why OkHttp uses Okio but not BufferedInputStream and BufferedOutputStream to buffer data. I used following code to verify: private String targetPath = Environment.getExternalStorageDirectory() + File.separator +…
dreamer
  • 153
  • 2
  • 10
8
votes
1 answer

Streaming okhttp response body

I'm implementing a Server-Sent Events library using OkHttp. Server Sent Events works by keeping an open HTTP connection to the server on which 'events' can be streamed back to the client. The connection will only close on errors, or if the client…
Mike Fougere
  • 157
  • 1
  • 6
8
votes
2 answers

Android-Studio-1.2.RC Proguard warnings on Square's Okio library reference

WIth Android Studio: 1.2.RC I enabled proguard in .gradle: ``` minifyEnabled=true and added these rules to my proguard-rules.pro: -dontwarn com.squareup.** -dontwarn okio.** and added these lint rules to my .gradle file: warningsAsErrors…
Bodhi Hu
  • 196
  • 4
  • 11
7
votes
0 answers

How to read file with okio inside commanMain module in a Kotlin Multiplatform Project?

I am trying to read/write files in the commonMain module. I created a new Kotlin Multiplatform App for Android and iOS using the Android Studio Wizard. (New -> New Project ... -> Koltin Multiplatform App) Then I added okio as dependency to common in…
Sven
  • 1,648
  • 1
  • 21
  • 31
7
votes
1 answer

How to configure Kotlin jvmTarget in a Multiplatform Android module?

I'm getting this build error: Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6. Please specify proper '-jvm-target' option Adding support for Java 8 language features could solve this…
7
votes
4 answers

java.lang.NoSuchMethodError: okio.BufferedSource.rangeEquals(JLokio/ByteString;)Z

I am integrating Outlook API and for making HTTP Calls I am using Retrofit version 2.3.0 and okHttp3 version 3.9.1. However when I'm making an HTTP Call, for example : // Create a logging interceptor to log request and responses …
Paras
  • 3,191
  • 6
  • 41
  • 77
6
votes
1 answer

why can I only read 2048 bytes at a time from an okhttp.Response InputStream?

I am downloading a file using an OkHttp GET request: import com.squareup.okhttp.OkHttpClient; import com.squareup.okhttp.Request; import com.squareup.okhttp.Response; ... OkHttpClient okClient = new OkHttpClient(); Request request =…
weefbellington
  • 302
  • 3
  • 8
1
2 3 4 5 6