Search in sources :

Example 6 with ForwardingSource

use of okio.ForwardingSource in project okhttp by square.

the class InterceptorTest method uppercase.

private static Source uppercase(Source original) {
    return new ForwardingSource(original) {

        @Override
        public long read(Buffer sink, long byteCount) throws IOException {
            Buffer mixedCase = new Buffer();
            long count = original.read(mixedCase, byteCount);
            sink.writeUtf8(mixedCase.readUtf8().toUpperCase(Locale.US));
            return count;
        }
    };
}
Also used : Buffer(okio.Buffer) ForwardingSource(okio.ForwardingSource)

Example 7 with ForwardingSource

use of okio.ForwardingSource in project okhttp by square.

the class ResponseBodyTest method readerClosedAfterBomClosesUnderlyingSource.

@Test
public void readerClosedAfterBomClosesUnderlyingSource() 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() {
            ResponseBody body = body("fffe680065006c006c006f00");
            return Okio.buffer(new ForwardingSource(body.source()) {

                @Override
                public void close() throws IOException {
                    closed.set(true);
                    super.close();
                }
            });
        }
    };
    Reader reader = body.charStream();
    assertEquals('h', reader.read());
    reader.close();
    assertTrue(closed.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ForwardingSource(okio.ForwardingSource) IOException(java.io.IOException) Test(org.junit.Test)

Example 8 with ForwardingSource

use of okio.ForwardingSource in project okhttp by square.

the class ResponseBodyTest method byteStreamClosesUnderlyingSource.

@Test
public void byteStreamClosesUnderlyingSource() 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();
                }
            });
        }
    };
    body.byteStream().close();
    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 9 with ForwardingSource

use of okio.ForwardingSource in project okhttp by square.

the class ResponseBodyTest method readerClosedBeforeBomClosesUnderlyingSource.

@Test
public void readerClosedBeforeBomClosesUnderlyingSource() 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() {
            ResponseBody body = body("fffe680065006c006c006f00");
            return Okio.buffer(new ForwardingSource(body.source()) {

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

Example 10 with ForwardingSource

use of okio.ForwardingSource in project okhttp by square.

the class ResponseBodyTest method sourceClosesUnderlyingSource.

@Test
public void sourceClosesUnderlyingSource() 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();
                }
            });
        }
    };
    body.source().close();
    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)

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