1 /* |
1 /* |
2 * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
23 * questions. |
23 * questions. |
24 */ |
24 */ |
25 |
25 |
26 package java.io; |
26 package java.io; |
27 |
27 |
|
28 import java.util.Objects; |
28 import java.util.Formatter; |
29 import java.util.Formatter; |
29 import java.util.Locale; |
30 import java.util.Locale; |
|
31 import java.nio.charset.Charset; |
|
32 import java.nio.charset.IllegalCharsetNameException; |
|
33 import java.nio.charset.UnsupportedCharsetException; |
30 |
34 |
31 /** |
35 /** |
32 * Prints formatted representations of objects to a text-output stream. This |
36 * Prints formatted representations of objects to a text-output stream. This |
33 * class implements all of the <tt>print</tt> methods found in {@link |
37 * class implements all of the <tt>print</tt> methods found in {@link |
34 * PrintStream}. It does not contain methods for writing raw bytes, for which |
38 * PrintStream}. It does not contain methods for writing raw bytes, for which |
57 * |
61 * |
58 * @since 1.2 |
62 * @since 1.2 |
59 */ |
63 */ |
60 protected Writer out; |
64 protected Writer out; |
61 |
65 |
62 private boolean autoFlush = false; |
66 private final boolean autoFlush; |
63 private boolean trouble = false; |
67 private boolean trouble = false; |
64 private Formatter formatter; |
68 private Formatter formatter; |
65 private PrintStream psOut = null; |
69 private PrintStream psOut = null; |
66 |
70 |
67 /** |
71 /** |
68 * Line separator string. This is the value of the line.separator |
72 * Line separator string. This is the value of the line.separator |
69 * property at the moment that the stream was created. |
73 * property at the moment that the stream was created. |
70 */ |
74 */ |
71 private String lineSeparator; |
75 private final String lineSeparator; |
|
76 |
|
77 /** |
|
78 * Returns a charset object for the given charset name. |
|
79 * @throws NullPointerException is csn is null |
|
80 * @throws UnsupportedEncodingException if the charset is not supported |
|
81 */ |
|
82 private static Charset toCharset(String csn) |
|
83 throws UnsupportedEncodingException |
|
84 { |
|
85 Objects.nonNull(csn, "charsetName"); |
|
86 try { |
|
87 return Charset.forName(csn); |
|
88 } catch (IllegalCharsetNameException|UnsupportedCharsetException unused) { |
|
89 // UnsupportedEncodingException should be thrown |
|
90 throw new UnsupportedEncodingException(csn); |
|
91 } |
|
92 } |
72 |
93 |
73 /** |
94 /** |
74 * Creates a new PrintWriter, without automatic line flushing. |
95 * Creates a new PrintWriter, without automatic line flushing. |
75 * |
96 * |
76 * @param out A character-output stream |
97 * @param out A character-output stream |
162 public PrintWriter(String fileName) throws FileNotFoundException { |
183 public PrintWriter(String fileName) throws FileNotFoundException { |
163 this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))), |
184 this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))), |
164 false); |
185 false); |
165 } |
186 } |
166 |
187 |
|
188 /* Private constructor */ |
|
189 private PrintWriter(Charset charset, File file) |
|
190 throws FileNotFoundException |
|
191 { |
|
192 this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset)), |
|
193 false); |
|
194 } |
|
195 |
167 /** |
196 /** |
168 * Creates a new PrintWriter, without automatic line flushing, with the |
197 * Creates a new PrintWriter, without automatic line flushing, with the |
169 * specified file name and charset. This convenience constructor creates |
198 * specified file name and charset. This convenience constructor creates |
170 * the necessary intermediate {@link java.io.OutputStreamWriter |
199 * the necessary intermediate {@link java.io.OutputStreamWriter |
171 * OutputStreamWriter}, which will encode characters using the provided |
200 * OutputStreamWriter}, which will encode characters using the provided |
198 * @since 1.5 |
227 * @since 1.5 |
199 */ |
228 */ |
200 public PrintWriter(String fileName, String csn) |
229 public PrintWriter(String fileName, String csn) |
201 throws FileNotFoundException, UnsupportedEncodingException |
230 throws FileNotFoundException, UnsupportedEncodingException |
202 { |
231 { |
203 this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), csn)), |
232 this(toCharset(csn), new File(fileName)); |
204 false); |
|
205 } |
233 } |
206 |
234 |
207 /** |
235 /** |
208 * Creates a new PrintWriter, without automatic line flushing, with the |
236 * Creates a new PrintWriter, without automatic line flushing, with the |
209 * specified file. This convenience constructor creates the necessary |
237 * specified file. This convenience constructor creates the necessary |
270 * @since 1.5 |
298 * @since 1.5 |
271 */ |
299 */ |
272 public PrintWriter(File file, String csn) |
300 public PrintWriter(File file, String csn) |
273 throws FileNotFoundException, UnsupportedEncodingException |
301 throws FileNotFoundException, UnsupportedEncodingException |
274 { |
302 { |
275 this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), csn)), |
303 this(toCharset(csn), file); |
276 false); |
|
277 } |
304 } |
278 |
305 |
279 /** Checks to make sure that the stream has not been closed */ |
306 /** Checks to make sure that the stream has not been closed */ |
280 private void ensureOpen() throws IOException { |
307 private void ensureOpen() throws IOException { |
281 if (out == null) |
308 if (out == null) |