/** * Executes a command and sends the result to the specified output stream. * * @param cmd server command * @param arg argument * @param os target output stream * @return string * @throws IOException I/O exception */ protected String exec(final ServerCmd cmd, final String arg, final OutputStream os) throws IOException { final OutputStream o = os == null ? new ArrayOutput() : os; sout.write(cmd.code); send(arg); sout.flush(); final BufferInput bi = new BufferInput(sin); ClientSession.receive(bi, o); if (!ClientSession.ok(bi)) throw new BaseXException(bi.readString()); return o.toString(); }
/** * 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); }
/** * Retrieves data from the server. * * @param bi buffered server input * @param os output stream * @throws IOException I/O exception */ protected static void receive(final BufferInput bi, final OutputStream os) throws IOException { final DecodingInput di = new DecodingInput(bi); for (int b; (b = di.read()) != -1; ) os.write(b); }