KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > console > IOConsoleInputStream


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.console;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.UnsupportedEncodingException JavaDoc;
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.graphics.Color;
19
20 /**
21  * InputStream used to read input from an IOConsole.
22  * This stream will buffer input that it receives until it has been read.
23  * <p>
24  * Clients are not intended to instantiate this class directly, instead
25  * use <code>IOConsole.getInputStream()</code>. Clients are not intended
26  * to subclass this class.
27  * </p>
28  * @since 3.1
29  *
30  */

31 public class IOConsoleInputStream extends InputStream JavaDoc {
32     /**
33      * Buffer to hold data from console until it is read.
34      */

35     private byte[] input = new byte[100];
36     
37     /**
38      * Location in the buffer that the next byte of data from the
39      * console should be stored.
40      */

41     private int inPointer = 0;
42     
43     /**
44      * Location in the buffer that the next byte of data read from
45      * this stream should come from.
46      */

47     private int outPointer = 0;
48     
49     /**
50      * The number of bytes of real data currently in the buffer.
51      */

52     private int size = 0;
53     
54     /**
55      * Flag to indicate that EOF has been sent already.
56      */

57     private boolean eofSent = false;
58     
59     /**
60      * Flag to indicate that the stream has been closed.
61      */

62     private boolean closed = false;
63     
64     /**
65      * The console that this stream is connected to.
66      */

67     private IOConsole console;
68     
69     /**
70      * The color used to display input in the console.
71      */

72     private Color color;
73     
74     /**
75      * The font style used to decorate input in the console.
76      */

77     private int fontStyle = SWT.NORMAL;
78
79
80     /**
81      * Constructs a new input stream on the given console.
82      *
83      * @param console I/O console
84      */

85     IOConsoleInputStream(IOConsole console) {
86         this.console = console;
87     }
88     
89     /*
90      * (non-Javadoc)
91      * @see java.io.InputStream#read(byte[], int, int)
92      */

93     public synchronized int read(byte[] b, int off, int len) throws IOException JavaDoc {
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     /*
115      * (non-Javadoc)
116      * @see java.io.InputStream#read(byte[])
117      */

118     public int read(byte[] b) throws IOException JavaDoc {
119         return read(b, 0, b.length);
120     }
121
122     /*
123      * (non-Javadoc)
124      * @see java.io.InputStream#read()
125      */

126     public synchronized int read() throws IOException JavaDoc {
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     /**
142      * Blocks until data is available to be read.
143      * Ensure that the monitor for this object is obtained before
144      * calling this method.
145      */

146     private void waitForData() {
147         while (size == 0 && !closed) {
148             try {
149                 wait();
150             } catch (InterruptedException JavaDoc e) {
151             }
152         }
153     }
154
155     /**
156      * Appends text to this input stream's buffer.
157      *
158      * @param text the text to append to the buffer.
159      */

160     public synchronized void appendData(String JavaDoc text) {
161         String JavaDoc encoding = console.getEncoding();
162         byte[] newData;
163         if (encoding!=null)
164             try {
165                 newData = text.getBytes(encoding);
166             } catch (UnsupportedEncodingException JavaDoc 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) { //inPointer == outPointer
177
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     /**
199      * Enlarges the buffer.
200      */

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     /**
216      * Returns this stream's font style.
217      *
218      * @return the font style used to decorate input in the associated console
219      */

220     public int getFontStyle() {
221         return fontStyle;
222     }
223
224     /**
225      * Sets this stream's font style.
226      *
227      * @param newFontStyle the font style to be used to decorate input in the associated console
228      */

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 JavaDoc(old), new Integer JavaDoc(fontStyle));
234         }
235     }
236     
237     /**
238      * Sets the color to used to decorate input in the associated console.
239      *
240      * @param newColor the color to used to decorate input in the associated console.
241      */

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     /**
251      * Returns the color used to decorate input in the associated console
252      *
253      * @return the color used to decorate input in the associated console
254      */

255     public Color getColor() {
256         return color;
257     }
258     
259     /* (non-Javadoc)
260      * @see java.io.InputStream#available()
261      */

262     public int available() throws IOException JavaDoc {
263         if (closed && eofSent) {
264             throw new IOException JavaDoc("Input Stream Closed"); //$NON-NLS-1$
265
} else if (size == 0) {
266             if (!eofSent) {
267                 eofSent = true;
268                 return -1;
269             }
270             throw new IOException JavaDoc("Input Stream Closed"); //$NON-NLS-1$
271
}
272         
273         return size;
274     }
275     
276     /* (non-Javadoc)
277      * @see java.io.InputStream#close()
278      */

279     public synchronized void close() throws IOException JavaDoc {
280         if(closed) {
281             throw new IOException JavaDoc("Input Stream Closed"); //$NON-NLS-1$
282
}
283         closed = true;
284         notifyAll();
285         console.streamClosed(this);
286     }
287 }
288
Popular Tags