KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > jar > JarURLConnectionImpl


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.vfs.provider.jar;
17
18 import org.apache.commons.vfs.FileContent;
19 import org.apache.commons.vfs.FileSystemException;
20
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.net.JarURLConnection JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.security.cert.Certificate JavaDoc;
28 import java.util.jar.Attributes JavaDoc;
29 import java.util.jar.JarEntry JavaDoc;
30 import java.util.jar.JarFile JavaDoc;
31 import java.util.jar.Manifest JavaDoc;
32
33 /**
34  * A default URL connection that will work for most file systems.
35  *
36  * @author <a HREF="mailto:brian@mmmanager.org">Brian Olsen</a>
37  */

38 public class JarURLConnectionImpl
39     extends JarURLConnection JavaDoc
40 {
41     // This is because JarURLConnection SUCKS
42
private static final String JavaDoc HACK_URL = "jar:http://somehost/somejar.jar!/";
43
44     private FileContent content;
45     private URL JavaDoc parentURL;
46     private JarFileObject file;
47     private String JavaDoc entryName;
48
49     public JarURLConnectionImpl(JarFileObject file, FileContent content)
50         throws MalformedURLException JavaDoc, FileSystemException
51     {
52         //This is because JarURLConnection SUCKS!!
53
super(new URL JavaDoc(HACK_URL));
54
55         this.url = file.getURL();
56         this.content = content;
57         this.parentURL = file.getURL();
58         this.entryName = file.getName().getPath();
59         this.file = file;
60     }
61
62
63     public URL JavaDoc getJarFileURL()
64     {
65         return parentURL;
66     }
67
68
69     public String JavaDoc getEntryName()
70     {
71         return entryName;
72     }
73
74
75     public JarFile JavaDoc getJarFile() throws IOException JavaDoc
76     {
77         throw new FileSystemException("vfs.provider.jar/jar-file-no-access.error");
78     }
79
80
81     public Manifest JavaDoc getManifest() throws IOException JavaDoc
82     {
83         return file.getManifest();
84     }
85
86
87     public JarEntry JavaDoc getJarEntry() throws IOException JavaDoc
88     {
89         throw new FileSystemException("vfs.provider.jar/jar-entry-no-access.error");
90     }
91
92
93     public Attributes JavaDoc getAttributes() throws IOException JavaDoc
94     {
95         return file.getAttributes();
96     }
97
98
99     public Certificate JavaDoc[] getCertificates()
100     {
101         return file.doGetCertificates();
102     }
103
104
105     public void connect()
106     {
107         connected = true;
108     }
109
110     public InputStream JavaDoc getInputStream()
111         throws IOException JavaDoc
112     {
113         return content.getInputStream();
114     }
115
116     public OutputStream JavaDoc getOutputStream()
117         throws IOException JavaDoc
118     {
119         return content.getOutputStream();
120     }
121
122     public int getContentLength()
123     {
124         try
125         {
126             return (int) content.getSize();
127         }
128         catch (FileSystemException fse)
129         {
130         }
131
132         return -1;
133     }
134
135 }
136
Popular Tags