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