1 19 package gnu.mail.util; 20 21 import java.io.*; 22 23 28 public class CRLFOutputStream 29 extends FilterOutputStream 30 { 31 32 33 public final static int CR = 13; 34 35 36 public final static int LF = 10; 37 38 39 public final static byte[] CRLF = {CR, LF}; 40 41 42 protected int last; 43 44 45 50 public CRLFOutputStream(OutputStream out) 51 { 52 super(out); 53 last = -1; 54 } 55 56 57 63 public void write(int ch) 64 throws IOException 65 { 66 if (ch == CR) { 67 out.write(CRLF); 68 } 69 else if (ch == LF) { 70 if (last != CR) { 71 out.write(CRLF); 72 } 73 } 74 else { 75 out.write(ch); 76 } 77 last = ch; 78 } 79 80 81 87 public void write(byte b[]) 88 throws IOException 89 { 90 write(b, 0, b.length); 91 } 92 93 94 102 public void write(byte b[], int off, int len) 103 throws IOException 104 { 105 int d = off; 106 len += off; 107 for (int i = off; i < len; i++) { 108 switch (b[i]) { 109 case CR: 110 out.write(b, d, i - d); 111 out.write(CRLF, 0, 2); 112 d = i + 1; 113 break; 114 case LF: 115 if (last != CR) { 116 out.write(b, d, i - d); 117 out.write(CRLF, 0, 2); 118 } 119 d = i + 1; 120 break; 121 } 122 last = b[i]; 123 } 124 if (len - d > 0) { 125 out.write(b, d, len - d); 126 } 127 } 128 129 130 136 public void write(String text) 137 throws IOException 138 { 139 byte[] bytes = text.getBytes(); 140 write(bytes, 0, bytes.length); 141 } 142 143 144 149 public void writeln() 150 throws IOException 151 { 152 out.write(CRLF); 153 } 154 155 } 156 157 | Popular Tags |