This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-net.git

commit 1ffef37846f480cca2d73d8c2ee329664d0ae4bf
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Sat May 11 17:04:18 2024 -0400

    [SpotBugs] Make explicit use of the default encoding
    
    Remove unused imports
---
 .../apache/commons/net/PrintCommandListener.java   | 23 ++++++----------------
 .../net/examples/cidr/SubnetUtilsExample.java      |  1 -
 .../commons/net/examples/ftp/FTPClientExample.java |  4 ++--
 .../net/examples/ftp/ServerToServerFTP.java        |  4 ++--
 .../org/apache/commons/net/examples/unix/echo.java | 15 ++++++++------
 src/main/java/org/apache/commons/net/io/Util.java  | 16 +++++++++++++++
 6 files changed, 35 insertions(+), 28 deletions(-)

diff --git a/src/main/java/org/apache/commons/net/PrintCommandListener.java 
b/src/main/java/org/apache/commons/net/PrintCommandListener.java
index 4b6000e9..900f99dd 100644
--- a/src/main/java/org/apache/commons/net/PrintCommandListener.java
+++ b/src/main/java/org/apache/commons/net/PrintCommandListener.java
@@ -17,10 +17,10 @@
 
 package org.apache.commons.net;
 
-import java.io.OutputStreamWriter;
 import java.io.PrintStream;
 import java.io.PrintWriter;
-import java.nio.charset.Charset;
+
+import org.apache.commons.net.io.Util;
 
 /**
  * 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,19 +28,8 @@ import java.nio.charset.Charset;
  *
  * @since 2.0
  */
-
 public class PrintCommandListener implements ProtocolCommandListener {
 
-    /**
-     * Creates a new PrintWriter using the default encoding.
-     *
-     * @param printStream the target PrintStream.
-     * @return a new PrintWriter.
-     */
-    private static PrintWriter newPrintWriter(final PrintStream printStream) {
-        return new PrintWriter(new OutputStreamWriter(printStream, 
Charset.defaultCharset()));
-    }
-
     private final PrintWriter writer;
     private final boolean nologin;
     private final char eolMarker;
@@ -55,7 +44,7 @@ public class PrintCommandListener implements 
ProtocolCommandListener {
      */
     @SuppressWarnings("resource")
     public PrintCommandListener(final PrintStream printStream) {
-        this(newPrintWriter(printStream));
+        this(Util.newPrintWriter(printStream));
     }
 
     /**
@@ -68,7 +57,7 @@ public class PrintCommandListener implements 
ProtocolCommandListener {
      */
     @SuppressWarnings("resource")
     public PrintCommandListener(final PrintStream printStream, final boolean 
suppressLogin) {
-        this(newPrintWriter(printStream), suppressLogin);
+        this(Util.newPrintWriter(printStream), suppressLogin);
     }
 
     /**
@@ -82,7 +71,7 @@ public class PrintCommandListener implements 
ProtocolCommandListener {
      */
     @SuppressWarnings("resource")
     public PrintCommandListener(final PrintStream printStream, final boolean 
suppressLogin, final char eolMarker) {
-        this(newPrintWriter(printStream), suppressLogin, eolMarker);
+        this(Util.newPrintWriter(printStream), suppressLogin, eolMarker);
     }
 
     /**
@@ -97,7 +86,7 @@ public class PrintCommandListener implements 
ProtocolCommandListener {
      */
     @SuppressWarnings("resource")
     public PrintCommandListener(final PrintStream printStream, final boolean 
suppressLogin, final char eolMarker, final boolean showDirection) {
-        this(newPrintWriter(printStream), suppressLogin, eolMarker, 
showDirection);
+        this(Util.newPrintWriter(printStream), suppressLogin, eolMarker, 
showDirection);
     }
 
     /**
diff --git 
a/src/main/java/org/apache/commons/net/examples/cidr/SubnetUtilsExample.java 
b/src/main/java/org/apache/commons/net/examples/cidr/SubnetUtilsExample.java
index ec39da0d..879e57ae 100644
--- a/src/main/java/org/apache/commons/net/examples/cidr/SubnetUtilsExample.java
+++ b/src/main/java/org/apache/commons/net/examples/cidr/SubnetUtilsExample.java
@@ -20,7 +20,6 @@ import java.nio.charset.Charset;
 import java.util.Arrays;
 import java.util.Scanner;
 
-import org.apache.commons.net.util.Charsets;
 import org.apache.commons.net.util.SubnetUtils;
 import org.apache.commons.net.util.SubnetUtils.SubnetInfo;
 
diff --git 
a/src/main/java/org/apache/commons/net/examples/ftp/FTPClientExample.java 
b/src/main/java/org/apache/commons/net/examples/ftp/FTPClientExample.java
index 7402aa1d..b7928d5b 100644
--- a/src/main/java/org/apache/commons/net/examples/ftp/FTPClientExample.java
+++ b/src/main/java/org/apache/commons/net/examples/ftp/FTPClientExample.java
@@ -22,7 +22,6 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.io.PrintWriter;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.time.Duration;
@@ -39,6 +38,7 @@ import org.apache.commons.net.ftp.FTPReply;
 import org.apache.commons.net.ftp.FTPSClient;
 import org.apache.commons.net.io.CopyStreamEvent;
 import org.apache.commons.net.io.CopyStreamListener;
+import org.apache.commons.net.io.Util;
 import org.apache.commons.net.util.TrustManagerUtils;
 
 /**
@@ -280,7 +280,7 @@ public final class FTPClientExample {
         ftp.setListHiddenFiles(hidden);
 
         // suppress login details
-        ftp.addProtocolCommandListener(new PrintCommandListener(new 
PrintWriter(System.out), true));
+        ftp.addProtocolCommandListener(new 
PrintCommandListener(Util.newPrintWriter(System.out), true));
 
         final FTPClientConfig config;
         if (serverType != null) {
diff --git 
a/src/main/java/org/apache/commons/net/examples/ftp/ServerToServerFTP.java 
b/src/main/java/org/apache/commons/net/examples/ftp/ServerToServerFTP.java
index 8405f1cf..36b3c1c4 100644
--- a/src/main/java/org/apache/commons/net/examples/ftp/ServerToServerFTP.java
+++ b/src/main/java/org/apache/commons/net/examples/ftp/ServerToServerFTP.java
@@ -18,13 +18,13 @@
 package org.apache.commons.net.examples.ftp;
 
 import java.io.IOException;
-import java.io.PrintWriter;
 import java.net.InetAddress;
 
 import org.apache.commons.net.PrintCommandListener;
 import org.apache.commons.net.ProtocolCommandListener;
 import org.apache.commons.net.ftp.FTPClient;
 import org.apache.commons.net.ftp.FTPReply;
+import org.apache.commons.net.io.Util;
 
 /**
  * This is an example program demonstrating how to use the FTPClient class. 
This program arranges a server to server file transfer that transfers a file 
from
@@ -74,7 +74,7 @@ public final class ServerToServerFTP {
         password2 = args[6];
         file2 = args[7];
 
-        listener = new PrintCommandListener(new PrintWriter(System.out), true);
+        listener = new PrintCommandListener(Util.newPrintWriter(System.out), 
true);
         ftp1 = new FTPClient();
         ftp1.addProtocolCommandListener(listener);
         ftp2 = new FTPClient();
diff --git a/src/main/java/org/apache/commons/net/examples/unix/echo.java 
b/src/main/java/org/apache/commons/net/examples/unix/echo.java
index d8c3b4a0..e87e85d5 100644
--- a/src/main/java/org/apache/commons/net/examples/unix/echo.java
+++ b/src/main/java/org/apache/commons/net/examples/unix/echo.java
@@ -25,6 +25,7 @@ import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.net.InetAddress;
 import java.net.SocketException;
+import java.nio.charset.Charset;
 import java.time.Duration;
 
 import org.apache.commons.net.echo.EchoTCPClient;
@@ -49,9 +50,10 @@ public final class echo {
         client.connect(host);
         try {
             System.out.println("Connected to " + host + ".");
-            final BufferedReader input = new BufferedReader(new 
InputStreamReader(System.in));
-            try (PrintWriter echoOutput = new PrintWriter(new 
OutputStreamWriter(client.getOutputStream()), true);
-                    BufferedReader echoInput = new BufferedReader(new 
InputStreamReader(client.getInputStream()))) {
+            final Charset charset = Charset.defaultCharset();
+            final BufferedReader input = new BufferedReader(new 
InputStreamReader(System.in, charset));
+            try (PrintWriter echoOutput = new PrintWriter(new 
OutputStreamWriter(client.getOutputStream(), charset), true);
+                    BufferedReader echoInput = new BufferedReader(new 
InputStreamReader(client.getInputStream(), charset))) {
                 while ((line = input.readLine()) != null) {
                     echoOutput.println(line);
                     System.out.println(echoInput.readLine());
@@ -70,7 +72,8 @@ public final class echo {
         final BufferedReader input;
         final InetAddress address;
 
-        input = new BufferedReader(new InputStreamReader(System.in));
+        final Charset charset = Charset.defaultCharset();
+        input = new BufferedReader(new InputStreamReader(System.in, charset));
         address = InetAddress.getByName(host);
         try (EchoUDPClient client = new EchoUDPClient()) {
 
@@ -82,7 +85,7 @@ public final class echo {
             // Remember, there are no guarantees about the ordering of returned
             // UDP packets, so there is a chance the output may be jumbled.
             while ((line = input.readLine()) != null) {
-                data = line.getBytes();
+                data = line.getBytes(charset);
                 client.send(data, address);
                 count = 0;
                 do {
@@ -102,7 +105,7 @@ public final class echo {
                         System.err.println("InterruptedIOException: Timed out 
and dropped packet");
                         break;
                     }
-                    System.out.print(new String(data, 0, length));
+                    System.out.print(new String(data, 0, length, charset));
                     count += length;
                 } while (count < data.length);
 
diff --git a/src/main/java/org/apache/commons/net/io/Util.java 
b/src/main/java/org/apache/commons/net/io/Util.java
index 41b2a40d..ffc4d4fc 100644
--- a/src/main/java/org/apache/commons/net/io/Util.java
+++ b/src/main/java/org/apache/commons/net/io/Util.java
@@ -21,9 +21,13 @@ import java.io.Closeable;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintStream;
+import java.io.PrintWriter;
 import java.io.Reader;
 import java.io.Writer;
 import java.net.Socket;
+import java.nio.charset.Charset;
 
 import org.apache.commons.net.util.NetConstants;
 
@@ -37,6 +41,7 @@ import org.apache.commons.net.util.NetConstants;
  */
 
 public final class Util {
+
     /**
      * The default buffer size ({@value}) used by {@link #copyStream 
copyStream } and {@link #copyReader copyReader} and by the 
copyReader/copyStream methods if
      * a zero or negative buffer size is supplied.
@@ -283,6 +288,17 @@ public final class Util {
         return total;
     }
 
+    /**
+     * Creates a new PrintWriter using the default encoding.
+     *
+     * @param printStream the target PrintStream.
+     * @return a new PrintWriter.
+     * @since 3.11.0
+     */
+    public static PrintWriter newPrintWriter(final PrintStream printStream) {
+        return new PrintWriter(new OutputStreamWriter(printStream, 
Charset.defaultCharset()));
+    }
+
     /** Cannot be instantiated. */
     private Util() {
     }

Reply via email to