1 /* 2 * Copyright (c) 1994, 2011, 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.io.*; 29 import java.util.Vector; 30 31 /** 32 * This class implements that basic intefaces of transfer protocols. 33 * It is used by subclasses implementing specific protocols. 34 * 35 * @author Jonathan Payne 36 * @see sun.net.ftp.FtpClient 37 * @see sun.net.nntp.NntpClient 38 */ 39 40 public class TransferProtocolClient extends NetworkClient { 41 static final boolean debug = false; 42 43 /** Array of strings (usually 1 entry) for the last reply 44 from the server. */ 45 protected Vector<String> serverResponse = new Vector<>(1); 46 47 /** code for last reply */ 48 protected int lastReplyCode; 49 50 51 /** 52 * Pulls the response from the server and returns the code as a 53 * number. Returns -1 on failure. 54 */ 55 public int readServerResponse() throws IOException { 56 StringBuilder replyBuf = new StringBuilder(32); 57 int c; 58 int continuingCode = -1; 59 int code; 60 String response; 61 62 serverResponse.setSize(0); 63 while (true) { 64 while ((c = serverInput.read()) != -1) { 65 if (c == '\r') { 66 if ((c = serverInput.read()) != '\n') 67 replyBuf.append('\r'); 68 } 69 replyBuf.append((char)c); 70 if (c == '\n') 71 break; 72 } 73 response = replyBuf.toString(); 74 replyBuf.setLength(0); 75 if (debug) { 76 System.out.print(response); 77 } 78 79 if (response.length() == 0) { 80 code = -1; 81 } else { 82 try { 83 code = Integer.parseInt(response, 0, 3, 10); 84 } catch (NumberFormatException e) { 85 code = -1; 86 } catch (IndexOutOfBoundsException e) { 87 /* this line doesn't contain a response code, so 88 we just completely ignore it */ 89 continue; 90 } 91 } 92 serverResponse.addElement(response); 93 if (continuingCode != -1) { 94 /* we've seen a XXX- sequence */ 95 if (code != continuingCode || 96 (response.length() >= 4 && response.charAt(3) == '-')) { 97 continue; 98 } else { 99 /* seen the end of code sequence */ 100 continuingCode = -1; 101 break; 102 } 103 } else if (response.length() >= 4 && response.charAt(3) == '-') { 104 continuingCode = code; 105 continue; 106 } else { 107 break; 108 } 109 } 110 111 return lastReplyCode = code; 112 } 113 114 /** Sends command <i>cmd</i> to the server. */ 115 public void sendServer(String cmd) { 116 serverOutput.print(cmd); 117 if (debug) { 118 System.out.print("Sending: " + cmd); 119 } 120 } 121 122 /** converts the server response into a string. */ 123 public String getResponseString() { 124 return serverResponse.elementAt(0); 125 } 126 127 /** Returns all server response strings. */ 128 public Vector<String> getResponseStrings() { 129 return serverResponse; 130 } 131 132 /** standard constructor to host <i>host</i>, port <i>port</i>. */ 133 public TransferProtocolClient(String host, int port) throws IOException { 134 super(host, port); 135 } 136 137 /** creates an uninitialized instance of this class. */ 138 public TransferProtocolClient() {} 139 }