--- /dev/null 2009-07-15 19:05:43.000000000 -0700
+++ new/src/share/classes/com/sun/tools/javac/nio/JavacPathFileManager.java 2009-09-24 17:46:29.000000000 -0700
@@ -0,0 +1,797 @@
+/*
+ * Copyright 2009 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+package com.sun.tools.javac.nio;
+
+
+import java.io.ByteArrayOutputStream;
+import java.io.Closeable;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStreamWriter;
+import java.lang.ref.SoftReference;
+import java.lang.reflect.Constructor;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetDecoder;
+import java.nio.charset.CoderResult;
+import java.nio.charset.CodingErrorAction;
+import java.nio.charset.IllegalCharsetNameException;
+import java.nio.charset.UnsupportedCharsetException;
+import java.nio.file.DirectoryStream;
+import java.nio.file.FileSystem;
+import java.nio.file.FileSystems;
+import java.nio.file.Path;
+import java.nio.file.attribute.Attributes;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Deque;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedHashSet;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.Set;
+import javax.lang.model.SourceVersion;
+import javax.tools.FileObject;
+import javax.tools.JavaFileManager;
+import javax.tools.JavaFileObject;
+import javax.tools.JavaFileObject.Kind;
+import javax.tools.StandardLocation;
+
+import static javax.tools.StandardLocation.*;
+
+import com.sun.tools.javac.code.Source;
+import com.sun.tools.javac.file.CloseableURLClassLoader;
+import com.sun.tools.javac.file.Paths;
+import com.sun.tools.javac.main.JavacOption;
+import com.sun.tools.javac.main.OptionName;
+import com.sun.tools.javac.main.RecognizedOptions;
+import com.sun.tools.javac.util.Context;
+import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
+import com.sun.tools.javac.util.List;
+import com.sun.tools.javac.util.ListBuffer;
+import com.sun.tools.javac.util.Log;
+import com.sun.tools.javac.util.Options;
+
+import static com.sun.tools.javac.main.OptionName.*;
+
+/**
+ *
This is NOT part of any API supported by Sun Microsystems. If
+ * you write code that depends on this, you do so at your own risk.
+ * This code and its internal interfaces are subject to change or
+ * deletion without notice.
+ */
+public class JavacPathFileManager implements PathFileManager {
+ protected FileSystem defaultFileSystem;
+
+ /**
+ * Create a JavacPathFileManager using a given context, optionally registering
+ * it as the JavaFileManager for that context.
+ */
+ public JavacPathFileManager(Context context, boolean register, Charset charset) {
+ if (register)
+ context.put(JavaFileManager.class, this);
+ this.charset = charset;
+ byteBufferCache = new ByteBufferCache();
+ pathsForLocation = new HashMap();
+ fileSystems = new HashMap();
+ setContext(context);
+ }
+
+ /**
+ * Set the context for JavacPathFileManager.
+ */
+ void setContext(Context context) {
+ log = Log.instance(context);
+ options = Options.instance(context);
+ searchPaths = Paths.instance(context);
+ }
+
+ @Override
+ public FileSystem getDefaultFileSystem() {
+ if (defaultFileSystem == null)
+ defaultFileSystem = FileSystems.getDefault();
+ return defaultFileSystem;
+ }
+
+ @Override
+ public void setDefaultFileSystem(FileSystem fs) {
+ defaultFileSystem = fs;
+ }
+
+ @Override
+ public void flush() throws IOException {
+ contentCache.clear();
+ }
+
+ @Override
+ public void close() throws IOException {
+ for (FileSystem fs: fileSystems.values())
+ fs.close();
+ }
+
+ @Override
+ public ClassLoader getClassLoader(Location location) {
+ nullCheck(location);
+ Iterable extends Path> path = getLocation(location);
+ if (path == null)
+ return null;
+ ListBuffer lb = new ListBuffer();
+ for (Path p: path) {
+ try {
+ lb.append(p.toUri().toURL());
+ } catch (MalformedURLException e) {
+ throw new AssertionError(e);
+ }
+ }
+
+ return getClassLoader(lb.toArray(new URL[lb.size()]));
+ }
+
+ //
+
+ public boolean hasLocation(Location location) {
+ return (getLocation(location) != null);
+ }
+
+ public Iterable extends Path> getLocation(Location location) {
+ nullCheck(location);
+ lazyInitSearchPaths();
+ PathsForLocation path = pathsForLocation.get(location);
+ if (path == null && !pathsForLocation.containsKey(location)) {
+ setDefaultForLocation(location);
+ path = pathsForLocation.get(location);
+ }
+ return path;
+ }
+
+ private Path getOutputLocation(Location location) {
+ Iterable extends Path> paths = getLocation(location);
+ return (paths == null ? null : paths.iterator().next());
+ }
+
+ public void setLocation(Location location, Iterable extends Path> searchPath)
+ throws IOException
+ {
+ nullCheck(location);
+ lazyInitSearchPaths();
+ if (searchPath == null) {
+ setDefaultForLocation(location);
+ } else {
+ if (location.isOutputLocation())
+ checkOutputPath(searchPath);
+ PathsForLocation pl = new PathsForLocation();
+ for (Path p: searchPath)
+ pl.add(p); // TODO -Xlint:path warn if path not found
+ pathsForLocation.put(location, pl);
+ }
+ }
+
+ private void checkOutputPath(Iterable extends Path> searchPath) throws IOException {
+ Iterator extends Path> pathIter = searchPath.iterator();
+ if (!pathIter.hasNext())
+ throw new IllegalArgumentException("empty path for directory");
+ Path path = pathIter.next();
+ if (pathIter.hasNext())
+ throw new IllegalArgumentException("path too long for directory");
+ if (!path.exists())
+ throw new FileNotFoundException(path + ": does not exist");
+ else if (!isDirectory(path))
+ throw new IOException(path + ": not a directory");
+ }
+
+ private void setDefaultForLocation(Location locn) {
+ Collection files = null;
+ if (locn instanceof StandardLocation) {
+ switch ((StandardLocation) locn) {
+ case CLASS_PATH:
+ files = searchPaths.userClassPath();
+ break;
+ case PLATFORM_CLASS_PATH:
+ files = searchPaths.bootClassPath();
+ break;
+ case SOURCE_PATH:
+ files = searchPaths.sourcePath();
+ break;
+ case CLASS_OUTPUT: {
+ String arg = options.get(D);
+ files = (arg == null ? null : Collections.singleton(new File(arg)));
+ break;
+ }
+ case SOURCE_OUTPUT: {
+ String arg = options.get(S);
+ files = (arg == null ? null : Collections.singleton(new File(arg)));
+ break;
+ }
+ }
+ }
+
+ PathsForLocation pl = new PathsForLocation();
+ if (files != null) {
+ for (File f: files)
+ pl.add(f.toPath());
+ }
+ pathsForLocation.put(locn, pl);
+ }
+
+ private void lazyInitSearchPaths() {
+ if (!inited) {
+ setDefaultForLocation(PLATFORM_CLASS_PATH);
+ setDefaultForLocation(CLASS_PATH);
+ setDefaultForLocation(SOURCE_PATH);
+ inited = true;
+ }
+ }
+ // where
+ private boolean inited = false;
+
+ private Map pathsForLocation;
+ private Paths searchPaths;
+
+ private static class PathsForLocation extends LinkedHashSet {
+ private static final long serialVersionUID = 6788510222394486733L;
+ }
+
+ //
+
+ //
+
+ @Override
+ public Path getPath(FileObject fo) {
+ fo.getClass(); // null check
+ if (!(fo instanceof PathFileObject))
+ throw new IllegalArgumentException();
+ return ((PathFileObject) fo).getPath();
+ }
+
+ @Override
+ public boolean isSameFile(FileObject a, FileObject b) {
+ nullCheck(a);
+ nullCheck(b);
+ if (!(a instanceof PathFileObject))
+ throw new IllegalArgumentException("Not supported: " + a);
+ if (!(b instanceof PathFileObject))
+ throw new IllegalArgumentException("Not supported: " + b);
+ return ((PathFileObject) a).isSameFile((PathFileObject) b);
+ }
+
+ public Iterable list(Location location,
+ String packageName, Set kinds, boolean recurse)
+ throws IOException {
+ // validatePackageName(packageName);
+ nullCheck(packageName);
+ nullCheck(kinds);
+
+ Iterable extends Path> paths = getLocation(location);
+ if (paths == null)
+ return List.nil();
+ ListBuffer results = new ListBuffer();
+
+ for (Path path : paths)
+ list(path, packageName, kinds, recurse, results);
+
+ return results.toList();
+ }
+
+ private void list(Path path, String packageName, Set kinds,
+ boolean recurse, ListBuffer results)
+ throws IOException {
+ if (!path.exists())
+ return;
+
+ Path pathDir;
+ if (isDirectory(path))
+ pathDir = path;
+ else {
+ FileSystem fs = getFileSystem(path);
+ if (fs == null)
+ return;
+ pathDir = fs.getRootDirectories().iterator().next();
+ }
+ String sep = path.getFileSystem().getSeparator();
+ Path packageDir = packageName.isEmpty() ? pathDir
+ : pathDir.resolve(packageName.replace(".", sep));
+ if (!packageDir.exists())
+ return;
+
+// if (!caseMapCheck(d, subdirectory))
+// return;
+
+ Deque queue = new LinkedList();
+ queue.add(packageDir);
+
+ Path dir;
+ while ((dir = queue.poll()) != null) {
+ DirectoryStream ds = dir.newDirectoryStream();
+ try {
+ for (Path p: ds) {
+ String name = p.getName().toString();
+ if (isDirectory(p)) {
+ if (recurse && SourceVersion.isIdentifier(name)) {
+ queue.add(p);
+ }
+ } else {
+ if (kinds.contains(getKind(name))) {
+ JavaFileObject fe =
+ PathFileObject.createDirectoryPathFileObject(this, p, pathDir);
+ results.append(fe);
+ }
+ }
+ }
+ } finally {
+ ds.close();
+ }
+ }
+ }
+
+ @Override
+ public Iterable extends JavaFileObject> getJavaFileObjectsFromPaths(
+ Iterable extends Path> paths) {
+ ArrayList result;
+ if (paths instanceof Collection>)
+ result = new ArrayList(((Collection>)paths).size());
+ else
+ result = new ArrayList();
+ for (Path p: paths)
+ result.add(PathFileObject.createSimplePathFileObject(this, nullCheck(p)));
+ return result;
+ }
+
+ @Override
+ public Iterable extends JavaFileObject> getJavaFileObjects(Path... paths) {
+ return getJavaFileObjectsFromPaths(Arrays.asList(nullCheck(paths)));
+ }
+
+ public JavaFileObject getJavaFileForInput(Location location,
+ String className, Kind kind) throws IOException {
+ return getFileForInput(location, getRelativePath(className, kind));
+ }
+
+ public FileObject getFileForInput(Location location,
+ String packageName, String relativeName) throws IOException {
+ return getFileForInput(location, getRelativePath(packageName, relativeName));
+ }
+
+ private JavaFileObject getFileForInput(Location location, String relativePath)
+ throws IOException {
+ for (Path p: getLocation(location)) {
+ if (isDirectory(p)) {
+ Path f = resolve(p, relativePath);
+ if (f.exists())
+ return PathFileObject.createDirectoryPathFileObject(this, f, p);
+ } else {
+ FileSystem fs = getFileSystem(p);
+ if (fs != null) {
+ Path file = getPath(fs, relativePath);
+ if (file.exists())
+ return PathFileObject.createJarPathFileObject(this, file);
+ }
+ }
+ }
+ return null;
+ }
+
+ public JavaFileObject getJavaFileForOutput(Location location,
+ String className, Kind kind, FileObject sibling) throws IOException {
+ return getFileForOutput(location, getRelativePath(className, kind), sibling);
+ }
+
+ public FileObject getFileForOutput(Location location, String packageName,
+ String relativeName, FileObject sibling)
+ throws IOException {
+ return getFileForOutput(location, getRelativePath(packageName, relativeName), sibling);
+ }
+
+ private JavaFileObject getFileForOutput(Location location,
+ String relativePath, FileObject sibling) {
+ Path dir = getOutputLocation(location);
+ if (dir == null) {
+ if (location == CLASS_OUTPUT) {
+ Path siblingDir = null;
+ if (sibling != null && sibling instanceof PathFileObject) {
+ siblingDir = ((PathFileObject) sibling).getPath().getParent();
+ }
+ return PathFileObject.createSiblingPathFileObject(this,
+ siblingDir.resolve(getBaseName(relativePath)),
+ relativePath);
+ } else if (location == SOURCE_OUTPUT) {
+ dir = getOutputLocation(CLASS_OUTPUT);
+ }
+ }
+
+ Path file;
+ if (dir != null) {
+ file = resolve(dir, relativePath);
+ return PathFileObject.createDirectoryPathFileObject(this, file, dir);
+ } else {
+ file = getPath(getDefaultFileSystem(), relativePath);
+ return PathFileObject.createSimplePathFileObject(this, file);
+ }
+
+ }
+
+ public String inferBinaryName(Location location, JavaFileObject fo) {
+ nullCheck(fo);
+ // Need to match the path semantics of list(location, ...)
+ Iterable extends Path> paths = getLocation(location);
+ if (paths == null) {
+ return null;
+ }
+
+ if (!(fo instanceof PathFileObject))
+ throw new IllegalArgumentException(fo.getClass().getName());
+
+ return ((PathFileObject) fo).inferBinaryName(paths);
+ }
+
+ private FileSystem getFileSystem(Path p) throws IOException {
+ FileSystem fs = fileSystems.get(p);
+ if (fs == null) {
+ fs = FileSystems.newFileSystem(p, Collections.emptyMap(), null);
+ fileSystems.put(p, fs);
+ }
+ return fs;
+ }
+
+ private Map fileSystems;
+
+ //
+
+ //
+
+ private static String getRelativePath(String className, Kind kind) {
+ return className.replace(".", "/") + kind.extension;
+ }
+
+ private static String getRelativePath(String packageName, String relativeName) {
+ return packageName.replace(".", "/") + relativeName;
+ }
+
+ private static String getBaseName(String relativePath) {
+ int lastSep = relativePath.lastIndexOf("/");
+ return relativePath.substring(lastSep + 1); // safe if "/" not found
+ }
+
+ private static String getExtension(String fileName) {
+ int lastDot = fileName.lastIndexOf(".");
+ return (lastDot == -1 ? "" : fileName.substring(lastDot));
+ }
+
+ protected Kind getKind(String fileName) {
+ String extn = getExtension(fileName);
+ if (extn.equals(Kind.CLASS.extension))
+ return JavaFileObject.Kind.CLASS;
+ else if (extn.equals(Kind.SOURCE.extension))
+ return JavaFileObject.Kind.SOURCE;
+ else if (extn.equals(Kind.HTML.extension))
+ return JavaFileObject.Kind.HTML;
+ else
+ return JavaFileObject.Kind.OTHER;
+ }
+
+ private static boolean isDirectory(Path path) throws IOException {
+ BasicFileAttributes attrs = Attributes.readBasicFileAttributes(path);
+ return attrs.isDirectory();
+ }
+
+ private static Path getPath(FileSystem fs, String relativePath) {
+ return fs.getPath(relativePath.replace("/", fs.getSeparator()));
+ }
+
+ private static Path resolve(Path base, String relativePath) {
+ FileSystem fs = base.getFileSystem();
+ Path rp = fs.getPath(relativePath.replace("/", fs.getSeparator()));
+ return base.resolve(rp);
+ }
+
+ //
+
+ //
+
+ // The following code is mostly all cut n paste from JavacFileManager and
+ // could/should be shared with it, perhaps by moving to a new class
+ // BaseFileManager in com.sun.tools.javac.util.
+ // There are no references to java.io.File or java.nio.file.Path here.
+
+ /**
+ * The log to be used for error reporting.
+ */
+ protected Log log;
+
+ /**
+ * User provided charset (through javax.tools).
+ */
+ protected Charset charset;
+
+ private Options options;
+
+ protected String classLoaderClass;
+
+ protected Source getSource() {
+ String sourceName = options.get(OptionName.SOURCE);
+ Source source = null;
+ if (sourceName != null)
+ source = Source.lookup(sourceName);
+ return (source != null ? source : Source.DEFAULT);
+ }
+
+ protected ClassLoader getClassLoader(URL[] urls) {
+ ClassLoader thisClassLoader = getClass().getClassLoader();
+
+ // Bug: 6558476
+ // Ideally, ClassLoader should be Closeable, but before JDK7 it is not.
+ // On older versions, try the following, to get a closeable classloader.
+
+ // 1: Allow client to specify the class to use via hidden option
+ if (classLoaderClass != null) {
+ try {
+ Class extends ClassLoader> loader =
+ Class.forName(classLoaderClass).asSubclass(ClassLoader.class);
+ Class>[] constrArgTypes = { URL[].class, ClassLoader.class };
+ Constructor extends ClassLoader> constr = loader.getConstructor(constrArgTypes);
+ return constr.newInstance(new Object[] { urls, thisClassLoader });
+ } catch (Throwable t) {
+ // ignore errors loading user-provided class loader, fall through
+ }
+ }
+
+ // 2: If URLClassLoader implements Closeable, use that.
+ if (Closeable.class.isAssignableFrom(URLClassLoader.class))
+ return new URLClassLoader(urls, thisClassLoader);
+
+ // 3: Try using private reflection-based CloseableURLClassLoader
+ try {
+ return new CloseableURLClassLoader(urls, thisClassLoader);
+ } catch (Throwable t) {
+ // ignore errors loading workaround class loader, fall through
+ }
+
+ // 4: If all else fails, use plain old standard URLClassLoader
+ return new URLClassLoader(urls, thisClassLoader);
+ }
+
+
+ //
+ public boolean handleOption(String current, Iterator remaining) {
+ for (JavacOption o: javacFileManagerOptions) {
+ if (o.matches(current)) {
+ if (o.hasArg()) {
+ if (remaining.hasNext()) {
+ if (!o.process(options, current, remaining.next()))
+ return true;
+ }
+ } else {
+ if (!o.process(options, current))
+ return true;
+ }
+ // operand missing, or process returned false
+ throw new IllegalArgumentException(current);
+ }
+ }
+
+ return false;
+ }
+ // where
+ private static JavacOption[] javacFileManagerOptions =
+ RecognizedOptions.getJavacFileManagerOptions(
+ new RecognizedOptions.GrumpyHelper());
+
+ public int isSupportedOption(String option) {
+ for (JavacOption o : javacFileManagerOptions) {
+ if (o.matches(option))
+ return o.hasArg() ? 1 : 0;
+ }
+ return -1;
+ }
+ //
+
+ //
+ private String defaultEncodingName;
+ private String getDefaultEncodingName() {
+ if (defaultEncodingName == null) {
+ defaultEncodingName =
+ new OutputStreamWriter(new ByteArrayOutputStream()).getEncoding();
+ }
+ return defaultEncodingName;
+ }
+
+ protected String getEncodingName() {
+ String encName = options.get(OptionName.ENCODING);
+ if (encName == null)
+ return getDefaultEncodingName();
+ else
+ return encName;
+ }
+
+ CharBuffer decode(ByteBuffer inbuf, boolean ignoreEncodingErrors) {
+ String encodingName = getEncodingName();
+ CharsetDecoder decoder;
+ try {
+ decoder = getDecoder(encodingName, ignoreEncodingErrors);
+ } catch (IllegalCharsetNameException e) {
+ log.error("unsupported.encoding", encodingName);
+ return (CharBuffer)CharBuffer.allocate(1).flip();
+ } catch (UnsupportedCharsetException e) {
+ log.error("unsupported.encoding", encodingName);
+ return (CharBuffer)CharBuffer.allocate(1).flip();
+ }
+
+ // slightly overestimate the buffer size to avoid reallocation.
+ float factor =
+ decoder.averageCharsPerByte() * 0.8f +
+ decoder.maxCharsPerByte() * 0.2f;
+ CharBuffer dest = CharBuffer.
+ allocate(10 + (int)(inbuf.remaining()*factor));
+
+ while (true) {
+ CoderResult result = decoder.decode(inbuf, dest, true);
+ dest.flip();
+
+ if (result.isUnderflow()) { // done reading
+ // make sure there is at least one extra character
+ if (dest.limit() == dest.capacity()) {
+ dest = CharBuffer.allocate(dest.capacity()+1).put(dest);
+ dest.flip();
+ }
+ return dest;
+ } else if (result.isOverflow()) { // buffer too small; expand
+ int newCapacity =
+ 10 + dest.capacity() +
+ (int)(inbuf.remaining()*decoder.maxCharsPerByte());
+ dest = CharBuffer.allocate(newCapacity).put(dest);
+ } else if (result.isMalformed() || result.isUnmappable()) {
+ // bad character in input
+
+ // report coding error (warn only pre 1.5)
+ if (!getSource().allowEncodingErrors()) {
+ log.error(new SimpleDiagnosticPosition(dest.limit()),
+ "illegal.char.for.encoding",
+ charset == null ? encodingName : charset.name());
+ } else {
+ log.warning(new SimpleDiagnosticPosition(dest.limit()),
+ "illegal.char.for.encoding",
+ charset == null ? encodingName : charset.name());
+ }
+
+ // skip past the coding error
+ inbuf.position(inbuf.position() + result.length());
+
+ // undo the flip() to prepare the output buffer
+ // for more translation
+ dest.position(dest.limit());
+ dest.limit(dest.capacity());
+ dest.put((char)0xfffd); // backward compatible
+ } else {
+ throw new AssertionError(result);
+ }
+ }
+ // unreached
+ }
+
+ CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErrors) {
+ Charset cs = (this.charset == null)
+ ? Charset.forName(encodingName)
+ : this.charset;
+ CharsetDecoder decoder = cs.newDecoder();
+
+ CodingErrorAction action;
+ if (ignoreEncodingErrors)
+ action = CodingErrorAction.REPLACE;
+ else
+ action = CodingErrorAction.REPORT;
+
+ return decoder
+ .onMalformedInput(action)
+ .onUnmappableCharacter(action);
+ }
+ //
+
+ //
+ /**
+ * Make a byte buffer from an input stream.
+ */
+ ByteBuffer makeByteBuffer(InputStream in)
+ throws IOException {
+ int limit = in.available();
+ if (limit < 1024) limit = 1024;
+ ByteBuffer result = byteBufferCache.get(limit);
+ int position = 0;
+ while (in.available() != 0) {
+ if (position >= limit)
+ // expand buffer
+ result = ByteBuffer.
+ allocate(limit <<= 1).
+ put((ByteBuffer)result.flip());
+ int count = in.read(result.array(),
+ position,
+ limit - position);
+ if (count < 0) break;
+ result.position(position += count);
+ }
+ return (ByteBuffer)result.flip();
+ }
+
+ void recycleByteBuffer(ByteBuffer bb) {
+ byteBufferCache.put(bb);
+ }
+
+ /**
+ * A single-element cache of direct byte buffers.
+ */
+ private static class ByteBufferCache {
+ private ByteBuffer cached;
+ ByteBuffer get(int capacity) {
+ if (capacity < 20480) capacity = 20480;
+ ByteBuffer result =
+ (cached != null && cached.capacity() >= capacity)
+ ? (ByteBuffer)cached.clear()
+ : ByteBuffer.allocate(capacity + capacity>>1);
+ cached = null;
+ return result;
+ }
+ void put(ByteBuffer x) {
+ cached = x;
+ }
+ }
+
+ private final ByteBufferCache byteBufferCache;
+ //
+
+ //
+ CharBuffer getCachedContent(JavaFileObject file) {
+ SoftReference r = contentCache.get(file);
+ return (r == null ? null : r.get());
+ }
+
+ void cache(JavaFileObject file, CharBuffer cb) {
+ contentCache.put(file, new SoftReference(cb));
+ }
+
+ private final Map> contentCache
+ = new HashMap>();
+ //
+
+ private static T nullCheck(T o) {
+ o.getClass(); // null check
+ return o;
+ }
+
+ private static Collection nullCheck(Collection it) {
+ for (T t : it)
+ t.getClass(); // null check
+ return it;
+ }
+ //
+}