1 22 23 package org.netbeans.lib.terminalemulator; 24 25 import java.io.*; 26 import java.awt.*; 27 import javax.swing.SwingUtilities ; 28 29 public class StreamTerm extends Term { 30 31 static class Fmt { 32 public static String pad(int what, int width) { 33 return pad("" + what, width); 34 } 35 public static String pad(byte what, int width) { 36 return pad("" + (char) what, width); 37 } 38 public static String pad0(String what, int width) { 39 return padwith(what, width, '0'); 40 } 41 public static String pad(String what, int width) { 42 return padwith(what, width, ' '); 43 } 44 private static String padwith(String what, int width, char with) { 45 boolean left = false; 46 if (width < 0) { 47 left = true; 48 width = -width; 49 } 50 String sub; 51 if (what.length() > width) 52 sub = what.substring(0, width); else 54 sub = what; 55 int pad = width - sub.length(); 56 StringBuffer buf = new StringBuffer (sub); 57 if (left) { 58 while(pad-- > 0) 59 buf.append(with); 60 } else { 61 while(pad-- > 0) 62 buf.insert(0, with); 63 } 64 return new String (buf); 65 } 66 } 67 68 private OutputStreamWriter writer; 70 76 public OutputStreamWriter getOutputStreamWriter() { 77 return writer; 78 } 79 80 public StreamTerm() { 81 super(); 82 83 addInputListener(new TermInputListener() { 84 public void sendChars(char c[], int offset, int count) { 85 if (writer == null) 86 return; 87 try { 88 writer.write(c, offset, count); 89 writer.flush(); 90 } catch(Exception x) { 91 x.printStackTrace(); 92 } 93 } 94 95 public void sendChar(char c) { 96 if (writer == null) 97 return; 98 try { 99 writer.write(c); 100 writer.flush(); 104 } catch(Exception x) { 105 x.printStackTrace(); 106 } 107 } 108 } ); 109 } 110 111 114 private class OutputMonitor extends Thread { 115 private static final int BUFSZ = 1024; 116 private char[] buf = new char[BUFSZ]; 117 private Term term; 118 private InputStreamReader reader; 119 120 OutputMonitor(InputStreamReader reader, Term term) { 121 super("StreamTerm.OutputMonitor"); this.reader = reader; 123 this.term = term; 124 125 setPriority(1); 130 } 131 132 private void db_echo_receipt(char buf[], int offset, int count) { 133 136 System.out.println("Received:"); final int width = 20; 138 int cx = 0; 139 while (cx < count) { 140 int cx0 = cx; 142 System.out.print(Fmt.pad(cx, 4) + ": "); for (int x = 0; x < width && cx < count; cx++, x++) { 144 String hex = Integer.toHexString(buf[offset+cx]); 145 System.out.print(Fmt.pad0(hex, 2) + " "); } 147 System.out.println(); 148 149 cx = cx0; 151 System.out.print(" "); for (int x = 0; x < width && cx < count; cx++, x++) { 153 char c = (char) buf[offset+cx]; 154 if (Character.isISOControl(c)) 155 c = ' '; 156 System.out.print(Fmt.pad((byte)c, 2) + " "); } 158 System.out.println(); 159 } 160 } 161 162 final private class Trampoline implements Runnable { 163 public int nread; 164 public void run() { 165 term.putChars(buf, 0, nread); 166 } 167 } 168 169 public void run() { 170 Trampoline tramp = new Trampoline(); 171 172 try { 173 while(true) { 174 int nread = reader.read(buf, 0, BUFSZ); 175 if (nread == -1) { 176 183 break; 184 } 185 if (debugInput()) 186 db_echo_receipt(buf, 0, nread); 187 188 if (false) { 189 term.putChars(buf, 0, nread); 190 } else { 191 tramp.nread = nread; 194 SwingUtilities.invokeAndWait(tramp); 195 } 196 } 197 reader.close(); 198 } catch(Exception x) { 199 x.printStackTrace(); 200 } 201 } 202 } 203 204 215 public void connect(OutputStream pin, InputStream pout, InputStream perr) { 216 217 updateTtySize(); 219 220 if (pin != null) 221 writer = new OutputStreamWriter(pin); 222 223 InputStreamReader out_reader = new InputStreamReader(pout); 224 OutputMonitor out_monitor = new OutputMonitor(out_reader, this); 225 out_monitor.start(); 226 227 if (perr != null) { 228 InputStreamReader err_reader = new InputStreamReader(perr); 229 OutputMonitor err_monitor = new OutputMonitor(err_reader, this); 230 err_monitor.start(); 231 } 232 } 233 234 } 235 | Popular Tags |