use of java.nio.charset.UnsupportedCharsetException in project linuxtools by eclipse.
the class DockerDocumentProvider method doSaveDocument.
@Override
protected void doSaveDocument(IProgressMonitor monitor, Object element, IDocument document, boolean overwrite) throws CoreException {
if (element instanceof FileStoreEditorInput) {
String encoding = null;
ElementInfo info = getElementInfo(element);
Path filePath = Paths.get(((FileStoreEditorInput) element).getURI());
encoding = getEncoding(element);
Charset charset;
try {
charset = Charset.forName(encoding);
} catch (UnsupportedCharsetException ex) {
String message = NLS.bind(Messages.DockerDocumentProvider_encoding_not_supported, encoding);
IStatus s = new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, IStatus.OK, message, ex);
throw new CoreException(s);
} catch (IllegalCharsetNameException ex) {
String message = NLS.bind(Messages.DockerDocumentProvider_encoding_not_legal, encoding);
IStatus s = new Status(IStatus.ERROR, EditorsUI.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 = NLS.bind(Messages.DockerDocumentProvider_cannot_be_mapped + Messages.DockerDocumentProvider_chars_not_supported, encoding);
IStatus s = new Status(IStatus.ERROR, EditorsUI.PLUGIN_ID, EditorsUI.CHARSET_MAPPING_FAILED, message, null);
throw new CoreException(s);
}
if (Files.exists(filePath)) {
// inform about the upcoming content change
fireElementStateChanging(element);
try (FileWriter fw = new FileWriter(filePath.toFile());
InputStreamReader istream = new InputStreamReader(stream)) {
char[] bb = new char[1024];
int nRead = istream.read(bb);
while (nRead > 0) {
fw.write(bb, 0, nRead);
nRead = istream.read(bb);
}
} catch (RuntimeException | IOException x) {
// inform about failure
fireElementStateChangeFailed(element);
throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, x.getMessage()));
}
if (info != null) {
ResourceMarkerAnnotationModel model = (ResourceMarkerAnnotationModel) info.fModel;
if (model != null)
model.updateMarkers(info.fDocument);
}
} else {
try {
Files.createFile(filePath);
try (FileWriter fw = new FileWriter(filePath.toFile());
InputStreamReader istream = new InputStreamReader(stream)) {
char[] bb = new char[1024];
int nRead = istream.read(bb);
while (nRead > 0) {
fw.write(bb, 0, nRead);
nRead = istream.read(bb);
}
} catch (IOException x) {
throw x;
}
} catch (IOException e) {
throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage()));
} finally {
monitor.done();
}
}
} else {
super.doSaveDocument(monitor, element, document, overwrite);
}
}
use of java.nio.charset.UnsupportedCharsetException in project webtools.sourceediting by eclipse.
the class URLModelProviderCSS method getModelForRead.
/**
*/
private IStructuredModel getModelForRead(IFile file) throws IOException {
if (file == null)
return null;
IModelManager manager = getModelManager();
// create a fake InputStream
IStructuredModel model = null;
try {
model = manager.getModelForRead(file);
} catch (UnsupportedCharsetException ex) {
try {
model = manager.getModelForRead(file, EncodingRule.FORCE_DEFAULT);
} catch (IOException ioe) {
} catch (CoreException ce) {
}
} catch (CoreException ce) {
}
return model;
}
use of java.nio.charset.UnsupportedCharsetException in project webtools.sourceediting by eclipse.
the class URLModelProviderCSS method getModelForEdit.
/**
*/
private IStructuredModel getModelForEdit(IFile file) throws IOException {
if (file == null)
return null;
IModelManager manager = getModelManager();
// create a fake InputStream
IStructuredModel model = null;
try {
model = manager.getModelForEdit(file);
} catch (UnsupportedCharsetException ex) {
try {
model = manager.getModelForEdit(file, EncodingRule.FORCE_DEFAULT);
} catch (IOException ioe) {
} catch (CoreException ce) {
}
} catch (CoreException ce) {
}
return model;
}
use of java.nio.charset.UnsupportedCharsetException in project webtools.sourceediting by eclipse.
the class AbstractResourceEncodingDetector method getAppropriateJavaCharset.
/**
* This method can return null, if invalid charset name (in which case
* "appropriateDefault" should be used, if a name is really need for some
* "save anyway" cases).
*
* @param detectedCharsetName
* @return
*/
private String getAppropriateJavaCharset(String detectedCharsetName) {
String result = null;
// 1. Check explicit mapping overrides from
// property file -- its here we pick up "rules" for cases
// that are not even in Java
result = CodedIO.checkMappingOverrides(detectedCharsetName);
// 2. Use the "canonical" name from JRE mappings
// Note: see Charset JavaDoc, the name you get one
// with can be alias,
// the name you get back is "standard" name.
Charset javaCharset = null;
try {
javaCharset = Charset.forName(detectedCharsetName);
} catch (UnsupportedCharsetException e) {
// overridden
if (result != null && result.equals(detectedCharsetName)) {
fEncodingMemento.setInvalidEncoding(detectedCharsetName);
}
} catch (IllegalCharsetNameException e) {
// overridden
if (result != null && result.equals(detectedCharsetName)) {
fEncodingMemento.setInvalidEncoding(detectedCharsetName);
}
}
// give priority to java cononical name, if present
if (javaCharset != null) {
result = javaCharset.name();
// but still allow overrides
result = CodedIO.checkMappingOverrides(result);
}
return result;
}
use of java.nio.charset.UnsupportedCharsetException in project UniversalMediaServer by UniversalMediaServer.
the class PMS method verifyPidName.
/*
* This method is only called for Windows OS'es, so specialized Windows charset handling is allowed
*/
private static boolean verifyPidName(String pid) throws IOException, IllegalAccessException {
if (!Platform.isWindows()) {
throw new IllegalAccessException("verifyPidName can only be called from Windows!");
}
ProcessBuilder pb = new ProcessBuilder("tasklist", "/FI", "\"PID eq " + pid + "\"", "/V", "/NH", "/FO", "CSV");
pb.redirectErrorStream(true);
Process p = pb.start();
String line;
Charset charset = null;
int codepage = WinUtils.getOEMCP();
String[] aliases = { "cp" + codepage, "MS" + codepage };
for (String alias : aliases) {
try {
charset = Charset.forName(alias);
break;
} catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
charset = null;
}
}
if (charset == null) {
charset = Charset.defaultCharset();
LOGGER.warn("Couldn't find a supported charset for {}, using default ({})", aliases, charset);
}
try (BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream(), charset))) {
try {
p.waitFor();
} catch (InterruptedException e) {
in.close();
return false;
}
line = in.readLine();
}
if (line == null) {
return false;
}
// remove all " and convert to common case before splitting result on ,
String[] tmp = line.toLowerCase().replaceAll("\"", "").split(",");
// if the line is too short we don't kill the process
if (tmp.length < 9) {
return false;
}
// check first and last, update since taskkill changed
// also check 2nd last since we migh have ", POSSIBLY UNSTABLE" in there
boolean ums = tmp[tmp.length - 1].contains("universal media server") || tmp[tmp.length - 2].contains("universal media server");
return tmp[0].equals("javaw.exe") && ums;
}
Aggregations