Search in sources :

Example 71 with Charset

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

the class CreatePatchConfigurationPanel method initEncodingCombo.

private void initEncodingCombo() {
    final DefaultComboBoxModel<Charset> encodingsModel = new DefaultComboBoxModel<>(CharsetToolkit.getAvailableCharsets());
    myEncoding.setModel(encodingsModel);
    Charset projectCharset = EncodingProjectManager.getInstance(myProject).getDefaultCharset();
    myEncoding.setSelectedItem(projectCharset);
}
Also used : Charset(java.nio.charset.Charset)

Example 72 with Charset

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

the class EncodingManager method applySettings.

private void applySettings(VirtualFile file) {
    if (file == null)
        return;
    if (!Utils.isEnabled(CodeStyleSettingsManager.getInstance(myProject).getCurrentSettings()))
        return;
    // Prevent "setEncoding" calling "saveAll" from causing an endless loop
    isApplyingSettings = true;
    try {
        final String filePath = Utils.getFilePath(myProject, file);
        final List<OutPair> outPairs = SettingsProviderComponent.getInstance().getOutPairs(myProject, filePath);
        final EncodingProjectManager encodingProjectManager = EncodingProjectManager.getInstance(myProject);
        final String charset = Utils.configValueForKey(outPairs, charsetKey);
        if (!charset.isEmpty()) {
            final Charset newCharset = encodingMap.get(charset);
            if (newCharset != null) {
                if (Comparing.equal(newCharset, file.getCharset()))
                    return;
                encodingProjectManager.setEncoding(file, newCharset);
            } else {
                Utils.invalidConfigMessage(myProject, charset, charsetKey, filePath);
            }
        }
    } finally {
        isApplyingSettings = false;
    }
}
Also used : OutPair(org.editorconfig.core.EditorConfig.OutPair) EncodingProjectManager(com.intellij.openapi.vfs.encoding.EncodingProjectManager) Charset(java.nio.charset.Charset)

Example 73 with Charset

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

the class VirtualFile method getCharset.

/**
   * @return Retrieve the charset file has been loaded with (if loaded) and would be saved with (if would).
   */
@NotNull
public Charset getCharset() {
    Charset charset = getStoredCharset();
    if (charset == null) {
        charset = EncodingRegistry.getInstance().getDefaultCharset();
        setCharset(charset);
    }
    return charset;
}
Also used : Charset(java.nio.charset.Charset) NotNull(org.jetbrains.annotations.NotNull)

Example 74 with Charset

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

the class LightVirtualFile method contentsToByteArray.

@Override
@NotNull
public byte[] contentsToByteArray() throws IOException {
    final Charset charset = getCharset();
    final String s = getContent().toString();
    return s.getBytes(charset.name());
}
Also used : Charset(java.nio.charset.Charset) NotNull(org.jetbrains.annotations.NotNull)

Example 75 with Charset

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

the class ExternalDiffToolUtil method createTempFile.

@NotNull
private static File createTempFile(@NotNull final DocumentContent content, @NotNull FileNameInfo fileName) throws IOException {
    FileDocumentManager.getInstance().saveDocument(content.getDocument());
    LineSeparator separator = content.getLineSeparator();
    if (separator == null)
        separator = LineSeparator.getSystemLineSeparator();
    Charset charset = content.getCharset();
    if (charset == null)
        charset = Charset.defaultCharset();
    Boolean hasBom = content.hasBom();
    if (hasBom == null)
        hasBom = CharsetToolkit.getMandatoryBom(charset) != null;
    String contentData = ReadAction.compute(() -> {
        return content.getDocument().getText();
    });
    if (separator != LineSeparator.LF) {
        contentData = StringUtil.convertLineSeparators(contentData, separator.getSeparatorString());
    }
    byte[] bytes = contentData.getBytes(charset);
    byte[] bom = hasBom ? CharsetToolkit.getPossibleBom(charset) : null;
    if (bom != null) {
        bytes = ArrayUtil.mergeArrays(bom, bytes);
    }
    return createFile(bytes, fileName);
}
Also used : Charset(java.nio.charset.Charset) LineSeparator(com.intellij.util.LineSeparator) NotNull(org.jetbrains.annotations.NotNull)

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