import java.io.IOException;
import java.io.OutputStream;
public class CharTerminatedOutputStream extends OutputStream {
private OutputStream out;
private byte[] match;
public CharTerminatedOutputStream(OutputStream os, byte[] terminator) {
if (terminator == null) {
throw new IllegalArgumentException("The terminating character array cannot be null.");
}
if (terminator.length == 0) {
throw new IllegalArgumentException("The terminating character array cannot be of zero length.");
}
match = new byte[terminator.length];
for (int i = 0; i < terminator.length; i++) {
match[i] = terminator[i];
}
this.out = os;
}
public void write(int b) throws IOException {
out.write(b);
}
public void flush() throws IOException {
out.write(match);
out.flush();
}
}
此博客展示了Java代码,定义了一个名为CharTerminatedOutputStream的类,继承自OutputStream。该类构造函数对终止字符数组进行非空和非零长度检查,还实现了write和flush方法,在flush时会写入终止字符数组并刷新输出流。

被折叠的 条评论
为什么被折叠?



