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  
21  
22  package org.apache.james.util;
23  
24  import java.io.OutputStream;
25  import java.io.PrintWriter;
26  import java.io.Writer;
27  
28  /**
29   * Writes to a wrapped Writer class, ensuring that all line separators are '\r\n', regardless
30   * of platform.
31   */
32  public class InternetPrintWriter
33      extends PrintWriter {
34  
35      /**
36       * The line separator to use.
37       */
38      private static String lineSeparator = "\r\n";
39  
40      /**
41       * Whether the Writer autoflushes on line feeds
42       */
43      private final boolean autoFlush;
44  
45      /**
46       * Constructor that takes a writer to wrap.
47       *
48       * @param out the wrapped Writer
49       */
50      public InternetPrintWriter (Writer out) {
51          super (out);
52          autoFlush = false;
53      }
54  
55      /**
56       * Constructor that takes a writer to wrap.
57       *
58       * @param out the wrapped Writer
59       * @param autoFlush whether to flush after each print call
60       */
61      public InternetPrintWriter (Writer out, boolean autoFlush) {
62          super (out, autoFlush);
63          this.autoFlush = autoFlush;
64      }
65  
66      /**
67       * Constructor that takes a stream to wrap.
68       *
69       * @param out the wrapped OutputStream
70       */
71      public InternetPrintWriter (OutputStream out) {
72          super (out);
73          autoFlush = false;
74      }
75  
76      /**
77       * Constructor that takes a stream to wrap.
78       *
79       * @param out the wrapped OutputStream
80       * @param autoFlush whether to flush after each print call
81       */
82      public InternetPrintWriter (OutputStream out, boolean autoFlush) {
83          super (out, autoFlush);
84          this.autoFlush = autoFlush;
85      }
86  
87      /**
88       * Print a line separator.
89       */
90      public void println () {
91          synchronized (lock) {
92              write(lineSeparator);
93              if (autoFlush) {
94                  flush();
95              }
96          }
97      }
98  
99      /**
100      * Print a boolean followed by a line separator.
101      *
102      * @param x the boolean to print
103      */
104     public void println(boolean x) {
105         synchronized (lock) {
106             print(x);
107             println();
108         }
109     }
110 
111     /**
112      * Print a char followed by a line separator.
113      *
114      * @param x the char to print
115      */
116     public void println(char x) {
117         synchronized (lock) {
118             print (x);
119             println ();
120         }
121     }
122 
123     /**
124      * Print a int followed by a line separator.
125      *
126      * @param x the int to print
127      */
128     public void println (int x) {
129         synchronized (lock) {
130             print (x);
131             println ();
132         }
133     }
134 
135     /**
136      * Print a long followed by a line separator.
137      *
138      * @param x the long to print
139      */
140     public void println (long x) {
141         synchronized (lock) {
142             print (x);
143             println ();
144         }
145     }
146 
147     /**
148      * Print a float followed by a line separator.
149      *
150      * @param x the float to print
151      */
152     public void println (float x) {
153         synchronized (lock) {
154             print (x);
155             println ();
156         }
157     }
158 
159     /**
160      * Print a double followed by a line separator.
161      *
162      * @param x the double to print
163      */
164     public void println (double x) {
165         synchronized (lock) {
166             print (x);
167             println ();
168         }
169     }
170 
171     /**
172      * Print a character array followed by a line separator.
173      *
174      * @param x the character array to print
175      */
176     public void println (char[] x) {
177         synchronized (lock) {
178             print (x);
179             println ();
180         }
181     }
182 
183     /**
184      * Print a String followed by a line separator.
185      *
186      * @param x the String to print
187      */
188     public void println (String x) {
189         synchronized (lock) {
190             print (x);
191             println ();
192         }
193     }
194 
195     /**
196      * Print an Object followed by a line separator.
197      *
198      * @param x the Object to print
199      */
200     public void println (Object x) {
201         synchronized (lock) {
202             print (x);
203             println ();
204         }
205     }
206 }