Path: blob/master/src/java.base/share/classes/sun/net/TransferProtocolClient.java
41152 views
/*1* Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.net;2627import java.io.*;28import java.util.Vector;2930/**31* This class implements that basic intefaces of transfer protocols.32* It is used by subclasses implementing specific protocols.33*34* @author Jonathan Payne35* @see sun.net.ftp.FtpClient36* @see sun.net.nntp.NntpClient37*/3839public class TransferProtocolClient extends NetworkClient {40static final boolean debug = false;4142/** Array of strings (usually 1 entry) for the last reply43from the server. */44protected Vector<String> serverResponse = new Vector<>(1);4546/** code for last reply */47protected int lastReplyCode;484950/**51* Pulls the response from the server and returns the code as a52* number. Returns -1 on failure.53*/54public int readServerResponse() throws IOException {55StringBuilder replyBuf = new StringBuilder(32);56int c;57int continuingCode = -1;58int code;59String response;6061serverResponse.setSize(0);62while (true) {63while ((c = serverInput.read()) != -1) {64if (c == '\r') {65if ((c = serverInput.read()) != '\n')66replyBuf.append('\r');67}68replyBuf.append((char)c);69if (c == '\n')70break;71}72response = replyBuf.toString();73replyBuf.setLength(0);74if (debug) {75System.out.print(response);76}7778if (response.isEmpty()) {79code = -1;80} else {81try {82code = Integer.parseInt(response, 0, 3, 10);83} catch (NumberFormatException e) {84code = -1;85} catch (IndexOutOfBoundsException e) {86/* this line doesn't contain a response code, so87we just completely ignore it */88continue;89}90}91serverResponse.addElement(response);92if (continuingCode != -1) {93/* we've seen a XXX- sequence */94if (code != continuingCode ||95(response.length() >= 4 && response.charAt(3) == '-')) {96continue;97} else {98/* seen the end of code sequence */99continuingCode = -1;100break;101}102} else if (response.length() >= 4 && response.charAt(3) == '-') {103continuingCode = code;104continue;105} else {106break;107}108}109110return lastReplyCode = code;111}112113/** Sends command <i>cmd</i> to the server. */114public void sendServer(String cmd) {115serverOutput.print(cmd);116if (debug) {117System.out.print("Sending: " + cmd);118}119}120121/** converts the server response into a string. */122public String getResponseString() {123return serverResponse.elementAt(0);124}125126/** Returns all server response strings. */127public Vector<String> getResponseStrings() {128return serverResponse;129}130131/** standard constructor to host <i>host</i>, port <i>port</i>. */132public TransferProtocolClient(String host, int port) throws IOException {133super(host, port);134}135136/** creates an uninitialized instance of this class. */137public TransferProtocolClient() {}138}139140141