Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/tools/java/ClassFile.java
38918 views
/*1* Copyright (c) 1995, 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.tools.java;2627import java.io.File;28import java.io.InputStream;29import java.io.FileInputStream;30import java.io.IOException;31import java.util.zip.*;3233/**34* This class is used to represent a file loaded from the class path, and35* can either be a regular file or a zip file entry.36*37* WARNING: The contents of this source file are not part of any38* supported API. Code that depends on them does so at its own risk:39* they are subject to change or removal without notice.40*/41public42class ClassFile {43/*44* Non-null if this represents a regular file45*/46private File file;4748/*49* Non-null if this represents a zip file entry50*/51private ZipFile zipFile;52private ZipEntry zipEntry;5354/**55* Constructor for instance representing a regular file56*/57public ClassFile(File file) {58this.file = file;59}6061/**62* Constructor for instance representing a zip file entry63*/64public ClassFile(ZipFile zf, ZipEntry ze) {65this.zipFile = zf;66this.zipEntry = ze;67}6869/**70* Returns true if this is zip file entry71*/72public boolean isZipped() {73return zipFile != null;74}7576/**77* Returns input stream to either regular file or zip file entry78*/79public InputStream getInputStream() throws IOException {80if (file != null) {81return new FileInputStream(file);82} else {83try {84return zipFile.getInputStream(zipEntry);85} catch (ZipException e) {86throw new IOException(e.getMessage());87}88}89}9091/**92* Returns true if file exists.93*/94public boolean exists() {95return file != null ? file.exists() : true;96}9798/**99* Returns true if this is a directory.100*/101public boolean isDirectory() {102return file != null ? file.isDirectory() :103zipEntry.getName().endsWith("/");104}105106/**107* Return last modification time108*/109public long lastModified() {110return file != null ? file.lastModified() : zipEntry.getTime();111}112113/**114* Get file path. The path for a zip file entry will also include115* the zip file name.116*/117public String getPath() {118if (file != null) {119return file.getPath();120} else {121return zipFile.getName() + "(" + zipEntry.getName() + ")";122}123}124125/**126* Get name of file entry excluding directory name127*/128public String getName() {129return file != null ? file.getName() : zipEntry.getName();130}131132//JCOV133/**134* Get absolute name of file entry135*/136public String getAbsoluteName() {137String absoluteName;138if (file != null) {139try {140absoluteName = file.getCanonicalPath();141} catch (IOException e) {142absoluteName = file.getAbsolutePath();143}144} else {145absoluteName = zipFile.getName() + "(" + zipEntry.getName() + ")";146}147return absoluteName;148}149// end JCOV150151/**152* Get length of file153*/154public long length() {155return file != null ? file.length() : zipEntry.getSize();156}157158public String toString() {159return (file != null) ? file.toString() : zipEntry.toString();160}161}162163164