Search in sources :

Example 76 with UnsupportedCharsetException

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);
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) FileWriter(java.io.FileWriter) CharsetEncoder(java.nio.charset.CharsetEncoder) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) UnmappableCharacterException(java.nio.charset.UnmappableCharacterException) Path(java.nio.file.Path) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) ResourceMarkerAnnotationModel(org.eclipse.ui.texteditor.ResourceMarkerAnnotationModel) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset) CharacterCodingException(java.nio.charset.CharacterCodingException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) FileStoreEditorInput(org.eclipse.ui.ide.FileStoreEditorInput)

Example 77 with UnsupportedCharsetException

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;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IOException(java.io.IOException)

Example 78 with UnsupportedCharsetException

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;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IOException(java.io.IOException)

Example 79 with UnsupportedCharsetException

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;
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) Charset(java.nio.charset.Charset)

Example 80 with UnsupportedCharsetException

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;
}
Also used : Charset(java.nio.charset.Charset) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException)

Aggregations

UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)116 Charset (java.nio.charset.Charset)55 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)43 IOException (java.io.IOException)35 File (java.io.File)11 ByteBuffer (java.nio.ByteBuffer)11 InputStream (java.io.InputStream)10 UnsupportedEncodingException (java.io.UnsupportedEncodingException)10 CoreException (org.eclipse.core.runtime.CoreException)10 CharacterCodingException (java.nio.charset.CharacterCodingException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 HashMap (java.util.HashMap)7 MediaType (okhttp3.MediaType)7 Request (okhttp3.Request)7 Response (okhttp3.Response)7 ResponseBody (okhttp3.ResponseBody)7 Buffer (okio.Buffer)7 BufferedSource (okio.BufferedSource)7 InputStreamReader (java.io.InputStreamReader)6 OutputStream (java.io.OutputStream)6