Skip to content

Commit 28d1787

Browse files
committed
Fix some uses of System.out/err.println()
1 parent bd62304 commit 28d1787

File tree

6 files changed

+25
-27
lines changed

6 files changed

+25
-27
lines changed

src/main/java/com/laytonsmith/PureUtilities/Common/StreamUtils.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.io.OutputStream;
1010
import java.io.PrintStream;
1111
import java.io.UnsupportedEncodingException;
12+
import java.nio.charset.StandardCharsets;
1213

1314
/**
1415
* Streams are hard sometimes. This class abstracts most of the functionality that is commonly used.
@@ -145,11 +146,7 @@ public static InputStream GetInputStream(byte[] bytes) {
145146
* @return A new PrintStream object, based on System.out
146147
*/
147148
public static PrintStream GetSystemOut() {
148-
try {
149-
return new PrintStream(System.out, true, "UTF-8");
150-
} catch(UnsupportedEncodingException ex) {
151-
throw new Error(ex);
152-
}
149+
return new PrintStream(System.out, true, StandardCharsets.UTF_8);
153150
}
154151

155152
/**
@@ -159,11 +156,7 @@ public static PrintStream GetSystemOut() {
159156
* @return A new PrintStream object, based on System.err
160157
*/
161158
public static PrintStream GetSystemErr() {
162-
try {
163-
return new PrintStream(System.err, true, "UTF-8");
164-
} catch(UnsupportedEncodingException ex) {
165-
throw new Error(ex);
166-
}
159+
return new PrintStream(System.err, true, StandardCharsets.UTF_8);
167160
}
168161

169162
/**

src/main/java/com/laytonsmith/abstraction/StaticLayer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private static synchronized void InitConvertor() {
4141
try {
4242
if(convertor != null) {
4343
//Uh... There are more than one implementations for this server type
44-
System.out.println("More than one Convertor for this server type was detected!");
44+
StreamUtils.GetSystemErr().println("More than one Convertor for this server type was detected!");
4545
}
4646
convertor = (Convertor) c.newInstance();
4747
//At this point we are all set

src/main/java/com/laytonsmith/core/MSLog.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ public static void initialize(File root) {
189189
if(o instanceof Tag) {
190190
tags.add((Tag) o);
191191
} else {
192-
System.err.println("Element tagged with LogTag, but is not an instance of Tag: "
192+
StreamUtils.GetSystemErr().println("Element tagged with LogTag, but is not an instance of Tag: "
193193
+ f.getDeclaringClass() + "." + f.getName());
194194
}
195195
} catch (IllegalArgumentException | IllegalAccessException ex) {
196-
System.err.println("Could not properly configure logger tag: " + ex.getMessage());
196+
StreamUtils.GetSystemErr().println("Could not properly configure logger tag: " + ex.getMessage());
197197
}
198198
}
199199
for(Tag t : tags) {
@@ -220,7 +220,7 @@ private static LogLevel GetLevel(Tag tag) {
220220
+ ". Tags must be registered with the @LogTag annotation, otherwise they"
221221
+ " are not configurable by the user!";
222222
// Log this everywhere, since this is a problem for the developer.
223-
System.err.println(message);
223+
StreamUtils.GetSystemErr().println(message);
224224
GetLogger().e(Tags.GENERAL, message, Target.UNKNOWN);
225225
}
226226
if(LOOKUP.containsKey(tag)) {

src/main/java/com/laytonsmith/core/Script.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ public Mixed eval(ParseTree c, final Environment env) throws CancelCommandExcept
548548
+ TermColors.CYAN + brand + TermColors.RED + " version: " + TermColors.RESET + version + TermColors.RED + ";\n"
549549
+ "Loaded extensions and versions:\n" + extensionData
550550
+ "Here's the stacktrace:\n" + TermColors.RESET + Static.GetStacktraceString(e);
551-
System.err.println(emsg);
551+
StreamUtils.GetSystemErr().println(emsg);
552552
throw new CancelCommandException(null, Target.UNKNOWN);
553553
}
554554
} finally {

src/main/java/com/laytonsmith/core/functions/Cmdline.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,13 @@ public Boolean runAsync() {
9999
@Override
100100
public CVoid exec(Target t, Environment environment, Mixed... args) throws ConfigRuntimeException {
101101
String msg = Static.MCToANSIColors(args[0].val());
102-
StreamUtils.GetSystemOut().print(msg);
102+
PrintStream out = StreamUtils.GetSystemOut();
103+
out.print(msg);
103104
if(msg.contains("\033")) {
104105
//We have color codes in it, we need to reset them
105-
StreamUtils.GetSystemOut().print(TermColors.reset());
106+
out.print(TermColors.reset());
106107
}
107-
StreamUtils.GetSystemOut().println();
108+
out.println();
108109
return CVoid.VOID;
109110
}
110111

src/main/java/com/laytonsmith/core/functions/Sandbox.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.laytonsmith.core.functions;
22

3+
import com.laytonsmith.PureUtilities.Common.StreamUtils;
4+
import com.laytonsmith.abstraction.enums.MCChatColor;
35
import com.laytonsmith.core.FileWriteMode;
46
import com.laytonsmith.PureUtilities.Common.StringUtils;
57
import com.laytonsmith.PureUtilities.TermColors;
@@ -45,6 +47,7 @@
4547

4648
import java.io.File;
4749
import java.io.IOException;
50+
import java.io.PrintStream;
4851
import java.nio.charset.Charset;
4952
import java.util.HashMap;
5053
import java.util.Map;
@@ -334,10 +337,9 @@ public static class norway extends DummyFunction {
334337

335338
@Override
336339
public Mixed exec(Target t, Environment environment, Mixed... args) throws ConfigRuntimeException {
337-
Function color = new Echoes.color();
338-
String red = color.exec(t, environment, args.length == 3 ? args[0] : new CString("RED", t)).val();
339-
String white = color.exec(t, environment, args.length == 3 ? args[1] : new CString("WHITE", t)).val();
340-
String blue = color.exec(t, environment, args.length == 3 ? args[2] : new CString("BLUE", t)).val();
340+
MCChatColor red = MCChatColor.RED;
341+
MCChatColor white = MCChatColor.WHITE;
342+
MCChatColor blue = MCChatColor.BLUE;
341343
int multiplier = 2;
342344
char c = '=';
343345
String one = multiply(c, 1 * multiplier);
@@ -347,16 +349,18 @@ public Mixed exec(Target t, Environment environment, Mixed... args) throws Confi
347349
String twelve = multiply(c, 12 * multiplier);
348350
String thirteen = multiply(c, 13 * multiplier);
349351
String twentytwo = multiply(c, 22 * multiplier);
352+
PrintStream out = StreamUtils.GetSystemOut();
353+
String vertical = Static.MCToANSIColors(red + six + white + one + blue + two + white + one + red + twelve);
350354
for(int i = 0; i < 6; ++i) {
351-
System.out.println(Static.MCToANSIColors(red + six + white + one + blue + two + white + one + red + twelve) + TermColors.RESET);
355+
out.println(vertical + TermColors.RESET);
352356
}
353-
System.out.println(Static.MCToANSIColors(white + seven + blue + two + white + thirteen) + TermColors.RESET);
357+
out.println(Static.MCToANSIColors(white + seven + blue + two + white + thirteen) + TermColors.RESET);
354358
for(int i = 0; i < 2; ++i) {
355-
System.out.println(Static.MCToANSIColors(blue + twentytwo) + TermColors.RESET);
359+
out.println(Static.MCToANSIColors(blue + twentytwo) + TermColors.RESET);
356360
}
357-
System.out.println(Static.MCToANSIColors(white + seven + blue + two + white + thirteen) + TermColors.RESET);
361+
out.println(Static.MCToANSIColors(white + seven + blue + two + white + thirteen) + TermColors.RESET);
358362
for(int i = 0; i < 6; ++i) {
359-
System.out.println(Static.MCToANSIColors(red + six + white + one + blue + two + white + one + red + twelve) + TermColors.RESET);
363+
out.println(vertical + TermColors.RESET);
360364
}
361365

362366
return CVoid.VOID;

0 commit comments

Comments
 (0)