Search in sources :

Example 76 with Charset

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

the class DiffContentFactoryImpl method guessCharsetFromContent.

@Nullable
private static Charset guessCharsetFromContent(@NotNull byte[] content) {
    // can't use CharsetToolkit.guessEncoding here because of false-positive INVALID_UTF8
    CharsetToolkit toolkit = new CharsetToolkit(content);
    Charset fromBOM = toolkit.guessFromBOM();
    if (fromBOM != null)
        return fromBOM;
    CharsetToolkit.GuessedEncoding guessedEncoding = toolkit.guessFromContent(content.length);
    switch(guessedEncoding) {
        case SEVEN_BIT:
            return Charset.forName("US-ASCII");
        case VALID_UTF8:
            return CharsetToolkit.UTF8_CHARSET;
        default:
            return null;
    }
}
Also used : CharsetToolkit(com.intellij.openapi.vfs.CharsetToolkit) Charset(java.nio.charset.Charset) Nullable(org.jetbrains.annotations.Nullable)

Example 77 with Charset

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

the class ChangeFileEncodingAction method chosen.

// returns true if charset was changed, false if failed
protected boolean chosen(final Document document, final Editor editor, @Nullable final VirtualFile virtualFile, byte[] bytes, @NotNull final Charset charset) {
    if (virtualFile == null)
        return false;
    String text = document.getText();
    EncodingUtil.Magic8 isSafeToConvert = EncodingUtil.isSafeToConvertTo(virtualFile, text, bytes, charset);
    EncodingUtil.Magic8 isSafeToReload = EncodingUtil.isSafeToReloadIn(virtualFile, text, bytes, charset);
    final Project project = ProjectLocator.getInstance().guessProjectForFile(virtualFile);
    final Charset oldCharset = virtualFile.getCharset();
    final Runnable undo;
    final Runnable redo;
    if (isSafeToConvert == EncodingUtil.Magic8.ABSOLUTELY && isSafeToReload == EncodingUtil.Magic8.ABSOLUTELY) {
        //change and forget
        undo = () -> EncodingManager.getInstance().setEncoding(virtualFile, oldCharset);
        redo = () -> EncodingManager.getInstance().setEncoding(virtualFile, charset);
    } else {
        IncompatibleEncodingDialog dialog = new IncompatibleEncodingDialog(virtualFile, charset, isSafeToReload, isSafeToConvert);
        dialog.show();
        if (dialog.getExitCode() == IncompatibleEncodingDialog.RELOAD_EXIT_CODE) {
            undo = () -> EncodingUtil.reloadIn(virtualFile, oldCharset);
            redo = () -> EncodingUtil.reloadIn(virtualFile, charset);
        } else if (dialog.getExitCode() == IncompatibleEncodingDialog.CONVERT_EXIT_CODE) {
            undo = () -> EncodingUtil.saveIn(document, editor, virtualFile, oldCharset);
            redo = () -> EncodingUtil.saveIn(document, editor, virtualFile, charset);
        } else {
            return false;
        }
    }
    final UndoableAction action = new GlobalUndoableAction(virtualFile) {

        @Override
        public void undo() {
            // invoke later because changing document inside undo/redo is not allowed
            Application application = ApplicationManager.getApplication();
            application.invokeLater(undo, ModalityState.NON_MODAL, (project == null ? application : project).getDisposed());
        }

        @Override
        public void redo() {
            // invoke later because changing document inside undo/redo is not allowed
            Application application = ApplicationManager.getApplication();
            application.invokeLater(redo, ModalityState.NON_MODAL, (project == null ? application : project).getDisposed());
        }
    };
    redo.run();
    CommandProcessor.getInstance().executeCommand(project, () -> {
        UndoManager undoManager = project == null ? UndoManager.getGlobalInstance() : UndoManager.getInstance(project);
        undoManager.undoableActionPerformed(action);
    }, "Change encoding for '" + virtualFile.getName() + "'", null, UndoConfirmationPolicy.REQUEST_CONFIRMATION);
    return true;
}
Also used : Project(com.intellij.openapi.project.Project) UndoManager(com.intellij.openapi.command.undo.UndoManager) GlobalUndoableAction(com.intellij.openapi.command.undo.GlobalUndoableAction) Charset(java.nio.charset.Charset) GlobalUndoableAction(com.intellij.openapi.command.undo.GlobalUndoableAction) UndoableAction(com.intellij.openapi.command.undo.UndoableAction) Application(com.intellij.openapi.application.Application)

Example 78 with Charset

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

the class ChooseFileEncodingAction method fillCharsetActions.

private void fillCharsetActions(@NotNull DefaultActionGroup group, @Nullable final VirtualFile virtualFile, @NotNull List<Charset> charsets, @NotNull final Function<Charset, String> charsetFilter) {
    for (final Charset charset : charsets) {
        AnAction action = new DumbAwareAction(charset.displayName(), null, EmptyIcon.ICON_16) {

            @Override
            public void actionPerformed(AnActionEvent e) {
                chosen(virtualFile, charset);
            }

            @Override
            public void update(AnActionEvent e) {
                super.update(e);
                String description = charsetFilter.fun(charset);
                e.getPresentation().setIcon(description == null ? AllIcons.General.Warning : null);
                e.getPresentation().setDescription(description);
            }
        };
        group.add(action);
    }
}
Also used : Charset(java.nio.charset.Charset) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) AnAction(com.intellij.openapi.actionSystem.AnAction) DumbAwareAction(com.intellij.openapi.project.DumbAwareAction)

Example 79 with Charset

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

the class EncodingManagerImpl method handleDocument.

private void handleDocument(@NotNull final Document document) {
    if (document.getUserData(DETECTING_ENCODING_KEY) == null)
        return;
    try {
        VirtualFile virtualFile = FileDocumentManager.getInstance().getFile(document);
        if (virtualFile == null)
            return;
        Project project = guessProject(virtualFile);
        if (project != null && project.isDisposed())
            return;
        Charset charset = LoadTextUtil.charsetFromContentOrNull(project, virtualFile, document.getImmutableCharSequence());
        Charset oldCached = getCachedCharsetFromContent(document);
        if (!Comparing.equal(charset, oldCached)) {
            setCachedCharsetFromContent(charset, oldCached, document);
        }
    } finally {
        document.putUserData(DETECTING_ENCODING_KEY, null);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) Charset(java.nio.charset.Charset)

Example 80 with Charset

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

the class EncodingManagerImpl method computeCharsetFromContent.

@Nullable("returns null if charset set cannot be determined from content")
Charset computeCharsetFromContent(@NotNull final VirtualFile virtualFile) {
    final Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
    if (document == null) {
        return null;
    }
    Charset cached = EncodingManager.getInstance().getCachedCharsetFromContent(document);
    if (cached != null) {
        return cached;
    }
    final Project project = ProjectLocator.getInstance().guessProjectForFile(virtualFile);
    return ReadAction.compute(() -> {
        Charset charsetFromContent = LoadTextUtil.charsetFromContentOrNull(project, virtualFile, document.getImmutableCharSequence());
        if (charsetFromContent != null) {
            setCachedCharsetFromContent(charsetFromContent, null, document);
        }
        return charsetFromContent;
    });
}
Also used : Project(com.intellij.openapi.project.Project) Charset(java.nio.charset.Charset) Document(com.intellij.openapi.editor.Document) Nullable(org.jetbrains.annotations.Nullable)

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