src/share/classes/com/sun/tools/javac/file/ZipFileIndexArchive.java

Tue, 26 Aug 2008 14:52:59 -0700

author
jjg
date
Tue, 26 Aug 2008 14:52:59 -0700
changeset 103
e571266ae14f
parent 57
aa67a5da66e3
child 333
7c2d6da61646
permissions
-rw-r--r--

6508981: cleanup file separator handling in JavacFileManager
Reviewed-by: mcimadamore

jjg@57 1 /*
jjg@57 2 * Copyright 2005-2008 Sun Microsystems, Inc. All Rights Reserved.
jjg@57 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@57 4 *
jjg@57 5 * This code is free software; you can redistribute it and/or modify it
jjg@57 6 * under the terms of the GNU General Public License version 2 only, as
jjg@57 7 * published by the Free Software Foundation. Sun designates this
jjg@57 8 * particular file as subject to the "Classpath" exception as provided
jjg@57 9 * by Sun in the LICENSE file that accompanied this code.
jjg@57 10 *
jjg@57 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@57 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@57 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@57 14 * version 2 for more details (a copy is included in the LICENSE file that
jjg@57 15 * accompanied this code).
jjg@57 16 *
jjg@57 17 * You should have received a copy of the GNU General Public License version
jjg@57 18 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@57 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@57 20 *
jjg@57 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
jjg@57 22 * CA 95054 USA or visit www.sun.com if you need additional information or
jjg@57 23 * have any questions.
jjg@57 24 */
jjg@57 25
jjg@57 26 package com.sun.tools.javac.file;
jjg@57 27
jjg@57 28 import java.io.IOException;
jjg@57 29 import java.util.Set;
jjg@57 30 import javax.tools.JavaFileObject;
jjg@57 31
jjg@57 32 import java.io.ByteArrayInputStream;
jjg@57 33 import java.io.File;
jjg@57 34 import java.io.InputStream;
jjg@57 35 import java.io.OutputStream;
jjg@57 36 import java.io.Writer;
jjg@57 37 import java.net.URI;
jjg@57 38 import java.nio.ByteBuffer;
jjg@57 39 import java.nio.CharBuffer;
jjg@57 40 import java.nio.charset.CharsetDecoder;
jjg@57 41
jjg@103 42 import com.sun.tools.javac.file.JavacFileManager.Archive;
jjg@103 43 import com.sun.tools.javac.file.RelativePath.RelativeDirectory;
jjg@103 44 import com.sun.tools.javac.file.RelativePath.RelativeFile;
jjg@103 45 import com.sun.tools.javac.util.List;
jjg@103 46
jjg@57 47 public class ZipFileIndexArchive implements Archive {
jjg@57 48
jjg@57 49 private final ZipFileIndex zfIndex;
jjg@57 50 private JavacFileManager fileManager;
jjg@57 51
jjg@57 52 public ZipFileIndexArchive(JavacFileManager fileManager, ZipFileIndex zdir) throws IOException {
jjg@57 53 super();
jjg@57 54 this.fileManager = fileManager;
jjg@57 55 this.zfIndex = zdir;
jjg@57 56 }
jjg@57 57
jjg@103 58 public boolean contains(RelativePath name) {
jjg@57 59 return zfIndex.contains(name);
jjg@57 60 }
jjg@57 61
jjg@103 62 public List<String> getFiles(RelativeDirectory subdirectory) {
jjg@103 63 return zfIndex.getFiles(subdirectory);
jjg@57 64 }
jjg@57 65
jjg@103 66 public JavaFileObject getFileObject(RelativeDirectory subdirectory, String file) {
jjg@103 67 RelativeFile fullZipFileName = new RelativeFile(subdirectory, file);
jjg@57 68 ZipFileIndex.Entry entry = zfIndex.getZipIndexEntry(fullZipFileName);
jjg@57 69 JavaFileObject ret = new ZipFileIndexFileObject(fileManager, zfIndex, entry, zfIndex.getZipFile().getPath());
jjg@57 70 return ret;
jjg@57 71 }
jjg@57 72
jjg@103 73 public Set<RelativeDirectory> getSubdirectories() {
jjg@57 74 return zfIndex.getAllDirectories();
jjg@57 75 }
jjg@57 76
jjg@57 77 public void close() throws IOException {
jjg@57 78 zfIndex.close();
jjg@57 79 }
jjg@57 80
jjg@103 81 public String toString() {
jjg@103 82 return "ZipFileIndexArchive[" + zfIndex + "]";
jjg@103 83 }
jjg@103 84
jjg@57 85 /**
jjg@57 86 * A subclass of JavaFileObject representing zip entries using the com.sun.tools.javac.file.ZipFileIndex implementation.
jjg@57 87 */
jjg@57 88 public static class ZipFileIndexFileObject extends BaseFileObject {
jjg@57 89
jjg@57 90 /** The entry's name.
jjg@57 91 */
jjg@57 92 private String name;
jjg@57 93
jjg@57 94 /** The zipfile containing the entry.
jjg@57 95 */
jjg@57 96 ZipFileIndex zfIndex;
jjg@57 97
jjg@57 98 /** The underlying zip entry object.
jjg@57 99 */
jjg@57 100 ZipFileIndex.Entry entry;
jjg@57 101
jjg@57 102 /** The InputStream for this zip entry (file.)
jjg@57 103 */
jjg@57 104 InputStream inputStream = null;
jjg@57 105
jjg@57 106 /** The name of the zip file where this entry resides.
jjg@57 107 */
jjg@57 108 String zipName;
jjg@57 109
jjg@57 110
jjg@57 111 ZipFileIndexFileObject(JavacFileManager fileManager, ZipFileIndex zfIndex, ZipFileIndex.Entry entry, String zipFileName) {
jjg@57 112 super(fileManager);
jjg@57 113 this.name = entry.getFileName();
jjg@57 114 this.zfIndex = zfIndex;
jjg@57 115 this.entry = entry;
jjg@57 116 this.zipName = zipFileName;
jjg@57 117 }
jjg@57 118
jjg@57 119 public InputStream openInputStream() throws IOException {
jjg@57 120
jjg@57 121 if (inputStream == null) {
jjg@57 122 inputStream = new ByteArrayInputStream(read());
jjg@57 123 }
jjg@57 124 return inputStream;
jjg@57 125 }
jjg@57 126
jjg@57 127 protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
jjg@57 128 return fileManager.getDecoder(fileManager.getEncodingName(), ignoreEncodingErrors);
jjg@57 129 }
jjg@57 130
jjg@57 131 public OutputStream openOutputStream() throws IOException {
jjg@57 132 throw new UnsupportedOperationException();
jjg@57 133 }
jjg@57 134
jjg@57 135 public Writer openWriter() throws IOException {
jjg@57 136 throw new UnsupportedOperationException();
jjg@57 137 }
jjg@57 138
jjg@57 139 /** @deprecated see bug 6410637 */
jjg@57 140 @Deprecated
jjg@57 141 public String getName() {
jjg@57 142 return name;
jjg@57 143 }
jjg@57 144
jjg@57 145 public boolean isNameCompatible(String cn, JavaFileObject.Kind k) {
jjg@57 146 cn.getClass(); // null check
jjg@57 147 if (k == Kind.OTHER && getKind() != k)
jjg@57 148 return false;
jjg@57 149 return name.equals(cn + k.extension);
jjg@57 150 }
jjg@57 151
jjg@57 152 /** @deprecated see bug 6410637 */
jjg@57 153 @Deprecated
jjg@57 154 public String getPath() {
jjg@57 155 return zipName + "(" + entry.getName() + ")";
jjg@57 156 }
jjg@57 157
jjg@57 158 public long getLastModified() {
jjg@57 159 return entry.getLastModified();
jjg@57 160 }
jjg@57 161
jjg@57 162 public boolean delete() {
jjg@57 163 throw new UnsupportedOperationException();
jjg@57 164 }
jjg@57 165
jjg@57 166 @Override
jjg@57 167 public boolean equals(Object other) {
jjg@57 168 if (!(other instanceof ZipFileIndexFileObject))
jjg@57 169 return false;
jjg@57 170 ZipFileIndexFileObject o = (ZipFileIndexFileObject) other;
jjg@57 171 return entry.equals(o.entry);
jjg@57 172 }
jjg@57 173
jjg@57 174 @Override
jjg@57 175 public int hashCode() {
jjg@57 176 return zipName.hashCode() + (name.hashCode() << 10);
jjg@57 177 }
jjg@57 178
jjg@57 179 public String getZipName() {
jjg@57 180 return zipName;
jjg@57 181 }
jjg@57 182
jjg@57 183 public String getZipEntryName() {
jjg@57 184 return entry.getName();
jjg@57 185 }
jjg@57 186
jjg@57 187 public URI toUri() {
jjg@57 188 String zipName = new File(getZipName()).toURI().normalize().getPath();
jjg@57 189 String entryName = getZipEntryName();
jjg@57 190 return URI.create("jar:" + zipName + "!" + entryName);
jjg@57 191 }
jjg@57 192
jjg@57 193 private byte[] read() throws IOException {
jjg@103 194 assert entry != null; // see constructor
jjg@57 195 return zfIndex.read(entry);
jjg@57 196 }
jjg@57 197
jjg@57 198 public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
jjg@57 199 CharBuffer cb = fileManager.getCachedContent(this);
jjg@57 200 if (cb == null) {
jjg@57 201 InputStream in = new ByteArrayInputStream(zfIndex.read(entry));
jjg@57 202 try {
jjg@57 203 ByteBuffer bb = fileManager.makeByteBuffer(in);
jjg@57 204 JavaFileObject prev = fileManager.log.useSource(this);
jjg@57 205 try {
jjg@57 206 cb = fileManager.decode(bb, ignoreEncodingErrors);
jjg@57 207 } finally {
jjg@57 208 fileManager.log.useSource(prev);
jjg@57 209 }
jjg@57 210 fileManager.recycleByteBuffer(bb); // save for next time
jjg@57 211 if (!ignoreEncodingErrors)
jjg@57 212 fileManager.cache(this, cb);
jjg@57 213 } finally {
jjg@57 214 in.close();
jjg@57 215 }
jjg@57 216 }
jjg@57 217 return cb;
jjg@57 218 }
jjg@57 219
jjg@57 220 @Override
jjg@57 221 protected String inferBinaryName(Iterable<? extends File> path) {
jjg@57 222 String entryName = getZipEntryName();
jjg@57 223 if (zfIndex.symbolFilePrefix != null) {
jjg@103 224 String prefix = zfIndex.symbolFilePrefix.path;
jjg@57 225 if (entryName.startsWith(prefix))
jjg@57 226 entryName = entryName.substring(prefix.length());
jjg@57 227 }
jjg@103 228 return removeExtension(entryName).replace('/', '.');
jjg@57 229 }
jjg@57 230 }
jjg@57 231
jjg@57 232 }

mercurial