用于判断字符结尾的输出流类

来源:互联网 发布:淘宝网店转让qc41 编辑:程序博客网 时间:2024/05/17 04:38

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();
  }
}