org.nutz.lang.stream.StringOutputStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nutz Show documentation
Show all versions of nutz Show documentation
Nutz, which is a collections of lightweight frameworks, each of them can be used independently
package org.nutz.lang.stream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.nutz.lang.Encoding;
public class StringOutputStream extends OutputStream {
private StringBuilder sb;
private ByteArrayOutputStream baos;
private String charset;
public StringOutputStream(StringBuilder sb) {
this(sb, Encoding.UTF8);
}
public StringOutputStream(StringBuilder sb, String charset) {
this.sb = sb;
baos = new ByteArrayOutputStream();
this.charset = charset;
}
/**
* 完成本方法后,确认字符串已经完成写入后,务必调用flash方法!
*/
@Override
public void write(int b) throws IOException {
if (null == baos)
throw new IOException("Stream is closed");
baos.write(b);
}
/**
* 使用StringBuilder前,务必调用
*/
@Override
public void flush() throws IOException {
if (null != baos) {
baos.flush();
if (baos.size() > 0) {
if (charset == null)
sb.append(new String(baos.toByteArray()));
else
sb.append(new String(baos.toByteArray(), charset));
baos.reset();
}
}
}
@Override
public void close() throws IOException {
flush();
baos = null;
}
public StringBuilder getStringBuilder() {
return sb;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy