|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| InputStreamRedirector.java | 78.6% | 90.9% | 83.3% | 85.7% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK | |
| 2 | * | |
| 3 | * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu) | |
| 4 | * All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * * Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * * Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the | |
| 14 | * names of its contributors may be used to endorse or promote products | |
| 15 | * derived from this software without specific prior written permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
| 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| 25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| 26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 28 | * | |
| 29 | * This software is Open Source Initiative approved Open Source Software. | |
| 30 | * Open Source Initative Approved is a trademark of the Open Source Initiative. | |
| 31 | * | |
| 32 | * This file is part of DrJava. Download the current version of this project | |
| 33 | * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ | |
| 34 | * | |
| 35 | * END_COPYRIGHT_BLOCK*/ | |
| 36 | ||
| 37 | package edu.rice.cs.util; | |
| 38 | ||
| 39 | import java.io.*; | |
| 40 | import java.util.ArrayList; | |
| 41 | ||
| 42 | /** Redirects requests for input through the abstract method _getInput(). | |
| 43 | * @version $Id: InputStreamRedirector.java 5443 2011-08-17 04:58:50Z rcartwright $ | |
| 44 | */ | |
| 45 | public abstract class InputStreamRedirector extends InputStream { | |
| 46 | /** Buffer that stores the current set of bytes. | |
| 47 | * TODO: perhaps this should use an array for efficiency | |
| 48 | * This is only used as a char queue. | |
| 49 | */ | |
| 50 | protected volatile ArrayList<Character> _buffer; | |
| 51 | ||
| 52 | /** Constructs a new InputStreamRedirector. */ | |
| 53 | 184 | public InputStreamRedirector() { _buffer = new ArrayList<Character>(60); } |
| 54 | ||
| 55 | /** This method gets called whenever input is requested from the stream and | |
| 56 | * nothing is currently available. Subclasses should return the appropriate | |
| 57 | * input to feed to the input stream. When using a readLine() method, be sure | |
| 58 | * to append a newline to the end of the input. | |
| 59 | * @return the input to the stream, empty string to indicate end of stream | |
| 60 | */ | |
| 61 | protected abstract String _getInput() throws IOException; | |
| 62 | ||
| 63 | /** Reads a single "line" of input into the buffer, i.e. makes a single call | |
| 64 | * to _getInput() and puts the result into the buffer. | |
| 65 | */ | |
| 66 | 9 | private void _readInputIntoBuffer() throws IOException { |
| 67 | 9 | String input = _getInput(); |
| 68 | ||
| 69 | 8 | for(int i = 0; i < input.length(); i++) { |
| 70 | 52 | _buffer.add(new Character(input.charAt(i))); |
| 71 | } | |
| 72 | } | |
| 73 | ||
| 74 | /** Tries to fill b with bytes from the user, prompting for input only if the stream is already empty. | |
| 75 | * @param b the byte array to fill | |
| 76 | * @return the number of bytes successfully read | |
| 77 | */ | |
| 78 | 0 | public synchronized int read(byte[] b) throws IOException { return read(b, 0, b.length); } |
| 79 | ||
| 80 | /** Tries to fill b with bytes from the user, prompting for input only if the stream is already empty. | |
| 81 | * @param b the byte array to fill | |
| 82 | * @param off the offset in the byte array | |
| 83 | * @param len the number of characters to try to read | |
| 84 | * @return the number of bytes successfully read | |
| 85 | */ | |
| 86 | 8 | public synchronized int read(byte[] b, int off, int len) throws IOException { |
| 87 | 8 | int numRead = 0; |
| 88 | 8 | if (available() == 0) { |
| 89 | 7 | _readInputIntoBuffer(); |
| 90 | 0 | if (available() == 0) return -1; |
| 91 | } | |
| 92 | ||
| 93 | 58 | for(int i = off; i < off + len; i++) { |
| 94 | 7 | if (available() == 0) break; |
| 95 | else { | |
| 96 | 51 | b[i] = (byte) _buffer.remove(0).charValue(); |
| 97 | 51 | numRead++; |
| 98 | } | |
| 99 | } | |
| 100 | 7 | return numRead; |
| 101 | } | |
| 102 | ||
| 103 | /** Overrides the read() in PipedInputStream so that if the stream is empty, it asks for more input from _getInput(). | |
| 104 | * @return the next character in the stream | |
| 105 | * @throws IOException if an I/O exception | |
| 106 | */ | |
| 107 | 2 | public synchronized int read() throws IOException { |
| 108 | 2 | if (available() == 0) { |
| 109 | 2 | _readInputIntoBuffer(); |
| 110 | 1 | if (available() == 0) return -1; |
| 111 | } | |
| 112 | 1 | return _buffer.remove(0).charValue(); |
| 113 | } | |
| 114 | ||
| 115 | /** @return the number of characters available in this stream. */ | |
| 116 | 83 | public int available() { return _buffer.size(); } |
| 117 | } | |
| 118 |
|
||||||||||