1 11 package org.eclipse.ui.console; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.io.UnsupportedEncodingException ; 16 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.graphics.Color; 19 20 31 public class IOConsoleInputStream extends InputStream { 32 35 private byte[] input = new byte[100]; 36 37 41 private int inPointer = 0; 42 43 47 private int outPointer = 0; 48 49 52 private int size = 0; 53 54 57 private boolean eofSent = false; 58 59 62 private boolean closed = false; 63 64 67 private IOConsole console; 68 69 72 private Color color; 73 74 77 private int fontStyle = SWT.NORMAL; 78 79 80 85 IOConsoleInputStream(IOConsole console) { 86 this.console = console; 87 } 88 89 93 public synchronized int read(byte[] b, int off, int len) throws IOException { 94 waitForData(); 95 if (available() == -1) { 96 return -1; 97 } 98 99 int toCopy = Math.min(len, size); 100 if(input.length-outPointer > toCopy) { 101 System.arraycopy(input, outPointer, b, off, toCopy); 102 outPointer += toCopy; 103 size -= toCopy; 104 } else { 105 int bytesToEnd = input.length-outPointer; 106 System.arraycopy(input, outPointer, b, off, bytesToEnd); 107 System.arraycopy(input, 0, b, off+bytesToEnd, toCopy-bytesToEnd); 108 outPointer = toCopy-bytesToEnd; 109 size -=toCopy; 110 } 111 return toCopy; 112 } 113 114 118 public int read(byte[] b) throws IOException { 119 return read(b, 0, b.length); 120 } 121 122 126 public synchronized int read() throws IOException { 127 waitForData(); 128 if (available() == -1) { 129 return -1; 130 } 131 132 byte b = input[outPointer]; 133 outPointer++; 134 if (outPointer == input.length) { 135 outPointer = 0; 136 } 137 size -= 1; 138 return b; 139 } 140 141 146 private void waitForData() { 147 while (size == 0 && !closed) { 148 try { 149 wait(); 150 } catch (InterruptedException e) { 151 } 152 } 153 } 154 155 160 public synchronized void appendData(String text) { 161 String encoding = console.getEncoding(); 162 byte[] newData; 163 if (encoding!=null) 164 try { 165 newData = text.getBytes(encoding); 166 } catch (UnsupportedEncodingException e) { 167 newData = text.getBytes(); 168 } 169 else 170 newData = text.getBytes(); 171 172 while(input.length-size < newData.length) { 173 growArray(); 174 } 175 176 if (size == 0) { System.arraycopy(newData, 0, input, 0, newData.length); 178 inPointer = newData.length; 179 size = newData.length; 180 outPointer = 0; 181 } else if (inPointer < outPointer || input.length - inPointer > newData.length) { 182 System.arraycopy(newData, 0, input, inPointer, newData.length); 183 inPointer += newData.length; 184 size += newData.length; 185 } else { 186 System.arraycopy(newData, 0, input, inPointer, input.length-inPointer); 187 System.arraycopy(newData, input.length-inPointer, input, 0, newData.length-(input.length-inPointer)); 188 inPointer = newData.length-(input.length-inPointer); 189 size += newData.length; 190 } 191 192 if (inPointer == input.length) { 193 inPointer = 0; 194 } 195 notifyAll(); 196 } 197 198 201 private void growArray() { 202 byte[] newInput = new byte[input.length+1024]; 203 if (outPointer < inPointer) { 204 System.arraycopy(input, outPointer, newInput, 0, size); 205 } else { 206 System.arraycopy(input, outPointer, newInput, 0, input.length-outPointer); 207 System.arraycopy(input, 0, newInput, input.length-outPointer, inPointer); 208 } 209 outPointer = 0; 210 inPointer = size; 211 input = newInput; 212 newInput = null; 213 } 214 215 220 public int getFontStyle() { 221 return fontStyle; 222 } 223 224 229 public void setFontStyle(int newFontStyle) { 230 if (newFontStyle != fontStyle) { 231 int old = fontStyle; 232 fontStyle = newFontStyle; 233 console.firePropertyChange(this, IConsoleConstants.P_FONT_STYLE, new Integer (old), new Integer (fontStyle)); 234 } 235 } 236 237 242 public void setColor(Color newColor) { 243 Color old = color; 244 if (old == null || !old.equals(newColor)) { 245 color = newColor; 246 console.firePropertyChange(this, IConsoleConstants.P_STREAM_COLOR, old, newColor); 247 } 248 } 249 250 255 public Color getColor() { 256 return color; 257 } 258 259 262 public int available() throws IOException { 263 if (closed && eofSent) { 264 throw new IOException ("Input Stream Closed"); } else if (size == 0) { 266 if (!eofSent) { 267 eofSent = true; 268 return -1; 269 } 270 throw new IOException ("Input Stream Closed"); } 272 273 return size; 274 } 275 276 279 public synchronized void close() throws IOException { 280 if(closed) { 281 throw new IOException ("Input Stream Closed"); } 283 closed = true; 284 notifyAll(); 285 console.streamClosed(this); 286 } 287 } 288 | Popular Tags |