KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > CRLFOutputStream


1 /*
2  * @(#)CRLFOutputStream.java 1.3 01/05/23
3  *
4  * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * - Redistribution in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * Neither the name of Sun Microsystems, Inc. or the names of contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * This software is provided "AS IS," without a warranty of any kind. ALL
22  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
25  * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
26  * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
27  * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
28  * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
29  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
30  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
31  * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
32  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33  *
34  * You acknowledge that Software is not designed, licensed or intended
35  * for use in the design, construction, operation or maintenance of any
36  * nuclear facility.
37  */

38
39 import java.io.*;
40
41 /**
42  * Convert lines into the canonical MIME format, that is,
43  * terminate lines with CRLF. <p>
44  *
45  * This stream can be used with the Part.writeTo and Message.writeTo
46  * methods to generate the canonical MIME format of the data for the
47  * purpose of (e.g.) sending it via SMTP or computing a digital
48  * signature.
49  */

50 public class CRLFOutputStream extends FilterOutputStream {
51     protected int lastb = -1;
52     protected static byte[] newline;
53     static {
54     newline = new byte[2];
55     newline[0] = (byte)'\r';
56     newline[1] = (byte)'\n';
57     }
58
59     public CRLFOutputStream(OutputStream os) {
60     super(os);
61     }
62
63     public void write(int b) throws IOException {
64     if (b == '\r') {
65         out.write(newline);
66     } else if (b == '\n') {
67         if (lastb != '\r')
68         out.write(newline);
69     } else {
70         out.write(b);
71     }
72     lastb = b;
73     }
74
75     public void write(byte b[]) throws IOException {
76     write(b, 0, b.length);
77     }
78
79     public void write(byte b[], int off, int len) throws IOException {
80     int start = off;
81
82     len += off;
83     for (int i = start; i < len ; i++) {
84         if (b[i] == '\r') {
85         out.write(b, start, i - start);
86         out.write(newline);
87         start = i + 1;
88         } else if (b[i] == '\n') {
89         if (lastb != '\r') {
90             out.write(b, start, i - start);
91             out.write(newline);
92         }
93         start = i + 1;
94         }
95         lastb = b[i];
96     }
97     if ((len - start) > 0)
98         out.write(b, start, len - start);
99     }
100 }
101
Popular Tags