Java Code Examples for com.intellij.openapi.vfs.VirtualFile#getCharset()
The following examples show how to use
com.intellij.openapi.vfs.VirtualFile#getCharset() .
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 File: OpenSampleAction.java From mule-intellij-plugins with Apache License 2.0 | 6 votes |
@Override public void actionPerformed(AnActionEvent anActionEvent) { logger.debug("Loading sample!"); final Project project = anActionEvent.getProject(); final PsiFile psiFile = anActionEvent.getData(CommonDataKeys.PSI_FILE); VirtualFile sample = FileChooser.chooseFile(FileChooserDescriptorFactory.createSingleFileNoJarsDescriptor(), project, null); if (sample == null) return; try { final String text = new String(sample.contentsToByteArray(), sample.getCharset()); new WriteCommandAction.Simple(project, psiFile) { @Override protected void run() throws Throwable { document.setText(text); } }.execute(); } catch (Exception e) { logger.error(e); } }
Example 2
Source File: EncodingUtil.java From consulo with Apache License 2.0 | 6 votes |
@Nullable static FailReason checkCanReload(@Nonnull VirtualFile virtualFile, @Nullable Ref<? super Charset> current) { if (virtualFile.isDirectory()) { return FailReason.IS_DIRECTORY; } FileDocumentManager documentManager = FileDocumentManager.getInstance(); Document document = documentManager.getDocument(virtualFile); if (document == null) return FailReason.IS_BINARY; Charset charsetFromContent = ((EncodingManagerImpl)EncodingManager.getInstance()).computeCharsetFromContent(virtualFile); Charset existing = virtualFile.getCharset(); LoadTextUtil.AutoDetectionReason autoDetectedFrom = LoadTextUtil.getCharsetAutoDetectionReason(virtualFile); FailReason result; if (autoDetectedFrom != null) { // no point changing encoding if it was auto-detected result = autoDetectedFrom == LoadTextUtil.AutoDetectionReason.FROM_BOM ? FailReason.BY_BOM : FailReason.BY_BYTES; } else if (charsetFromContent != null) { result = FailReason.BY_FILE; existing = charsetFromContent; } else { result = fileTypeDescriptionError(virtualFile); } if (current != null) current.set(existing); return result; }
Example 3
Source File: FilePathImpl.java From consulo with Apache License 2.0 | 6 votes |
@Override public Charset getCharset(Project project) { // try to find existing virtual file VirtualFile existing = myVirtualFile != null && myVirtualFile.isValid() ? myVirtualFile : null; if (existing == null) { LocalFileSystem lfs = LocalFileSystem.getInstance(); for (File f = myFile; f != null; f = f.getParentFile()) { existing = lfs.findFileByIoFile(f); if (existing != null && existing.isValid()) { break; } } } if (existing != null) { Charset rc = existing.getCharset(); if (rc != null) { return rc; } } EncodingManager e = project != null ? EncodingProjectManager.getInstance(project) : null; if (e == null) { e = EncodingManager.getInstance(); } return e.getDefaultCharset(); }
Example 4
Source File: FileConfigPart.java From p4ic4idea with Apache License 2.0 | 5 votes |
private static Properties loadProps(@Nullable VirtualFile filePath) throws IOException { Properties props = new Properties(); if (filePath != null) { try (InputStreamReader reader = new InputStreamReader(filePath.getInputStream(), filePath.getCharset())) { // The Perforce config file is NOT the same as a Java // config file. Java config files will read the "\" as // an escape character, whereas the Perforce config file // will keep it. try (BufferedReader inp = new BufferedReader(reader)) { String line; while ((line = inp.readLine()) != null) { int pos = line.indexOf('='); if (pos > 0) { final String key = line.substring(0, pos).trim(); final String value = line.substring(pos + 1).trim(); // NOTE: an empty value is a set value! if (key.length() > 0) { props.setProperty(key, value); } } } } } } LOG.debug("Loaded property file " + filePath + " keys " + props.keySet()); return props; }
Example 5
Source File: LoadTextUtil.java From consulo with Apache License 2.0 | 5 votes |
@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 6
Source File: LoadTextUtil.java From consulo with Apache License 2.0 | 5 votes |
/** * 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); }
Example 7
Source File: AbstractFileViewProvider.java From consulo with Apache License 2.0 | 5 votes |
@SuppressWarnings("MethodDoesntCallSuperMethod") @Override public FileViewProvider clone() { VirtualFile origFile = getVirtualFile(); LightVirtualFile copy = new LightVirtualFile(origFile.getName(), origFile.getFileType(), getContents(), origFile.getCharset(), getModificationStamp()); origFile.copyCopyableDataTo(copy); copy.setOriginalFile(origFile); copy.putUserData(UndoConstants.DONT_RECORD_UNDO, Boolean.TRUE); copy.setCharset(origFile.getCharset()); return createCopy(copy); }
Example 8
Source File: LocalFilePath.java From consulo with Apache License 2.0 | 5 votes |
@Override @Nonnull public Charset getCharset(@Nullable Project project) { VirtualFile file = getVirtualFile(); String path = myPath; while ((file == null || !file.isValid()) && !path.isEmpty()) { path = PathUtil.getParentPath(path); file = LocalFileSystem.getInstance().findFileByPath(path); } if (file != null) { return file.getCharset(); } EncodingManager e = project == null ? EncodingManager.getInstance() : EncodingProjectManager.getInstance(project); return e.getDefaultCharset(); }
Example 9
Source File: VcsHistoryUtil.java From consulo with Apache License 2.0 | 5 votes |
public static String loadRevisionContentGuessEncoding(@Nonnull final VcsFileRevision revision, @Nullable final VirtualFile file, @javax.annotation.Nullable final Project project) throws VcsException, IOException { final byte[] bytes = loadRevisionContent(revision); if (file != null) { return new String(bytes, file.getCharset()); } EncodingManager e = project != null ? EncodingProjectManager.getInstance(project) : null; if (e == null) { e = EncodingManager.getInstance(); } return CharsetToolkit.bytesToString(bytes, e.getDefaultCharset()); }
Example 10
Source File: VcsSelectionHistoryDialog.java From consulo with Apache License 2.0 | 5 votes |
public BlockLoader(@Nonnull List<VcsFileRevision> revisions, @Nonnull VirtualFile file, @Nonnull Document document, int selectionStart, int selectionEnd) { myRevisions = revisions; myCharset = file.getCharset(); String[] lastContent = Block.tokenize(document.getText()); myBlocks.add(new Block(lastContent, selectionStart, selectionEnd + 1)); }
Example 11
Source File: IgnoreFileSet.java From p4ic4idea with Apache License 2.0 | 4 votes |
static IgnoreFileSet create(@NotNull VirtualFile ignoreFile) throws IOException { try (InputStreamReader reader = new InputStreamReader(ignoreFile.getInputStream(), ignoreFile.getCharset())) { return new IgnoreFileSet(ignoreFile, IgnoreFilePattern.parseFile(reader)); } }
Example 12
Source File: FileDocumentContentImpl.java From consulo with Apache License 2.0 | 4 votes |
public FileDocumentContentImpl(@Nullable Project project, @Nonnull Document document, @Nonnull VirtualFile file) { super(project, document, file.getFileType(), file, getSeparator(file), file.getCharset(), file.getBOM() != null); myFile = file; }