All Downloads are FREE. Search and download functionalities are using the official Maven repository.

aQute.junit.Tee Maven / Gradle / Ivy

There is a newer version: 7.0.0
Show newest version
package aQute.junit;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

public class Tee extends OutputStream {
	private final PrintStream			wrapped;
	private final ByteArrayOutputStream	buffer	= new ByteArrayOutputStream();
	private volatile boolean			capture;
	private volatile boolean			echo;

	public Tee(PrintStream toWrap) {
		wrapped = toWrap;
	}

	public PrintStream getStream() {
		return new PrintStream(this);
	}

	@Override
	public void write(int b) throws IOException {
		if (capture)
			buffer.write(b);
		if (echo)
			wrapped.write(b);
	}

	@Override
	public void write(byte b[], int off, int len) throws IOException {
		if (capture)
			buffer.write(b, off, len);
		if (echo)
			wrapped.write(b, off, len);
	}

	public String getContent() {
		if (buffer.size() == 0)
			return null;
		try {
			return buffer.toString(Charset.defaultCharset()
				.name());
		} catch (UnsupportedEncodingException e) {
			return null;
		}
	}

	public Tee clear() {
		buffer.reset();
		return this;
	}

	public Tee capture(boolean capture) {
		this.capture = capture;
		return this;
	}

	public Tee echo(boolean echo) {
		this.echo = echo;
		return this;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy