blob: b3aed66b8d781def16d0149766f5ab62bb55b663 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1994-2002 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.net;
27
28import java.lang.StringIndexOutOfBoundsException;
29import java.io.*;
30import java.util.Vector;
31import 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
42public 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 */
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. */
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. */
125 public String getResponseString() {
126 return (String) serverResponse.elementAt(0);
127 }
128
129 /** Returns all server response strings. */
130 public Vector getResponseStrings() {
131 return serverResponse;
132 }
133
134 /** standard constructor to host <i>host</i>, port <i>port</i>. */
135 public TransferProtocolClient(String host, int port) throws IOException {
136 super(host, port);
137 }
138
139 /** creates an uninitialized instance of this class. */
140 public TransferProtocolClient() {}
141}