You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/05/11 22:03:30 UTC
(commons-net) 01/09: [SpotBugs] Make explicit use of the default encoding
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 f019babad7c4d48f96ef9545d15f9211b05b26df
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat May 11 17:36:04 2024 -0400
[SpotBugs] Make explicit use of the default encoding
Remove unused imports
---
.../commons/net/examples/ftp/TFTPExample.java | 6 ----
.../commons/net/examples/mail/IMAPExportMbox.java | 32 ++++++++++-------
.../commons/net/examples/mail/IMAPImportMbox.java | 42 +++++++++++-----------
.../apache/commons/net/examples/mail/POP3Mail.java | 4 +--
.../apache/commons/net/examples/mail/SMTPMail.java | 13 +++----
.../apache/commons/net/examples/mail/Utils.java | 3 +-
.../commons/net/examples/nntp/ArticleReader.java | 4 +--
.../commons/net/examples/nntp/ExtendedNNTPOps.java | 4 +--
.../net/examples/nntp/MessageThreading.java | 4 +--
.../commons/net/examples/nntp/PostMessage.java | 14 ++++----
.../net/examples/telnet/TelnetClientExample.java | 28 ++++++++-------
.../apache/commons/net/examples/unix/chargen.java | 3 +-
.../apache/commons/net/tftp/TFTPErrorPacket.java | 5 +--
.../apache/commons/net/tftp/TFTPRequestPacket.java | 5 +--
14 files changed, 88 insertions(+), 79 deletions(-)
diff --git a/src/main/java/org/apache/commons/net/examples/ftp/TFTPExample.java b/src/main/java/org/apache/commons/net/examples/ftp/TFTPExample.java
index a76d0820..121d9ade 100644
--- a/src/main/java/org/apache/commons/net/examples/ftp/TFTPExample.java
+++ b/src/main/java/org/apache/commons/net/examples/ftp/TFTPExample.java
@@ -159,15 +159,12 @@ public final class TFTPExample {
final boolean closed;
FileOutputStream output;
final File file;
-
file = new File(localFilename);
-
// If file exists, don't overwrite it.
if (file.exists()) {
System.err.println("Error: " + localFilename + " already exists.");
return false;
}
-
// Try to open local file for writing
try {
output = new FileOutputStream(file);
@@ -175,9 +172,7 @@ public final class TFTPExample {
tftp.close();
throw new IOException("Error: could not open local file for writing.", e);
}
-
open(tftp);
-
// Try to receive remote file via TFTP
try {
final String[] parts = hostname.split(":");
@@ -198,7 +193,6 @@ public final class TFTPExample {
// Close local socket and output file
closed = close(tftp, output);
}
-
return closed;
}
diff --git a/src/main/java/org/apache/commons/net/examples/mail/IMAPExportMbox.java b/src/main/java/org/apache/commons/net/examples/mail/IMAPExportMbox.java
index 18cfa9cc..ac13a96f 100644
--- a/src/main/java/org/apache/commons/net/examples/mail/IMAPExportMbox.java
+++ b/src/main/java/org/apache/commons/net/examples/mail/IMAPExportMbox.java
@@ -18,12 +18,15 @@
package org.apache.commons.net.examples.mail;
import java.io.BufferedWriter;
-import java.io.File;
-import java.io.FileWriter;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.net.URISyntaxException;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
@@ -318,20 +321,23 @@ public final class IMAPExportMbox {
if (file.equals("-")) {
mboxListener = null;
} else if (file.startsWith("+")) {
- final File mbox = new File(file.substring(1));
- System.out.println("Appending to file " + mbox);
- mboxListener = new MboxListener(new BufferedWriter(new FileWriter(mbox, true)), eol, printHash, printMarker, checkSequence);
+ final Path mboxPath = Paths.get(file.substring(1));
+ System.out.println("Appending to file " + mboxPath);
+ mboxListener = new MboxListener(Files.newBufferedWriter(mboxPath, Charset.defaultCharset(), StandardOpenOption.CREATE, StandardOpenOption.APPEND),
+ eol, printHash, printMarker, checkSequence);
} else if (file.startsWith("-")) {
- final File mbox = new File(file.substring(1));
- System.out.println("Writing to file " + mbox);
- mboxListener = new MboxListener(new BufferedWriter(new FileWriter(mbox, false)), eol, printHash, printMarker, checkSequence);
+ final Path mboxPath = Paths.get(file.substring(1));
+ System.out.println("Writing to file " + mboxPath);
+ mboxListener = new MboxListener(Files.newBufferedWriter(mboxPath, Charset.defaultCharset(), StandardOpenOption.CREATE), eol, printHash, printMarker,
+ checkSequence);
} else {
- final File mboxFile = new File(file);
- if (mboxFile.exists() && mboxFile.length() > 0) {
- throw new IOException("mailbox file: " + mboxFile + " already exists and is non-empty!");
+ final Path mboxPath = Paths.get(file);
+ if (Files.exists(mboxPath) && Files.size(mboxPath) > 0) {
+ throw new IOException("mailbox file: " + mboxPath + " already exists and is non-empty!");
}
- System.out.println("Creating file " + mboxFile);
- mboxListener = new MboxListener(new BufferedWriter(new FileWriter(mboxFile)), eol, printHash, printMarker, checkSequence);
+ System.out.println("Creating file " + mboxPath);
+ mboxListener = new MboxListener(Files.newBufferedWriter(mboxPath, Charset.defaultCharset(), StandardOpenOption.CREATE), eol, printHash, printMarker,
+ checkSequence);
}
final String path = uri.getPath();
diff --git a/src/main/java/org/apache/commons/net/examples/mail/IMAPImportMbox.java b/src/main/java/org/apache/commons/net/examples/mail/IMAPImportMbox.java
index 3c12ee0c..645bce81 100644
--- a/src/main/java/org/apache/commons/net/examples/mail/IMAPImportMbox.java
+++ b/src/main/java/org/apache/commons/net/examples/mail/IMAPImportMbox.java
@@ -19,9 +19,11 @@ package org.apache.commons.net.examples.mail;
import java.io.BufferedReader;
import java.io.File;
-import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
@@ -133,30 +135,28 @@ public final class IMAPImportMbox {
int loaded = 0;
try {
imap.setSoTimeout(6000);
-
- final BufferedReader br = new BufferedReader(new FileReader(file)); // TODO charset?
-
- String line;
- final StringBuilder sb = new StringBuilder();
boolean wanted = false; // Skip any leading rubbish
- while ((line = br.readLine()) != null) {
- if (line.startsWith("From ")) { // start of message; i.e. end of previous (if any)
- if (process(sb, imap, folder, total)) { // process previous message (if any)
- loaded++;
+ final StringBuilder sb = new StringBuilder();
+ try (BufferedReader br = Files.newBufferedReader(Paths.get(file), Charset.defaultCharset())) {
+ String line;
+ while ((line = br.readLine()) != null) {
+ if (line.startsWith("From ")) { // start of message; i.e. end of previous (if any)
+ if (process(sb, imap, folder, total)) { // process previous message (if any)
+ loaded++;
+ }
+ sb.setLength(0);
+ total++;
+ wanted = wanted(total, line, msgNums, contains);
+ } else if (startsWith(line, PATFROM)) { // Unescape ">+From " in body text
+ line = line.substring(1);
+ }
+ // TODO process first Received: line to determine arrival date?
+ if (wanted) {
+ sb.append(line);
+ sb.append(CRLF);
}
- sb.setLength(0);
- total++;
- wanted = wanted(total, line, msgNums, contains);
- } else if (startsWith(line, PATFROM)) { // Unescape ">+From " in body text
- line = line.substring(1);
- }
- // TODO process first Received: line to determine arrival date?
- if (wanted) {
- sb.append(line);
- sb.append(CRLF);
}
}
- br.close();
if (wanted && process(sb, imap, folder, total)) { // last message (if any)
loaded++;
}
diff --git a/src/main/java/org/apache/commons/net/examples/mail/POP3Mail.java b/src/main/java/org/apache/commons/net/examples/mail/POP3Mail.java
index 5a60e4e1..25075228 100644
--- a/src/main/java/org/apache/commons/net/examples/mail/POP3Mail.java
+++ b/src/main/java/org/apache/commons/net/examples/mail/POP3Mail.java
@@ -19,10 +19,10 @@ package org.apache.commons.net.examples.mail;
import java.io.BufferedReader;
import java.io.IOException;
-import java.io.PrintWriter;
import java.util.Locale;
import org.apache.commons.net.PrintCommandListener;
+import org.apache.commons.net.io.Util;
import org.apache.commons.net.pop3.POP3Client;
import org.apache.commons.net.pop3.POP3MessageInfo;
import org.apache.commons.net.pop3.POP3SClient;
@@ -77,7 +77,7 @@ public final class POP3Mail {
pop3.setDefaultTimeout(60000);
// suppress login details
- pop3.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
+ pop3.addProtocolCommandListener(new PrintCommandListener(Util.newPrintWriter(System.out), true));
try {
pop3.connect(server);
diff --git a/src/main/java/org/apache/commons/net/examples/mail/SMTPMail.java b/src/main/java/org/apache/commons/net/examples/mail/SMTPMail.java
index 366c7b48..3c699a99 100644
--- a/src/main/java/org/apache/commons/net/examples/mail/SMTPMail.java
+++ b/src/main/java/org/apache/commons/net/examples/mail/SMTPMail.java
@@ -19,11 +19,12 @@ package org.apache.commons.net.examples.mail;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
-import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
-import java.io.PrintWriter;
import java.io.Writer;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
@@ -49,7 +50,7 @@ public final class SMTPMail {
String cc;
final List<String> ccList = new ArrayList<>();
final BufferedReader stdin;
- FileReader fileReader = null;
+ BufferedReader fileReader = null;
final Writer writer;
final SimpleSMTPHeader header;
final SMTPClient client;
@@ -61,7 +62,7 @@ public final class SMTPMail {
server = args[0];
- stdin = new BufferedReader(new InputStreamReader(System.in));
+ stdin = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
try {
System.out.print("From: ");
@@ -101,13 +102,13 @@ public final class SMTPMail {
fileName = stdin.readLine();
try {
- fileReader = new FileReader(fileName);
+ fileReader = Files.newBufferedReader(Paths.get(fileName), Charset.defaultCharset());
} catch (final FileNotFoundException e) {
System.err.println("File not found. " + e.getMessage());
}
client = new SMTPClient();
- client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
+ client.addProtocolCommandListener(new PrintCommandListener(Util.newPrintWriter(System.out), true));
client.connect(server);
diff --git a/src/main/java/org/apache/commons/net/examples/mail/Utils.java b/src/main/java/org/apache/commons/net/examples/mail/Utils.java
index 5a41d4de..55ea94f1 100644
--- a/src/main/java/org/apache/commons/net/examples/mail/Utils.java
+++ b/src/main/java/org/apache/commons/net/examples/mail/Utils.java
@@ -21,6 +21,7 @@ import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;
+import java.nio.charset.Charset;
import java.util.Locale;
/**
@@ -40,7 +41,7 @@ final class Utils {
*/
static String getPassword(final String user, String password) throws IOException {
if ("-".equals(password)) { // stdin
- final BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
+ final BufferedReader in = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
password = in.readLine();
} else if ("*".equals(password)) { // console
final Console con = System.console(); // Java 1.6
diff --git a/src/main/java/org/apache/commons/net/examples/nntp/ArticleReader.java b/src/main/java/org/apache/commons/net/examples/nntp/ArticleReader.java
index 7f8a1e55..b7872a0f 100644
--- a/src/main/java/org/apache/commons/net/examples/nntp/ArticleReader.java
+++ b/src/main/java/org/apache/commons/net/examples/nntp/ArticleReader.java
@@ -19,10 +19,10 @@ package org.apache.commons.net.examples.nntp;
import java.io.BufferedReader;
import java.io.IOException;
-import java.io.PrintWriter;
import java.net.SocketException;
import org.apache.commons.net.PrintCommandListener;
+import org.apache.commons.net.io.Util;
import org.apache.commons.net.nntp.NNTPClient;
import org.apache.commons.net.nntp.NewsgroupInfo;
@@ -44,7 +44,7 @@ public class ArticleReader {
final String articleSpec = args.length >= 3 ? args[2] : null;
final NNTPClient client = new NNTPClient();
- client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
+ client.addProtocolCommandListener(new PrintCommandListener(Util.newPrintWriter(System.out), true));
client.connect(hostname);
if (args.length == 5) { // Optional auth
diff --git a/src/main/java/org/apache/commons/net/examples/nntp/ExtendedNNTPOps.java b/src/main/java/org/apache/commons/net/examples/nntp/ExtendedNNTPOps.java
index c36f050f..94523dbd 100644
--- a/src/main/java/org/apache/commons/net/examples/nntp/ExtendedNNTPOps.java
+++ b/src/main/java/org/apache/commons/net/examples/nntp/ExtendedNNTPOps.java
@@ -18,9 +18,9 @@
package org.apache.commons.net.examples.nntp;
import java.io.IOException;
-import java.io.PrintWriter;
import org.apache.commons.net.PrintCommandListener;
+import org.apache.commons.net.io.Util;
import org.apache.commons.net.nntp.Article;
import org.apache.commons.net.nntp.NNTPClient;
import org.apache.commons.net.nntp.NewsgroupInfo;
@@ -47,7 +47,7 @@ public class ExtendedNNTPOps {
public ExtendedNNTPOps() {
client = new NNTPClient();
- client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
+ client.addProtocolCommandListener(new PrintCommandListener(Util.newPrintWriter(System.out), true));
}
private void demo(final String host, final String user, final String password) {
diff --git a/src/main/java/org/apache/commons/net/examples/nntp/MessageThreading.java b/src/main/java/org/apache/commons/net/examples/nntp/MessageThreading.java
index 54a5653e..7711f69c 100644
--- a/src/main/java/org/apache/commons/net/examples/nntp/MessageThreading.java
+++ b/src/main/java/org/apache/commons/net/examples/nntp/MessageThreading.java
@@ -18,10 +18,10 @@
package org.apache.commons.net.examples.nntp;
import java.io.IOException;
-import java.io.PrintWriter;
import java.net.SocketException;
import org.apache.commons.net.PrintCommandListener;
+import org.apache.commons.net.io.Util;
import org.apache.commons.net.nntp.Article;
import org.apache.commons.net.nntp.NNTPClient;
import org.apache.commons.net.nntp.NewsgroupInfo;
@@ -42,7 +42,7 @@ public class MessageThreading {
final String newsgroup = args[1];
final NNTPClient client = new NNTPClient();
- client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
+ client.addProtocolCommandListener(new PrintCommandListener(Util.newPrintWriter(System.out), true));
client.connect(hostname);
if (args.length == 4) { // Optional auth
diff --git a/src/main/java/org/apache/commons/net/examples/nntp/PostMessage.java b/src/main/java/org/apache/commons/net/examples/nntp/PostMessage.java
index 9b2f7168..38e3cda1 100644
--- a/src/main/java/org/apache/commons/net/examples/nntp/PostMessage.java
+++ b/src/main/java/org/apache/commons/net/examples/nntp/PostMessage.java
@@ -19,11 +19,13 @@ package org.apache.commons.net.examples.nntp;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
-import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
-import java.io.PrintWriter;
+import java.io.Reader;
import java.io.Writer;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Paths;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.io.Util;
@@ -47,7 +49,7 @@ public final class PostMessage {
final String organization;
final String references;
final BufferedReader stdin;
- FileReader fileReader = null;
+ Reader fileReader = null;
final SimpleNNTPHeader header;
final NNTPClient client;
@@ -58,7 +60,7 @@ public final class PostMessage {
server = args[0];
- stdin = new BufferedReader(new InputStreamReader(System.in));
+ stdin = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
try {
System.out.print("From: ");
@@ -123,14 +125,14 @@ public final class PostMessage {
fileName = stdin.readLine();
try {
- fileReader = new FileReader(fileName);
+ fileReader = Files.newBufferedReader(Paths.get(fileName), Charset.defaultCharset());
} catch (final FileNotFoundException e) {
System.err.println("File not found. " + e.getMessage());
System.exit(1);
}
client = new NNTPClient();
- client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
+ client.addProtocolCommandListener(new PrintCommandListener(Util.newPrintWriter(System.out), true));
client.connect(server);
diff --git a/src/main/java/org/apache/commons/net/examples/telnet/TelnetClientExample.java b/src/main/java/org/apache/commons/net/examples/telnet/TelnetClientExample.java
index 1bfda6ea..1c315a66 100644
--- a/src/main/java/org/apache/commons/net/examples/telnet/TelnetClientExample.java
+++ b/src/main/java/org/apache/commons/net/examples/telnet/TelnetClientExample.java
@@ -21,6 +21,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.nio.charset.Charset;
import java.time.Duration;
import java.util.StringTokenizer;
@@ -106,13 +107,14 @@ public class TelnetClientExample implements Runnable, TelnetNotificationHandler
final OutputStream outstr = tc.getOutputStream();
final byte[] buff = new byte[1024];
- int ret_read = 0;
+ int readCount = 0;
do {
try {
- ret_read = System.in.read(buff);
- if (ret_read > 0) {
- final String line = new String(buff, 0, ret_read); // deliberate use of default charset
+ readCount = System.in.read(buff);
+ if (readCount > 0) {
+ final Charset charset = Charset.defaultCharset();
+ final String line = new String(buff, 0, readCount, charset); // deliberate use of default charset
if (line.startsWith("AYT")) {
try {
System.out.println("Sending AYT");
@@ -128,7 +130,7 @@ public class TelnetClientExample implements Runnable, TelnetNotificationHandler
+ tc.getRemoteOptionState(ii));
}
} else if (line.startsWith("REGISTER")) {
- final StringTokenizer st = new StringTokenizer(new String(buff));
+ final StringTokenizer st = new StringTokenizer(new String(buff, charset));
try {
st.nextToken();
final int opcode = Integer.parseInt(st.nextToken());
@@ -149,7 +151,7 @@ public class TelnetClientExample implements Runnable, TelnetNotificationHandler
}
}
} else if (line.startsWith("UNREGISTER")) {
- final StringTokenizer st = new StringTokenizer(new String(buff));
+ final StringTokenizer st = new StringTokenizer(new String(buff, charset));
try {
st.nextToken();
final int opcode = Integer.parseInt(st.nextToken());
@@ -177,7 +179,7 @@ public class TelnetClientExample implements Runnable, TelnetNotificationHandler
outstr.flush();
} else {
try {
- outstr.write(buff, 0, ret_read);
+ outstr.write(buff, 0, readCount);
outstr.flush();
} catch (final IOException e) {
end_loop = true;
@@ -188,7 +190,7 @@ public class TelnetClientExample implements Runnable, TelnetNotificationHandler
System.err.println("Exception while reading keyboard:" + e.getMessage());
end_loop = true;
}
- } while (ret_read > 0 && !end_loop);
+ } while (readCount > 0 && !end_loop);
try {
tc.disconnect();
@@ -243,14 +245,14 @@ public class TelnetClientExample implements Runnable, TelnetNotificationHandler
try {
final byte[] buff = new byte[1024];
- int ret_read = 0;
+ int readCount = 0;
do {
- ret_read = instr.read(buff);
- if (ret_read > 0) {
- System.out.print(new String(buff, 0, ret_read));
+ readCount = instr.read(buff);
+ if (readCount > 0) {
+ System.out.print(new String(buff, 0, readCount, Charset.defaultCharset()));
}
- } while (ret_read >= 0);
+ } while (readCount >= 0);
} catch (final IOException e) {
System.err.println("Exception while reading socket:" + e.getMessage());
}
diff --git a/src/main/java/org/apache/commons/net/examples/unix/chargen.java b/src/main/java/org/apache/commons/net/examples/unix/chargen.java
index cbefbddb..928dfda6 100644
--- a/src/main/java/org/apache/commons/net/examples/unix/chargen.java
+++ b/src/main/java/org/apache/commons/net/examples/unix/chargen.java
@@ -23,6 +23,7 @@ import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.net.InetAddress;
import java.net.SocketException;
+import java.nio.charset.Charset;
import java.time.Duration;
import org.apache.commons.net.chargen.CharGenTCPClient;
@@ -45,7 +46,7 @@ public final class chargen {
// We want to timeout if a response takes longer than 60 seconds
client.setDefaultTimeout(60000);
client.connect(host);
- try (final BufferedReader chargenInput = new BufferedReader(new InputStreamReader(client.getInputStream()))) {
+ try (final BufferedReader chargenInput = new BufferedReader(new InputStreamReader(client.getInputStream(), Charset.defaultCharset()))) {
// We assume the chargen service outputs lines, but it really doesn't
// have to, so this code might actually not work if no newlines are
diff --git a/src/main/java/org/apache/commons/net/tftp/TFTPErrorPacket.java b/src/main/java/org/apache/commons/net/tftp/TFTPErrorPacket.java
index 02d420ca..60ca88a4 100644
--- a/src/main/java/org/apache/commons/net/tftp/TFTPErrorPacket.java
+++ b/src/main/java/org/apache/commons/net/tftp/TFTPErrorPacket.java
@@ -19,6 +19,7 @@ package org.apache.commons.net.tftp;
import java.net.DatagramPacket;
import java.net.InetAddress;
+import java.nio.charset.Charset;
/**
* A final class derived from TFTPPacket defining the TFTP Error packet type.
@@ -157,7 +158,7 @@ public final class TFTPErrorPacket extends TFTPPacket {
data[2] = (byte) ((error & 0xffff) >> 8);
data[3] = (byte) (error & 0xff);
- System.arraycopy(message.getBytes(), 0, data, 4, length);
+ System.arraycopy(message.getBytes(Charset.defaultCharset()), 0, data, 4, length);
data[length + 4] = 0;
@@ -183,7 +184,7 @@ public final class TFTPErrorPacket extends TFTPPacket {
data[2] = (byte) ((error & 0xffff) >> 8);
data[3] = (byte) (error & 0xff);
- System.arraycopy(message.getBytes(), 0, data, 4, length);
+ System.arraycopy(message.getBytes(Charset.defaultCharset()), 0, data, 4, length);
data[length + 4] = 0;
diff --git a/src/main/java/org/apache/commons/net/tftp/TFTPRequestPacket.java b/src/main/java/org/apache/commons/net/tftp/TFTPRequestPacket.java
index 1452e3df..64ec423c 100644
--- a/src/main/java/org/apache/commons/net/tftp/TFTPRequestPacket.java
+++ b/src/main/java/org/apache/commons/net/tftp/TFTPRequestPacket.java
@@ -19,6 +19,7 @@ package org.apache.commons.net.tftp;
import java.net.DatagramPacket;
import java.net.InetAddress;
+import java.nio.charset.Charset;
/**
* An abstract class derived from TFTPPacket definiing a TFTP Request packet type. It is subclassed by the
@@ -169,7 +170,7 @@ public abstract class TFTPRequestPacket extends TFTPPacket {
data = new byte[fileLength + modeLength + 4];
data[0] = 0;
data[1] = (byte) type;
- System.arraycopy(fileName.getBytes(), 0, data, 2, fileLength);
+ System.arraycopy(fileName.getBytes(Charset.defaultCharset()), 0, data, 2, fileLength);
data[fileLength + 2] = 0;
System.arraycopy(modeBytes[mode], 0, data, fileLength + 3, modeLength);
@@ -194,7 +195,7 @@ public abstract class TFTPRequestPacket extends TFTPPacket {
data[0] = 0;
data[1] = (byte) type;
- System.arraycopy(fileName.getBytes(), 0, data, 2, fileLength);
+ System.arraycopy(fileName.getBytes(Charset.defaultCharset()), 0, data, 2, fileLength);
data[fileLength + 2] = 0;
System.arraycopy(modeBytes[mode], 0, data, fileLength + 3, modeLength);