/** * Stores the specified source to the specified file. * * @param in input source * @param file target file * @throws IOException I/O exception */ public static void store(final InputSource in, final IOFile file) throws IOException { // add directory if it does not exist anyway file.dir().md(); final PrintOutput po = new PrintOutput(file.path()); try { final Reader r = in.getCharacterStream(); final InputStream is = in.getByteStream(); final String id = in.getSystemId(); if (r != null) { for (int c; (c = r.read()) != -1; ) po.utf8(c); } else if (is != null) { for (int b; (b = is.read()) != -1; ) po.write(b); } else if (id != null) { final BufferInput bi = new BufferInput(IO.get(id)); try { for (int b; (b = bi.read()) != -1; ) po.write(b); } finally { bi.close(); } } } finally { po.close(); } }
/** * Sends the specified stream to the server. * * @param input input stream * @throws IOException I/O exception */ private void send(final InputStream input) throws IOException { final EncodingOutput eo = new EncodingOutput(sout); for (int b; (b = input.read()) != -1; ) eo.write(b); sout.write(0); sout.flush(); receive(null); }
@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(); } }
/** * Watches an event. * * @param name event name * @param notifier event notification * @throws IOException I/O exception */ public void watch(final String name, final EventNotifier notifier) throws IOException { sout.write(ServerCmd.WATCH.code); if (esocket == null) { sout.flush(); final BufferInput bi = new BufferInput(sin); final int eport = Integer.parseInt(bi.readString()); // initialize event socket esocket = new Socket(); esocket.connect(new InetSocketAddress(ehost, eport), 5000); final OutputStream so = esocket.getOutputStream(); so.write(bi.readBytes()); so.write(0); so.flush(); final InputStream is = esocket.getInputStream(); is.read(); listen(is); } send(name); sout.flush(); receive(null); notifiers.put(name, notifier); }