1 /*
2 * Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
28 import java.util.Formatter;
29 import java.util.Locale;
30 import java.nio.charset.Charset;
31 import java.nio.charset.IllegalCharsetNameException;
32 import java.nio.charset.UnsupportedCharsetException;
33
34 /**
35 * A {@code PrintStream} adds functionality to another output stream,
36 * namely the ability to print representations of various data values
37 * conveniently. Two other features are provided as well. Unlike other output
38 * streams, a {@code PrintStream} never throws an
39 * {@code IOException}; instead, exceptional situations merely set an
40 * internal flag that can be tested via the {@code checkError} method.
41 * Optionally, a {@code PrintStream} can be created so as to flush
42 * automatically; this means that the {@code flush} method is
43 * automatically invoked after a byte array is written, one of the
44 * {@code println} methods is invoked, or a newline character or byte
45 * ({@code '\n'}) is written.
46 *
47 * <p> All characters printed by a {@code PrintStream} are converted into
48 * bytes using the given encoding or charset, or platform's default character
49 * encoding if not specified.
50 * The {@link PrintWriter} class should be used in situations that require
51 * writing characters rather than bytes.
52 *
53 * <p> This class always replaces malformed and unmappable character sequences with
54 * the charset's default replacement string.
55 * The {@linkplain java.nio.charset.CharsetEncoder} class should be used when more
56 * control over the encoding process is required.
57 *
58 * @author Frank Yellin
59 * @author Mark Reinhold
60 * @since 1.0
61 */
62
63 public class PrintStream extends FilterOutputStream
64 implements Appendable, Closeable
65 {
66
67 private final boolean autoFlush;
68 private boolean trouble = false;
69 private Formatter formatter;
104 }
105
106 /* Private constructors */
107 private PrintStream(boolean autoFlush, OutputStream out) {
108 super(out);
109 this.autoFlush = autoFlush;
110 this.charOut = new OutputStreamWriter(this);
111 this.textOut = new BufferedWriter(charOut);
112 }
113
114 /* Variant of the private constructor so that the given charset name
115 * can be verified before evaluating the OutputStream argument. Used
116 * by constructors creating a FileOutputStream that also take a
117 * charset name.
118 */
119 private PrintStream(boolean autoFlush, Charset charset, OutputStream out) {
120 this(out, autoFlush, charset);
121 }
122
123 /**
124 * Creates a new print stream. This stream will not flush automatically.
125 *
126 * @param out The output stream to which values and objects will be
127 * printed
128 *
129 * @see java.io.PrintWriter#PrintWriter(java.io.OutputStream)
130 */
131 public PrintStream(OutputStream out) {
132 this(out, false);
133 }
134
135 /**
136 * Creates a new print stream.
137 *
138 * @param out The output stream to which values and objects will be
139 * printed
140 * @param autoFlush A boolean; if true, the output buffer will be flushed
141 * whenever a byte array is written, one of the
142 * {@code println} methods is invoked, or a newline
143 * character or byte ({@code '\n'}) is written
144 *
145 * @see java.io.PrintWriter#PrintWriter(java.io.OutputStream, boolean)
146 */
147 public PrintStream(OutputStream out, boolean autoFlush) {
148 this(autoFlush, requireNonNull(out, "Null output stream"));
149 }
150
151 /**
152 * Creates a new print stream.
153 *
154 * @param out The output stream to which values and objects will be
155 * printed
156 * @param autoFlush A boolean; if true, the output buffer will be flushed
157 * whenever a byte array is written, one of the
158 * {@code println} methods is invoked, or a newline
159 * character or byte ({@code '\n'}) is written
160 * @param encoding The name of a supported
161 * <a href="../lang/package-summary.html#charenc">
162 * character encoding</a>
163 *
164 * @throws UnsupportedEncodingException
165 * If the named encoding is not supported
166 *
167 * @since 1.4
168 */
169 public PrintStream(OutputStream out, boolean autoFlush, String encoding)
170 throws UnsupportedEncodingException
171 {
172 this(requireNonNull(out, "Null output stream"), autoFlush, toCharset(encoding));
173 }
174
175 /**
176 * Creates a new print stream, with the specified OutputStream, automatic line
177 * flushing and charset. This convenience constructor creates the necessary
178 * intermediate {@link java.io.OutputStreamWriter OutputStreamWriter},
179 * which will encode characters using the provided charset.
180 *
181 * @param out The output stream to which values and objects will be
182 * printed
183 * @param autoFlush A boolean; if true, the output buffer will be flushed
184 * whenever a byte array is written, one of the
185 * {@code println} methods is invoked, or a newline
186 * character or byte ({@code '\n'}) is written
187 * @param charset A {@linkplain java.nio.charset.Charset charset}
188 *
189 * @since 10
190 */
191 public PrintStream(OutputStream out, boolean autoFlush, Charset charset) {
192 super(out);
193 this.autoFlush = autoFlush;
194 this.charOut = new OutputStreamWriter(this, charset);
195 this.textOut = new BufferedWriter(charOut);
196 }
197
198 /**
199 * Creates a new print stream, without automatic line flushing, with the
200 * specified file name. This convenience constructor creates
201 * the necessary intermediate {@link java.io.OutputStreamWriter
202 * OutputStreamWriter}, which will encode characters using the
203 * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}
683 }
684 }
685
686 /* Methods that do not terminate lines */
687
688 /**
689 * Prints a boolean value. The string produced by {@link
690 * java.lang.String#valueOf(boolean)} is translated into bytes
691 * according to the platform's default character encoding, and these bytes
692 * are written in exactly the manner of the
693 * {@link #write(int)} method.
694 *
695 * @param b The {@code boolean} to be printed
696 */
697 public void print(boolean b) {
698 write(String.valueOf(b));
699 }
700
701 /**
702 * Prints a character. The character is translated into one or more bytes
703 * according to the platform's default character encoding, and these bytes
704 * are written in exactly the manner of the
705 * {@link #write(int)} method.
706 *
707 * @param c The {@code char} to be printed
708 */
709 public void print(char c) {
710 write(String.valueOf(c));
711 }
712
713 /**
714 * Prints an integer. The string produced by {@link
715 * java.lang.String#valueOf(int)} is translated into bytes
716 * according to the platform's default character encoding, and these bytes
717 * are written in exactly the manner of the
718 * {@link #write(int)} method.
719 *
720 * @param i The {@code int} to be printed
721 * @see java.lang.Integer#toString(int)
722 */
723 public void print(int i) {
724 write(String.valueOf(i));
725 }
751 public void print(float f) {
752 write(String.valueOf(f));
753 }
754
755 /**
756 * Prints a double-precision floating-point number. The string produced by
757 * {@link java.lang.String#valueOf(double)} is translated into
758 * bytes according to the platform's default character encoding, and these
759 * bytes are written in exactly the manner of the {@link
760 * #write(int)} method.
761 *
762 * @param d The {@code double} to be printed
763 * @see java.lang.Double#toString(double)
764 */
765 public void print(double d) {
766 write(String.valueOf(d));
767 }
768
769 /**
770 * Prints an array of characters. The characters are converted into bytes
771 * according to the platform's default character encoding, and these bytes
772 * are written in exactly the manner of the
773 * {@link #write(int)} method.
774 *
775 * @param s The array of chars to be printed
776 *
777 * @throws NullPointerException If {@code s} is {@code null}
778 */
779 public void print(char s[]) {
780 write(s);
781 }
782
783 /**
784 * Prints a string. If the argument is {@code null} then the string
785 * {@code "null"} is printed. Otherwise, the string's characters are
786 * converted into bytes according to the platform's default character
787 * encoding, and these bytes are written in exactly the manner of the
788 * {@link #write(int)} method.
789 *
790 * @param s The {@code String} to be printed
791 */
792 public void print(String s) {
793 write(String.valueOf(s));
794 }
795
796 /**
797 * Prints an object. The string produced by the {@link
798 * java.lang.String#valueOf(Object)} method is translated into bytes
799 * according to the platform's default character encoding, and these bytes
800 * are written in exactly the manner of the
801 * {@link #write(int)} method.
802 *
803 * @param obj The {@code Object} to be printed
804 * @see java.lang.Object#toString()
805 */
806 public void print(Object obj) {
807 write(String.valueOf(obj));
|
1 /*
2 * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
28 import java.util.Formatter;
29 import java.util.Locale;
30 import java.nio.charset.Charset;
31 import java.nio.charset.IllegalCharsetNameException;
32 import java.nio.charset.UnsupportedCharsetException;
33
34 /**
35 * A {@code PrintStream} adds functionality to another output stream,
36 * namely the ability to print representations of various data values
37 * conveniently. Two other features are provided as well. Unlike other output
38 * streams, a {@code PrintStream} never throws an
39 * {@code IOException}; instead, exceptional situations merely set an
40 * internal flag that can be tested via the {@code checkError} method.
41 * Optionally, a {@code PrintStream} can be created so as to flush
42 * automatically; this means that the {@code flush} method is
43 * automatically invoked after a byte array is written, one of the
44 * {@code println} methods is invoked, or a newline character or byte
45 * ({@code '\n'}) is written.
46 *
47 * <p> All characters printed by a {@code PrintStream} are converted into
48 * bytes using the given encoding or charset, or the platform's default
49 * character encoding if not specified.
50 * The {@link PrintWriter} class should be used in situations that require
51 * writing characters rather than bytes.
52 *
53 * <p> This class always replaces malformed and unmappable character sequences with
54 * the charset's default replacement string.
55 * The {@linkplain java.nio.charset.CharsetEncoder} class should be used when more
56 * control over the encoding process is required.
57 *
58 * @author Frank Yellin
59 * @author Mark Reinhold
60 * @since 1.0
61 */
62
63 public class PrintStream extends FilterOutputStream
64 implements Appendable, Closeable
65 {
66
67 private final boolean autoFlush;
68 private boolean trouble = false;
69 private Formatter formatter;
104 }
105
106 /* Private constructors */
107 private PrintStream(boolean autoFlush, OutputStream out) {
108 super(out);
109 this.autoFlush = autoFlush;
110 this.charOut = new OutputStreamWriter(this);
111 this.textOut = new BufferedWriter(charOut);
112 }
113
114 /* Variant of the private constructor so that the given charset name
115 * can be verified before evaluating the OutputStream argument. Used
116 * by constructors creating a FileOutputStream that also take a
117 * charset name.
118 */
119 private PrintStream(boolean autoFlush, Charset charset, OutputStream out) {
120 this(out, autoFlush, charset);
121 }
122
123 /**
124 * Creates a new print stream, without automatic line flushing, with the
125 * specified OutputStream. Characters written to the stream are converted
126 * to bytes using the platform's default character encoding.
127 *
128 * @param out The output stream to which values and objects will be
129 * printed
130 *
131 * @see java.io.PrintWriter#PrintWriter(java.io.OutputStream)
132 */
133 public PrintStream(OutputStream out) {
134 this(out, false);
135 }
136
137 /**
138 * Creates a new print stream, with the specified OutputStream and line
139 * flushing. Characters written to the stream are converted to bytes using
140 * the platform's default character encoding.
141 *
142 * @param out The output stream to which values and objects will be
143 * printed
144 * @param autoFlush Whether the output buffer will be flushed
145 * whenever a byte array is written, one of the
146 * {@code println} methods is invoked, or a newline
147 * character or byte ({@code '\n'}) is written
148 *
149 * @see java.io.PrintWriter#PrintWriter(java.io.OutputStream, boolean)
150 */
151 public PrintStream(OutputStream out, boolean autoFlush) {
152 this(autoFlush, requireNonNull(out, "Null output stream"));
153 }
154
155 /**
156 * Creates a new print stream, with the specified OutputStream, line
157 * flushing, and character encoding.
158 *
159 * @param out The output stream to which values and objects will be
160 * printed
161 * @param autoFlush Whether the output buffer will be flushed
162 * whenever a byte array is written, one of the
163 * {@code println} methods is invoked, or a newline
164 * character or byte ({@code '\n'}) is written
165 * @param encoding The name of a supported
166 * <a href="../lang/package-summary.html#charenc">
167 * character encoding</a>
168 *
169 * @throws UnsupportedEncodingException
170 * If the named encoding is not supported
171 *
172 * @since 1.4
173 */
174 public PrintStream(OutputStream out, boolean autoFlush, String encoding)
175 throws UnsupportedEncodingException
176 {
177 this(requireNonNull(out, "Null output stream"), autoFlush, toCharset(encoding));
178 }
179
180 /**
181 * Creates a new print stream, with the specified OutputStream, line
182 * flushing and charset. This convenience constructor creates the necessary
183 * intermediate {@link java.io.OutputStreamWriter OutputStreamWriter},
184 * which will encode characters using the provided charset.
185 *
186 * @param out The output stream to which values and objects will be
187 * printed
188 * @param autoFlush Whether the output buffer will be flushed
189 * whenever a byte array is written, one of the
190 * {@code println} methods is invoked, or a newline
191 * character or byte ({@code '\n'}) is written
192 * @param charset A {@linkplain java.nio.charset.Charset charset}
193 *
194 * @since 10
195 */
196 public PrintStream(OutputStream out, boolean autoFlush, Charset charset) {
197 super(out);
198 this.autoFlush = autoFlush;
199 this.charOut = new OutputStreamWriter(this, charset);
200 this.textOut = new BufferedWriter(charOut);
201 }
202
203 /**
204 * Creates a new print stream, without automatic line flushing, with the
205 * specified file name. This convenience constructor creates
206 * the necessary intermediate {@link java.io.OutputStreamWriter
207 * OutputStreamWriter}, which will encode characters using the
208 * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}
688 }
689 }
690
691 /* Methods that do not terminate lines */
692
693 /**
694 * Prints a boolean value. The string produced by {@link
695 * java.lang.String#valueOf(boolean)} is translated into bytes
696 * according to the platform's default character encoding, and these bytes
697 * are written in exactly the manner of the
698 * {@link #write(int)} method.
699 *
700 * @param b The {@code boolean} to be printed
701 */
702 public void print(boolean b) {
703 write(String.valueOf(b));
704 }
705
706 /**
707 * Prints a character. The character is translated into one or more bytes
708 * according to the character encoding given to the constructor, or the
709 * platform's default character encoding if none specified. These bytes
710 * are written in exactly the manner of the {@link #write(int)} method.
711 *
712 * @param c The {@code char} to be printed
713 */
714 public void print(char c) {
715 write(String.valueOf(c));
716 }
717
718 /**
719 * Prints an integer. The string produced by {@link
720 * java.lang.String#valueOf(int)} is translated into bytes
721 * according to the platform's default character encoding, and these bytes
722 * are written in exactly the manner of the
723 * {@link #write(int)} method.
724 *
725 * @param i The {@code int} to be printed
726 * @see java.lang.Integer#toString(int)
727 */
728 public void print(int i) {
729 write(String.valueOf(i));
730 }
756 public void print(float f) {
757 write(String.valueOf(f));
758 }
759
760 /**
761 * Prints a double-precision floating-point number. The string produced by
762 * {@link java.lang.String#valueOf(double)} is translated into
763 * bytes according to the platform's default character encoding, and these
764 * bytes are written in exactly the manner of the {@link
765 * #write(int)} method.
766 *
767 * @param d The {@code double} to be printed
768 * @see java.lang.Double#toString(double)
769 */
770 public void print(double d) {
771 write(String.valueOf(d));
772 }
773
774 /**
775 * Prints an array of characters. The characters are converted into bytes
776 * according to the character encoding given to the constructor, or the
777 * platform's default character encoding if none specified. These bytes
778 * are written in exactly the manner of the {@link #write(int)} method.
779 *
780 * @param s The array of chars to be printed
781 *
782 * @throws NullPointerException If {@code s} is {@code null}
783 */
784 public void print(char s[]) {
785 write(s);
786 }
787
788 /**
789 * Prints a string. If the argument is {@code null} then the string
790 * {@code "null"} is printed. Otherwise, the string's characters are
791 * converted into bytes according to the character encoding given to the
792 * constructor, or the platform's default character encoding if none
793 * specified. These bytes are written in exactly the manner of the
794 * {@link #write(int)} method.
795 *
796 * @param s The {@code String} to be printed
797 */
798 public void print(String s) {
799 write(String.valueOf(s));
800 }
801
802 /**
803 * Prints an object. The string produced by the {@link
804 * java.lang.String#valueOf(Object)} method is translated into bytes
805 * according to the platform's default character encoding, and these bytes
806 * are written in exactly the manner of the
807 * {@link #write(int)} method.
808 *
809 * @param obj The {@code Object} to be printed
810 * @see java.lang.Object#toString()
811 */
812 public void print(Object obj) {
813 write(String.valueOf(obj));
|