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 135272f513aebfba442647d3be215c73b7da5f3a Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Wed Feb 19 15:37:56 2025 -0500 Add FTP.setControlEncoding(Charset) --- src/changes/changes.xml | 1 + src/main/java/org/apache/commons/net/ftp/FTP.java | 15 +++++++++++++++ src/main/java/org/apache/commons/net/ftp/FTPClient.java | 6 ++++-- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 131a0ef6..71860020 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -93,6 +93,7 @@ The <action> type attribute can be add,update,fix,remove. <action type="add" dev="ggregory" due-to="Gary Gregory">Add SubnetInfo.streamAddressStrings().</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add FTPCmd.OPTS.</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add FTP.opts(String, String).</action> + <action type="add" dev="ggregory" due-to="Gary Gregory">Add FTP.setControlEncoding(Charset).</action> <!-- UPDATE --> <action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 70 to 81 #261, #278, #280, #285, #298, #293, #300.</action> <action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-lang3 from 3.14.0 to 3.17.0 #268, #273, #281.</action> diff --git a/src/main/java/org/apache/commons/net/ftp/FTP.java b/src/main/java/org/apache/commons/net/ftp/FTP.java index 4e68a8f3..3e06eba9 100644 --- a/src/main/java/org/apache/commons/net/ftp/FTP.java +++ b/src/main/java/org/apache/commons/net/ftp/FTP.java @@ -28,9 +28,11 @@ import java.net.Inet6Address; import java.net.InetAddress; import java.net.SocketException; import java.net.SocketTimeoutException; +import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import org.apache.commons.io.Charsets; import org.apache.commons.net.MalformedServerReplyException; import org.apache.commons.net.ProtocolCommandSupport; import org.apache.commons.net.SocketClient; @@ -1279,6 +1281,19 @@ public class FTP extends SocketClient { return getReply(); } + /** + * Sets the character encoding to be used by the FTP control connection. Some FTP servers require that commands be issued in a non-ASCII encoding like + * UTF-8 so that file names with multi-byte character representations (e.g, Big 8) can be specified. + * <p> + * Please note that this has to be set before the connection is established. + * </p> + * @param charset The new character encoding for the control connection. + * @since 3.12.0 + */ + public void setControlEncoding(final Charset charset) { + _controlEncoding = Charsets.toCharset(charset).name(); + } + /** * Sets the character encoding to be used by the FTP control connection. Some FTP servers require that commands be issued in a non-ASCII encoding like * UTF-8 so that file names with multi-byte character representations (e.g, Big 8) can be specified. diff --git a/src/main/java/org/apache/commons/net/ftp/FTPClient.java b/src/main/java/org/apache/commons/net/ftp/FTPClient.java index 0d4cf266..8878f7a9 100644 --- a/src/main/java/org/apache/commons/net/ftp/FTPClient.java +++ b/src/main/java/org/apache/commons/net/ftp/FTPClient.java @@ -34,6 +34,7 @@ import java.net.Socket; import java.net.SocketException; import java.net.SocketTimeoutException; import java.net.UnknownHostException; +import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.time.Duration; import java.time.Instant; @@ -648,8 +649,9 @@ public class FTPClient extends FTP implements Configurable { final ArrayList<String> oldReplyLines = new ArrayList<>(_replyLines); final int oldReplyCode = _replyCode; // UTF-8 appears to be the default - if (hasFeature("UTF8") || hasFeature(StandardCharsets.UTF_8.name())) { - setControlEncoding(StandardCharsets.UTF_8.name()); + final Charset utf8 = StandardCharsets.UTF_8; + if (hasFeature("UTF8") || hasFeature(utf8.name())) { + setControlEncoding(utf8); _controlInput_ = new CRLFLineReader(new InputStreamReader(_input_, getControlEncoding())); _controlOutput_ = new BufferedWriter(new OutputStreamWriter(_output_, getControlEncoding())); }