001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.util;
019:
020: import java.io.OutputStream;
021: import java.io.PrintWriter;
022: import java.io.Writer;
023:
024: /**
025: * Writes to a wrapped Writer class, ensuring that all line separators are '\r\n', regardless
026: * of platform.
027: */
028: public class InternetPrintWriter extends PrintWriter {
029:
030: /**
031: * The line separator to use.
032: */
033: private static String lineSeparator = "\r\n";
034:
035: /**
036: * Whether the Writer autoflushes on line feeds
037: */
038: private final boolean autoFlush;
039:
040: /**
041: * Constructor that takes a writer to wrap.
042: *
043: * @param out the wrapped Writer
044: */
045: public InternetPrintWriter(Writer out) {
046: super (out);
047: autoFlush = false;
048: }
049:
050: /**
051: * Constructor that takes a writer to wrap.
052: *
053: * @param out the wrapped Writer
054: * @param autoFlush whether to flush after each print call
055: */
056: public InternetPrintWriter(Writer out, boolean autoFlush) {
057: super (out, autoFlush);
058: this .autoFlush = autoFlush;
059: }
060:
061: /**
062: * Constructor that takes a stream to wrap.
063: *
064: * @param out the wrapped OutputStream
065: */
066: public InternetPrintWriter(OutputStream out) {
067: super (out);
068: autoFlush = false;
069: }
070:
071: /**
072: * Constructor that takes a stream to wrap.
073: *
074: * @param out the wrapped OutputStream
075: * @param autoFlush whether to flush after each print call
076: */
077: public InternetPrintWriter(OutputStream out, boolean autoFlush) {
078: super (out, autoFlush);
079: this .autoFlush = autoFlush;
080: }
081:
082: /**
083: * Print a line separator.
084: */
085: public void println() {
086: synchronized (lock) {
087: write(lineSeparator);
088: if (autoFlush) {
089: flush();
090: }
091: }
092: }
093:
094: /**
095: * Print a boolean followed by a line separator.
096: *
097: * @param x the boolean to print
098: */
099: public void println(boolean x) {
100: synchronized (lock) {
101: print(x);
102: println();
103: }
104: }
105:
106: /**
107: * Print a char followed by a line separator.
108: *
109: * @param x the char to print
110: */
111: public void println(char x) {
112: synchronized (lock) {
113: print(x);
114: println();
115: }
116: }
117:
118: /**
119: * Print a int followed by a line separator.
120: *
121: * @param x the int to print
122: */
123: public void println(int x) {
124: synchronized (lock) {
125: print(x);
126: println();
127: }
128: }
129:
130: /**
131: * Print a long followed by a line separator.
132: *
133: * @param x the long to print
134: */
135: public void println(long x) {
136: synchronized (lock) {
137: print(x);
138: println();
139: }
140: }
141:
142: /**
143: * Print a float followed by a line separator.
144: *
145: * @param x the float to print
146: */
147: public void println(float x) {
148: synchronized (lock) {
149: print(x);
150: println();
151: }
152: }
153:
154: /**
155: * Print a double followed by a line separator.
156: *
157: * @param x the double to print
158: */
159: public void println(double x) {
160: synchronized (lock) {
161: print(x);
162: println();
163: }
164: }
165:
166: /**
167: * Print a character array followed by a line separator.
168: *
169: * @param x the character array to print
170: */
171: public void println(char[] x) {
172: synchronized (lock) {
173: print(x);
174: println();
175: }
176: }
177:
178: /**
179: * Print a String followed by a line separator.
180: *
181: * @param x the String to print
182: */
183: public void println(String x) {
184: synchronized (lock) {
185: print(x);
186: println();
187: }
188: }
189:
190: /**
191: * Print an Object followed by a line separator.
192: *
193: * @param x the Object to print
194: */
195: public void println(Object x) {
196: synchronized (lock) {
197: print(x);
198: println();
199: }
200: }
201: }
|