Skip to content

Commit ca2801f

Browse files
committed
[SpotBugs] Make explicit use of the default encoding
Javadoc
1 parent 336fa8d commit ca2801f

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

src/main/java/org/apache/commons/net/PrintCommandListener.java

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717

1818
package org.apache.commons.net;
1919

20+
import java.io.OutputStreamWriter;
2021
import java.io.PrintStream;
2122
import java.io.PrintWriter;
23+
import java.nio.charset.Charset;
2224

2325
/**
2426
* This is a support class for some example programs. It is a sample implementation of the ProtocolCommandListener interface which just prints out to a
@@ -28,62 +30,75 @@
2830
*/
2931

3032
public class PrintCommandListener implements ProtocolCommandListener {
33+
34+
/**
35+
* @param printStream
36+
* @return
37+
*/
38+
private static PrintWriter newPrintWriter(final PrintStream printStream) {
39+
return new PrintWriter(new OutputStreamWriter(printStream, Charset.defaultCharset()));
40+
}
3141
private final PrintWriter writer;
3242
private final boolean nologin;
3343
private final char eolMarker;
44+
3445
private final boolean directionMarker;
3546

3647
/**
37-
* Create the default instance which prints everything.
48+
* Constructs an instance which prints everything using the default Charset.
3849
*
39-
* @param stream where to write the commands and responses e.g. System.out
50+
* @param printStream where to write the commands and responses e.g. System.out
4051
* @since 3.0
4152
*/
42-
public PrintCommandListener(final PrintStream stream) {
43-
this(new PrintWriter(stream));
53+
@SuppressWarnings("resource")
54+
public PrintCommandListener(final PrintStream printStream) {
55+
this(newPrintWriter(printStream));
4456
}
4557

4658
/**
47-
* Create an instance which optionally suppresses login command text and indicates where the EOL starts with the specified character.
59+
* Constructs an instance which optionally suppresses login command text and indicates where the EOL starts with the specified character.
4860
*
49-
* @param stream where to write the commands and responses
61+
* @param printStream where to write the commands and responses
5062
* @param suppressLogin if {@code true}, only print command name for login
5163
*
5264
* @since 3.0
5365
*/
54-
public PrintCommandListener(final PrintStream stream, final boolean suppressLogin) {
55-
this(new PrintWriter(stream), suppressLogin);
66+
@SuppressWarnings("resource")
67+
public PrintCommandListener(final PrintStream printStream, final boolean suppressLogin) {
68+
this(newPrintWriter(printStream), suppressLogin);
5669
}
5770

5871
/**
59-
* Create an instance which optionally suppresses login command text and indicates where the EOL starts with the specified character.
72+
* Constructs an instance which optionally suppresses login command text and indicates where the EOL starts with the specified character.
6073
*
61-
* @param stream where to write the commands and responses
74+
* @param printStream where to write the commands and responses
6275
* @param suppressLogin if {@code true}, only print command name for login
6376
* @param eolMarker if non-zero, add a marker just before the EOL.
6477
*
6578
* @since 3.0
6679
*/
67-
public PrintCommandListener(final PrintStream stream, final boolean suppressLogin, final char eolMarker) {
68-
this(new PrintWriter(stream), suppressLogin, eolMarker);
80+
@SuppressWarnings("resource")
81+
public PrintCommandListener(final PrintStream printStream, final boolean suppressLogin, final char eolMarker) {
82+
this(newPrintWriter(printStream), suppressLogin, eolMarker);
6983
}
7084

7185
/**
72-
* Create an instance which optionally suppresses login command text and indicates where the EOL starts with the specified character.
86+
* Constructs an instance which optionally suppresses login command text and indicates where the EOL starts with the specified character.
7387
*
74-
* @param stream where to write the commands and responses
88+
* @param printStream where to write the commands and responses
7589
* @param suppressLogin if {@code true}, only print command name for login
7690
* @param eolMarker if non-zero, add a marker just before the EOL.
7791
* @param showDirection if {@code true}, add {@code "> "} or {@code "< "} as appropriate to the output
7892
*
7993
* @since 3.0
8094
*/
81-
public PrintCommandListener(final PrintStream stream, final boolean suppressLogin, final char eolMarker, final boolean showDirection) {
82-
this(new PrintWriter(stream), suppressLogin, eolMarker, showDirection);
95+
@SuppressWarnings("resource")
96+
public PrintCommandListener(final PrintStream printStream, final boolean suppressLogin, final char eolMarker, final boolean showDirection) {
97+
this(newPrintWriter(printStream), suppressLogin, eolMarker, showDirection);
8398
}
8499

85100
/**
86-
* Create the default instance which prints everything.
101+
* Constructs the default instance which prints everything.
87102
*
88103
* @param writer where to write the commands and responses
89104
*/
@@ -92,7 +107,7 @@ public PrintCommandListener(final PrintWriter writer) {
92107
}
93108

94109
/**
95-
* Create an instance which optionally suppresses login command text.
110+
* Constructs an instance which optionally suppresses login command text.
96111
*
97112
* @param writer where to write the commands and responses
98113
* @param suppressLogin if {@code true}, only print command name for login
@@ -104,7 +119,7 @@ public PrintCommandListener(final PrintWriter writer, final boolean suppressLogi
104119
}
105120

106121
/**
107-
* Create an instance which optionally suppresses login command text and indicates where the EOL starts with the specified character.
122+
* Constructs an instance which optionally suppresses login command text and indicates where the EOL starts with the specified character.
108123
*
109124
* @param writer where to write the commands and responses
110125
* @param suppressLogin if {@code true}, only print command name for login
@@ -117,7 +132,7 @@ public PrintCommandListener(final PrintWriter writer, final boolean suppressLogi
117132
}
118133

119134
/**
120-
* Create an instance which optionally suppresses login command text and indicates where the EOL starts with the specified character.
135+
* Constructs an instance which optionally suppresses login command text and indicates where the EOL starts with the specified character.
121136
*
122137
* @param writer where to write the commands and responses
123138
* @param suppressLogin if {@code true}, only print command name for login

0 commit comments

Comments
 (0)