Skip to content

Commit d4de715

Browse files
committed
Merge if statements that have identical blocks
Sort imports
1 parent e389ada commit d4de715

11 files changed

Lines changed: 26 additions & 119 deletions

File tree

src/main/java/org/apache/commons/net/SocketClient.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -562,25 +562,13 @@ public boolean getTcpNoDelay() throws SocketException {
562562
public boolean isAvailable() {
563563
if (isConnected()) {
564564
try {
565-
if (_socket_.getInetAddress() == null) {
566-
return false;
567-
}
568-
if (_socket_.getPort() == 0) {
569-
return false;
570-
}
571-
if (_socket_.getRemoteSocketAddress() == null) {
572-
return false;
573-
}
574-
if (_socket_.isClosed()) {
565+
if (_socket_.getInetAddress() == null || _socket_.getPort() == 0 || _socket_.getRemoteSocketAddress() == null || _socket_.isClosed()) {
575566
return false;
576567
}
577568
/*
578569
* these aren't exact checks (a Socket can be half-open), but since we usually require two-way data transfer, we check these here too:
579570
*/
580-
if (_socket_.isInputShutdown()) {
581-
return false;
582-
}
583-
if (_socket_.isOutputShutdown()) {
571+
if (_socket_.isInputShutdown() || _socket_.isOutputShutdown()) {
584572
return false;
585573
}
586574
/* ignore the result, catch exceptions: */

src/main/java/org/apache/commons/net/ftp/FTPClient.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -732,10 +732,7 @@ protected Socket _openDataConnection_(final String command, final String arg) th
732732
} else if (!FTPReply.isPositiveCompletion(port(getReportHostAddress(), server.getLocalPort()))) {
733733
return null;
734734
}
735-
if (restartOffset > 0 && !restart(restartOffset)) {
736-
return null;
737-
}
738-
if (!FTPReply.isPositivePreliminary(sendCommand(command, arg))) {
735+
if (restartOffset > 0 && !restart(restartOffset) || !FTPReply.isPositivePreliminary(sendCommand(command, arg))) {
739736
return null;
740737
}
741738
// For now, let's just use the data timeout value for waiting for
@@ -770,11 +767,8 @@ protected Socket _openDataConnection_(final String command, final String arg) th
770767
if (attemptEPSV && epsv() == FTPReply.ENTERING_EPSV_MODE) {
771768
_parseExtendedPassiveModeReply(_replyLines.get(0));
772769
} else {
773-
if (isInet6Address) {
774-
return null; // Must use EPSV for IPV6
775-
}
776770
// If EPSV failed on IPV4, revert to PASV
777-
if (pasv() != FTPReply.ENTERING_PASSIVE_MODE) {
771+
if (isInet6Address || pasv() != FTPReply.ENTERING_PASSIVE_MODE) {
778772
return null;
779773
}
780774
_parsePassiveModeReply(_replyLines.get(0));
@@ -797,11 +791,7 @@ protected Socket _openDataConnection_(final String command, final String arg) th
797791
socket.setSoTimeout(soTimeoutMillis);
798792
}
799793
socket.connect(new InetSocketAddress(passiveHost, passivePort), connectTimeout);
800-
if (restartOffset > 0 && !restart(restartOffset)) {
801-
socket.close();
802-
return null;
803-
}
804-
if (!FTPReply.isPositivePreliminary(sendCommand(command, arg))) {
794+
if (restartOffset > 0 && !restart(restartOffset) || !FTPReply.isPositivePreliminary(sendCommand(command, arg))) {
805795
socket.close();
806796
return null;
807797
}

src/main/java/org/apache/commons/net/ftp/FTPHTTPClient.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,8 @@ protected Socket _openDataConnection_(final String command, final String arg) th
133133
_parseExtendedPassiveModeReply(_replyLines.get(0));
134134
passiveHost = tunnelHost;
135135
} else {
136-
if (isInet6Address) {
137-
return null; // Must use EPSV for IPV6
138-
}
139136
// If EPSV failed on IPV4, revert to PASV
140-
if (pasv() != FTPReply.ENTERING_PASSIVE_MODE) {
137+
if (isInet6Address || pasv() != FTPReply.ENTERING_PASSIVE_MODE) {
141138
return null;
142139
}
143140
_parsePassiveModeReply(_replyLines.get(0));
@@ -148,12 +145,7 @@ protected Socket _openDataConnection_(final String command, final String arg) th
148145
final InputStream is = socket.getInputStream();
149146
final OutputStream os = socket.getOutputStream();
150147
tunnelHandshake(passiveHost, getPassivePort(), is, os);
151-
if (getRestartOffset() > 0 && !restart(getRestartOffset())) {
152-
socket.close();
153-
return null;
154-
}
155-
156-
if (!FTPReply.isPositivePreliminary(sendCommand(command, arg))) {
148+
if (getRestartOffset() > 0 && !restart(getRestartOffset()) || !FTPReply.isPositivePreliminary(sendCommand(command, arg))) {
157149
socket.close();
158150
return null;
159151
}

src/main/java/org/apache/commons/net/ftp/FTPSClient.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -816,11 +816,7 @@ private Socket openDataSecureConnection(final String command, final String arg)
816816
return null;
817817
}
818818

819-
if (getRestartOffset() > 0 && !restart(getRestartOffset())) {
820-
return null;
821-
}
822-
823-
if (!FTPReply.isPositivePreliminary(sendCommand(command, arg))) {
819+
if (getRestartOffset() > 0 && !restart(getRestartOffset()) || !FTPReply.isPositivePreliminary(sendCommand(command, arg))) {
824820
return null;
825821
}
826822

@@ -857,11 +853,8 @@ private Socket openDataSecureConnection(final String command, final String arg)
857853
if (attemptEPSV && epsv() == FTPReply.ENTERING_EPSV_MODE) {
858854
_parseExtendedPassiveModeReply(_replyLines.get(0));
859855
} else {
860-
if (isInet6Address) {
861-
return null; // Must use EPSV for IPV6
862-
}
863856
// If EPSV failed on IPV4, revert to PASV
864-
if (pasv() != FTPReply.ENTERING_PASSIVE_MODE) {
857+
if (isInet6Address || pasv() != FTPReply.ENTERING_PASSIVE_MODE) {
865858
return null;
866859
}
867860
_parsePassiveModeReply(_replyLines.get(0));
@@ -897,12 +890,7 @@ private Socket openDataSecureConnection(final String command, final String arg)
897890
sslSocket = context.getSocketFactory().createSocket(socket, getPassiveHost(), getPassivePort(), true);
898891
}
899892

900-
if (getRestartOffset() > 0 && !restart(getRestartOffset())) {
901-
closeSockets(socket, sslSocket);
902-
return null;
903-
}
904-
905-
if (!FTPReply.isPositivePreliminary(sendCommand(command, arg))) {
893+
if (getRestartOffset() > 0 && !restart(getRestartOffset()) || !FTPReply.isPositivePreliminary(sendCommand(command, arg))) {
906894
closeSockets(socket, sslSocket);
907895
return null;
908896
}

src/main/java/org/apache/commons/net/ftp/parser/OS400FTPEntryParser.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,7 @@ public FTPFile parseFTPEntry(final String entry) {
334334
mustScanForPathSeparator = false;
335335
type = FTPFile.FILE_TYPE;
336336

337-
if (isNullOrEmpty(name)) {
338-
return null;
339-
}
340-
if (!(isNullOrEmpty(fileSize) && isNullOrEmpty(datestr))) {
337+
if (isNullOrEmpty(name) || !(isNullOrEmpty(fileSize) && isNullOrEmpty(datestr))) {
341338
return null;
342339
}
343340

src/main/java/org/apache/commons/net/imap/IMAPClient.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,7 @@ public boolean list(final String refName, final String mailboxName) throws IOExc
450450
* @throws IOException If a network I/O error occurs in the process of logging in.
451451
*/
452452
public boolean login(final String user, final String password) throws IOException {
453-
if (getState() != IMAP.IMAPState.NOT_AUTH_STATE) {
454-
return false;
455-
}
456-
457-
if (!doCommand(IMAPCommand.LOGIN, user + " " + password)) {
453+
if (getState() != IMAP.IMAPState.NOT_AUTH_STATE || !doCommand(IMAPCommand.LOGIN, user + " " + password)) {
458454
return false;
459455
}
460456

src/main/java/org/apache/commons/net/pop3/POP3Client.java

Lines changed: 10 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,7 @@ public boolean deleteMessage(final int messageId) throws IOException {
150150
* @throws IOException If a network I/O error occurs in the process of sending the list command.
151151
*/
152152
public POP3MessageInfo listMessage(final int messageId) throws IOException {
153-
if (getState() != TRANSACTION_STATE) {
154-
return null;
155-
}
156-
if (sendCommand(POP3Command.LIST, Integer.toString(messageId)) != POP3Reply.OK) {
153+
if (getState() != TRANSACTION_STATE || sendCommand(POP3Command.LIST, Integer.toString(messageId)) != POP3Reply.OK) {
157154
return null;
158155
}
159156
return parseStatus(lastReplyLine.substring(3));
@@ -169,10 +166,7 @@ public POP3MessageInfo listMessage(final int messageId) throws IOException {
169166
* @throws IOException If a network I/O error occurs in the process of sending the list command.
170167
*/
171168
public POP3MessageInfo[] listMessages() throws IOException {
172-
if (getState() != TRANSACTION_STATE) {
173-
return null;
174-
}
175-
if (sendCommand(POP3Command.LIST) != POP3Reply.OK) {
169+
if (getState() != TRANSACTION_STATE || sendCommand(POP3Command.LIST) != POP3Reply.OK) {
176170
return null;
177171
}
178172
getAdditionalReply();
@@ -199,10 +193,7 @@ public POP3MessageInfo[] listMessages() throws IOException {
199193
* @throws IOException If a network I/O error occurs in the process of sending the list unique identifier command.
200194
*/
201195
public POP3MessageInfo listUniqueIdentifier(final int messageId) throws IOException {
202-
if (getState() != TRANSACTION_STATE) {
203-
return null;
204-
}
205-
if (sendCommand(POP3Command.UIDL, Integer.toString(messageId)) != POP3Reply.OK) {
196+
if (getState() != TRANSACTION_STATE || sendCommand(POP3Command.UIDL, Integer.toString(messageId)) != POP3Reply.OK) {
206197
return null;
207198
}
208199
return parseUID(lastReplyLine.substring(3));
@@ -218,10 +209,7 @@ public POP3MessageInfo listUniqueIdentifier(final int messageId) throws IOExcept
218209
* @throws IOException If a network I/O error occurs in the process of sending the list unique identifier command.
219210
*/
220211
public POP3MessageInfo[] listUniqueIdentifiers() throws IOException {
221-
if (getState() != TRANSACTION_STATE) {
222-
return null;
223-
}
224-
if (sendCommand(POP3Command.UIDL) != POP3Reply.OK) {
212+
if (getState() != TRANSACTION_STATE || sendCommand(POP3Command.UIDL) != POP3Reply.OK) {
225213
return null;
226214
}
227215
getAdditionalReply();
@@ -248,20 +236,11 @@ public POP3MessageInfo[] listUniqueIdentifiers() throws IOException {
248236
* @throws IOException If a network I/O error occurs in the process of logging in.
249237
*/
250238
public boolean login(final String user, final String password) throws IOException {
251-
if (getState() != AUTHORIZATION_STATE) {
252-
return false;
253-
}
254-
255-
if (sendCommand(POP3Command.USER, user) != POP3Reply.OK) {
239+
if (getState() != AUTHORIZATION_STATE || sendCommand(POP3Command.USER, user) != POP3Reply.OK
240+
|| sendCommand(POP3Command.PASS, password) != POP3Reply.OK) {
256241
return false;
257242
}
258-
259-
if (sendCommand(POP3Command.PASS, password) != POP3Reply.OK) {
260-
return false;
261-
}
262-
263243
setState(TRANSACTION_STATE);
264-
265244
return true;
266245
}
267246

@@ -291,35 +270,28 @@ public boolean login(final String user, String timestamp, final String secret) t
291270
final StringBuilder buffer;
292271
final StringBuilder digestBuffer;
293272
final MessageDigest md5;
294-
295273
if (getState() != AUTHORIZATION_STATE) {
296274
return false;
297275
}
298-
299276
md5 = MessageDigest.getInstance("MD5");
300277
timestamp += secret;
301278
digest = md5.digest(timestamp.getBytes(getCharset()));
302279
digestBuffer = new StringBuilder(128);
303-
304280
for (i = 0; i < digest.length; i++) {
305281
final int digit = digest[i] & 0xff;
306282
if (digit <= 15) { // Add leading zero if necessary (NET-351)
307283
digestBuffer.append("0");
308284
}
309285
digestBuffer.append(Integer.toHexString(digit));
310286
}
311-
312287
buffer = new StringBuilder(256);
313288
buffer.append(user);
314289
buffer.append(' ');
315290
buffer.append(digestBuffer.toString());
316-
317291
if (sendCommand(POP3Command.APOP, buffer.toString()) != POP3Reply.OK) {
318292
return false;
319293
}
320-
321294
setState(TRANSACTION_STATE);
322-
323295
return true;
324296
}
325297

@@ -384,13 +356,9 @@ public boolean reset() throws IOException {
384356
* @throws IOException If a network I/O error occurs in the process of sending the retrieve message command.
385357
*/
386358
public Reader retrieveMessage(final int messageId) throws IOException {
387-
if (getState() != TRANSACTION_STATE) {
359+
if (getState() != TRANSACTION_STATE || sendCommand(POP3Command.RETR, Integer.toString(messageId)) != POP3Reply.OK) {
388360
return null;
389361
}
390-
if (sendCommand(POP3Command.RETR, Integer.toString(messageId)) != POP3Reply.OK) {
391-
return null;
392-
}
393-
394362
return new DotTerminatedMessageReader(reader);
395363
}
396364

@@ -412,13 +380,10 @@ public Reader retrieveMessage(final int messageId) throws IOException {
412380
* @throws IOException If a network I/O error occurs in the process of sending the top command.
413381
*/
414382
public Reader retrieveMessageTop(final int messageId, final int numLines) throws IOException {
415-
if (numLines < 0 || getState() != TRANSACTION_STATE) {
416-
return null;
417-
}
418-
if (sendCommand(POP3Command.TOP, Integer.toString(messageId) + " " + Integer.toString(numLines)) != POP3Reply.OK) {
383+
if (numLines < 0 || getState() != TRANSACTION_STATE
384+
|| sendCommand(POP3Command.TOP, Integer.toString(messageId) + " " + Integer.toString(numLines)) != POP3Reply.OK) {
419385
return null;
420386
}
421-
422387
return new DotTerminatedMessageReader(reader);
423388
}
424389

@@ -432,10 +397,7 @@ public Reader retrieveMessageTop(final int messageId, final int numLines) throws
432397
* @throws IOException If a network I/O error occurs in the process of sending the status command.
433398
*/
434399
public POP3MessageInfo status() throws IOException {
435-
if (getState() != TRANSACTION_STATE) {
436-
return null;
437-
}
438-
if (sendCommand(POP3Command.STAT) != POP3Reply.OK) {
400+
if (getState() != TRANSACTION_STATE || sendCommand(POP3Command.STAT) != POP3Reply.OK) {
439401
return null;
440402
}
441403
return parseStatus(lastReplyLine.substring(3));

src/main/java/org/apache/commons/net/smtp/SMTPClient.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,7 @@ public boolean sendShortMessageData(final String message) throws IOException {
346346
* @throws IOException If an I/O error occurs while either sending a command to the server or receiving a reply from the server.
347347
*/
348348
public boolean sendSimpleMessage(final String sender, final String recipient, final String message) throws IOException {
349-
if (!setSender(sender)) {
350-
return false;
351-
}
352-
if (!addRecipient(recipient)) {
349+
if (!setSender(sender) || !addRecipient(recipient)) {
353350
return false;
354351
}
355352
return sendShortMessageData(message);

src/test/java/org/apache/commons/net/echo/EchoTCPClientTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
package org.apache.commons.net.echo;
1919

20-
import static org.junit.jupiter.api.Assertions.assertNull;
2120
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertNull;
2222

2323
import java.io.IOException;
2424
import java.io.InputStream;

src/test/java/org/apache/commons/net/ftp/parser/AbstractFTPParseTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,7 @@ protected void doAdditionalGoodTests(final String test, final FTPFile f) {
9898
* @return null if f is null or the date is null
9999
*/
100100
protected FTPFile nullFileOrNullDate(final FTPFile f) {
101-
if (f == null) {
102-
return null;
103-
}
104-
if (f.getTimestamp() == null) {
101+
if (f == null || f.getTimestamp() == null) {
105102
return null;
106103
}
107104
return f;

0 commit comments

Comments
 (0)