/** * Returns the concatenation of 8-bit, length prefixed protocol names. * http://tools.ietf.org/html/draft-agl-tls-nextprotoneg-04#page-4 */ static byte[] concatLengthPrefixed(List<Protocol> protocols) { Buffer result = new Buffer(); for (int i = 0, size = protocols.size(); i < size; i++) { Protocol protocol = protocols.get(i); if (protocol == Protocol.HTTP_1_0) continue; // No HTTP/1.0 for ALPN. result.writeByte(protocol.toString().length()); result.writeUtf8(protocol.toString()); } return result.readByteArray(); }
/** Encode {@code value} as a {@code byte[]}. */ public final byte[] encode(E value) { checkNotNull(value, "value == null"); Buffer buffer = new Buffer(); try { encode(buffer, value); } catch (IOException e) { throw new AssertionError(e); // No I/O writing to Buffer. } return buffer.readByteArray(); }
/** * Returns the concatenation of 8-bit, length prefixed protocol names. * http://tools.ietf.org/html/draft-agl-tls-nextprotoneg-04#page-4 */ static byte[] concatLengthPrefixed(List<Protocol> protocols) { Buffer result = new Buffer(); for (int i = 0, size = protocols.size(); i < size; i++) { Protocol protocol = protocols.get(i); if (protocol == Protocol.HTTP_1_0) continue; // No HTTP/1.0 for ALPN. result.writeByte(protocol.toString().length()); result.writeUtf8(protocol.toString()); } return result.readByteArray(); }
/** * 解析服务器响应的内容 * * @param responseBody {@link ResponseBody} * @param encoding 编码类型 * @param clone 克隆后的服务器响应内容 * @return 解析后的响应结果 */ private String parseContent(ResponseBody responseBody, String encoding, Buffer clone) { Charset charset = Charset.forName("UTF-8"); MediaType contentType = responseBody.contentType(); if (contentType != null) { charset = contentType.charset(charset); } if (encoding != null && encoding.equalsIgnoreCase("gzip")) {//content 使用 gzip 压缩 return ZipHelper.decompressForGzip(clone.readByteArray(), convertCharset(charset));//解压 } else if (encoding != null && encoding.equalsIgnoreCase("zlib")) {//content 使用 zlib 压缩 return ZipHelper.decompressToStringForZlib(clone.readByteArray(), convertCharset(charset));//解压 } else {//content 没有被压缩, 或者使用其他未知压缩方式 return clone.readString(charset); } }
/** * Converts retrofit request to async-http-client request. * * @param request retrofit request * @return async-http-client request. */ @SneakyThrows protected org.asynchttpclient.Request createRequest(@NonNull Request request) { // create async-http-client request builder val requestBuilder = new RequestBuilder(request.method()); // request uri requestBuilder.setUrl(request.url().toString()); // set headers val headers = request.headers(); headers.names().forEach(name -> requestBuilder.setHeader(name, headers.values(name))); // set request body val body = request.body(); if (body != null && body.contentLength() > 0) { if (body.contentType() != null) { requestBuilder.setHeader(HttpHeaderNames.CONTENT_TYPE, body.contentType().toString()); } // write body to buffer val okioBuffer = new Buffer(); body.writeTo(okioBuffer); requestBuilder.setBody(okioBuffer.readByteArray()); } // customize the request builder (external customizer can change the request url for example) runConsumers(this.requestCustomizers, requestBuilder); return requestBuilder.build(); }
body.writeTo(contentBuffer); return httpClient.execute(headers, contentBuffer.readByteArray()); } catch (IOException e) { throw new IllegalArgumentException(
@Test public void readFullyByteArray() throws IOException { Buffer data = new Buffer(); data.writeUtf8("Hello").writeUtf8(repeat('e', SEGMENT_SIZE)); byte[] expected = data.clone().readByteArray(); sink.write(data, data.size()); sink.emit(); byte[] sink = new byte[SEGMENT_SIZE + 5]; source.readFully(sink); assertByteArraysEquals(expected, sink); }
@Override public byte[] readByteArray() { try { return readByteArray(size); } catch (EOFException e) { throw new AssertionError(e); } }
private static byte[] bytes(final RequestBody body) throws IOException { final Buffer buffer = new Buffer(); body.writeTo(buffer); return buffer.readByteArray(); }
private static byte[] bytes(final RequestBody body) throws IOException { final Buffer buffer = new Buffer(); body.writeTo(buffer); return buffer.readByteArray(); }
@Override public byte[] readByteArray(long byteCount) throws IOException { require(byteCount); return buffer.readByteArray(byteCount); }
assertArrayEquals(requestContent.getBytes(), recordedApacheRequest.getBody().readByteArray());
/** Encode {@code value} as a {@code byte[]}. */ public final byte[] encode(E value) { checkNotNull(value, "value == null"); Buffer buffer = new Buffer(); try { encode(buffer, value); } catch (IOException e) { throw new AssertionError(e); // No I/O writing to Buffer. } return buffer.readByteArray(); }
private byte[] parseGzipResponseBody(ResponseBody body) throws IOException{ Buffer buffer = new Buffer(); GzipSource gzipSource = new GzipSource(body.source()); while (gzipSource.read(buffer, Integer.MAX_VALUE) != -1) { } gzipSource.close(); return buffer.readByteArray(); }
public static NetworkResponse create(MediaType mediaType, String data) { Utils.checkNotNull(mediaType, "mediaType==null"); return create(mediaType, new Buffer() .writeString(data, mediaType.charset(Charset.forName(HTTP.UTF_8))) .readByteArray()); }
public static NetworkRequest create(MediaType mediaType, String data) { Utils.checkNotNull(mediaType, "mediaType==null"); return create(mediaType, new Buffer() .writeString(data, mediaType.charset(Charset.forName(HTTP.UTF_8))) .readByteArray()); }
@Override public void write(Buffer source, long byteCount) throws IOException { Buffer copy = new Buffer(); source.copyTo(copy, 0, byteCount); super.write(source, byteCount); bytesWritten += byteCount; listener.onRequestProgress(copy.readByteArray(), byteCount, bytesWritten, contentLength()); } }
public NetworkRequest.Builder builder() { return new NetworkRequest.Builder() .setContentType(CONTENT_TYPE) .setBody(content.readByteArray()); }
public NetworkRequest build() { return new NetworkRequest.Builder() .setContentType(CONTENT_TYPE) .setBody(content.readByteArray()) .build(); } }
@Test public void testCborRequests() throws IOException, InterruptedException { LocalDate date = LocalDate.of(2001, 2, 3); server.enqueue(new MockResponse()); service.makeCborRequest(date).execute(); RecordedRequest request = server.takeRequest(); assertThat(request.getBody().readByteArray()) .isEqualTo(ObjectMappers.newCborClientObjectMapper().writeValueAsBytes(date)); }