2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / gnu / java / net / protocol / core / Connection.java
blob2319c0be923139ab8830810ae381f35f5e028409
1 // Connection.java - Implementation of URLConnection for core protocol.
3 /* Copyright (C) 2001, 2003 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 package gnu.java.net.protocol.core;
13 import gnu.gcj.Core;
14 import java.io.InputStream;
15 import java.io.IOException;
16 import java.net.ProtocolException;
17 import java.net.URL;
18 import java.net.URLConnection;
19 import java.util.Map;
20 import java.util.Vector;
21 import java.util.Hashtable;
22 import java.util.Enumeration;
24 /**
25 * @author Anthony Green <green@redhat.com>
26 * @date August 13, 2001
29 class Connection extends URLConnection
31 private Hashtable hdrHash = new Hashtable();
32 private Vector hdrVec = new Vector();
33 private boolean gotHeaders = false;
35 private Core core;
37 public Connection (URL url)
39 super(url);
42 // Implementation of abstract method.
43 public void connect() throws IOException
45 // Call is ignored if already connected.
46 if (connected)
47 return;
49 // If not connected, then file needs to be opened.
50 core = Core.create (url.getFile());
51 connected = true;
54 public InputStream getInputStream() throws IOException
56 if (!connected)
57 connect();
59 if (! doInput)
60 throw new ProtocolException("Can't open InputStream if doInput is false");
61 return new CoreInputStream (core);
64 // Override default method in URLConnection.
65 public String getHeaderField(String name)
67 try
69 getHeaders();
71 catch (IOException x)
73 return null;
75 return (String) hdrHash.get(name.toLowerCase());
78 // Override default method in URLConnection.
79 public Map getHeaderFields()
81 try
83 getHeaders();
85 catch (IOException x)
87 return null;
89 return hdrHash;
92 // Override default method in URLConnection.
93 public String getHeaderField(int n)
95 try
97 getHeaders();
99 catch (IOException x)
101 return null;
103 if (n < hdrVec.size())
104 return getField ((String) hdrVec.elementAt(n));
106 return null;
109 // Override default method in URLConnection.
110 public String getHeaderFieldKey(int n)
114 getHeaders();
116 catch (IOException x)
118 return null;
120 if (n < hdrVec.size())
121 return getKey ((String) hdrVec.elementAt(n));
123 return null;
126 private String getKey(String str)
128 if (str == null)
129 return null;
130 int index = str.indexOf(':');
131 if (index >= 0)
132 return str.substring(0, index);
133 else
134 return null;
137 private String getField(String str)
139 if (str == null)
140 return null;
141 int index = str.indexOf(':');
142 if (index >= 0)
143 return str.substring(index + 1).trim();
144 else
145 return str;
148 private void getHeaders() throws IOException
150 if (gotHeaders)
151 return;
152 gotHeaders = true;
154 connect();
156 // Yes, it is overkill to use the hash table and vector here since
157 // we're only putting one header in the file, but in case we need
158 // to add others later and for consistency, we'll implement it this way.
160 // Add the only header we know about right now: Content-length.
161 long len = core.length;
162 String line = "Content-length: " + len;
163 hdrVec.addElement(line);
165 // The key will never be null in this scenario since we build up the
166 // headers ourselves. If we ever rely on getting a header from somewhere
167 // else, then we may have to check if the result of getKey() is null.
168 String key = getKey(line);
169 hdrHash.put(key.toLowerCase(), Long.toString(len));