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