-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPrintWriter.java
378 lines (331 loc) · 8.62 KB
/
PrintWriter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* @(#)PrintWriter.java 1.13 98/07/01
*
* Copyright 1995-1998 by Sun Microsystems, Inc.,
* 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
* All rights reserved.
*
* This software is the confidential and proprietary information
* of Sun Microsystems, Inc. ("Confidential Information"). You
* shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement
* you entered into with Sun.
*/
package java.io;
/**
* Print formatted representations of objects to a text-output stream. This
* class implements all of the print methods found in PrintStream. It does not
* contain methods for writing raw bytes, for which a program should use
* unencoded byte streams.
*
* <p> Unlike the PrintStream class, if automatic flushing is enabled it will
* be done only when one of the println() methods is invoked, rather than
* whenever a newline character happens to be output. The println() methods
* use the platform's own notion of line separator rather than the newline
* character.
*
* <p> Methods in this class never throw I/O exceptions. The client may
* inquire as to whether any errors have occurred by invoking checkError().
*
* @version 1.11, 97/01/27
* @author Frank Yellin
* @author Mark Reinhold
* @since JDK1.1
*/
public class PrintWriter extends Writer {
private Writer out;
private boolean autoFlush = false;
private boolean trouble = false;
/**
* Line separator string. This is the value of the line.separator
* property at the moment that the stream was created.
*/
private String lineSeparator;
/**
* Create a new PrintWriter, without automatic line flushing.
*
* @param out A character-output stream
*/
public PrintWriter (Writer out) {
this(out, false);
}
/**
* Create a new PrintWriter.
*
* @param out A character-output stream
* @param autoFlush A boolean; if true, the println() methods will flush
* the output buffer
*/
public PrintWriter(Writer out,
boolean autoFlush) {
super(out);
this.out = out;
this.autoFlush = autoFlush;
lineSeparator = System.getProperty("line.separator");
}
/**
* Create a new PrintWriter, without automatic line flushing, from an
* existing OutputStream. This convenience constructor creates the
* necessary intermediate OutputStreamWriter, which will convert characters
* into bytes using the default character encoding.
*
* @param out An output stream
*
* @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
*/
public PrintWriter(OutputStream out) {
this(out, false);
}
/**
* Create a new PrintWriter from an existing OutputStream. This
* convenience constructor creates the necessary intermediate
* OutputStreamWriter, which will convert characters into bytes using the
* default character encoding.
*
* @param out An output stream
* @param autoFlush A boolean; if true, the println() methods will flush
* the output buffer
*
* @see java.io.OutputStreamWriter#OutputStreamWriter(java.io.OutputStream)
*/
public PrintWriter(OutputStream out, boolean autoFlush) {
this(new BufferedWriter(new OutputStreamWriter(out)), autoFlush);
}
/** Check to make sure that the stream has not been closed */
private void ensureOpen() throws IOException {
if (out == null)
throw new IOException("Stream closed");
}
/** Flush the stream. */
public void flush() {
try {
synchronized (lock) {
ensureOpen();
out.flush();
}
}
catch (IOException x) {
trouble = true;
}
}
/** Close the stream. */
public void close() {
try {
synchronized (lock) {
if (out == null)
return;
out.close();
out = null;
}
}
catch (IOException x) {
trouble = true;
}
}
/**
* Flush the stream and check its error state. Errors are cumulative;
* once the stream encounters an error, this routine will return true on
* all successive calls.
*
* @return True if the print stream has encountered an error, either on the
* underlying output stream or during a format conversion.
*/
public boolean checkError() {
if (out != null)
flush();
return trouble;
}
/** Indicate that an error has occurred. */
protected void setError() {
trouble = true;
}
/*
* Exception-catching, synchronized output operations,
* which also implement the write() methods of Writer
*/
/** Write a single character. */
public void write(int c) {
try {
synchronized (lock) {
ensureOpen();
out.write(c);
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
/** Write a portion of an array of characters. */
public void write(char buf[], int off, int len) {
try {
synchronized (lock) {
ensureOpen();
out.write(buf, off, len);
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
/**
* Write an array of characters. This method cannot be inherited from the
* Writer class because it must suppress I/O exceptions.
*/
public void write(char buf[]) {
write(buf, 0, buf.length);
}
/** Write a portion of a string. */
public void write(String s, int off, int len) {
try {
synchronized (lock) {
ensureOpen();
out.write(s, off, len);
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
/**
* Write a string. This method cannot be inherited from the Writer class
* because it must suppress I/O exceptions.
*/
public void write(String s) {
write(s, 0, s.length());
}
private void newLine() {
try {
synchronized (lock) {
ensureOpen();
out.write(lineSeparator);
if (autoFlush)
out.flush();
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
/* Methods that do not terminate lines */
/** Print a boolean. */
public void print(boolean b) {
write(b ? "true" : "false");
}
/** Print a character. */
public void print(char c) {
write(String.valueOf(c));
}
/** Print an integer. */
public void print(int i) {
write(String.valueOf(i));
}
/** Print a long. */
public void print(long l) {
write(String.valueOf(l));
}
/** Print a float. */
public void print(float f) {
write(String.valueOf(f));
}
/** Print a double. */
public void print(double d) {
write(String.valueOf(d));
}
/** Print an array of chracters. */
public void print(char s[]) {
write(s);
}
/** Print a String. */
public void print(String s) {
if (s == null) {
s = "null";
}
write(s);
}
/** Print an object. */
public void print(Object obj) {
write(String.valueOf(obj));
}
/* Methods that do terminate lines */
/** Finish the line. */
public void println() {
synchronized (lock) {
newLine();
}
}
/** Print a boolean, and then finish the line. */
public void println(boolean x) {
synchronized (lock) {
print(x);
newLine();
}
}
/** Print a character, and then finish the line. */
public void println(char x) {
synchronized (lock) {
print(x);
newLine();
}
}
/** Print an integer, and then finish the line. */
public void println(int x) {
synchronized (lock) {
print(x);
newLine();
}
}
/** Print a long, and then finish the line. */
public void println(long x) {
synchronized (lock) {
print(x);
newLine();
}
}
/** Print a float, and then finish the line. */
public void println(float x) {
synchronized (lock) {
print(x);
newLine();
}
}
/** Print a double, and then finish the line. */
public void println(double x) {
synchronized (lock) {
print(x);
newLine();
}
}
/** Print an array of characters, and then finish the line. */
public void println(char x[]) {
synchronized (lock) {
print(x);
newLine();
}
}
/** Print a String, and then finish the line. */
public void println(String x) {
synchronized (lock) {
print(x);
newLine();
}
}
/** Print an Object, and then finish the line. */
public void println(Object x) {
synchronized (lock) {
print(x);
newLine();
}
}
}