use of okio.BufferedSource in project retrofit by square.
the class CallTest method conversionProblemIncomingMaskedByConverterIsUnwrapped.
@Test
public void conversionProblemIncomingMaskedByConverterIsUnwrapped() throws IOException {
// MWS has no way to trigger IOExceptions during the response body so use an interceptor.
OkHttpClient client = //
new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
okhttp3.Response response = chain.proceed(chain.request());
ResponseBody body = response.body();
BufferedSource source = Okio.buffer(new ForwardingSource(body.source()) {
@Override
public long read(Buffer sink, long byteCount) throws IOException {
throw new IOException("cause");
}
});
body = ResponseBody.create(body.contentType(), body.contentLength(), source);
return response.newBuilder().body(body).build();
}
}).build();
Retrofit retrofit = new Retrofit.Builder().baseUrl(server.url("/")).client(client).addConverterFactory(new ToStringConverterFactory() {
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
return new Converter<ResponseBody, String>() {
@Override
public String convert(ResponseBody value) throws IOException {
try {
return value.string();
} catch (IOException e) {
// Some serialization libraries mask transport problems in runtime exceptions. Bad!
throw new RuntimeException("wrapper", e);
}
}
};
}
}).build();
Service example = retrofit.create(Service.class);
server.enqueue(new MockResponse().setBody("Hi"));
Call<String> call = example.getString();
try {
call.execute();
fail();
} catch (IOException e) {
assertThat(e).hasMessage("cause");
}
}
use of okio.BufferedSource in project tape by square.
the class QueueFileTest method removingElementZeroesData.
@Test
public void removingElementZeroesData() throws IOException {
QueueFile queueFile = newQueueFile(true);
queueFile.add(values[4]);
queueFile.remove();
queueFile.close();
BufferedSource source = Okio.buffer(Okio.source(file));
source.skip(headerLength);
source.skip(Element.HEADER_LENGTH);
assertThat(source.readByteString(4).hex()).isEqualTo("00000000");
}
use of okio.BufferedSource in project wire by square.
the class SchemaLoader method loadDescriptorProto.
/**
* Returns Google's protobuf descriptor, which defines standard options like default, deprecated,
* and java_package. If the user has provided their own version of the descriptor proto, that is
* preferred.
*/
private ProtoFile loadDescriptorProto() throws IOException {
InputStream resourceAsStream = SchemaLoader.class.getResourceAsStream("/" + DESCRIPTOR_PROTO);
try (BufferedSource buffer = Okio.buffer(Okio.source(resourceAsStream))) {
String data = buffer.readUtf8();
Location location = Location.get("", DESCRIPTOR_PROTO);
ProtoFileElement element = ProtoParser.parse(location, data);
return ProtoFile.get(element);
}
}
use of okio.BufferedSource in project azure-sdk-for-java by Azure.
the class MockIntegrationTestBase method extractResponseData.
private void extractResponseData(Map<String, String> responseData, Response response) throws Exception {
Map<String, List<String>> headers = response.headers().toMultimap();
boolean addedRetryAfter = false;
for (Entry<String, List<String>> header : headers.entrySet()) {
String headerValueToStore = header.getValue().get(0);
if (header.getKey().equalsIgnoreCase("location") || header.getKey().equalsIgnoreCase("azure-asyncoperation")) {
headerValueToStore = applyRegex(headerValueToStore);
}
if (header.getKey().equalsIgnoreCase("retry-after")) {
headerValueToStore = "0";
addedRetryAfter = true;
}
responseData.put(header.getKey().toLowerCase(), headerValueToStore);
}
if (!addedRetryAfter) {
responseData.put("retry-after", "0");
}
BufferedSource bufferedSource = response.body().source();
bufferedSource.request(9223372036854775807L);
Buffer buffer = bufferedSource.buffer().clone();
String content = null;
if (response.header("Content-Encoding") == null) {
content = new String(buffer.readString(Util.UTF_8));
} else if (response.header("Content-Encoding").equalsIgnoreCase("gzip")) {
GZIPInputStream gis = new GZIPInputStream(buffer.inputStream());
content = IOUtils.toString(gis);
responseData.remove("Content-Encoding".toLowerCase());
responseData.put("Content-Length".toLowerCase(), Integer.toString(content.length()));
}
if (content != null) {
content = applyRegex(content);
responseData.put("Body", content);
}
}
use of okio.BufferedSource in project azure-sdk-for-java by Azure.
the class ProviderRegistrationInterceptor method errorBody.
private String errorBody(ResponseBody responseBody) throws IOException {
if (responseBody == null) {
return null;
}
BufferedSource source = responseBody.source();
// Buffer the entire body.
source.request(Long.MAX_VALUE);
Buffer buffer = source.buffer();
return buffer.clone().readUtf8();
}
Aggregations