Search in sources :

Example 1 with ForwardingSource

use of okio.ForwardingSource in project okhttp by square.

the class InMemoryFileSystem method source.

@Override
public Source source(File file) throws FileNotFoundException {
    Buffer result = files.get(file);
    if (result == null)
        throw new FileNotFoundException();
    final Source source = result.clone();
    openSources.put(source, file);
    return new ForwardingSource(source) {

        @Override
        public void close() throws IOException {
            openSources.remove(source);
            super.close();
        }
    };
}
Also used : Buffer(okio.Buffer) FileNotFoundException(java.io.FileNotFoundException) ForwardingSource(okio.ForwardingSource) Source(okio.Source) ForwardingSource(okio.ForwardingSource)

Example 2 with ForwardingSource

use of okio.ForwardingSource in project okhttp by square.

the class ResponseBodyTest method throwingUnderlyingSourceClosesQuietly.

@Test
public void throwingUnderlyingSourceClosesQuietly() throws IOException {
    ResponseBody body = new ResponseBody() {

        @Override
        public MediaType contentType() {
            return null;
        }

        @Override
        public long contentLength() {
            return 5;
        }

        @Override
        public BufferedSource source() {
            Buffer source = new Buffer().writeUtf8("hello");
            return Okio.buffer(new ForwardingSource(source) {

                @Override
                public void close() throws IOException {
                    throw new IOException("Broken!");
                }
            });
        }
    };
    assertEquals("hello", body.source().readUtf8());
    body.close();
}
Also used : Buffer(okio.Buffer) ForwardingSource(okio.ForwardingSource) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with ForwardingSource

use of okio.ForwardingSource in project okhttp by square.

the class ResponseBodyTest method bytesClosesUnderlyingSource.

@Test
public void bytesClosesUnderlyingSource() throws IOException {
    final AtomicBoolean closed = new AtomicBoolean();
    ResponseBody body = new ResponseBody() {

        @Override
        public MediaType contentType() {
            return null;
        }

        @Override
        public long contentLength() {
            return 5;
        }

        @Override
        public BufferedSource source() {
            Buffer source = new Buffer().writeUtf8("hello");
            return Okio.buffer(new ForwardingSource(source) {

                @Override
                public void close() throws IOException {
                    closed.set(true);
                    super.close();
                }
            });
        }
    };
    assertEquals(5, body.bytes().length);
    assertTrue(closed.get());
}
Also used : Buffer(okio.Buffer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ForwardingSource(okio.ForwardingSource) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with ForwardingSource

use of okio.ForwardingSource in project moshi by square.

the class JsonUtf8ReaderTest method readObjectSource.

@Test
public void readObjectSource() throws IOException {
    Buffer buffer = new Buffer().writeUtf8("{\"a\": \"android\", \"b\": \"banana\"}");
    JsonReader reader = JsonReader.of(Okio.buffer(new ForwardingSource(buffer) {
    }));
    reader.beginObject();
    assertThat(reader.nextName()).isEqualTo("a");
    assertThat(reader.nextString()).isEqualTo("android");
    assertThat(reader.nextName()).isEqualTo("b");
    assertThat(reader.nextString()).isEqualTo("banana");
    reader.endObject();
    assertThat(reader.peek()).isEqualTo(JsonReader.Token.END_DOCUMENT);
}
Also used : Buffer(okio.Buffer) ForwardingSource(okio.ForwardingSource) Test(org.junit.Test)

Example 5 with ForwardingSource

use of okio.ForwardingSource in project apollo-android by apollographql.

the class ApolloHttpCache method read.

@Override
public Response read(@Nonnull final String cacheKey, final boolean expireAfterRead) {
    HttpCacheRecord responseCacheRecord = null;
    try {
        responseCacheRecord = cacheStore.cacheRecord(cacheKey);
        if (responseCacheRecord == null) {
            return null;
        }
        final HttpCacheRecord cacheRecord = responseCacheRecord;
        Source cacheResponseSource = new ForwardingSource(responseCacheRecord.bodySource()) {

            @Override
            public void close() throws IOException {
                super.close();
                closeQuietly(cacheRecord);
                if (expireAfterRead) {
                    removeQuietly(cacheKey);
                }
            }
        };
        Response response = new ResponseHeaderRecord(responseCacheRecord.headerSource()).response();
        String contentType = response.header("Content-Type");
        String contentLength = response.header("Content-Length");
        return response.newBuilder().body(new CacheResponseBody(cacheResponseSource, contentType, contentLength)).build();
    } catch (Exception e) {
        closeQuietly(responseCacheRecord);
        logger.e(e, "Failed to read http cache entry for key: %s", cacheKey);
        return null;
    }
}
Also used : Response(okhttp3.Response) HttpCacheRecord(com.apollographql.apollo.api.cache.http.HttpCacheRecord) ForwardingSource(okio.ForwardingSource) Source(okio.Source) ForwardingSource(okio.ForwardingSource) IOException(java.io.IOException)

Aggregations

ForwardingSource (okio.ForwardingSource)13 Buffer (okio.Buffer)10 IOException (java.io.IOException)9 Test (org.junit.Test)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Source (okio.Source)2 Message (android.os.Message)1 HttpCacheRecord (com.apollographql.apollo.api.cache.http.HttpCacheRecord)1 ProgressMessage (com.okhttplib.bean.ProgressMessage)1 ProgressCallback (com.okhttplib.callback.ProgressCallback)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 Type (java.lang.reflect.Type)1 Interceptor (okhttp3.Interceptor)1 OkHttpClient (okhttp3.OkHttpClient)1 Response (okhttp3.Response)1 ResponseBody (okhttp3.ResponseBody)1 MockResponse (okhttp3.mockwebserver.MockResponse)1 BufferedSource (okio.BufferedSource)1