Showing the changes to java/pkg/sun/tools/java/ClassFile.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,162 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1995, 2004, Oracle and/or its affiliates. All rights reserved. | ||
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| 4 | * | ||
| 5 | * This code is free software; you can redistribute it and/or modify it | ||
| 6 | * under the terms of the GNU General Public License version 2 only, as | ||
| 7 | * published by the Free Software Foundation. Oracle designates this | ||
| 8 | * particular file as subject to the "Classpath" exception as provided | ||
| 9 | * by Oracle in the LICENSE file that accompanied this code. | ||
| 10 | * | ||
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| 14 | * version 2 for more details (a copy is included in the LICENSE file that | ||
| 15 | * accompanied this code). | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU General Public License version | ||
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, | ||
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| 20 | * | ||
| 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| 22 | * or visit www.oracle.com if you need additional information or have any | ||
| 23 | * questions. | ||
| 24 | */ | ||
| 25 | |||
| 26 | package sun.tools.java; | ||
| 27 | |||
| 28 | import java.io.File; | ||
| 29 | import java.io.InputStream; | ||
| 30 | import java.io.FileInputStream; | ||
| 31 | import java.io.IOException; | ||
| 32 | import java.util.zip.*; | ||
| 33 | |||
| 34 | /** | ||
| 35 | * This class is used to represent a file loaded from the class path, and | ||
| 36 | * can either be a regular file or a zip file entry. | ||
| 37 | * | ||
| 38 | * WARNING: The contents of this source file are not part of any | ||
| 39 | * supported API. Code that depends on them does so at its own risk: | ||
| 40 | * they are subject to change or removal without notice. | ||
| 41 | */ | ||
| 42 | public | ||
| 43 | class ClassFile { | ||
| 44 | /* | ||
| 45 | * Non-null if this represents a regular file | ||
| 46 | */ | ||
| 47 | private File file; | ||
| 48 | |||
| 49 | /* | ||
| 50 | * Non-null if this represents a zip file entry | ||
| 51 | */ | ||
| 52 | private ZipFile zipFile; | ||
| 53 | private ZipEntry zipEntry; | ||
| 54 | |||
| 55 | /** | ||
| 56 | * Constructor for instance representing a regular file | ||
| 57 | */ | ||
| 58 | public ClassFile(File file) { | ||
| 59 | this.file = file; | ||
| 60 | } | ||
| 61 | |||
| 62 | /** | ||
| 63 | * Constructor for instance representing a zip file entry | ||
| 64 | */ | ||
| 65 | public ClassFile(ZipFile zf, ZipEntry ze) { | ||
| 66 | this.zipFile = zf; | ||
| 67 | this.zipEntry = ze; | ||
| 68 | } | ||
| 69 | |||
| 70 | /** | ||
| 71 | * Returns true if this is zip file entry | ||
| 72 | */ | ||
| 73 | public boolean isZipped() { | ||
| 74 | return zipFile != null; | ||
| 75 | } | ||
| 76 | |||
| 77 | /** | ||
| 78 | * Returns input stream to either regular file or zip file entry | ||
| 79 | */ | ||
| 80 | public InputStream getInputStream() throws IOException { | ||
| 81 | if (file != null) { | ||
| 82 | return new FileInputStream(file); | ||
| 83 | } else { | ||
| 84 | try { | ||
| 85 | return zipFile.getInputStream(zipEntry); | ||
| 86 | } catch (ZipException e) { | ||
| 87 | throw new IOException(e.getMessage()); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | /** | ||
| 93 | * Returns true if file exists. | ||
| 94 | */ | ||
| 95 | public boolean exists() { | ||
| 96 | return file != null ? file.exists() : true; | ||
| 97 | } | ||
| 98 | |||
| 99 | /** | ||
| 100 | * Returns true if this is a directory. | ||
| 101 | */ | ||
| 102 | public boolean isDirectory() { | ||
| 103 | return file != null ? file.isDirectory() : | ||
| 104 | zipEntry.getName().endsWith("/"); | ||
| 105 | } | ||
| 106 | |||
| 107 | /** | ||
| 108 | * Return last modification time | ||
| 109 | */ | ||
| 110 | public long lastModified() { | ||
| 111 | return file != null ? file.lastModified() : zipEntry.getTime(); | ||
| 112 | } | ||
| 113 | |||
| 114 | /** | ||
| 115 | * Get file path. The path for a zip file entry will also include | ||
| 116 | * the zip file name. | ||
| 117 | */ | ||
| 118 | public String getPath() { | ||
| 119 | if (file != null) { | ||
| 120 | return file.getPath(); | ||
| 121 | } else { | ||
| 122 | return zipFile.getName() + "(" + zipEntry.getName() + ")"; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | /** | ||
| 127 | * Get name of file entry excluding directory name | ||
| 128 | */ | ||
| 129 | public String getName() { | ||
| 130 | return file != null ? file.getName() : zipEntry.getName(); | ||
| 131 | } | ||
| 132 | |||
| 133 | //JCOV | ||
| 134 | /** | ||
| 135 | * Get absolute name of file entry | ||
| 136 | */ | ||
| 137 | public String getAbsoluteName() { | ||
| 138 | String absoluteName; | ||
| 139 | if (file != null) { | ||
| 140 | try { | ||
| 141 | absoluteName = file.getCanonicalPath(); | ||
| 142 | } catch (IOException e) { | ||
| 143 | absoluteName = file.getAbsolutePath(); | ||
| 144 | } | ||
| 145 | } else { | ||
| 146 | absoluteName = zipFile.getName() + "(" + zipEntry.getName() + ")"; | ||
| 147 | } | ||
| 148 | return absoluteName; | ||
| 149 | } | ||
| 150 | // end JCOV | ||
| 151 | |||
| 152 | /** | ||
| 153 | * Get length of file | ||
| 154 | */ | ||
| 155 | public long length() { | ||
| 156 | return file != null ? file.length() : zipEntry.getSize(); | ||
| 157 | } | ||
| 158 | |||
| 159 | public String toString() { | ||
| 160 | return (file != null) ? file.toString() : zipEntry.toString(); | ||
| 161 | } | ||
| 162 | } | ||