OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [gnu/] [java/] [net/] [protocol/] [core/] [Connection.java] - Blame information for rev 756

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 756 jeremybenn
// Connection.java - Implementation of URLConnection for core protocol.
2
 
3
/* Copyright (C) 2001, 2003  Free Software Foundation
4
 
5
   This file is part of libgcj.
6
 
7
This software is copyrighted work licensed under the terms of the
8
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9
details.  */
10
 
11
package gnu.java.net.protocol.core;
12
 
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;
23
 
24
/**
25
 * @author Anthony Green <green@redhat.com>
26
 * @date August 13, 2001
27
 */
28
 
29
class Connection extends URLConnection
30
{
31
  private Hashtable hdrHash = new Hashtable();
32
  private Vector hdrVec = new Vector();
33
  private boolean gotHeaders = false;
34
 
35
  private Core core;
36
 
37
  public Connection (URL url)
38
  {
39
    super(url);
40
  }
41
 
42
  // Implementation of abstract method.
43
  public void connect() throws IOException
44
  {
45
    // Call is ignored if already connected.
46
    if (connected)
47
      return;
48
 
49
    // If not connected, then file needs to be opened.
50
    core = Core.create (url.getFile());
51
    connected = true;
52
  }
53
 
54
  public InputStream getInputStream() throws IOException
55
  {
56
    if (!connected)
57
      connect();
58
 
59
    if (! doInput)
60
      throw new ProtocolException("Can't open InputStream if doInput is false");
61
    return new CoreInputStream (core);
62
  }
63
 
64
  // Override default method in URLConnection.
65
  public String getHeaderField(String name)
66
  {
67
    try
68
      {
69
        getHeaders();
70
      }
71
    catch (IOException x)
72
      {
73
        return null;
74
      }
75
    return (String) hdrHash.get(name.toLowerCase());
76
  }
77
 
78
  // Override default method in URLConnection.
79
  public Map getHeaderFields()
80
  {
81
    try
82
      {
83
        getHeaders();
84
      }
85
    catch (IOException x)
86
      {
87
        return null;
88
      }
89
    return hdrHash;
90
  }
91
 
92
  // Override default method in URLConnection.
93
  public String getHeaderField(int n)
94
  {
95
    try
96
      {
97
        getHeaders();
98
      }
99
    catch (IOException x)
100
      {
101
        return null;
102
      }
103
    if (n < hdrVec.size())
104
      return getField ((String) hdrVec.elementAt(n));
105
 
106
    return null;
107
  }
108
 
109
  // Override default method in URLConnection.
110
  public String getHeaderFieldKey(int n)
111
  {
112
    try
113
      {
114
        getHeaders();
115
      }
116
    catch (IOException x)
117
      {
118
        return null;
119
      }
120
    if (n < hdrVec.size())
121
      return getKey ((String) hdrVec.elementAt(n));
122
 
123
    return null;
124
  }
125
 
126
  private String getKey(String str)
127
  {
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;
135
  }
136
 
137
  private String getField(String str)
138
  {
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;
146
  }
147
 
148
  private void getHeaders() throws IOException
149
  {
150
    if (gotHeaders)
151
      return;
152
    gotHeaders = true;
153
 
154
    connect();
155
 
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.
159
 
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);
164
 
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));
170
  }
171
}
172
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.