public SessionTerminal(CommandProcessor commandProcessor, ThreadIO threadIO) throws IOException { try { this.terminal = new Terminal(TERM_WIDTH, TERM_HEIGHT); terminal.write("\u001b\u005B20\u0068"); // set newline mode on in = new PipedOutputStream(); out = new PipedInputStream(); PrintStream pipedOut = new PrintStream(new PipedOutputStream(out), true); console = new Console( commandProcessor, threadIO, new PipedInputStream(in), pipedOut, pipedOut, new WebTerminal(TERM_WIDTH, TERM_HEIGHT), null, null); CommandSession session = console.getSession(); session.put("APPLICATION", System.getProperty("karaf.name", "root")); session.put("USER", "karaf"); session.put("COLUMNS", Integer.toString(TERM_WIDTH)); session.put("LINES", Integer.toString(TERM_HEIGHT)); } catch (IOException e) { LOG.info("Exception attaching to console", e); throw e; } catch (Exception e) { LOG.info("Exception attaching to console", e); throw (IOException) new IOException().initCause(e); } new Thread(console).start(); new Thread(this).start(); }
public void run() { try { for (; ; ) { byte[] buf = new byte[8192]; int l = out.read(buf); InputStreamReader r = new InputStreamReader(new ByteArrayInputStream(buf, 0, l)); StringBuilder sb = new StringBuilder(); for (; ; ) { int c = r.read(); if (c == -1) { break; } sb.append((char) c); } if (sb.length() > 0) { terminal.write(sb.toString()); } String s = terminal.read(); if (s != null && s.length() > 0) { for (byte b : s.getBytes()) { in.write(b); } } } } catch (IOException e) { closed = true; } }