📄 javacfilemanager.java
字号:
log.useSource(prev); } byteBufferCache.put(bb); // save for next time if (!ignoreEncodingErrors) contentCache.put(this, new SoftReference<CharBuffer>(cb)); } finally { in.close(); } } return cb; } @Override public boolean equals(Object other) { if (!(other instanceof RegularFileObject)) return false; RegularFileObject o = (RegularFileObject) other; try { return f.equals(o.f) || f.getCanonicalFile().equals(o.f.getCanonicalFile()); } catch (IOException e) { return false; } } @Override public int hashCode() { return f.hashCode(); } public URI toUri() { try { // Do no use File.toURI to avoid file system access String path = f.getAbsolutePath().replace(File.separatorChar, '/'); return new URI("file://" + path).normalize(); } catch (URISyntaxException ex) { return f.toURI(); } } } /** * A subclass of JavaFileObject representing zip entries. */ public class ZipFileObject extends BaseFileObject { /** The entry's name. */ private String name; /** The zipfile containing the entry. */ ZipFile zdir; /** The underlying zip entry object. */ ZipEntry entry; public ZipFileObject(String name, ZipFile zdir, ZipEntry entry) { this.name = name; this.zdir = zdir; this.entry = entry; } public InputStream openInputStream() throws IOException { return zdir.getInputStream(entry); } public OutputStream openOutputStream() throws IOException { throw new UnsupportedOperationException(); } protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) { return JavacFileManager.this.getDecoder(getEncodingName(), ignoreEncodingErrors); } public Writer openWriter() throws IOException { throw new UnsupportedOperationException(); } /** @deprecated see bug 6410637 */ @Deprecated public String getName() { return name; } public boolean isNameCompatible(String cn, JavaFileObject.Kind k) { cn.getClass(); // null check if (k == Kind.OTHER && getKind() != k) return false; return name.equals(cn + k.extension); } /** @deprecated see bug 6410637 */ @Deprecated public String getPath() { return zdir.getName() + "(" + entry + ")"; } public long getLastModified() { return entry.getTime(); } public boolean delete() { throw new UnsupportedOperationException(); } public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException { SoftReference<CharBuffer> r = contentCache.get(this); CharBuffer cb = (r == null ? null : r.get()); if (cb == null) { InputStream in = zdir.getInputStream(entry); try { ByteBuffer bb = makeByteBuffer(in); JavaFileObject prev = log.useSource(this); try { cb = decode(bb, ignoreEncodingErrors); } finally { log.useSource(prev); } byteBufferCache.put(bb); // save for next time if (!ignoreEncodingErrors) contentCache.put(this, new SoftReference<CharBuffer>(cb)); } finally { in.close(); } } return cb; } @Override public boolean equals(Object other) { if (!(other instanceof ZipFileObject)) return false; ZipFileObject o = (ZipFileObject) other; return zdir.equals(o.zdir) || name.equals(o.name); } @Override public int hashCode() { return zdir.hashCode() + name.hashCode(); } public String getZipName() { return zdir.getName(); } public String getZipEntryName() { return entry.getName(); } public URI toUri() { String zipName = new File(getZipName()).toURI().normalize().getPath(); String entryName = getZipEntryName(); return URI.create("jar:" + zipName + "!" + entryName); } } /** * A subclass of JavaFileObject representing zip entries using the com.sun.tools.javac.zip.ZipFileIndex implementation. */ public class ZipFileIndexFileObject extends BaseFileObject { /** The entry's name. */ private String name; /** The zipfile containing the entry. */ ZipFileIndex zfIndex; /** The underlying zip entry object. */ ZipFileIndexEntry entry; /** The InputStream for this zip entry (file.) */ InputStream inputStream = null; /** The name of the zip file where this entry resides. */ String zipName; JavacFileManager defFileManager = null; public ZipFileIndexFileObject(JavacFileManager fileManager, ZipFileIndex zfIndex, ZipFileIndexEntry entry, String zipFileName) { super(); this.name = entry.getFileName(); this.zfIndex = zfIndex; this.entry = entry; this.zipName = zipFileName; defFileManager = fileManager; } public InputStream openInputStream() throws IOException { if (inputStream == null) { inputStream = new ByteArrayInputStream(read()); } return inputStream; } protected CharsetDecoder getDecoder(boolean ignoreEncodingErrors) { return JavacFileManager.this.getDecoder(getEncodingName(), ignoreEncodingErrors); } public OutputStream openOutputStream() throws IOException { throw new UnsupportedOperationException(); } public Writer openWriter() throws IOException { throw new UnsupportedOperationException(); } /** @deprecated see bug 6410637 */ @Deprecated public String getName() { return name; } public boolean isNameCompatible(String cn, JavaFileObject.Kind k) { cn.getClass(); // null check if (k == Kind.OTHER && getKind() != k) return false; return name.equals(cn + k.extension); } /** @deprecated see bug 6410637 */ @Deprecated public String getPath() { return entry.getName() + "(" + entry + ")"; } public long getLastModified() { return entry.getLastModified(); } public boolean delete() { throw new UnsupportedOperationException(); } @Override public boolean equals(Object other) { if (!(other instanceof ZipFileIndexFileObject)) return false; ZipFileIndexFileObject o = (ZipFileIndexFileObject) other; return entry.equals(o.entry); } @Override public int hashCode() { return zipName.hashCode() + (name.hashCode() << 10); } public String getZipName() { return zipName; } public String getZipEntryName() { return entry.getName(); } public URI toUri() { String zipName = new File(getZipName()).toURI().normalize().getPath(); String entryName = getZipEntryName(); if (File.separatorChar != '/') { entryName = entryName.replace(File.separatorChar, '/'); } return URI.create("jar:" + zipName + "!" + entryName); } private byte[] read() throws IOException { if (entry == null) { entry = zfIndex.getZipIndexEntry(name); if (entry == null) throw new FileNotFoundException(); } return zfIndex.read(entry); } public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException { SoftReference<CharBuffer> r = defFileManager.contentCache.get(this); CharBuffer cb = (r == null ? null : r.get()); if (cb == null) { InputStream in = new ByteArrayInputStream(zfIndex.read(entry)); try { ByteBuffer bb = makeByteBuffer(in); JavaFileObject prev = log.useSource(this); try { cb = decode(bb, ignoreEncodingErrors); } finally { log.useSource(prev); } byteBufferCache.put(bb); // save for next time if (!ignoreEncodingErrors) defFileManager.contentCache.put(this, new SoftReference<CharBuffer>(cb)); } finally { in.close(); } } return cb; } } public class ZipFileIndexArchive implements Archive { private final ZipFileIndex zfIndex; private JavacFileManager fileManager; public ZipFileIndexArchive(JavacFileManager fileManager, ZipFileIndex zdir) throws IOException { this.fileManager = fileManager; this.zfIndex = zdir; } public boolean contains(String name) { return zfIndex.contains(name); } public com.sun.tools.javac.util.List<String> getFiles(String subdirectory) { return zfIndex.getFiles(((subdirectory.endsWith("/") || subdirectory.endsWith("\\"))? subdirectory.substring(0, subdirectory.length() - 1) : subdirectory)); } public JavaFileObject getFileObject(String subdirectory, String file) { String fullZipFileName = subdirectory + file; ZipFileIndexEntry entry = zfIndex.getZipIndexEntry(fullZipFileName); JavaFileObject ret = new ZipFileIndexFileObject(fileManager, zfIndex, entry, zfIndex.getZipFile().getPath()); return ret; } public Set<String> getSubdirectories() { return zfIndex.getAllDirectories(); } public void close() throws IOException { zfIndex.close(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -