Author: sebb
Date: Mon Mar 27 17:11:07 2017
New Revision: 1788984

URL: http://svn.apache.org/viewvc?rev=1788984&view=rev
Log:
NET-634 Add SIZE command support

Modified:
    commons/proper/net/trunk/src/changes/changes.xml
    commons/proper/net/trunk/src/main/java/examples/ftp/FTPClientExample.java
    commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTP.java
    
commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTPClient.java
    
commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTPCmd.java

Modified: commons/proper/net/trunk/src/changes/changes.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/net/trunk/src/changes/changes.xml?rev=1788984&r1=1788983&r2=1788984&view=diff
==============================================================================
--- commons/proper/net/trunk/src/changes/changes.xml [utf-8] (original)
+++ commons/proper/net/trunk/src/changes/changes.xml [utf-8] Mon Mar 27 
17:11:07 2017
@@ -71,6 +71,9 @@ This is mainly a bug-fix release. See fu
  However it is not source compatible with releases before 3.4, as some methods 
were added to the interface NtpV3Packet in 3.4
 
 ">
+            <action issue="NET-634" type="add" dev="sebb" due-to="Mauro 
Molinari">
+            Add SIZE command support
+            </action>
             <action type="add" dev="sebb">
             Add POP3ExportMbox example code
             </action>

Modified: 
commons/proper/net/trunk/src/main/java/examples/ftp/FTPClientExample.java
URL: 
http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/examples/ftp/FTPClientExample.java?rev=1788984&r1=1788983&r2=1788984&view=diff
==============================================================================
--- commons/proper/net/trunk/src/main/java/examples/ftp/FTPClientExample.java 
(original)
+++ commons/proper/net/trunk/src/main/java/examples/ftp/FTPClientExample.java 
Mon Mar 27 17:11:07 2017
@@ -63,6 +63,7 @@ public final class FTPClientExample
         "\t-E - encoding to use for control channel\n" +
         "\t-f - issue FEAT command (remote and local files are ignored)\n" +
         "\t-h - list hidden files (applies to -l and -n only)\n" +
+        "\t-i - issue SIZE command for a file\n" +
         "\t-k secs - use keep-alive timer (setControlKeepAliveTimeout)\n" +
         "\t-l - list files using LIST (remote is used as the pathname if 
provided)\n" +
         "\t     Files are listed twice: first in raw mode, then as the 
formatted parsed data.\n" +
@@ -91,6 +92,7 @@ public final class FTPClientExample
         boolean storeFile = false, binaryTransfer = false, error = false, 
listFiles = false, listNames = false, hidden = false;
         boolean localActive = false, useEpsvWithIPv4 = false, feat = false, 
printHash = false;
         boolean mlst = false, mlsd = false, mdtm = false, saveUnparseable = 
false;
+        boolean size = false;
         boolean lenient = false;
         long keepAliveTimeout = -1;
         int controlKeepAliveReplyTimeout = -1;
@@ -149,6 +151,10 @@ public final class FTPClientExample
             else if (args[base].equals("-h")) {
                 hidden = true;
             }
+            else if (args[base].equals("-i")) {
+                size = true;
+                minParams = 3;
+            }
             else if (args[base].equals("-k")) {
                 keepAliveTimeout = Long.parseLong(args[++base]);
             }
@@ -404,7 +410,7 @@ __main:
                 }
             }
             // Allow multiple list types for single invocation
-            else if (listFiles || mlsd || mdtm || mlst || listNames)
+            else if (listFiles || mlsd || mdtm || mlst || listNames || size)
             {
                 if (mlsd) {
                     for (FTPFile f : ftp.mlistDir(remote)) {
@@ -432,6 +438,9 @@ __main:
                         System.out.println(s);
                     }
                 }
+                if (size) {
+                    System.out.println("Size="+ftp.getSize(remote));
+                }
                 // Do this last because it changes the client
                 if (listFiles) {
                     if (lenient || serverTimeZoneId != null) {

Modified: 
commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTP.java
URL: 
http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTP.java?rev=1788984&r1=1788983&r2=1788984&view=diff
==============================================================================
--- commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTP.java 
(original)
+++ commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTP.java 
Mon Mar 27 17:11:07 2017
@@ -1695,6 +1695,25 @@ public class FTP extends SocketClient
     }
 
     /***
+     * A convenience method to send the FTP SIZE command to the server,
+     * receive the reply, and return the reply code.
+     *
+     * @param parameters  The site parameters to send.
+     * @return The reply code received from the server.
+     * @throws FTPConnectionClosedException
+     *      If the FTP server prematurely closes the connection as a result
+     *      of the client being idle or some other reason causing the server
+     *      to send FTP reply code 421.  This exception may be caught either
+     *      as an IOException or independently as itself.
+     * @throws IOException  If an I/O error occurs while either sending the
+     *      command or receiving the server reply.
+     ***/
+    public int size(String parameters) throws IOException
+    {
+        return sendCommand(FTPCmd.SIZE, parameters);
+    }
+
+    /***
      * A convenience method to send the FTP SYST command to the server,
      * receive the reply, and return the reply code.
      *

Modified: 
commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTPClient.java
URL: 
http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTPClient.java?rev=1788984&r1=1788983&r2=1788984&view=diff
==============================================================================
--- 
commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTPClient.java
 (original)
+++ 
commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTPClient.java
 Mon Mar 27 17:11:07 2017
@@ -3524,6 +3524,30 @@ implements Configurable
 
 
     /**
+     * Issue the FTP SIZE command to the server for a given pathname.
+     * This should produce the size of the file.
+     *
+     * @param pathname the filename
+     *
+     * @return The size information returned by the server; {@code null} if 
there was an error
+     * @throws FTPConnectionClosedException
+     *      If the FTP server prematurely closes the connection as a result
+     *      of the client being idle or some other reason causing the server
+     *      to send FTP reply code 421.  This exception may be caught either
+     *      as an IOException or independently as itself.
+     * @throws IOException  If an I/O error occurs while either sending a
+     *      command to the server or receiving a reply from the server.
+     */
+    public String getSize(String pathname) throws IOException
+    {
+        if (FTPReply.isPositiveCompletion(size(pathname))) {
+            return getReplyStrings()[0].substring(4); // skip the return code 
(e.g. 213) and the space
+        }
+        return null;
+    }
+
+
+    /**
      * Issue the FTP MDTM command (not supported by all servers) to retrieve 
the last
      * modification time of a file. The modification string should be in the
      * ISO 3077 form "YYYYMMDDhhmmss(.xxx)?". The timestamp represented should 
also be in

Modified: 
commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTPCmd.java
URL: 
http://svn.apache.org/viewvc/commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTPCmd.java?rev=1788984&r1=1788983&r2=1788984&view=diff
==============================================================================
--- 
commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTPCmd.java 
(original)
+++ 
commons/proper/net/trunk/src/main/java/org/apache/commons/net/ftp/FTPCmd.java 
Mon Mar 27 17:11:07 2017
@@ -54,6 +54,8 @@ public enum FTPCmd {
     RNFR,
     RNTO,
     SITE,
+    /** @since 3.7 */
+    SIZE,
     SMNT,
     STAT,
     STOR,


Reply via email to