001: /*
002: * Copyright 1995-2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.java;
027:
028: import java.io.File;
029: import java.io.InputStream;
030: import java.io.FileInputStream;
031: import java.io.IOException;
032: import java.util.zip.*;
033:
034: /**
035: * This class is used to represent a file loaded from the class path, and
036: * can either be a regular file or a zip file entry.
037: *
038: * WARNING: The contents of this source file are not part of any
039: * supported API. Code that depends on them does so at its own risk:
040: * they are subject to change or removal without notice.
041: */
042: public class ClassFile {
043: /*
044: * Non-null if this represents a regular file
045: */
046: private File file;
047:
048: /*
049: * Non-null if this represents a zip file entry
050: */
051: private ZipFile zipFile;
052: private ZipEntry zipEntry;
053:
054: /**
055: * Constructor for instance representing a regular file
056: */
057: public ClassFile(File file) {
058: this .file = file;
059: }
060:
061: /**
062: * Constructor for instance representing a zip file entry
063: */
064: public ClassFile(ZipFile zf, ZipEntry ze) {
065: this .zipFile = zf;
066: this .zipEntry = ze;
067: }
068:
069: /**
070: * Returns true if this is zip file entry
071: */
072: public boolean isZipped() {
073: return zipFile != null;
074: }
075:
076: /**
077: * Returns input stream to either regular file or zip file entry
078: */
079: public InputStream getInputStream() throws IOException {
080: if (file != null) {
081: return new FileInputStream(file);
082: } else {
083: try {
084: return zipFile.getInputStream(zipEntry);
085: } catch (ZipException e) {
086: throw new IOException(e.getMessage());
087: }
088: }
089: }
090:
091: /**
092: * Returns true if file exists.
093: */
094: public boolean exists() {
095: return file != null ? file.exists() : true;
096: }
097:
098: /**
099: * Returns true if this is a directory.
100: */
101: public boolean isDirectory() {
102: return file != null ? file.isDirectory() : zipEntry.getName()
103: .endsWith("/");
104: }
105:
106: /**
107: * Return last modification time
108: */
109: public long lastModified() {
110: return file != null ? file.lastModified() : zipEntry.getTime();
111: }
112:
113: /**
114: * Get file path. The path for a zip file entry will also include
115: * the zip file name.
116: */
117: public String getPath() {
118: if (file != null) {
119: return file.getPath();
120: } else {
121: return zipFile.getName() + "(" + zipEntry.getName() + ")";
122: }
123: }
124:
125: /**
126: * Get name of file entry excluding directory name
127: */
128: public String getName() {
129: return file != null ? file.getName() : zipEntry.getName();
130: }
131:
132: //JCOV
133: /**
134: * Get absolute name of file entry
135: */
136: public String getAbsoluteName() {
137: String absoluteName;
138: if (file != null) {
139: try {
140: absoluteName = file.getCanonicalPath();
141: } catch (IOException e) {
142: absoluteName = file.getAbsolutePath();
143: }
144: } else {
145: absoluteName = zipFile.getName() + "(" + zipEntry.getName()
146: + ")";
147: }
148: return absoluteName;
149: }
150:
151: // end JCOV
152:
153: /**
154: * Get length of file
155: */
156: public long length() {
157: return file != null ? file.length() : zipEntry.getSize();
158: }
159:
160: public String toString() {
161: return (file != null) ? file.toString() : zipEntry.toString();
162: }
163: }
|