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());
}
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);
}
}
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);
}
}
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);
}
}
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());
}
Aggregations