View Javadoc

1   /************************************************************************
2    * Copyright (c) 2000-2006 The Apache Software Foundation.             *
3    * All rights reserved.                                                *
4    * ------------------------------------------------------------------- *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you *
6    * may not use this file except in compliance with the License. You    *
7    * may obtain a copy of the License at:                                *
8    *                                                                     *
9    *     http://www.apache.org/licenses/LICENSE-2.0                      *
10   *                                                                     *
11   * Unless required by applicable law or agreed to in writing, software *
12   * distributed under the License is distributed on an "AS IS" BASIS,   *
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     *
14   * implied.  See the License for the specific language governing       *
15   * permissions and limitations under the License.                      *
16   ***********************************************************************/
17  
18  package org.apache.james.util;
19  
20  import java.io.OutputStream;
21  import java.io.PrintWriter;
22  import java.io.Writer;
23  
24  /***
25   * Writes to a wrapped Writer class, ensuring that all line separators are '\r\n', regardless
26   * of platform.
27   */
28  public class InternetPrintWriter
29      extends PrintWriter {
30  
31      /***
32       * The line separator to use.
33       */
34      private static String lineSeparator = "\r\n";
35  
36      /***
37       * Whether the Writer autoflushes on line feeds
38       */
39      private final boolean autoFlush;
40  
41      /***
42       * Constructor that takes a writer to wrap.
43       *
44       * @param out the wrapped Writer
45       */
46      public InternetPrintWriter (Writer out) {
47          super (out);
48          autoFlush = false;
49      }
50  
51      /***
52       * Constructor that takes a writer to wrap.
53       *
54       * @param out the wrapped Writer
55       * @param autoFlush whether to flush after each print call
56       */
57      public InternetPrintWriter (Writer out, boolean autoFlush) {
58          super (out, autoFlush);
59          this.autoFlush = autoFlush;
60      }
61  
62      /***
63       * Constructor that takes a stream to wrap.
64       *
65       * @param out the wrapped OutputStream
66       */
67      public InternetPrintWriter (OutputStream out) {
68          super (out);
69          autoFlush = false;
70      }
71  
72      /***
73       * Constructor that takes a stream to wrap.
74       *
75       * @param out the wrapped OutputStream
76       * @param autoFlush whether to flush after each print call
77       */
78      public InternetPrintWriter (OutputStream out, boolean autoFlush) {
79          super (out, autoFlush);
80          this.autoFlush = autoFlush;
81      }
82  
83      /***
84       * Print a line separator.
85       */
86      public void println () {
87          synchronized (lock) {
88              write(lineSeparator);
89              if (autoFlush) {
90                  flush();
91              }
92          }
93      }
94  
95      /***
96       * Print a boolean followed by a line separator.
97       *
98       * @param x the boolean to print
99       */
100     public void println(boolean x) {
101         synchronized (lock) {
102             print(x);
103             println();
104         }
105     }
106 
107     /***
108      * Print a char followed by a line separator.
109      *
110      * @param x the char to print
111      */
112     public void println(char x) {
113         synchronized (lock) {
114             print (x);
115             println ();
116         }
117     }
118 
119     /***
120      * Print a int followed by a line separator.
121      *
122      * @param x the int to print
123      */
124     public void println (int x) {
125         synchronized (lock) {
126             print (x);
127             println ();
128         }
129     }
130 
131     /***
132      * Print a long followed by a line separator.
133      *
134      * @param x the long to print
135      */
136     public void println (long x) {
137         synchronized (lock) {
138             print (x);
139             println ();
140         }
141     }
142 
143     /***
144      * Print a float followed by a line separator.
145      *
146      * @param x the float to print
147      */
148     public void println (float x) {
149         synchronized (lock) {
150             print (x);
151             println ();
152         }
153     }
154 
155     /***
156      * Print a double followed by a line separator.
157      *
158      * @param x the double to print
159      */
160     public void println (double x) {
161         synchronized (lock) {
162             print (x);
163             println ();
164         }
165     }
166 
167     /***
168      * Print a character array followed by a line separator.
169      *
170      * @param x the character array to print
171      */
172     public void println (char[] x) {
173         synchronized (lock) {
174             print (x);
175             println ();
176         }
177     }
178 
179     /***
180      * Print a String followed by a line separator.
181      *
182      * @param x the String to print
183      */
184     public void println (String x) {
185         synchronized (lock) {
186             print (x);
187             println ();
188         }
189     }
190 
191     /***
192      * Print an Object followed by a line separator.
193      *
194      * @param x the Object to print
195      */
196     public void println (Object x) {
197         synchronized (lock) {
198             print (x);
199             println ();
200         }
201     }
202 }