< prev index next >

src/java.base/share/classes/sun/net/ftp/impl/FtpClient.java

Print this page
rev 52979 : 8215281: Use String.isEmpty() when applicable in java.base
Reviewed-by: TBD


 416 
 417         serverResponse.setSize(0);
 418         while (true) {
 419             while ((c = in.read()) != -1) {
 420                 if (c == '\r') {
 421                     if ((c = in.read()) != '\n') {
 422                         replyBuf.append('\r');
 423                     }
 424                 }
 425                 replyBuf.append((char) c);
 426                 if (c == '\n') {
 427                     break;
 428                 }
 429             }
 430             response = replyBuf.toString();
 431             replyBuf.setLength(0);
 432             if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
 433                 logger.finest("Server [" + serverAddr + "] --> " + response);
 434             }
 435 
 436             if (response.length() == 0) {
 437                 code = -1;
 438             } else {
 439                 try {
 440                     code = Integer.parseInt(response, 0, 3, 10);
 441                 } catch (NumberFormatException e) {
 442                     code = -1;
 443                 } catch (IndexOutOfBoundsException e) {
 444                     /* this line doesn't contain a response code, so
 445                     we just completely ignore it */
 446                     continue;
 447                 }
 448             }
 449             serverResponse.addElement(response);
 450             if (continuingCode != -1) {
 451                 /* we've seen a ###- sequence */
 452                 if (code != continuingCode ||
 453                         (response.length() >= 4 && response.charAt(3) == '-')) {
 454                     continue;
 455                 } else {
 456                     /* seen the end of code sequence */


1032          */
1033         if (lastReplyCode == FtpReplyCode.NEED_PASSWORD) {
1034             if ((password != null) && (password.length > 0)) {
1035                 issueCommandCheck("PASS " + String.valueOf(password));
1036             }
1037         }
1038     }
1039 
1040     /**
1041      * Attempts to log on the server with the specified user name and password.
1042      *
1043      * @param user The user name
1044      * @param password The password for that user
1045      * @return <code>true</code> if the login was successful.
1046      * @throws IOException if an error occurred during the transmission
1047      */
1048     public sun.net.ftp.FtpClient login(String user, char[] password) throws sun.net.ftp.FtpProtocolException, IOException {
1049         if (!isConnected()) {
1050             throw new sun.net.ftp.FtpProtocolException("Not connected yet", FtpReplyCode.BAD_SEQUENCE);
1051         }
1052         if (user == null || user.length() == 0) {
1053             throw new IllegalArgumentException("User name can't be null or empty");
1054         }
1055         tryLogin(user, password);
1056 
1057         // keep the welcome message around so we can
1058         // put it in the resulting HTML page.
1059         String l;
1060         StringBuilder sb = new StringBuilder();
1061         for (int i = 0; i < serverResponse.size(); i++) {
1062             l = serverResponse.elementAt(i);
1063             if (l != null) {
1064                 if (l.length() >= 4 && l.startsWith("230")) {
1065                     // get rid of the "230-" prefix
1066                     l = l.substring(4);
1067                 }
1068                 sb.append(l);
1069             }
1070         }
1071         welcomeMsg = sb.toString();
1072         loggedIn = true;
1073         return this;
1074     }
1075 
1076     /**
1077      * Attempts to log on the server with the specified user name, password and
1078      * account name.
1079      *
1080      * @param user The user name
1081      * @param password The password for that user.
1082      * @param account The account name for that user.
1083      * @return <code>true</code> if the login was successful.
1084      * @throws IOException if an error occurs during the transmission.
1085      */
1086     public sun.net.ftp.FtpClient login(String user, char[] password, String account) throws sun.net.ftp.FtpProtocolException, IOException {
1087 
1088         if (!isConnected()) {
1089             throw new sun.net.ftp.FtpProtocolException("Not connected yet", FtpReplyCode.BAD_SEQUENCE);
1090         }
1091         if (user == null || user.length() == 0) {
1092             throw new IllegalArgumentException("User name can't be null or empty");
1093         }
1094         tryLogin(user, password);
1095 
1096         /*
1097          * Checks for "332 Need account for login." answer
1098          */
1099         if (lastReplyCode == FtpReplyCode.NEED_ACCOUNT) {
1100             issueCommandCheck("ACCT " + account);
1101         }
1102 
1103         // keep the welcome message around so we can
1104         // put it in the resulting HTML page.
1105         StringBuilder sb = new StringBuilder();
1106         if (serverResponse != null) {
1107             for (String l : serverResponse) {
1108                 if (l != null) {
1109                     if (l.length() >= 4 && l.startsWith("230")) {
1110                         // get rid of the "230-" prefix
1111                         l = l.substring(4);


1135         disconnect();
1136     }
1137 
1138     /**
1139      * Checks whether the client is logged in to the server or not.
1140      *
1141      * @return <code>true</code> if the client has already completed a login.
1142      */
1143     public boolean isLoggedIn() {
1144         return loggedIn;
1145     }
1146 
1147     /**
1148      * Changes to a specific directory on a remote FTP server
1149      *
1150      * @param remoteDirectory path of the directory to CD to.
1151      * @return <code>true</code> if the operation was successful.
1152      * @exception <code>FtpProtocolException</code>
1153      */
1154     public sun.net.ftp.FtpClient changeDirectory(String remoteDirectory) throws sun.net.ftp.FtpProtocolException, IOException {
1155         if (remoteDirectory == null || "".equals(remoteDirectory)) {
1156             throw new IllegalArgumentException("directory can't be null or empty");
1157         }
1158 
1159         issueCommandCheck("CWD " + remoteDirectory);
1160         return this;
1161     }
1162 
1163     /**
1164      * Changes to the parent directory, sending the CDUP command to the server.
1165      *
1166      * @return <code>true</code> if the command was successful.
1167      * @throws IOException
1168      */
1169     public sun.net.ftp.FtpClient changeToParentDirectory() throws sun.net.ftp.FtpProtocolException, IOException {
1170         issueCommandCheck("CDUP");
1171         return this;
1172     }
1173 
1174     /**
1175      * Returns the server current working directory, or <code>null</code> if


1721         if (s != null) {
1722             return createInputStream(s.getInputStream());
1723         }
1724         return null;
1725     }
1726 
1727     /**
1728      * Issues the SIZE [path] command to the server to get the size of a
1729      * specific file on the server.
1730      * Note that this command may not be supported by the server. In which
1731      * case -1 will be returned.
1732      *
1733      * @param path a <code>String</code> containing the pathname of the
1734      *        file.
1735      * @return a <code>long</code> containing the size of the file or -1 if
1736      *         the server returned an error, which can be checked with
1737      *         {@link #getLastReplyCode()}.
1738      * @throws IOException if an error occurs during the transmission.
1739      */
1740     public long getSize(String path) throws sun.net.ftp.FtpProtocolException, IOException {
1741         if (path == null || path.length() == 0) {
1742             throw new IllegalArgumentException("path can't be null or empty");
1743         }
1744         issueCommandCheck("SIZE " + path);
1745         if (lastReplyCode == FtpReplyCode.FILE_STATUS) {
1746             String s = getResponseString();
1747             s = s.substring(4, s.length() - 1);
1748             return Long.parseLong(s);
1749         }
1750         return -1;
1751     }
1752     private static String[] MDTMformats = {
1753         "yyyyMMddHHmmss.SSS",
1754         "yyyyMMddHHmmss"
1755     };
1756     private static SimpleDateFormat[] dateFormats = new SimpleDateFormat[MDTMformats.length];
1757 
1758     static {
1759         for (int i = 0; i < MDTMformats.length; i++) {
1760             dateFormats[i] = new SimpleDateFormat(MDTMformats[i]);
1761             dateFormats[i].setTimeZone(TimeZone.getTimeZone("GMT"));




 416 
 417         serverResponse.setSize(0);
 418         while (true) {
 419             while ((c = in.read()) != -1) {
 420                 if (c == '\r') {
 421                     if ((c = in.read()) != '\n') {
 422                         replyBuf.append('\r');
 423                     }
 424                 }
 425                 replyBuf.append((char) c);
 426                 if (c == '\n') {
 427                     break;
 428                 }
 429             }
 430             response = replyBuf.toString();
 431             replyBuf.setLength(0);
 432             if (logger.isLoggable(PlatformLogger.Level.FINEST)) {
 433                 logger.finest("Server [" + serverAddr + "] --> " + response);
 434             }
 435 
 436             if (response.isEmpty()) {
 437                 code = -1;
 438             } else {
 439                 try {
 440                     code = Integer.parseInt(response, 0, 3, 10);
 441                 } catch (NumberFormatException e) {
 442                     code = -1;
 443                 } catch (IndexOutOfBoundsException e) {
 444                     /* this line doesn't contain a response code, so
 445                     we just completely ignore it */
 446                     continue;
 447                 }
 448             }
 449             serverResponse.addElement(response);
 450             if (continuingCode != -1) {
 451                 /* we've seen a ###- sequence */
 452                 if (code != continuingCode ||
 453                         (response.length() >= 4 && response.charAt(3) == '-')) {
 454                     continue;
 455                 } else {
 456                     /* seen the end of code sequence */


1032          */
1033         if (lastReplyCode == FtpReplyCode.NEED_PASSWORD) {
1034             if ((password != null) && (password.length > 0)) {
1035                 issueCommandCheck("PASS " + String.valueOf(password));
1036             }
1037         }
1038     }
1039 
1040     /**
1041      * Attempts to log on the server with the specified user name and password.
1042      *
1043      * @param user The user name
1044      * @param password The password for that user
1045      * @return <code>true</code> if the login was successful.
1046      * @throws IOException if an error occurred during the transmission
1047      */
1048     public sun.net.ftp.FtpClient login(String user, char[] password) throws sun.net.ftp.FtpProtocolException, IOException {
1049         if (!isConnected()) {
1050             throw new sun.net.ftp.FtpProtocolException("Not connected yet", FtpReplyCode.BAD_SEQUENCE);
1051         }
1052         if (user == null || user.isEmpty()) {
1053             throw new IllegalArgumentException("User name can't be null or empty");
1054         }
1055         tryLogin(user, password);
1056 
1057         // keep the welcome message around so we can
1058         // put it in the resulting HTML page.
1059         String l;
1060         StringBuilder sb = new StringBuilder();
1061         for (int i = 0; i < serverResponse.size(); i++) {
1062             l = serverResponse.elementAt(i);
1063             if (l != null) {
1064                 if (l.length() >= 4 && l.startsWith("230")) {
1065                     // get rid of the "230-" prefix
1066                     l = l.substring(4);
1067                 }
1068                 sb.append(l);
1069             }
1070         }
1071         welcomeMsg = sb.toString();
1072         loggedIn = true;
1073         return this;
1074     }
1075 
1076     /**
1077      * Attempts to log on the server with the specified user name, password and
1078      * account name.
1079      *
1080      * @param user The user name
1081      * @param password The password for that user.
1082      * @param account The account name for that user.
1083      * @return <code>true</code> if the login was successful.
1084      * @throws IOException if an error occurs during the transmission.
1085      */
1086     public sun.net.ftp.FtpClient login(String user, char[] password, String account) throws sun.net.ftp.FtpProtocolException, IOException {
1087 
1088         if (!isConnected()) {
1089             throw new sun.net.ftp.FtpProtocolException("Not connected yet", FtpReplyCode.BAD_SEQUENCE);
1090         }
1091         if (user == null || user.isEmpty()) {
1092             throw new IllegalArgumentException("User name can't be null or empty");
1093         }
1094         tryLogin(user, password);
1095 
1096         /*
1097          * Checks for "332 Need account for login." answer
1098          */
1099         if (lastReplyCode == FtpReplyCode.NEED_ACCOUNT) {
1100             issueCommandCheck("ACCT " + account);
1101         }
1102 
1103         // keep the welcome message around so we can
1104         // put it in the resulting HTML page.
1105         StringBuilder sb = new StringBuilder();
1106         if (serverResponse != null) {
1107             for (String l : serverResponse) {
1108                 if (l != null) {
1109                     if (l.length() >= 4 && l.startsWith("230")) {
1110                         // get rid of the "230-" prefix
1111                         l = l.substring(4);


1135         disconnect();
1136     }
1137 
1138     /**
1139      * Checks whether the client is logged in to the server or not.
1140      *
1141      * @return <code>true</code> if the client has already completed a login.
1142      */
1143     public boolean isLoggedIn() {
1144         return loggedIn;
1145     }
1146 
1147     /**
1148      * Changes to a specific directory on a remote FTP server
1149      *
1150      * @param remoteDirectory path of the directory to CD to.
1151      * @return <code>true</code> if the operation was successful.
1152      * @exception <code>FtpProtocolException</code>
1153      */
1154     public sun.net.ftp.FtpClient changeDirectory(String remoteDirectory) throws sun.net.ftp.FtpProtocolException, IOException {
1155         if (remoteDirectory == null || remoteDirectory.isEmpty()) {
1156             throw new IllegalArgumentException("directory can't be null or empty");
1157         }
1158 
1159         issueCommandCheck("CWD " + remoteDirectory);
1160         return this;
1161     }
1162 
1163     /**
1164      * Changes to the parent directory, sending the CDUP command to the server.
1165      *
1166      * @return <code>true</code> if the command was successful.
1167      * @throws IOException
1168      */
1169     public sun.net.ftp.FtpClient changeToParentDirectory() throws sun.net.ftp.FtpProtocolException, IOException {
1170         issueCommandCheck("CDUP");
1171         return this;
1172     }
1173 
1174     /**
1175      * Returns the server current working directory, or <code>null</code> if


1721         if (s != null) {
1722             return createInputStream(s.getInputStream());
1723         }
1724         return null;
1725     }
1726 
1727     /**
1728      * Issues the SIZE [path] command to the server to get the size of a
1729      * specific file on the server.
1730      * Note that this command may not be supported by the server. In which
1731      * case -1 will be returned.
1732      *
1733      * @param path a <code>String</code> containing the pathname of the
1734      *        file.
1735      * @return a <code>long</code> containing the size of the file or -1 if
1736      *         the server returned an error, which can be checked with
1737      *         {@link #getLastReplyCode()}.
1738      * @throws IOException if an error occurs during the transmission.
1739      */
1740     public long getSize(String path) throws sun.net.ftp.FtpProtocolException, IOException {
1741         if (path == null || path.isEmpty()) {
1742             throw new IllegalArgumentException("path can't be null or empty");
1743         }
1744         issueCommandCheck("SIZE " + path);
1745         if (lastReplyCode == FtpReplyCode.FILE_STATUS) {
1746             String s = getResponseString();
1747             s = s.substring(4, s.length() - 1);
1748             return Long.parseLong(s);
1749         }
1750         return -1;
1751     }
1752     private static String[] MDTMformats = {
1753         "yyyyMMddHHmmss.SSS",
1754         "yyyyMMddHHmmss"
1755     };
1756     private static SimpleDateFormat[] dateFormats = new SimpleDateFormat[MDTMformats.length];
1757 
1758     static {
1759         for (int i = 0; i < MDTMformats.length; i++) {
1760             dateFormats[i] = new SimpleDateFormat(MDTMformats[i]);
1761             dateFormats[i].setTimeZone(TimeZone.getTimeZone("GMT"));


< prev index next >