1 21 22 27 28 package com.sun.mail.util; 29 30 import java.io.*; 31 32 40 41 public class TraceOutputStream extends FilterOutputStream { 42 private boolean trace = false; 43 private boolean quote = false; 44 private OutputStream traceOut; 45 46 53 public TraceOutputStream(OutputStream out, OutputStream traceOut) { 54 super(out); 55 this.traceOut = traceOut; 56 } 57 58 61 public void setTrace(boolean trace) { 62 this.trace = trace; 63 } 64 65 69 public void setQuote(boolean quote) { 70 this.quote = quote; 71 } 72 73 78 public void write(int b) throws IOException { 79 if (trace) { 80 if (quote) 81 writeByte(b); 82 else 83 traceOut.write(b); 84 } 85 out.write(b); 86 } 87 88 93 public void write(byte b[], int off, int len) throws IOException { 94 if (trace) { 95 if (quote) { 96 for (int i = 0; i < len; i++) 97 writeByte(b[off + i]); 98 } else 99 traceOut.write(b, off, len); 100 } 101 out.write(b, off, len); 102 } 103 104 107 private final void writeByte(int b) throws IOException { 108 b &= 0xff; 109 if (b > 0x7f) { 110 traceOut.write('M'); 111 traceOut.write('-'); 112 b &= 0x7f; 113 } 114 if (b == '\r') { 115 traceOut.write('\\'); 116 traceOut.write('r'); 117 } else if (b == '\n') { 118 traceOut.write('\\'); 119 traceOut.write('n'); 120 traceOut.write('\n'); 121 } else if (b == '\t') { 122 traceOut.write('\\'); 123 traceOut.write('t'); 124 } else if (b < ' ') { 125 traceOut.write('^'); 126 traceOut.write('@' + b); 127 } else { 128 traceOut.write(b); 129 } 130 } 131 } 132 | Popular Tags |