17
17
18
18
package org .apache .commons .net ;
19
19
20
+ import java .io .OutputStreamWriter ;
20
21
import java .io .PrintStream ;
21
22
import java .io .PrintWriter ;
23
+ import java .nio .charset .Charset ;
22
24
23
25
/**
24
26
* 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
30
*/
29
31
30
32
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
+ }
31
41
private final PrintWriter writer ;
32
42
private final boolean nologin ;
33
43
private final char eolMarker ;
44
+
34
45
private final boolean directionMarker ;
35
46
36
47
/**
37
- * Create the default instance which prints everything.
48
+ * Constructs an instance which prints everything using the default Charset .
38
49
*
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
40
51
* @since 3.0
41
52
*/
42
- public PrintCommandListener (final PrintStream stream ) {
43
- this (new PrintWriter (stream ));
53
+ @ SuppressWarnings ("resource" )
54
+ public PrintCommandListener (final PrintStream printStream ) {
55
+ this (newPrintWriter (printStream ));
44
56
}
45
57
46
58
/**
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.
48
60
*
49
- * @param stream where to write the commands and responses
61
+ * @param printStream where to write the commands and responses
50
62
* @param suppressLogin if {@code true}, only print command name for login
51
63
*
52
64
* @since 3.0
53
65
*/
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 );
56
69
}
57
70
58
71
/**
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.
60
73
*
61
- * @param stream where to write the commands and responses
74
+ * @param printStream where to write the commands and responses
62
75
* @param suppressLogin if {@code true}, only print command name for login
63
76
* @param eolMarker if non-zero, add a marker just before the EOL.
64
77
*
65
78
* @since 3.0
66
79
*/
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 );
69
83
}
70
84
71
85
/**
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.
73
87
*
74
- * @param stream where to write the commands and responses
88
+ * @param printStream where to write the commands and responses
75
89
* @param suppressLogin if {@code true}, only print command name for login
76
90
* @param eolMarker if non-zero, add a marker just before the EOL.
77
91
* @param showDirection if {@code true}, add {@code "> "} or {@code "< "} as appropriate to the output
78
92
*
79
93
* @since 3.0
80
94
*/
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 );
83
98
}
84
99
85
100
/**
86
- * Create the default instance which prints everything.
101
+ * Constructs the default instance which prints everything.
87
102
*
88
103
* @param writer where to write the commands and responses
89
104
*/
@@ -92,7 +107,7 @@ public PrintCommandListener(final PrintWriter writer) {
92
107
}
93
108
94
109
/**
95
- * Create an instance which optionally suppresses login command text.
110
+ * Constructs an instance which optionally suppresses login command text.
96
111
*
97
112
* @param writer where to write the commands and responses
98
113
* @param suppressLogin if {@code true}, only print command name for login
@@ -104,7 +119,7 @@ public PrintCommandListener(final PrintWriter writer, final boolean suppressLogi
104
119
}
105
120
106
121
/**
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.
108
123
*
109
124
* @param writer where to write the commands and responses
110
125
* @param suppressLogin if {@code true}, only print command name for login
@@ -117,7 +132,7 @@ public PrintCommandListener(final PrintWriter writer, final boolean suppressLogi
117
132
}
118
133
119
134
/**
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.
121
136
*
122
137
* @param writer where to write the commands and responses
123
138
* @param suppressLogin if {@code true}, only print command name for login
0 commit comments