Skip to content

Commit

Permalink
Minor Improvement:
Browse files Browse the repository at this point in the history
* Add final
* Field may be 'final'
* Use Standard Charset
* Unnecessary semicolon
* Change String concatenation in loop for StringBuilder
* Fix JavaDoc
  • Loading branch information
arturobernalg committed Dec 24, 2020
1 parent ccc1b48 commit 562ba39
Show file tree
Hide file tree
Showing 70 changed files with 201 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public ProtocolCommandSupport(final Object source)
*/
public void fireCommandSent(final String command, final String message)
{
ProtocolCommandEvent event;
final ProtocolCommandEvent event;

event = new ProtocolCommandEvent(source, command, message);

Expand All @@ -91,7 +91,7 @@ public void fireCommandSent(final String command, final String message)
*/
public void fireReplyReceived(final int replyCode, final String message)
{
ProtocolCommandEvent event;
final ProtocolCommandEvent event;
event = new ProtocolCommandEvent(source, replyCode, message);

for (final EventListener listener : listeners)
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/apache/commons/net/SocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,8 @@ public InetAddress getRemoteAddress()
*/
public boolean verifyRemote(final Socket socket)
{
InetAddress host1, host2;
final InetAddress host1;
final InetAddress host2;

host1 = socket.getInetAddress();
host2 = getRemoteAddress();
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/apache/commons/net/bsd/RCommandClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;

import org.apache.commons.net.io.SocketInputStream;

Expand Down Expand Up @@ -110,7 +111,7 @@ InputStream createErrorStream() throws IOException
{
int localPort;
ServerSocket server;
Socket socket;
final Socket socket;

localPort = MAX_CLIENT_PORT;
server = null; // Keep compiler from barfing
Expand All @@ -133,7 +134,7 @@ InputStream createErrorStream() throws IOException
throw new BindException("All ports in use.");
}

_output_.write(Integer.toString(server.getLocalPort()).getBytes("UTF-8")); // $NON-NLS
_output_.write(Integer.toString(server.getLocalPort()).getBytes(StandardCharsets.UTF_8)); // $NON-NLS
_output_.write(NULL_CHAR);
_output_.flush();

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/apache/commons/net/bsd/RExecClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

import org.apache.commons.net.SocketClient;
import org.apache.commons.net.io.SocketInputStream;
Expand Down Expand Up @@ -88,12 +89,12 @@ public class RExecClient extends SocketClient
// limitations of rcmd and rlogin
InputStream createErrorStream() throws IOException
{
ServerSocket server;
Socket socket;
final ServerSocket server;
final Socket socket;

server = _serverSocketFactory_.createServerSocket(0, 1, getLocalAddress());

_output_.write(Integer.toString(server.getLocalPort()).getBytes("UTF-8")); // $NON-NLS-1$
_output_.write(Integer.toString(server.getLocalPort()).getBytes(StandardCharsets.UTF_8)); // $NON-NLS-1$
_output_.write(NULL_CHAR);
_output_.flush();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ public void send(final InetAddress host) throws IOException
*/
public byte[] receive() throws IOException
{
int length;
byte[] result;
final int length;
final byte[] result;

_socket_.receive(receivePacket);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public String getTime() throws IOException
{
int read;
final StringBuilder result = new StringBuilder(buffer.length);
BufferedReader reader;
final BufferedReader reader;

reader = new BufferedReader(new InputStreamReader(_input_, getCharset()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public final class DaytimeUDPClient extends DatagramSocketClient
*/
public String getTime(final InetAddress host, final int port) throws IOException
{
DatagramPacket sendPacket, receivePacket;
final DatagramPacket sendPacket;
final DatagramPacket receivePacket;

sendPacket =
new DatagramPacket(dummyData, dummyData.length, host, port);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ else if (args[base].equals("-#")) {
ftp = new FTPClient();
}
} else {
FTPSClient ftps;
final FTPSClient ftps;
if (protocol.equals("true")) {
ftps = new FTPSClient(true);
} else if (protocol.equals("false")) {
Expand Down Expand Up @@ -328,7 +328,7 @@ else if (args[base].equals("-#")) {

try
{
int reply;
final int reply;
if (port > 0) {
ftp.connect(server, port);
} else {
Expand Down Expand Up @@ -397,7 +397,7 @@ else if (args[base].equals("-#")) {

if (storeFile)
{
try (InputStream input = new FileInputStream(local)) {
try (final InputStream input = new FileInputStream(local)) {
ftp.storeFile(remote, input);
}

Expand Down Expand Up @@ -501,7 +501,7 @@ else if (doCommand != null)
}
else
{
try (OutputStream output = new FileOutputStream(local)) {
try (final OutputStream output = new FileOutputStream(local)) {
ftp.retrieveFile(remote, output);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,19 @@ public final class ServerToServerFTP

public static void main(final String[] args)
{
String server1, username1, password1, file1;
String server2, username2, password2, file2;
String server1;
final String username1;
final String password1;
final String file1;
String server2;
final String username2;
final String password2;
final String file2;
String [] parts;
int port1=0, port2=0;
FTPClient ftp1, ftp2;
ProtocolCommandListener listener;
final FTPClient ftp1;
final FTPClient ftp2;
final ProtocolCommandListener listener;

if (args.length < 8)
{
Expand Down Expand Up @@ -83,7 +90,7 @@ public static void main(final String[] args)

try
{
int reply;
final int reply;
if (port1 > 0) {
ftp1.connect(server1, port1);
} else {
Expand Down Expand Up @@ -120,7 +127,7 @@ public static void main(final String[] args)

try
{
int reply;
final int reply;
if (port2 > 0) {
ftp2.connect(server2, port2);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ public static void main(final String[] args)
{
boolean receiveFile = true, closed;
int transferMode = TFTP.BINARY_MODE, argc;
String arg, hostname, localFilename, remoteFilename;
String arg;
final String hostname;
final String localFilename;
final String remoteFilename;
final TFTPClient tftp;
int timeout = 60000;
boolean verbose = false;
Expand Down Expand Up @@ -152,7 +155,7 @@ protected void trace(final String direction, final TFTPPacket packet) {

private static boolean send(final int transferMode, final String hostname, final String localFilename,
final String remoteFilename, final TFTPClient tftp) {
boolean closed;
final boolean closed;
FileInputStream input = null;

// Try to open local file for reading
Expand Down Expand Up @@ -201,9 +204,9 @@ private static boolean send(final int transferMode, final String hostname, final

private static boolean receive(final int transferMode, final String hostname, final String localFilename,
final String remoteFilename, final TFTPClient tftp) {
boolean closed;
final boolean closed;
FileOutputStream output = null;
File file;
final File file;

file = new File(localFilename);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static void main(final String[] args)
final String proto = argCount > 3 ? args[argIdx++] : null;
final boolean implicit = argCount > 4 ? Boolean.parseBoolean(args[argIdx++]) : false;

POP3Client pop3;
final POP3Client pop3;

if (proto != null) {
System.out.println("Using secure protocol: "+proto);
Expand All @@ -93,7 +93,7 @@ public static void main(final String[] args)
pop3 = new POP3Client();
}

int port;
final int port;
if (arg0.length == 2) {
port = Integer.parseInt(arg0[1]);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static void main(final String[] args)
final String proto = args.length > 3 ? args[3] : null;
final boolean implicit = args.length > 4 ? Boolean.parseBoolean(args[4]) : false;

POP3Client pop3;
final POP3Client pop3;

if (proto != null) {
System.out.println("Using secure protocol: "+proto);
Expand All @@ -88,7 +88,7 @@ public static void main(final String[] args)
pop3 = new POP3Client();
}

int port;
final int port;
if (arg0.length == 2) {
port = Integer.parseInt(arg0[1]);
} else {
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/org/apache/commons/net/examples/mail/SMTPMail.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,18 @@ public final class SMTPMail

public static void main(final String[] args)
{
String sender, recipient, subject, fileName, server, cc;
final String sender;
final String recipient;
final String subject;
final String fileName;
final String server;
String cc;
final List<String> ccList = new ArrayList<>();
BufferedReader stdin;
final BufferedReader stdin;
FileReader fileReader = null;
Writer writer;
SimpleSMTPHeader header;
SMTPClient client;
final Writer writer;
final SimpleSMTPHeader header;
final SMTPClient client;

if (args.length < 1)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static void main(final String[] args) throws SocketException, IOException
final NewsgroupInfo group = new NewsgroupInfo();
client.selectNewsgroup(newsgroup, group);

BufferedReader brHdr;
final BufferedReader brHdr;
String line;
if (articleSpec != null) {
brHdr = (BufferedReader) client.retrieveArticleHeader(articleSpec);
Expand All @@ -73,7 +73,7 @@ public static void main(final String[] args) throws SocketException, IOException
}
brHdr.close();
}
BufferedReader brBody;
final BufferedReader brBody;
if (articleSpec != null) {
brBody = (BufferedReader) client.retrieveArticleBody(articleSpec);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private void demo(final String host, final String user, final String password) {
}

public static void main(final String[] args) {
ExtendedNNTPOps ops;
final ExtendedNNTPOps ops;

final int argc = args.length;
if (argc < 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,17 @@ public final class PostMessage

public static void main(final String[] args)
{
String from, subject, newsgroup, fileName, server, organization;
String references;
BufferedReader stdin;
final String from;
final String subject;
String newsgroup;
final String fileName;
final String server;
final String organization;
final String references;
final BufferedReader stdin;
FileReader fileReader = null;
SimpleNNTPHeader header;
NNTPClient client;
final SimpleNNTPHeader header;
final NNTPClient client;

if (args.length < 1)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static void processResponse(final TimeInfo info)
{
final NtpV3Packet message = info.getMessage();
final int stratum = message.getStratum();
String refType;
final String refType;
if (stratum <= 0) {
refType = "(Unspecified or Unavailable)";
} else if (stratum == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static void main(final String[] args) throws Exception

final String remoteip = args[0];

int remoteport;
final int remoteport;

if (args.length > 1)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public final class WeatherTelnet

public static void main(final String[] args)
{
TelnetClient telnet;
final TelnetClient telnet;

telnet = new TelnetClient();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static void chargenTCP(final String host) throws IOException
// We want to timeout if a response takes longer than 60 seconds
client.setDefaultTimeout(60000);
client.connect(host);
try (BufferedReader chargenInput = new BufferedReader(new InputStreamReader(client.getInputStream()))) {
try (final BufferedReader chargenInput = new BufferedReader(new InputStreamReader(client.getInputStream()))) {

// 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
Expand All @@ -70,8 +70,8 @@ public static void chargenUDP(final String host) throws IOException
{
int packets = 50;
byte[] data;
InetAddress address;
CharGenUDPClient client;
final InetAddress address;
final CharGenUDPClient client;

address = InetAddress.getByName(host);
client = new CharGenUDPClient();
Expand Down
Loading

0 comments on commit 562ba39

Please sign in to comment.