com.intellij.openapi.vfs.encoding.EncodingUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of platform-impl Show documentation
Show all versions of platform-impl Show documentation
A packaging of the IntelliJ Community Edition platform-impl library.
This is release number 1 of trunk branch 142.
The newest version!
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.openapi.vfs.encoding;
import com.intellij.AppTopics;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileDocumentManagerAdapter;
import com.intellij.openapi.fileEditor.impl.LoadTextUtil;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypes;
import com.intellij.openapi.fileTypes.StdFileTypes;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectLocator;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.ThrowableComputable;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.*;
import com.intellij.refactoring.util.CommonRefactoringUtil;
import com.intellij.util.ArrayUtil;
import com.intellij.util.messages.MessageBusConnection;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Arrays;
public class EncodingUtil {
enum Magic8 {
ABSOLUTELY,
WELL_IF_YOU_INSIST,
NO_WAY
}
// check if file can be loaded in the encoding correctly:
// returns true if bytes on disk, converted to text with the charset, converted back to bytes matched
static Magic8 isSafeToReloadIn(@NotNull VirtualFile virtualFile, @NotNull String text, @NotNull byte[] bytes, @NotNull Charset charset) {
// file has BOM but the charset hasn't
byte[] bom = virtualFile.getBOM();
if (bom != null && !CharsetToolkit.canHaveBom(charset, bom)) return Magic8.NO_WAY;
// the charset has mandatory BOM (e.g. UTF-xx) but the file hasn't or has wrong
byte[] mandatoryBom = CharsetToolkit.getMandatoryBom(charset);
if (mandatoryBom != null && !ArrayUtil.startsWith(bytes, mandatoryBom)) return Magic8.NO_WAY;
String loaded = LoadTextUtil.getTextByBinaryPresentation(bytes, charset).toString();
String separator = FileDocumentManager.getInstance().getLineSeparator(virtualFile, null);
String toSave = StringUtil.convertLineSeparators(loaded, separator);
String failReason = LoadTextUtil.wasCharsetDetectedFromBytes(virtualFile);
if (failReason != null && CharsetToolkit.UTF8_CHARSET.equals(virtualFile.getCharset()) && !CharsetToolkit.UTF8_CHARSET.equals(charset)) {
return Magic8.NO_WAY; // can't reload utf8-autodetected file in another charset
}
byte[] bytesToSave;
try {
bytesToSave = toSave.getBytes(charset);
}
catch (UnsupportedOperationException e) {
return Magic8.NO_WAY;
}
if (bom != null && !ArrayUtil.startsWith(bytesToSave, bom)) {
bytesToSave = ArrayUtil.mergeArrays(bom, bytesToSave); // for 2-byte encodings String.getBytes(Charset) adds BOM automatically
}
return !Arrays.equals(bytesToSave, bytes) ? Magic8.NO_WAY : loaded.equals(text) ? Magic8.ABSOLUTELY : Magic8.WELL_IF_YOU_INSIST;
}
static Magic8 isSafeToConvertTo(@NotNull VirtualFile virtualFile, @NotNull String text, @NotNull byte[] bytesOnDisk, @NotNull Charset charset) {
try {
String lineSeparator = FileDocumentManager.getInstance().getLineSeparator(virtualFile, null);
String textToSave = lineSeparator.equals("\n") ? text : StringUtil.convertLineSeparators(text, lineSeparator);
Pair chosen = LoadTextUtil.chooseMostlyHarmlessCharset(virtualFile.getCharset(), charset, textToSave);
byte[] saved = chosen.second;
CharSequence textLoadedBack = LoadTextUtil.getTextByBinaryPresentation(saved, charset);
return !text.equals(textLoadedBack.toString()) ? Magic8.NO_WAY : Arrays.equals(saved, bytesOnDisk) ? Magic8.ABSOLUTELY : Magic8.WELL_IF_YOU_INSIST;
}
catch (UnsupportedOperationException e) { // unsupported encoding
return Magic8.NO_WAY;
}
}
public static void saveIn(@NotNull final Document document, final Editor editor, @NotNull final VirtualFile virtualFile, @NotNull final Charset charset) {
FileDocumentManager documentManager = FileDocumentManager.getInstance();
documentManager.saveDocument(document);
final Project project = ProjectLocator.getInstance().guessProjectForFile(virtualFile);
boolean writable = project == null ? virtualFile.isWritable() : ReadonlyStatusHandler.ensureFilesWritable(project, virtualFile);
if (!writable) {
CommonRefactoringUtil.showErrorHint(project, editor, "Cannot save the file " + virtualFile.getPresentableUrl(), "Unable to Save", null);
return;
}
// first, save the file in the new charset and then mark the file as having the correct encoding
try {
ApplicationManager.getApplication().runWriteAction(new ThrowableComputable
© 2015 - 2025 Weber Informatics LLC | Privacy Policy