home *** CD-ROM | disk | FTP | other *** search
/ S283 Planetary Science &n…he Search for Life DVD 2 / DVD-ROM.iso / install / jre1_3 / lib / rt.jar / sun / net / ftp / FtpClient.class (.txt) next >
Encoding:
Java Class File  |  1979-12-31  |  5.0 KB  |  237 lines

  1. package sun.net.ftp;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.net.InetAddress;
  6. import java.net.ServerSocket;
  7. import java.net.Socket;
  8. import java.security.AccessController;
  9. import java.util.StringTokenizer;
  10. import sun.net.NetworkClient;
  11. import sun.net.TelnetInputStream;
  12. import sun.net.TelnetOutputStream;
  13. import sun.net.TransferProtocolClient;
  14.  
  15. public class FtpClient extends TransferProtocolClient {
  16.    public static final int FTP_PORT = 21;
  17.    static int FTP_SUCCESS = 1;
  18.    static int FTP_TRY_AGAIN = 2;
  19.    static int FTP_ERROR = 3;
  20.    private Socket dataSocket = null;
  21.    private boolean replyPending = false;
  22.    private boolean binaryMode = false;
  23.    String user = null;
  24.    String password = null;
  25.    String command;
  26.    int lastReplyCode;
  27.    public String welcomeMsg;
  28.    public static boolean useFtpProxy;
  29.    public static String ftpProxyHost;
  30.    public static int ftpProxyPort;
  31.  
  32.    public static boolean getUseFtpProxy() {
  33.       return getFtpProxyHost() != null;
  34.    }
  35.  
  36.    public static String getFtpProxyHost() {
  37.       return (String)AccessController.doPrivileged(new 1());
  38.    }
  39.  
  40.    public static int getFtpProxyPort() {
  41.       int[] var0 = new int[]{80};
  42.       AccessController.doPrivileged(new 2(var0));
  43.       return var0[0];
  44.    }
  45.  
  46.    public void closeServer() throws IOException {
  47.       if (((NetworkClient)this).serverIsOpen()) {
  48.          this.issueCommand("QUIT");
  49.          super.closeServer();
  50.       }
  51.  
  52.    }
  53.  
  54.    protected int issueCommand(String var1) throws IOException {
  55.       this.command = var1;
  56.       if (this.replyPending && this.readReply() == FTP_ERROR) {
  57.          System.out.print("Error reading FTP pending reply\n");
  58.       }
  59.  
  60.       this.replyPending = false;
  61.  
  62.       int var2;
  63.       do {
  64.          ((TransferProtocolClient)this).sendServer(var1 + "\r\n");
  65.          var2 = this.readReply();
  66.       } while(var2 == FTP_TRY_AGAIN);
  67.  
  68.       return var2;
  69.    }
  70.  
  71.    protected void issueCommandCheck(String var1) throws IOException {
  72.       if (this.issueCommand(var1) != FTP_SUCCESS) {
  73.          throw new FtpProtocolException(var1);
  74.       }
  75.    }
  76.  
  77.    protected int readReply() throws IOException {
  78.       this.lastReplyCode = ((TransferProtocolClient)this).readServerResponse();
  79.       switch (this.lastReplyCode / 100) {
  80.          case 1:
  81.             this.replyPending = true;
  82.          case 2:
  83.          case 3:
  84.             return FTP_SUCCESS;
  85.          case 5:
  86.             if (this.lastReplyCode == 530) {
  87.                if (this.user == null) {
  88.                   throw new FtpLoginException("Not logged in");
  89.                }
  90.  
  91.                return FTP_ERROR;
  92.             } else if (this.lastReplyCode == 550) {
  93.                throw new FileNotFoundException(this.command + ": " + ((TransferProtocolClient)this).getResponseString());
  94.             }
  95.          case 4:
  96.          default:
  97.             return FTP_ERROR;
  98.       }
  99.    }
  100.  
  101.    protected Socket openDataConnection(String var1) throws IOException {
  102.       InetAddress var4 = InetAddress.getLocalHost();
  103.       byte[] var5 = var4.getAddress();
  104.       ServerSocket var2 = new ServerSocket(0, 1);
  105.       String var3 = "PORT ";
  106.  
  107.       for(int var8 = 0; var8 < var5.length; ++var8) {
  108.          var3 = var3 + (var5[var8] & 255) + ",";
  109.       }
  110.  
  111.       var3 = var3 + (var2.getLocalPort() >>> 8 & 255) + "," + (var2.getLocalPort() & 255);
  112.       if (this.issueCommand(var3) == FTP_ERROR) {
  113.          FtpProtocolException var10 = new FtpProtocolException("PORT");
  114.          var2.close();
  115.          throw var10;
  116.       } else if (this.issueCommand(var1) == FTP_ERROR) {
  117.          FtpProtocolException var7 = new FtpProtocolException(var1);
  118.          var2.close();
  119.          throw var7;
  120.       } else {
  121.          this.dataSocket = var2.accept();
  122.          var2.close();
  123.          return this.dataSocket;
  124.       }
  125.    }
  126.  
  127.    public void openServer(String var1) throws IOException {
  128.       byte var2 = 21;
  129.       this.openServer(var1, var2);
  130.    }
  131.  
  132.    public void openServer(String var1, int var2) throws IOException {
  133.       super.openServer(var1, var2);
  134.       if (this.readReply() == FTP_ERROR) {
  135.          throw new FtpProtocolException("Welcome message");
  136.       }
  137.    }
  138.  
  139.    public void login(String var1, String var2) throws IOException {
  140.       if (!((NetworkClient)this).serverIsOpen()) {
  141.          throw new FtpLoginException("not connected to host");
  142.       } else {
  143.          this.user = var1;
  144.          this.password = var2;
  145.          if (this.issueCommand("USER " + var1) == FTP_ERROR) {
  146.             throw new FtpLoginException("user");
  147.          } else if (var2 != null && this.issueCommand("PASS " + var2) == FTP_ERROR) {
  148.             throw new FtpLoginException("password");
  149.          } else {
  150.             for(int var4 = 0; var4 < super.serverResponse.size(); ++var4) {
  151.                String var3 = (String)super.serverResponse.elementAt(var4);
  152.                if (var3 != null) {
  153.                   if (var3.charAt(3) != '-') {
  154.                      break;
  155.                   }
  156.  
  157.                   var3 = var3.substring(4);
  158.                   if (this.welcomeMsg == null) {
  159.                      this.welcomeMsg = var3;
  160.                   } else {
  161.                      this.welcomeMsg = this.welcomeMsg + var3;
  162.                   }
  163.                }
  164.             }
  165.  
  166.          }
  167.       }
  168.    }
  169.  
  170.    public TelnetInputStream get(String var1) throws IOException {
  171.       Socket var2;
  172.       try {
  173.          var2 = this.openDataConnection("RETR " + var1);
  174.       } catch (FileNotFoundException var8) {
  175.          StringTokenizer var4 = new StringTokenizer(var1, "/");
  176.          String var5 = null;
  177.  
  178.          while(var4.hasMoreElements()) {
  179.             var5 = var4.nextToken();
  180.             if (!var4.hasMoreElements()) {
  181.                break;
  182.             }
  183.  
  184.             try {
  185.                this.method_0(var5);
  186.             } catch (FtpProtocolException var7) {
  187.                throw var8;
  188.             }
  189.          }
  190.  
  191.          if (var5 == null) {
  192.             throw var8;
  193.          }
  194.  
  195.          var2 = this.openDataConnection("RETR " + var5);
  196.       }
  197.  
  198.       return new FtpInputStream(this, var2.getInputStream(), this.binaryMode);
  199.    }
  200.  
  201.    public TelnetOutputStream put(String var1) throws IOException {
  202.       Socket var2 = this.openDataConnection("STOR " + var1);
  203.       return new TelnetOutputStream(var2.getOutputStream(), this.binaryMode);
  204.    }
  205.  
  206.    public TelnetInputStream list() throws IOException {
  207.       Socket var1 = this.openDataConnection("LIST");
  208.       return new TelnetInputStream(var1.getInputStream(), this.binaryMode);
  209.    }
  210.  
  211.    // $FF: renamed from: cd (java.lang.String) void
  212.    public void method_0(String var1) throws IOException {
  213.       this.issueCommandCheck("CWD " + var1);
  214.    }
  215.  
  216.    public void binary() throws IOException {
  217.       this.issueCommandCheck("TYPE I");
  218.       this.binaryMode = true;
  219.    }
  220.  
  221.    public void ascii() throws IOException {
  222.       this.issueCommandCheck("TYPE A");
  223.       this.binaryMode = false;
  224.    }
  225.  
  226.    public FtpClient(String var1) throws IOException {
  227.       this.openServer(var1, 21);
  228.    }
  229.  
  230.    public FtpClient(String var1, int var2) throws IOException {
  231.       this.openServer(var1, var2);
  232.    }
  233.  
  234.    public FtpClient() {
  235.    }
  236. }
  237.