KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > NewlineOutputStream


1 /*
2  * @(#)NewlineOutputStream.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 the various newline conventions to the local platform's
43  * newline convention. <p>
44  *
45  * This stream can be used with the Message.writeTo method to
46  * generate a message that uses the local plaform's line terminator
47  * for the purpose of (e.g.) saving the message to a local file.
48  */

49 public class NewlineOutputStream extends FilterOutputStream {
50     private int lastb = -1;
51     private static byte[] newline;
52
53     public NewlineOutputStream(OutputStream os) {
54     super(os);
55     if (newline == null) {
56         String JavaDoc s = System.getProperty("line.separator");
57         if (s == null || s.length() <= 0)
58         s = "\n";
59         newline = new byte[s.length()];
60         s.getBytes(0, s.length(), newline, 0);
61     }
62     }
63
64     public void write(int b) throws IOException {
65     if (b == '\r') {
66         out.write(newline);
67     } else if (b == '\n') {
68         if (lastb != '\r')
69         out.write(newline);
70     } else {
71         out.write(b);
72     }
73     lastb = b;
74     }
75
76     public void write(byte b[]) throws IOException {
77     write(b, 0, b.length);
78     }
79
80     public void write(byte b[], int off, int len) throws IOException {
81     for (int i = 0 ; i < len ; i++) {
82         write(b[off + i]);
83     }
84     }
85 }
86
Popular Tags