ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Zsolt" <zkoppanyl...@intland.com>
Subject Need a better solution to redirect PumpStreamHandler.outputStream to a BufferedReader
Date Thu, 24 Aug 2006 06:19:19 GMT
Hi,

I have written this class to redirect PumpStreamHandler.outputStream to a
Reader and splitting the stream into lines. I don't like my solution very
much. Any ideas for a better implementation?

outputStream = new OutputStreamConsumer(consumer, this.getCharsetName());
PumpStreamHandler handler = new PumpStreamHandler(outputStream, errorStream,
inputStream);

...


public class OutputStreamConsumer extends ByteArrayOutputStream {
	private ScmStreamLineConsumer consumer;
	private String charsetName;
	private StringBuffer buffer = new StringBuffer();

	public OutputStreamConsumer(ScmStreamLineConsumer consumer, String
charsetName) {
		this.consumer = consumer;
		this.charsetName = StringUtils.trimToNull(charsetName);
	}

	public void write(byte b[], int off, int len) {
		if (len > 0) {
			try {
				String string = charsetName != null ? new
String(b, off, len, charsetName) : new String(b, off, len);
				buffer.append(string);

				checkForNewLine();
			} catch (UnsupportedEncodingException ex) {
				throw new RuntimeException(ex);
			}
		}
	}

	public void write(int b) {
		byte[] content = new byte[1];
		content[0] = (byte)b;

		write(content, 0, 1);
	}

	protected void checkForNewLine() {
		for (int off = -1; (off = buffer.indexOf("\n")) != -1; ) {
			String line = null;
			if (off == 0) {
				line = "";
			} else {
				line = buffer.substring(0, off - 1);
			}
			buffer.delete(0, off + 1);

			consumer.consumeLine(line);
		}
	}
}




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message