• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 1994, 2002, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package sun.net;
27 
28 import java.lang.StringIndexOutOfBoundsException;
29 import java.io.*;
30 import java.util.Vector;
31 import sun.net.NetworkClient;
32 
33 /**
34  * This class implements that basic intefaces of transfer protocols.
35  * It is used by subclasses implementing specific protocols.
36  *
37  * @author      Jonathan Payne
38  * @see         sun.net.ftp.FtpClient
39  * @see         sun.net.nntp.NntpClient
40  */
41 
42 public class TransferProtocolClient extends NetworkClient {
43     static final boolean debug = false;
44 
45     /** Array of strings (usually 1 entry) for the last reply
46         from the server. */
47     protected Vector    serverResponse = new Vector(1);
48 
49     /** code for last reply */
50     protected int       lastReplyCode;
51 
52 
53     /**
54      * Pulls the response from the server and returns the code as a
55      * number. Returns -1 on failure.
56      */
readServerResponse()57     public int readServerResponse() throws IOException {
58         StringBuffer    replyBuf = new StringBuffer(32);
59         int             c;
60         int             continuingCode = -1;
61         int             code;
62         String          response;
63 
64         serverResponse.setSize(0);
65         while (true) {
66             while ((c = serverInput.read()) != -1) {
67                 if (c == '\r') {
68                     if ((c = serverInput.read()) != '\n')
69                         replyBuf.append('\r');
70                 }
71                 replyBuf.append((char)c);
72                 if (c == '\n')
73                     break;
74             }
75             response = replyBuf.toString();
76             replyBuf.setLength(0);
77             if (debug) {
78                 System.out.print(response);
79             }
80 
81             if (response.length() == 0) {
82                 code = -1;
83             } else {
84                 try {
85                     code = Integer.parseInt(response.substring(0, 3));
86                 } catch (NumberFormatException e) {
87                     code = -1;
88                 } catch (StringIndexOutOfBoundsException e) {
89                     /* this line doesn't contain a response code, so
90                        we just completely ignore it */
91                     continue;
92                 }
93             }
94             serverResponse.addElement(response);
95             if (continuingCode != -1) {
96                 /* we've seen a XXX- sequence */
97                 if (code != continuingCode ||
98                     (response.length() >= 4 && response.charAt(3) == '-')) {
99                     continue;
100                 } else {
101                     /* seen the end of code sequence */
102                     continuingCode = -1;
103                     break;
104                 }
105             } else if (response.length() >= 4 && response.charAt(3) == '-') {
106                 continuingCode = code;
107                 continue;
108             } else {
109                 break;
110             }
111         }
112 
113         return lastReplyCode = code;
114     }
115 
116     /** Sends command <i>cmd</i> to the server. */
sendServer(String cmd)117     public void sendServer(String cmd) {
118         serverOutput.print(cmd);
119         if (debug) {
120             System.out.print("Sending: " + cmd);
121         }
122     }
123 
124     /** converts the server response into a string. */
getResponseString()125     public String getResponseString() {
126         return (String) serverResponse.elementAt(0);
127     }
128 
129     /** Returns all server response strings. */
getResponseStrings()130     public Vector getResponseStrings() {
131         return serverResponse;
132     }
133 
134     /** standard constructor to host <i>host</i>, port <i>port</i>. */
TransferProtocolClient(String host, int port)135     public TransferProtocolClient(String host, int port) throws IOException {
136         super(host, port);
137     }
138 
139     /** creates an uninitialized instance of this class. */
TransferProtocolClient()140     public TransferProtocolClient() {}
141 }
142