package survey.utils;

import java.io.OutputStream;
import java.io.EOFException;

public class OutputStreamStringBuffer extends OutputStream
{
	int maxLen;
	int len = 0;
	StringBuffer s = new StringBuffer();
	
	public OutputStreamStringBuffer(int maxLen) { this.maxLen = maxLen; }
	
	public void close() {}
	public void flush() {}
	
	public void write(byte[] b) throws EOFException
	{
		if (len + b.length > maxLen) throw new EOFException("OutputStreamStringBuffer max length exceeded");
		s.append(new String(b));
		len += b.length;
	}
	
	public void write(byte[] b, int off, int len) throws EOFException
	{
		if (this.len + len > maxLen) throw new EOFException("OutputStreamStringBuffer max length exceeded");
		s.append(new String(b, off, len));
		this.len += len;
	}
	
	public void write(int b) throws EOFException
	{
		if (len + 1 > maxLen) throw new EOFException("OutputStreamStringBuffer max length exceeded");
		s.append((char) b);
		len++;
	}

	public String toString() { return s.toString(); }
};


