Charset charset;
try {
charset= Charset.forName(encoding);
} catch (UnsupportedCharsetException ex) {
String message= "Unsupported encoding '" + encoding + "'.";
IStatus s= new Status(IStatus.ERROR, Plugin.PLUGIN_ID, IStatus.OK, message, ex);
throw new CoreException(s);
} catch (IllegalCharsetNameException ex) {
String message= "Unsupported encoding '" + encoding + "'.";
IStatus s= new Status(IStatus.ERROR, Plugin.PLUGIN_ID, IStatus.OK, message, ex);
throw new CoreException(s);
}
CharsetEncoder encoder= charset.newEncoder();
encoder.onMalformedInput(CodingErrorAction.REPLACE);
encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
InputStream stream;
try {
byte[] bytes;
ByteBuffer byteBuffer= encoder.encode(CharBuffer.wrap(document.get()));
if (byteBuffer.hasArray())
bytes= byteBuffer.array();
else {
bytes= new byte[byteBuffer.limit()];
byteBuffer.get(bytes);
}
stream= new ByteArrayInputStream(bytes, 0, byteBuffer.limit());
} catch (CharacterCodingException ex) {
Assert.isTrue(ex instanceof UnmappableCharacterException);
String message= "Charset mapping failed.";
IStatus s= new Status(IStatus.ERROR, Plugin.PLUGIN_ID, EditorsUI.CHARSET_MAPPING_FAILED, message, null);
throw new CoreException(s);
}
if (file.exists()) {