/
PrintStream.java
170 lines (134 loc) · 3.79 KB
/
PrintStream.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
/* Copyright (c) 2008-2015, Avian Contributors
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
There is NO WARRANTY for this software. See license.txt for
details. */
package java.io;
public class PrintStream extends OutputStream {
private final OutputStream out;
private final boolean autoFlush;
private static class Static {
private static final byte[] newline
= System.getProperty("line.separator").getBytes();
}
public PrintStream(OutputStream out, boolean autoFlush) {
this.out = out;
this.autoFlush = autoFlush;
}
public PrintStream(OutputStream out, boolean autoFlush, String encoding)
throws UnsupportedEncodingException
{
this.out = out;
this.autoFlush = autoFlush;
if (! (encoding.equals("UTF-8") || encoding.equals("ISO-8859-1"))) {
throw new UnsupportedEncodingException(encoding);
}
}
public PrintStream(OutputStream out) {
this(out, false);
}
public synchronized void print(String s) {
try {
out.write(s.getBytes());
if (autoFlush) flush();
} catch (IOException e) { }
}
public void print(Object o) {
print(String.valueOf(o));
}
public void print(boolean v) {
print(String.valueOf(v));
}
public void print(char c) {
print(String.valueOf(c));
}
public void print(int v) {
print(String.valueOf(v));
}
public void print(long v) {
print(String.valueOf(v));
}
public void print(float v) {
print(String.valueOf(v));
}
public void print(double v) {
print(String.valueOf(v));
}
public void print(char[] s) {
print(String.valueOf(s));
}
public synchronized PrintStream printf(java.util.Locale locale, String format, Object... args) {
// should this be cached in an instance variable??
final java.util.Formatter formatter = new java.util.Formatter(this);
formatter.format(locale, format, args);
return this;
}
public synchronized PrintStream printf(String format, Object... args) {
final java.util.Formatter formatter = new java.util.Formatter(this);
formatter.format(format, args);
return this;
}
public PrintStream format(String format, Object... args) {
return printf(format, args);
}
public PrintStream format(java.util.Locale locale, String format, Object... args) {
return printf(locale, format, args);
}
public synchronized void println(String s) {
try {
out.write(s.getBytes());
out.write(Static.newline);
if (autoFlush) flush();
} catch (IOException e) { }
}
public synchronized void println() {
try {
out.write(Static.newline);
if (autoFlush) flush();
} catch (IOException e) { }
}
public void println(Object o) {
println(String.valueOf(o));
}
public void println(boolean v) {
println(String.valueOf(v));
}
public void println(char c) {
println(String.valueOf(c));
}
public void println(int v) {
println(String.valueOf(v));
}
public void println(long v) {
println(String.valueOf(v));
}
public void println(float v) {
println(String.valueOf(v));
}
public void println(double v) {
println(String.valueOf(v));
}
public void println(char[] s) {
println(String.valueOf(s));
}
public void write(int c) throws IOException {
out.write(c);
if (autoFlush && c == '\n') flush();
}
public void write(byte[] buffer, int offset, int length) throws IOException {
out.write(buffer, offset, length);
if (autoFlush) flush();
}
public void flush() {
try {
out.flush();
} catch (IOException e) { }
}
public void close() {
try {
out.close();
} catch (IOException e) { }
}
}