/** * @return All characters that the given {@link Reader} produces */ public static String readAll(Reader reader) throws IOException { return IoUtil.readAll(reader, false); }
@Override public String string(Charset charset) throws IOException { InputStream in3 = this.in2; if (in3 == null) throw new IllegalStateException(); final String result = IoUtil.readAll(new InputStreamReader(in3, charset)); try { in3.close(); } catch (Exception e) {} this.in2 = null; return result; }
private static String percentDecode(byte[] bytes, int off, int len) throws IOException { PercentEncodingInputStream is = new PercentEncodingInputStream(new ByteArrayInputStream(bytes, off, len)); try { // According to https://tools.ietf.org/html/rfc3986#section-2.5, "... the data should first be encoded as // octets according to the UTF-8 character encoding". return IoUtil.readAll(new InputStreamReader(is, CHARSET_UTF_8)); } catch (MalformedInputException mie) { // Bytes could not be UTF-8-decoded, so fall back to ISO 8859-1. return IoUtil.readAll(new InputStreamReader(is, CHARSET_ISO_8859_1)); } }
@Override public String string(Charset charset) throws IOException { ConsumerWhichThrows<OutputStream, IOException> w3 = this.writer2; if (w3 == null) throw new IllegalStateException(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); w3.consume(baos); this.writer2 = null; return IoUtil.readAll(new InputStreamReader(new ByteArrayInputStream(baos.toByteArray()), charset)); }
/** * @return An {@link IOException} that contains detailed information about the request, the response, * and the response's "error stream" */ public static IOException toException(HttpURLConnection httpConn, @Nullable Throwable cause) throws IOException { int responseCode = httpConn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_NOT_FOUND && cause == null) { return new FileNotFoundException(httpConn.getURL().toString()); } StringBuilder sb = new StringBuilder(); sb.append(httpConn.getRequestMethod()).append(" ").append(httpConn.getURL()).append('\n'); sb.append(responseCode).append(" ").append(httpConn.getResponseMessage()).append('\n'); InputStream errorStream = httpConn.getErrorStream(); if (errorStream != null) { sb.append(IoUtil.readAll(new InputStreamReader(errorStream, Charset.forName("ISO-8859-1")))); } return new IOException(sb.toString(), cause); }
private void updateParameterListFromQueryOrBody() throws IOException { String query; if (this.method == Method.POST || this.method == Method.PUT) { query = IoUtil.readAll(new InputStreamReader( this.removeBody().inputStream(), this.getCharset() )); } else { assert this.uriQueryValid; query = this.uri.getQuery(); } List<Entry<String, String>> pl = (this.parameterList = new ArrayList<Map.Entry<String, String>>()); pl.addAll(HttpRequest.decodeParameters(query)); }