KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > util > bytearray > ByteArrayURLConnection


1 /*
2 * CLIF is a Load Injection Framework
3 * Copyright (C) 2004 France Telecom R&D
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * CLIF $Name: $
20 *
21 * Contact: clif@objectweb.org
22 */

23
24 package org.objectweb.clif.util.bytearray;
25
26 import org.objectweb.clif.util.ClifClassLoader;
27 import java.net.URLConnection JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.ByteArrayInputStream JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.OutputStream JavaDoc;
33
34
35 /**
36  *
37  * @author Bruno Dillenseger
38  */

39 public class ByteArrayURLConnection extends URLConnection JavaDoc
40 {
41     String JavaDoc name;
42     ClifClassLoader loader;
43     byte[] bytes;
44
45
46     public ByteArrayURLConnection(URL JavaDoc url, ClifClassLoader classloader)
47     {
48         super(url);
49         name = url.getFile();
50         loader = classloader;
51         connected = false;
52     }
53
54
55     public void connect()
56     {
57         if (! connected)
58         {
59             try
60             {
61                 bytes = loader.getBytes(name);
62                 connected = true;
63             }
64             catch (IOException JavaDoc ex)
65             {
66
67             }
68         }
69     }
70
71
72     public int getContentLength()
73     {
74         if (! connected)
75         {
76             connect();
77         }
78         return bytes.length;
79     }
80
81
82     public String JavaDoc getContentType()
83     {
84         String JavaDoc type = guessContentTypeFromName(name);
85         if (type == null)
86         {
87             if (! connected)
88             {
89                 connect();
90             }
91             try
92             {
93                 type = guessContentTypeFromStream(new ByteArrayInputStream JavaDoc(bytes));
94             }
95             catch (IOException JavaDoc ex)
96             {
97             }
98         }
99         return type;
100     }
101
102
103     public Object JavaDoc getContent()
104         throws IOException JavaDoc
105     {
106         if (! connected)
107         {
108             connect();
109         }
110         return bytes;
111     }
112
113
114     public InputStream JavaDoc getInputStream()
115         throws IOException JavaDoc
116     {
117         if (! connected)
118         {
119             connect();
120         }
121         return new ByteArrayInputStream JavaDoc(bytes);
122     }
123
124
125     public OutputStream JavaDoc getOutputStream()
126         throws IOException JavaDoc
127     {
128         throw new IOException JavaDoc("bytearray URLs cannot be written to");
129     }
130
131
132     public String JavaDoc toString()
133     {
134         return "ByteArrayURLConnection for " + loader + ", URL " + getURL();
135     }
136 }
137
138
Popular Tags