KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > util > InputStreamRedirector


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2006 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.util;
35
36 import java.io.*;
37 import java.util.ArrayList JavaDoc;
38
39 /** Redirects requests for input through the abstract method _getInput().
40   * @version $Id: InputStreamRedirector.java 4031 2006-11-15 22:09:06Z rcartwright $
41   */

42 public abstract class InputStreamRedirector extends InputStream {
43   /** Buffer that stores the current set of bytes.
44     * TODO: perhaps this should use an array for efficiency
45     * This is only used as a char queue.
46     */

47   protected ArrayList JavaDoc<Character JavaDoc> _buffer;
48
49   /** Constructs a new InputStreamRedirector. */
50   public InputStreamRedirector() { _buffer = new ArrayList JavaDoc<Character JavaDoc>(60); }
51
52   /** This method gets called whenever input is requested from the stream and
53     * nothing is currently available. Subclasses should return the appropriate
54     * input to feed to the input stream. When using a readLine() method, be sure
55     * to append a newline to the end of the input.
56     * @return the input to the stream, not the empty string
57     */

58   protected abstract String JavaDoc _getInput() throws IOException;
59
60   /** Reads a single "line" of input into the buffer, i.e. makes a single call
61     * to _getInput() and puts the result into the buffer.
62     * @throws IOException if _getInput() returns the empty string
63     */

64   private void _readInputIntoBuffer() throws IOException {
65     String JavaDoc input = _getInput();
66     if (input.equals("")) throw new IOException("_getInput() must return non-empty input!");
67
68     for(int i = 0; i < input.length(); i++) {
69       _buffer.add(new Character JavaDoc(input.charAt(i)));
70     }
71   }
72
73   /** Tries to fill b with bytes from the user, prompting for input only if the stream is already empty.
74     * @param b the byte array to fill
75     * @return the number of bytes successfully read
76     */

77   public synchronized int read(byte[] b) throws IOException { return read(b, 0, b.length); }
78
79   /** Tries to fill b with bytes from the user, prompting for input only if the stream is already empty.
80     * @param b the byte array to fill
81     * @param off the offset in the byte array
82     * @param len the number of characters to try to read
83     * @return the number of bytes successfully read
84     */

85   public synchronized int read(byte[] b, int off, int len) throws IOException {
86     int numRead = 0;
87     if (available() == 0) _readInputIntoBuffer();
88
89     for(int i = off; i < off + len; i++) {
90       if (available() == 0) break;
91       else {
92         b[i] = (byte) _buffer.remove(0).charValue();
93         numRead++;
94       }
95     }
96     return numRead;
97   }
98
99   /** Overrides the read() in PipedInputStream so that if the stream is empty, it asks for more input from _getInput().
100     * @return the next character in the stream
101     * @throws IOException if an I/O exception
102     */

103   public synchronized int read() throws IOException {
104     if (available() == 0) _readInputIntoBuffer();
105     return _buffer.remove(0).charValue();
106   }
107
108   /** @return the number of characters available in this stream. */
109   public int available() { return _buffer.size(); }
110 }
111
112
Popular Tags