home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / sun / net / TransferProtocolClient.java < prev    next >
Encoding:
Java Source  |  1997-01-27  |  3.8 KB  |  132 lines

  1. /*
  2.  * @(#)TransferProtocolClient.java    1.23 95/12/07 Jonathan Payne
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package sun.net;
  21.  
  22. import java.lang.StringIndexOutOfBoundsException;
  23. import java.io.*;
  24. import java.util.Vector;
  25. import sun.net.NetworkClient;
  26.  
  27. /**
  28.  * This class implements that basic intefaces of transfer protocols.
  29.  * It is used by subclasses implementing specific protocols.
  30.  *
  31.  * @version     1.23, 12/07/95
  32.  * @author     Jonathan Payne
  33.  * @see     sun.net.ftp.FtpClient
  34.  * @see        sun.net.nntp.NntpClient
  35.  */
  36.  
  37. public class TransferProtocolClient extends NetworkClient {
  38.     static final boolean debug = false;
  39.  
  40.     /** Array of strings (usually 1 entry) for the last reply
  41.     from the server. */
  42.     protected Vector    serverResponse = new Vector(1);
  43.  
  44.     /** code for last reply */
  45.     protected int    lastReplyCode;
  46.  
  47.  
  48.     /**
  49.      * Pulls the response from the server and returns the code as a
  50.      * number. Returns -1 on failure.
  51.      */
  52.     public int readServerResponse() throws IOException {
  53.     StringBuffer    replyBuf = new StringBuffer(32);
  54.     int        c;
  55.     int        continuingCode = -1;
  56.     int        code = -1;
  57.     String        response;
  58.  
  59.     serverResponse.setSize(0);
  60.     while (true) {
  61.         while ((c = serverInput.read()) != -1) {
  62.         if (c == '\r') {
  63.             if ((c = serverInput.read()) != '\n')
  64.             replyBuf.append('\r');
  65.         }
  66.         replyBuf.append((char)c);
  67.         if (c == '\n')
  68.             break;
  69.         }
  70.         response = replyBuf.toString();
  71.         replyBuf.setLength(0);
  72.         if (debug) {
  73.         System.out.print(response);
  74.         }
  75.         try {
  76.         code = Integer.parseInt(response.substring(0, 3));
  77.         } catch (NumberFormatException e) {
  78.         code = -1;
  79.         } catch (StringIndexOutOfBoundsException e) {
  80.         /* this line doesn't contain a response code, so
  81.            we just completely ignore it */
  82.         continue;
  83.         }
  84.         serverResponse.addElement(response);
  85.         if (continuingCode != -1) {
  86.         /* we've seen a XXX- sequence */
  87.         if (code != continuingCode ||
  88.             (response.length() >= 4 && response.charAt(3) == '-')) {
  89.             continue;
  90.         } else {
  91.             /* seen the end of code sequence */
  92.             continuingCode = -1;
  93.             break;
  94.         }
  95.         } else if (response.length() >= 4 && response.charAt(3) == '-') {
  96.         continuingCode = code;
  97.         continue;
  98.         } else {
  99.         break;
  100.         }
  101.     }
  102.  
  103.     return lastReplyCode = code;
  104.     }
  105.  
  106.     /** Sends command <i>cmd</i> to the server. */
  107.     public void sendServer(String cmd) {
  108.     serverOutput.print(cmd);
  109.     if (debug) {
  110.         System.out.print("Sending: " + cmd);
  111.     }
  112.     }
  113.  
  114.     /** converts the server response into a string. */
  115.     public String getResponseString() {
  116.     return (String) serverResponse.elementAt(0);
  117.     }
  118.  
  119.     /** Returns all server response strings. */
  120.     public Vector getResponseStrings() {
  121.     return serverResponse;
  122.     }
  123.  
  124.     /** standard constructor to host <i>host</i>, port <i>port</i>. */
  125.     public TransferProtocolClient(String host, int port) throws IOException {
  126.     super(host, port);
  127.     }
  128.  
  129.     /** creates an uninitialized instance of this class. */
  130.     public TransferProtocolClient() {}
  131. }
  132.