private Object deserialize(ByteString byteString) throws IOException, ClassNotFoundException { Buffer buffer = new Buffer(); buffer.write(byteString); try (ObjectInputStream objectIn = new ObjectInputStream(buffer.inputStream())) { Object result = objectIn.readObject(); if (objectIn.read() != -1) throw new IOException("Unconsumed bytes in stream"); return result; } }
private List<Certificate> readCertificateList(BufferedSource source) throws IOException { int length = readInt(source); if (length == -1) return Collections.emptyList(); // OkHttp v1.2 used -1 to indicate null. try { CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); List<Certificate> result = new ArrayList<>(length); for (int i = 0; i < length; i++) { String line = source.readUtf8LineStrict(); Buffer bytes = new Buffer(); bytes.write(ByteString.decodeBase64(line)); result.add(certificateFactory.generateCertificate(bytes.inputStream())); } return result; } catch (CertificateException e) { throw new IOException(e.getMessage()); } }
private static String charsetDecode(EncodedWord word) { try { InputStream inputStream = new Buffer().write(word.data).inputStream(); return CharsetSupport.readToString(inputStream, word.charset); } catch (IOException e) { return null; } }
.writeUtf8(comodoRsaCertificationAuthority) .writeUtf8(entrustRootCertificateAuthority) .inputStream();
private List<Certificate> readCertificateList(BufferedSource source) throws IOException { int length = readInt(source); if (length == -1) return Collections.emptyList(); // OkHttp v1.2 used -1 to indicate null. try { CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); List<Certificate> result = new ArrayList<>(length); for (int i = 0; i < length; i++) { String line = source.readUtf8LineStrict(); Buffer bytes = new Buffer(); bytes.write(ByteString.decodeBase64(line)); result.add(certificateFactory.generateCertificate(bytes.inputStream())); } return result; } catch (CertificateException e) { throw new IOException(e.getMessage()); } }
private List<Certificate> readCertificateList(BufferedSource source) throws IOException { int length = readInt(source); if (length == -1) return Collections.emptyList(); // OkHttp v1.2 used -1 to indicate null. try { CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); List<Certificate> result = new ArrayList<>(length); for (int i = 0; i < length; i++) { String line = source.readUtf8LineStrict(); Buffer bytes = new Buffer(); bytes.write(ByteString.decodeBase64(line)); result.add(certificateFactory.generateCertificate(bytes.inputStream())); } return result; } catch (CertificateException e) { throw new IOException(e.getMessage()); } }
public static Object deserialize(ByteString data) throws Exception { Buffer buffer = new Buffer().write(data); ObjectInputStream stream = new ObjectInputStream(buffer.inputStream()); return stream.readObject(); } }
private InputStream inputStream(String data) { return new Buffer().writeUtf8(data).inputStream(); } }
@Test public void bufferInputStreamBulkReads() throws Exception { Buffer source = new Buffer(); source.writeUtf8("abc"); byte[] byteArray = new byte[4]; Arrays.fill(byteArray, (byte) -5); InputStream in = source.inputStream(); assertEquals(3, in.read(byteArray)); assertEquals("[97, 98, 99, -5]", Arrays.toString(byteArray)); Arrays.fill(byteArray, (byte) -7); assertEquals(-1, in.read(byteArray)); assertEquals("[-7, -7, -7, -7]", Arrays.toString(byteArray)); }
/** * Uses streaming decompression to inflate {@code deflated}. The input must * either be finished or have a trailing sync flush. */ private Buffer inflate(Buffer deflated) throws IOException { InputStream deflatedIn = deflated.inputStream(); Inflater inflater = new Inflater(); InputStream inflatedIn = new InflaterInputStream(deflatedIn, inflater); Buffer result = new Buffer(); byte[] buffer = new byte[8192]; while (!inflater.needsInput() || deflated.size() > 0 || deflatedIn.available() > 0) { int count = inflatedIn.read(buffer, 0, buffer.length); if (count != -1) { result.write(buffer, 0, count); } } return result; } }
@Test public void bufferInputStreamByteByByte() throws Exception { Buffer source = new Buffer(); source.writeUtf8("abc"); InputStream in = source.inputStream(); assertEquals(3, in.available()); assertEquals('a', in.read()); assertEquals('b', in.read()); assertEquals('c', in.read()); assertEquals(-1, in.read()); assertEquals(0, in.available()); }
@Test public void upload_file() throws Exception { answerHelloWorld(); File reportFile = temp.newFile(); FileUtils.write(reportFile, "the report content"); PostRequest request = new PostRequest("api/report/upload") .setParam("project", "theKey") .setPart("report", new PostRequest.Part(MediaTypes.TXT, reportFile)) .setMediaType(MediaTypes.PROTOBUF); underTest = HttpConnector.newBuilder().url(serverUrl).build(); WsResponse response = underTest.call(request); assertThat(response.hasContent()).isTrue(); RecordedRequest recordedRequest = server.takeRequest(); assertThat(recordedRequest.getMethod()).isEqualTo("POST"); assertThat(recordedRequest.getPath()).isEqualTo("/api/report/upload?project=theKey"); String body = IOUtils.toString(recordedRequest.getBody().inputStream()); assertThat(body) .contains("Content-Disposition: form-data; name=\"report\"") .contains("Content-Type: text/plain") .contains("the report content"); }
if (request.body() != null && !request.method().equalsIgnoreCase(PATCH)) { request.body().writeTo(buffer); Object object = Serialization.unmarshal(buffer.inputStream()); if (object instanceof HasMetadata) { HasMetadata h = (HasMetadata) object;
private String getRequestContent(RequestBody body) throws IOException { Buffer buffer = new Buffer(); body.writeTo(buffer); BufferedReader reader = new BufferedReader(new InputStreamReader(buffer.inputStream())); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line); } return sb.toString(); }
public static InputStream stringToInputStream(String str) { if (str == null) return null; return new Buffer().writeUtf8(str).inputStream(); } }
@Override public InputStream getMessagePayload() throws IOException { if (request.body() == null) { return null; } Buffer buf = new Buffer(); request.body().writeTo(buf); return buf.inputStream(); }
private static ClientHttpRequest toRequest(RecordedRequest request) throws IOException { MockClientHttpRequest clientRequest = new MockClientHttpRequest(); clientRequest.setMethod(HttpMethod.valueOf(request.getMethod())); clientRequest.setURI(URI.create(request.getPath())); request.getHeaders().toMultimap().forEach((name, values) -> values.forEach(value -> clientRequest.getHeaders().add(name, value))); IOUtils.copy(request.getBody().inputStream(), clientRequest.getBody()); return clientRequest; }
public static Map<String, Object> bodyFromRequest(RecordedRequest request) throws IOException { ObjectMapper mapper = new ObjectMapper(); MapType mapType = mapper.getTypeFactory().constructMapType(HashMap.class, String.class, Object.class); Buffer body = request.getBody(); try { return mapper.readValue(body.inputStream(), mapType); } catch (IOException e) { throw e; } finally { body.close(); } } }
@Test public void readFully() throws IOException { assertThat(Utils.readFully(Utils.buffer(new Buffer().writeUtf8("foo\nbar").inputStream()))) .isEqualTo("foobar"); } }
@Test public void noneCryptoRead() throws IOException { Crypto crypto = Crypto.none(); ByteString foo = ByteString.encodeUtf8("foo"); Buffer buffer = new Buffer(); buffer.write(foo); assertThat(Okio.buffer(Okio.source(crypto.decrypt(buffer.inputStream()))).readByteString()) // .isEqualTo(foo); } }