KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > JarEntryFile


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.jdt.internal.core;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.util.zip.ZipEntry JavaDoc;
17 import java.util.zip.ZipFile JavaDoc;
18
19 import org.eclipse.core.resources.IStorage;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.jdt.core.IJarEntryResource;
22 import org.eclipse.jdt.core.IJavaModelStatusConstants;
23 import org.eclipse.jdt.core.JavaModelException;
24 import org.eclipse.jdt.internal.compiler.util.Util;
25
26 /**
27  * A jar entry that represents a non-java file found in a JAR.
28  *
29  * @see IStorage
30  */

31 public class JarEntryFile extends JarEntryResource {
32     private static final IJarEntryResource[] NO_CHILDREN = new IJarEntryResource[0];
33     
34     public JarEntryFile(String JavaDoc simpleName) {
35         super(simpleName);
36     }
37     
38     public JarEntryResource clone(Object JavaDoc newParent) {
39         JarEntryFile file = new JarEntryFile(simpleName);
40         file.setParent(newParent);
41         return file;
42     }
43     
44     public InputStream JavaDoc getContents() throws CoreException {
45         ZipFile JavaDoc zipFile = null;
46         try {
47             zipFile = getZipFile();
48             if (JavaModelManager.ZIP_ACCESS_VERBOSE) {
49                 System.out.println("(" + Thread.currentThread() + ") [JarEntryFile.getContents()] Creating ZipFile on " +zipFile.getName()); //$NON-NLS-1$ //$NON-NLS-2$
50
}
51             String JavaDoc entryName = getEntryName();
52             ZipEntry JavaDoc zipEntry = zipFile.getEntry(entryName);
53             if (zipEntry == null){
54                 throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_PATH, entryName));
55             }
56             byte[] contents = Util.getZipEntryByteContent(zipEntry, zipFile);
57             return new ByteArrayInputStream JavaDoc(contents);
58         } catch (IOException JavaDoc e){
59             throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
60         } finally {
61             // avoid leaking ZipFiles
62
JavaModelManager.getJavaModelManager().closeZipFile(zipFile);
63         }
64     }
65     
66     public IJarEntryResource[] getChildren() {
67         return NO_CHILDREN;
68     }
69     
70     public boolean isFile() {
71         return true;
72     }
73     
74     public String JavaDoc toString() {
75         return "JarEntryFile["+getEntryName()+"]"; //$NON-NLS-2$ //$NON-NLS-1$
76
}
77 }
78
Popular Tags