Search in sources :

Example 66 with Charset

use of java.nio.charset.Charset in project intellij-community by JetBrains.

the class EncodingPanel method update.

private void update() {
    if (update.isDisposed())
        return;
    update.cancelAllRequests();
    update.addRequest(() -> {
        if (isDisposed())
            return;
        VirtualFile file = getSelectedFile();
        actionEnabled = false;
        String charsetName;
        String toolTipText;
        if (file == null) {
            toolTipText = "";
            charsetName = "n/a";
        } else {
            Pair<Charset, String> check = EncodingUtil.checkSomeActionEnabled(file);
            String failReason = check == null ? null : check.second;
            actionEnabled = failReason == null;
            Charset charset = ObjectUtils.notNull(check == null ? null : check.first, file.getCharset());
            charsetName = ObjectUtils.notNull(charset.displayName(), "n/a");
            if (failReason == null) {
                toolTipText = "File Encoding: " + charsetName;
                myComponent.setForeground(UIUtil.getActiveTextColor());
                myComponent.setTextAlignment(Component.LEFT_ALIGNMENT);
            } else {
                toolTipText = "File encoding is disabled because\n" + failReason;
                myComponent.setForeground(UIUtil.getInactiveTextColor());
                myComponent.setTextAlignment(Component.CENTER_ALIGNMENT);
            }
        }
        myComponent.setToolTipText(toolTipText);
        myComponent.setText(charsetName);
        if (myStatusBar != null) {
            myStatusBar.updateWidget(ID());
        }
    }, 200, ModalityState.any());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Charset(java.nio.charset.Charset)

Example 67 with Charset

use of java.nio.charset.Charset in project intellij-community by JetBrains.

the class FileEncodingTest method testNewFileCreatedInProjectEncodingEvenIfItSetToDefault.

public void testNewFileCreatedInProjectEncodingEvenIfItSetToDefault() throws Exception {
    Charset defaultCharset = Charset.defaultCharset();
    String oldIDE = EncodingManager.getInstance().getDefaultCharsetName();
    EncodingManager.getInstance().setDefaultCharsetName(defaultCharset.name().equals("UTF-8") ? "windows-1251" : "UTF-8");
    String oldProject = EncodingProjectManager.getInstance(getProject()).getDefaultCharsetName();
    // default charset
    EncodingProjectManager.getInstance(getProject()).setDefaultCharsetName("");
    try {
        PsiFile psiFile = createFile("x.txt", "xx");
        VirtualFile file = psiFile.getVirtualFile();
        assertEquals(EncodingManager.getInstance().getDefaultCharset(), file.getCharset());
    } finally {
        EncodingManager.getInstance().setDefaultCharsetName(oldIDE);
        EncodingProjectManager.getInstance(getProject()).setDefaultCharsetName(oldProject);
    }
}
Also used : Charset(java.nio.charset.Charset) PsiFile(com.intellij.psi.PsiFile)

Example 68 with Charset

use of java.nio.charset.Charset in project intellij-community by JetBrains.

the class FileEncodingTest method testTheDefaultProjectEncodingIfNotSpecifiedShouldBeIDEEncoding.

public void testTheDefaultProjectEncodingIfNotSpecifiedShouldBeIDEEncoding() throws Exception {
    String differentFromDefault = Charset.defaultCharset().name().equals("UTF-8") ? "windows-1251" : "UTF-8";
    String oldIDE = EncodingManager.getInstance().getDefaultCharsetName();
    try {
        EncodingManager.getInstance().setDefaultCharsetName(differentFromDefault);
        File temp = createTempDirectory();
        VirtualFile tempDir = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(temp);
        final Project newProject = ProjectManagerEx.getInstanceEx().newProject("new", tempDir.getPath(), false, false);
        Disposer.register(getTestRootDisposable(), () -> ApplicationManager.getApplication().runWriteAction(() -> Disposer.dispose(newProject)));
        PlatformTestUtil.saveProject(newProject);
        Charset newProjectEncoding = EncodingProjectManager.getInstance(newProject).getDefaultCharset();
        assertEquals(differentFromDefault, newProjectEncoding.name());
        PsiFile psiFile = createFile("x.txt", "xx");
        VirtualFile file = psiFile.getVirtualFile();
        assertEquals(differentFromDefault, file.getCharset().name());
    } finally {
        EncodingManager.getInstance().setDefaultCharsetName(oldIDE);
    }
}
Also used : Project(com.intellij.openapi.project.Project) Charset(java.nio.charset.Charset) PsiFile(com.intellij.psi.PsiFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File)

Example 69 with Charset

use of java.nio.charset.Charset in project intellij-community by JetBrains.

the class PlatformTestUtil method withEncoding.

public static void withEncoding(@NotNull String encoding, @NotNull ThrowableRunnable r) {
    try {
        Charset oldCharset = Charset.defaultCharset();
        try {
            patchSystemFileEncoding(encoding);
            r.run();
        } finally {
            patchSystemFileEncoding(oldCharset.name());
        }
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
}
Also used : Charset(java.nio.charset.Charset)

Example 70 with Charset

use of java.nio.charset.Charset in project intellij-community by JetBrains.

the class ContentRevisionCache method bytesToString.

private static String bytesToString(FilePath path, @NotNull byte[] bytes) {
    Charset charset = null;
    if (path.getVirtualFile() != null) {
        charset = path.getVirtualFile().getCharset();
    }
    if (charset != null) {
        int bomLength = CharsetToolkit.getBOMLength(bytes, charset);
        final CharBuffer charBuffer = charset.decode(ByteBuffer.wrap(bytes, bomLength, bytes.length - bomLength));
        return charBuffer.toString();
    }
    return CharsetToolkit.bytesToString(bytes, EncodingRegistry.getInstance().getDefaultCharset());
}
Also used : CharBuffer(java.nio.CharBuffer) Charset(java.nio.charset.Charset)

Aggregations

Charset (java.nio.charset.Charset)1427 IOException (java.io.IOException)268 Test (org.junit.Test)186 InputStream (java.io.InputStream)115 ByteBuffer (java.nio.ByteBuffer)111 File (java.io.File)106 ArrayList (java.util.ArrayList)106 InputStreamReader (java.io.InputStreamReader)102 HashMap (java.util.HashMap)76 CharBuffer (java.nio.CharBuffer)66 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)58 CharsetDecoder (java.nio.charset.CharsetDecoder)57 List (java.util.List)57 Map (java.util.Map)57 OutputStreamWriter (java.io.OutputStreamWriter)56 ByteArrayInputStream (java.io.ByteArrayInputStream)54 CharsetEncoder (java.nio.charset.CharsetEncoder)50 Path (java.nio.file.Path)50 FileInputStream (java.io.FileInputStream)49 BufferedReader (java.io.BufferedReader)48