1 22 23 27 28 package org.netbeans.lib.terminalemulator; 29 30 49 50 public class LineDiscipline extends TermStream { 51 52 private static final char bs_sequence[] = {(char)8, (char)32, (char)8}; 53 54 private StringBuffer line = new StringBuffer (); 56 57 private int send_buf_sz = 2; 59 private char send_buf[] = new char[send_buf_sz]; 60 char [] send_buf(int n) { 61 if (n >= send_buf_sz) { 62 send_buf_sz = n+1; 63 send_buf = new char[send_buf_sz]; 64 } 65 return send_buf; 66 } 67 68 private int put_capacity = 16; 70 private int put_length = 0; 71 private char put_buf[] = new char[put_capacity]; 72 73 public void flush() { 74 toDTE.flush(); 75 } 76 77 public void putChar(char c) { 78 82 put_length = 0; 84 85 processChar(c); 87 88 toDTE.putChars(put_buf, 0, put_length); 90 } 91 92 public void putChars(char buf[], int offset, int count) { 93 94 put_length = 0; 96 97 for (int bx = 0; bx < count; bx++) 99 processChar(buf[offset+bx]); 100 101 toDTE.putChars(put_buf, 0, put_length); 103 } 104 105 private void processChar(char c) { 106 appendChar(c); 108 109 if (c == 10) 111 appendChar((char) 13); 112 } 113 114 private void appendChar(char c) { 115 116 118 if (put_length >= put_capacity) { 119 int new_capacity = put_capacity * 2; 120 if (new_capacity < 0) 121 new_capacity = Integer.MAX_VALUE; 122 char new_buf[] = new char[new_capacity]; 123 System.arraycopy(put_buf, 0, new_buf, 0, put_length); 124 put_buf = new_buf; 125 put_capacity = new_capacity; 126 } 127 128 put_buf[put_length++] = c; 129 } 130 131 132 133 134 public void sendChar(char c) { 135 137 if (c == 13) { 139 toDTE.putChar(c); toDTE.flush(); 141 142 c = (char) 10; 143 toDTE.putChar(c); toDTE.flush(); 145 146 line.append(c); 147 148 int nchars = line.length(); 149 char [] tmp = send_buf(nchars); 150 line.getChars(0, nchars, tmp, 0); 151 toDCE.sendChars(tmp, 0, nchars); 152 line.delete(0, 99999); 154 } else if (c == 10) { 155 toDTE.putChar((char) 13); toDTE.flush(); 157 158 toDTE.putChar(c); toDTE.flush(); 160 161 line.append(c); 162 163 int nchars = line.length(); 164 char [] tmp = send_buf(nchars); 165 line.getChars(0, nchars, tmp, 0); 166 toDCE.sendChars(tmp, 0, nchars); 167 line.delete(0, 99999); 169 } else if (c == 8) { 170 int nchars = line.length(); 172 173 if (nchars == 0) 174 return; 176 char erased_char = ' '; try { 178 erased_char = line.charAt(nchars-1); 179 } catch (Exception x) { 180 return; } 182 int cwidth = getTerm().charWidth(erased_char); 183 184 line.delete(nchars-1, nchars); 186 187 209 while(--cwidth > 0 ) 210 line.append(' '); 211 212 toDTE.putChars(bs_sequence, 0, 3); 214 toDTE.flush(); 215 216 } else { 217 toDTE.putChar(c); toDTE.flush(); 219 line.append(c); 220 } 221 } 222 223 public void sendChars(char c[], int offset, int count) { 224 for (int cx = 0; cx < count; cx++) 225 sendChar(c[offset+cx]); 226 } 227 } 228 | Popular Tags |