01: /*******************************************************************************
02: * Copyright (c) 2000, 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.core;
11:
12: import java.io.ByteArrayInputStream;
13: import java.io.IOException;
14: import java.io.InputStream;
15: import java.util.zip.ZipEntry;
16: import java.util.zip.ZipFile;
17:
18: import org.eclipse.core.resources.IStorage;
19: import org.eclipse.core.runtime.CoreException;
20: import org.eclipse.jdt.core.IJarEntryResource;
21: import org.eclipse.jdt.core.IJavaModelStatusConstants;
22: import org.eclipse.jdt.core.JavaModelException;
23: import org.eclipse.jdt.internal.compiler.util.Util;
24:
25: /**
26: * A jar entry that represents a non-java file found in a JAR.
27: *
28: * @see IStorage
29: */
30: public class JarEntryFile extends JarEntryResource {
31: private static final IJarEntryResource[] NO_CHILDREN = new IJarEntryResource[0];
32:
33: public JarEntryFile(String simpleName) {
34: super (simpleName);
35: }
36:
37: public JarEntryResource clone(Object newParent) {
38: JarEntryFile file = new JarEntryFile(simpleName);
39: file.setParent(newParent);
40: return file;
41: }
42:
43: public InputStream getContents() throws CoreException {
44: ZipFile zipFile = null;
45: try {
46: zipFile = getZipFile();
47: if (JavaModelManager.ZIP_ACCESS_VERBOSE) {
48: System.out
49: .println("(" + Thread.currentThread() + ") [JarEntryFile.getContents()] Creating ZipFile on " + zipFile.getName()); //$NON-NLS-1$ //$NON-NLS-2$
50: }
51: String entryName = getEntryName();
52: ZipEntry zipEntry = zipFile.getEntry(entryName);
53: if (zipEntry == null) {
54: throw new JavaModelException(new JavaModelStatus(
55: IJavaModelStatusConstants.INVALID_PATH,
56: entryName));
57: }
58: byte[] contents = Util.getZipEntryByteContent(zipEntry,
59: zipFile);
60: return new ByteArrayInputStream(contents);
61: } catch (IOException e) {
62: throw new JavaModelException(e,
63: IJavaModelStatusConstants.IO_EXCEPTION);
64: } finally {
65: // avoid leaking ZipFiles
66: JavaModelManager.getJavaModelManager()
67: .closeZipFile(zipFile);
68: }
69: }
70:
71: public IJarEntryResource[] getChildren() {
72: return NO_CHILDREN;
73: }
74:
75: public boolean isFile() {
76: return true;
77: }
78:
79: public String toString() {
80: return "JarEntryFile[" + getEntryName() + "]"; //$NON-NLS-2$ //$NON-NLS-1$
81: }
82: }
|