View Javadoc

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