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