Code example for PrintStream

Methods: checkError, print

0
 
  private PrintStream stream;
  private boolean isFirst = true;
     
  private void throwExceptionOnError(String tag) throws IOException {
    if (stream.checkError()) {
      throw new IOException("Error serializing "+tag);
    } 
  } 
  
  private void printCommaUnlessFirst() { 
    if (!isFirst) {
      stream.print(",");
    } 
    isFirst = false;
  } 
     
  /** Creates a new instance of CsvRecordOutput */ 
  public CsvRecordOutput(OutputStream out) {
    try { 
      stream = new PrintStream(out, true, "UTF-8");
    } catch (UnsupportedEncodingException ex) {
      throw new RuntimeException(ex);
    } 
  } 
     
  public void writeByte(byte b, String tag) throws IOException {
    writeLong((long)b, tag);
  } 
     
  public void writeBool(boolean b, String tag) throws IOException {
    printCommaUnlessFirst(); 
    String val = b ? "T" : "F";
    stream.print(val);
    throwExceptionOnError(tag);
  } 
     
  public void writeInt(int i, String tag) throws IOException {
    writeLong((long)i, tag);
  } 
     
  public void writeLong(long l, String tag) throws IOException {
    printCommaUnlessFirst(); 
    stream.print(l);
    throwExceptionOnError(tag);
  } 
     
  public void writeFloat(float f, String tag) throws IOException {
    writeDouble((double)f, tag);
  } 
     
  public void writeDouble(double d, String tag) throws IOException {
    printCommaUnlessFirst(); 
    stream.print(d);
    throwExceptionOnError(tag);
  } 
     
  public void writeString(String s, String tag) throws IOException {
    printCommaUnlessFirst();