Java Code Examples for com.intellij.openapi.vfs.VirtualFile#setCharset()

The following examples show how to use com.intellij.openapi.vfs.VirtualFile#setCharset() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: consulo   File: ApplyTextFilePatch.java    License: Apache License 2.0 6 votes vote down vote up
protected void applyCreate(Project project, final VirtualFile newFile, CommitContext commitContext) throws IOException {
  final Document document = FileDocumentManager.getInstance().getDocument(newFile);
  if (document == null) {
    throw new IOException("Failed to set contents for new file " + newFile.getPath());
  }
  final String charsetName = CharsetEP.getCharset(newFile.getPath(), commitContext);
  if (charsetName != null) {
    try {
      final Charset charset = Charset.forName(charsetName);
      newFile.setCharset(charset);
    } catch (IllegalArgumentException e) {
      //
    }
  }
  document.setText(myPatch.getNewFileText());
  FileDocumentManager.getInstance().saveDocument(document);
}
 
Example 2
Source Project: saros   File: IntellijFile.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void setCharset(@Nullable String charset) throws IOException {
  if (charset == null) {
    return;
  }

  VirtualFile virtualFile = getVirtualFile();

  if (!existsInternal(virtualFile)) {
    throw new FileNotFoundException(
        "Could not set charset for " + this + " as it does not exist or is not valid");
  }

  virtualFile.setCharset(Charset.forName(charset));
}
 
Example 3
Source Project: consulo   File: LoadTextUtil.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
private static DetectResult detectInternalCharsetAndSetBOM(@Nonnull VirtualFile file, @Nonnull byte[] content, int length, boolean saveBOM, @Nonnull FileType fileType) {
  DetectResult info = detectHardCharset(file, content, length, fileType);

  Charset charset;
  if (info.hardCodedCharset == null) {
    charset = file.isCharsetSet() ? file.getCharset() : getDefaultCharsetFromEncodingManager(file);
  }
  else {
    charset = info.hardCodedCharset;
  }

  byte[] bom = info.BOM;
  if (saveBOM && bom != null && bom.length != 0) {
    file.setBOM(bom);
    setCharsetAutoDetectionReason(file, AutoDetectionReason.FROM_BOM);
  }

  file.setCharset(charset);

  Charset result = charset;
  // optimisation
  if (info.guessed == CharsetToolkit.GuessedEncoding.SEVEN_BIT) {
    if (charset == StandardCharsets.UTF_8) {
      result = INTERNAL_SEVEN_BIT_UTF8;
    }
    else if (charset == CharsetToolkit.ISO_8859_1_CHARSET) {
      result = INTERNAL_SEVEN_BIT_ISO_8859_1;
    }
    else if (charset == CharsetToolkit.WIN_1251_CHARSET) {
      result = INTERNAL_SEVEN_BIT_WIN_1251;
    }
  }

  return new DetectResult(result, info.guessed, bom);
}
 
Example 4
Source Project: consulo   File: LoadTextUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Overwrites file with text and sets modification stamp and time stamp to the specified values.
 * <p/>
 * Normally you should not use this method.
 *
 * @param requestor            any object to control who called this method. Note that
 *                             it is considered to be an external change if {@code requestor} is {@code null}.
 *                             See {@link VirtualFileEvent#getRequestor}
 * @param newModificationStamp new modification stamp or -1 if no special value should be set @return {@code Writer}
 * @throws IOException if an I/O error occurs
 * @see VirtualFile#getModificationStamp()
 */
public static void write(@Nullable Project project, @Nonnull VirtualFile virtualFile, @Nonnull Object requestor, @Nonnull String text, long newModificationStamp) throws IOException {
  Charset existing = virtualFile.getCharset();
  Pair.NonNull<Charset, byte[]> chosen = charsetForWriting(project, virtualFile, text, existing);
  Charset charset = chosen.first;
  byte[] buffer = chosen.second;
  if (!charset.equals(existing)) {
    virtualFile.setCharset(charset);
  }
  setDetectedFromBytesFlagBack(virtualFile, buffer);

  virtualFile.setBinaryContent(buffer, newModificationStamp, -1, requestor);
}