IString charset = values.string("UTF8");
if (append) {
// in case the file already has a encoding, we have to correctly append that.
InputStream in = null;
Charset detected = null;
try {
detected = ctx.getResolverRegistry().getCharset(sloc.getURI());
if (detected == null) {
in = ctx.getResolverRegistry().getInputStream(sloc.getURI());
detected = UnicodeDetector.estimateCharset(in);
}
}catch(FileNotFoundException fnfex){
throw RuntimeExceptionFactory.pathNotFound(sloc, ctx.getCurrentAST(), null);
} catch (IOException e) {
throw RuntimeExceptionFactory.io(values.string(e.getMessage()), ctx.getCurrentAST(), null);
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
throw RuntimeExceptionFactory.io(values.string(e.getMessage()), ctx.getCurrentAST(), null);
}
}
}
if (detected != null)
charset = values.string(detected.name());
else {
charset = values.string(Charset.defaultCharset().name());
}
}
writeFileEnc(sloc, charset, V, append, ctx);