KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > baseadaptor > bundlefile > BundleEntry


1 /*******************************************************************************
2  * Copyright (c) 2004, 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
12 package org.eclipse.osgi.baseadaptor.bundlefile;
13
14 import java.io.*;
15 import java.net.URL JavaDoc;
16 import org.eclipse.osgi.framework.debug.Debug;
17
18 /**
19  * A BundleEntry represents one entry of a BundleFile.
20  * <p>
21  * Clients may extend this class.
22  * </p>
23  * @since 3.2
24  */

25 public abstract class BundleEntry {
26     protected static final int BUF_SIZE = 8 * 1024;
27     /**
28      * Return an InputStream for the entry.
29      *
30      * @return InputStream for the entry.
31      * @throws java.io.IOException If an error occurs reading the bundle.
32      */

33     public abstract InputStream getInputStream() throws IOException;
34
35     /**
36      * Return the size of the entry (uncompressed).
37      *
38      * @return size of entry.
39      */

40     public abstract long getSize();
41
42     /**
43      * Return the name of the entry.
44      *
45      * @return name of entry.
46      */

47     public abstract String JavaDoc getName();
48
49     /**
50      * Get the modification time for this BundleEntry.
51      * <p>If the modification time has not been set,
52      * this method will return <tt>-1</tt>.
53      *
54      * @return last modification time.
55      */

56     public abstract long getTime();
57
58     /**
59      * Get a URL to the bundle entry that uses a common protocol (i.e. file:
60      * jar: or http: etc.).
61      * @return a URL to the bundle entry that uses a common protocol
62      */

63     public abstract URL JavaDoc getLocalURL();
64
65     /**
66      * Get a URL to the content of the bundle entry that uses the file: protocol.
67      * The content of the bundle entry may be downloaded or extracted to the local
68      * file system in order to create a file: URL.
69      * @return a URL to the content of the bundle entry that uses the file: protocol
70      */

71     public abstract URL JavaDoc getFileURL();
72
73     /**
74      * Return the name of this BundleEntry by calling getName().
75      *
76      * @return String representation of this BundleEntry.
77      */

78     public String JavaDoc toString() {
79         return (getName());
80     }
81
82     /**
83      * Used for class loading. This default implementation gets the input stream from this entry
84      * and copies the content into a byte array.
85      * @return a byte array containing the content of this entry
86      * @throws IOException
87      */

88     public byte[] getBytes() throws IOException{
89         InputStream in = getInputStream();
90         int length = (int) getSize();
91         byte[] classbytes;
92         int bytesread = 0;
93         int readcount;
94         if (Debug.DEBUG && Debug.DEBUG_LOADER)
95             Debug.println(" about to read " + length + " bytes from " + getName()); //$NON-NLS-1$ //$NON-NLS-2$
96

97         try {
98             if (length > 0) {
99                 classbytes = new byte[length];
100                 for (; bytesread < length; bytesread += readcount) {
101                     readcount = in.read(classbytes, bytesread, length - bytesread);
102                     if (readcount <= 0) /* if we didn't read anything */
103                         break; /* leave the loop */
104                 }
105             } else /* BundleEntry does not know its own length! */{
106                 length = BUF_SIZE;
107                 classbytes = new byte[length];
108                 readloop: while (true) {
109                     for (; bytesread < length; bytesread += readcount) {
110                         readcount = in.read(classbytes, bytesread, length - bytesread);
111                         if (readcount <= 0) /* if we didn't read anything */
112                             break readloop; /* leave the loop */
113                     }
114                     byte[] oldbytes = classbytes;
115                     length += BUF_SIZE;
116                     classbytes = new byte[length];
117                     System.arraycopy(oldbytes, 0, classbytes, 0, bytesread);
118                 }
119             }
120             if (classbytes.length > bytesread) {
121                 byte[] oldbytes = classbytes;
122                 classbytes = new byte[bytesread];
123                 System.arraycopy(oldbytes, 0, classbytes, 0, bytesread);
124             }
125         } finally {
126             try {
127                 in.close();
128             } catch (IOException ee) {
129                 // nothing to do here
130
}
131         }
132         return classbytes;
133     }
134 }
135
Popular Tags