01: package org.bouncycastle.mail.smime.util;
02:
03: import java.io.FilterOutputStream;
04: import java.io.IOException;
05: import java.io.OutputStream;
06:
07: public class CRLFOutputStream extends FilterOutputStream {
08: protected int lastb;
09: protected static byte newline[];
10:
11: public CRLFOutputStream(OutputStream outputstream) {
12: super (outputstream);
13: lastb = -1;
14: }
15:
16: public void write(int i) throws IOException {
17: if (i == '\r') {
18: out.write(newline);
19: } else if (i == '\n') {
20: if (lastb != '\r') {
21: out.write(newline);
22: }
23: } else {
24: out.write(i);
25: }
26:
27: lastb = i;
28: }
29:
30: public void write(byte[] buf) throws IOException {
31: this .write(buf, 0, buf.length);
32: }
33:
34: public void write(byte buf[], int off, int len) throws IOException {
35: for (int i = off; i != off + len; i++) {
36: this .write(buf[i]);
37: }
38: }
39:
40: public void writeln() throws IOException {
41: super .out.write(newline);
42: }
43:
44: static {
45: newline = new byte[2];
46: newline[0] = '\r';
47: newline[1] = '\n';
48: }
49: }
|