Method from com.sun.tools.javac.util.BaseFileManager Detail: |
public void cache(JavaFileObject file,
CharBuffer cb) {
contentCache.put(file, new SoftReference< CharBuffer >(cb));
}
|
public 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
}
|
public CharBuffer getCachedContent(JavaFileObject file) {
SoftReference< CharBuffer > r = contentCache.get(file);
return (r == null ? null : r.get());
}
|
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 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);
}
|
public String getEncodingName() {
String encName = options.get(OptionName.ENCODING);
if (encName == null)
return getDefaultEncodingName();
else
return encName;
}
|
public static Kind getKind(String name) {
if (name.endsWith(Kind.CLASS.extension))
return Kind.CLASS;
else if (name.endsWith(Kind.SOURCE.extension))
return Kind.SOURCE;
else if (name.endsWith(Kind.HTML.extension))
return Kind.HTML;
else
return Kind.OTHER;
}
|
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);
}
|
public boolean handleOption(String current,
Iterator<String> 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;
}
|
abstract public boolean isDefaultBootClassPath()
|
public int isSupportedOption(String option) {
for (JavacOption o : javacFileManagerOptions) {
if (o.matches(option))
return o.hasArg() ? 1 : 0;
}
return -1;
}
|
public 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();
}
Make a byte buffer from an input stream. |
protected static T nullCheck(T o) {
o.getClass(); // null check
return o;
}
|
protected static Collection<T> nullCheck(Collection<T> it) {
for (T t : it)
t.getClass(); // null check
return it;
}
|
public void recycleByteBuffer(ByteBuffer bb) {
byteBufferCache.put(bb);
}
|
protected void setContext(Context context) {
log = Log.instance(context);
options = Options.instance(context);
classLoaderClass = options.get("procloader");
}
Set the context for JavacPathFileManager. |