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
11 package gnu
.java
.net
.protocol
.core
;
14 import java
.io
.InputStream
;
15 import java
.io
.IOException
;
16 import java
.net
.ProtocolException
;
18 import java
.net
.URLConnection
;
20 import java
.util
.Vector
;
21 import java
.util
.Hashtable
;
22 import java
.util
.Enumeration
;
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;
37 public Connection (URL url
)
42 // Implementation of abstract method.
43 public void connect() throws IOException
45 // Call is ignored if already connected.
49 // If not connected, then file needs to be opened.
50 core
= Core
.create (url
.getFile());
54 public InputStream
getInputStream() throws IOException
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
)
75 return (String
) hdrHash
.get(name
.toLowerCase());
78 // Override default method in URLConnection.
79 public Map
getHeaderFields()
92 // Override default method in URLConnection.
93 public String
getHeaderField(int n
)
103 if (n
< hdrVec
.size())
104 return getField ((String
) hdrVec
.elementAt(n
));
109 // Override default method in URLConnection.
110 public String
getHeaderFieldKey(int n
)
116 catch (IOException x
)
120 if (n
< hdrVec
.size())
121 return getKey ((String
) hdrVec
.elementAt(n
));
126 private String
getKey(String str
)
130 int index
= str
.indexOf(':');
132 return str
.substring(0, index
);
137 private String
getField(String str
)
141 int index
= str
.indexOf(':');
143 return str
.substring(index
+ 1).trim();
148 private void getHeaders() throws IOException
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
));