/** * Retrieves and returns the whole text and closes the stream. * * @return contents * @throws IOException I/O exception */ public byte[] content() throws IOException { try { if (length > 0) { // input length is known in advance final int sl = (int) Math.min(Integer.MAX_VALUE, length); final byte[] bytes = new byte[sl]; for (int c = 0; c < sl; c++) bytes[c] = (byte) next(); return bytes; } // parse until end of stream final ByteList bl = new ByteList(); for (int ch; (ch = next()) != -1; ) bl.add(ch); return bl.toArray(); } finally { close(); } }
@Override public HTTPResponse call() throws Exception { final HttpURLConnection hc = (HttpURLConnection) uri.toURL().openConnection(); hc.setReadTimeout(SOCKET_TIMEOUT); try { while (!stop) { try { final int code = hc.getResponseCode(); final InputStream input = hc.getInputStream(); final ByteList bl = new ByteList(); for (int i; (i = input.read()) != -1; ) bl.add(i); return new HTTPResponse(code, bl.toString()); } catch (final SocketTimeoutException e) { } } return null; } finally { hc.disconnect(); } }
/** * Reads a byte array from the input stream, suffixed by a {@code 0} byte. * * @return token * @throws IOException I/O Exception */ public final byte[] readBytes() throws IOException { final ByteList bl = new ByteList(); for (int l; (l = next()) > 0; ) bl.add(l); return bl.toArray(); }
/** * Reads a string from the input stream, suffixed by a {@code 0} byte. * * @return string * @throws IOException I/O Exception */ public final String readString() throws IOException { final ByteList bl = new ByteList(); for (int l; (l = next()) > 0; ) bl.add(l); return bl.toString(); }