KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > core > JarEntryContentReference


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.update.core;
12
13 import java.io.*;
14 import java.net.*;
15 import java.util.jar.*;
16
17 /**
18  * .jar file entry content reference.
19  * <p>
20  * This class may be instantiated or subclassed by clients.
21  * </p>
22  * <p>
23  * <b>Note:</b> This class/interface is part of an interim API that is still under development and expected to
24  * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
25  * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
26  * (repeatedly) as the API evolves.
27  * </p>
28  * @see org.eclipse.update.core.ContentReference
29  * @see org.eclipse.update.core.JarContentReference
30  * @since 2.0
31  */

32 public class JarEntryContentReference extends ContentReference {
33
34     private JarContentReference jarContentReference;
35     private JarEntry entry;
36
37     /**
38      * Create jar entry content reference.
39      *
40      * @param id "symbolic" path identifier
41      * @param jarContentReference jar file content reference
42      * @param entry jar entry
43      * @since 2.0
44      */

45     public JarEntryContentReference(
46         String JavaDoc id,
47         JarContentReference jarContentReference,
48         JarEntry entry) {
49         super(id, (File) null);
50         this.jarContentReference = jarContentReference;
51         this.entry = entry;
52     }
53     
54     /**
55      * Creates an input stream for the reference.
56      *
57      * @return input stream
58      * @exception IOException unable to create stream
59      * @since 2.0
60      */

61     public InputStream getInputStream() throws IOException {
62         return jarContentReference.asJarFile().getInputStream(entry);
63     }
64     
65     /**
66      * Returns the size of the referenced entry.
67      *
68      * @return input size
69      * @since 2.0
70      */

71     public long getInputSize() {
72         return entry.getSize();
73     }
74     
75     /**
76      * Indicates whether the reference is to a an entry within a local jar.
77      *
78      * @return <code>true</code> if the reference is local,
79      * otherwise <code>false</code>
80      * @since 2.0
81      */

82     public boolean isLocalReference() {
83         return jarContentReference.isLocalReference();
84     }
85         
86     /**
87      * Returns the content reference as a URL.
88      *
89      * @return reference as URL
90      * @exception IOException reference cannot be returned as URL
91      * @since 2.0
92      */

93     public URL asURL() throws IOException {
94         String JavaDoc fileName =
95             jarContentReference.asFile().getAbsolutePath().replace(File.separatorChar, '/');
96         return new URL("jar:file:" + fileName + "!/" + entry.getName()); //$NON-NLS-1$ //$NON-NLS-2$
97
}
98             
99     /**
100      * Return string representation of this reference.
101      *
102      * @return string representation
103      * @since 2.0
104      */

105     public String JavaDoc toString() {
106         URL url;
107         try {
108             url = asURL();
109         } catch (IOException e) {
110             url = null;
111         }
112         if (url != null)
113             return url.toExternalForm();
114         else
115             return getClass().getName() + "@" + hashCode(); //$NON-NLS-1$
116
}
117 }
118
Popular Tags