aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/net/URLStreamHandler.java
blob: 29e0e6d4137c54e15736b230eb417a00058bbf69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// URLStreamHandler.java - Superclass of all stream protocol handlers.

/* Copyright (C) 1999  Cygnus Solutions

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

package java.net;

/**
 * @author Warren Levy <warrenl@cygnus.com>
 * @date March 4, 1999.
 */

/**
 * Written using on-line Java Platform 1.2 API Specification, as well
 * as "The Java Class Libraries", 2nd edition (Addison-Wesley, 1998).
 * Status:  Believed complete and correct.
 */

public abstract class URLStreamHandler
{
  protected abstract URLConnection openConnection(URL u)
    throws java.io.IOException;

protected void parseURL(URL u, String spec, int start, int limit)
  {
    String tmpStr;
    String host = "";	// Initialize to null string.
    String file;
    int port = -1;
    int colon;

    /* TBD: The JDK 1.2 doc specifically says that limit is the position
     * to stop parsing at and that it will be either the end of the string
     * or the position of '#'; thus the doc infers that this method does
     * not set the ref.
     */
    tmpStr = spec.substring(start, limit);
    int hostEnd = 0;
    if (tmpStr.startsWith("//"))
      {
	int slash = tmpStr.indexOf('/', 2);
	hostEnd = tmpStr.length();
	if (slash >= 0)
	  hostEnd = slash;

	host = tmpStr.substring(2, hostEnd);

	// Look for optional port number.
	if ((colon = host.indexOf(':')) >= 0)
	  {
	    try
	      {
		port = Integer.parseInt(host.substring(colon + 1));
	      }
	    catch (NumberFormatException e)
	      {
		; // Ignore invalid port values; port is already set to -1.
	      }
	    host = host.substring(0, colon);
	  }
      }

    if (hostEnd < tmpStr.length())
      file = ((tmpStr.startsWith("/")) ? "" : "/") + tmpStr.substring(hostEnd);
    else
      file = "/";

    u.set(u.getProtocol(), host, port, file, u.getRef());
  }
  
  protected void setURL(URL u, String protocol, String host, int port,
			String file, String ref)
  {
    u.set(protocol, host, port, file, ref);
  }

  protected String toExternalForm(URL u)
  {
    String resStr, host, file, ref;
    int port;

    resStr = u.getProtocol() + ":";
    host = u.getHost();
    port = u.getPort();
    file = u.getFile();
    ref = u.getRef();

    if (! host.equals(""))
      {
        resStr = resStr + "//" + host;
	if (port >= 0)
	  resStr = resStr + ":" + port;
      }

    resStr = resStr + file;

    if (ref != null)
      resStr = resStr + "#" + ref;

    return resStr;
  }
}