com.intellij.openapi.vfs.CharsetToolkit Java Examples
The following examples show how to use
com.intellij.openapi.vfs.CharsetToolkit.
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: FileDocumentManagerImplTest.java From consulo with Apache License 2.0 | 6 votes |
public void testContentChanged_DoNotReloadChangedDocument() throws Exception { final VirtualFile file = createFile(); final Document document = myDocumentManager.getDocument(file); assertNotNull(file.toString(), document); WriteCommandAction.runWriteCommandAction(myProject, new Runnable() { @Override public void run() { document.insertString(0, "old "); } }); myReloadFromDisk = Boolean.FALSE; long oldDocumentStamp = document.getModificationStamp(); file.setBinaryContent("xxx".getBytes(CharsetToolkit.UTF8_CHARSET)); assertEquals("old test", document.getText()); assertEquals(oldDocumentStamp, document.getModificationStamp()); }
Example #2
Source File: LightPlatformCodeInsightTestCase.java From consulo with Apache License 2.0 | 6 votes |
@Nonnull private static Document setupFileEditorAndDocument(@Nonnull String fileName, @Nonnull String fileText) throws IOException { EncodingProjectManager.getInstance(getProject()).setEncoding(null, CharsetToolkit.UTF8_CHARSET); EncodingProjectManager.getInstance(ProjectManager.getInstance().getDefaultProject()).setEncoding(null, CharsetToolkit.UTF8_CHARSET); PostprocessReformattingAspect.getInstance(ourProject).doPostponedFormatting(); deleteVFile(); myVFile = getSourceRoot().createChildData(null, fileName); VfsUtil.saveText(myVFile, fileText); final FileDocumentManager manager = FileDocumentManager.getInstance(); final Document document = manager.getDocument(myVFile); assertNotNull("Can't create document for '" + fileName + "'", document); manager.reloadFromDisk(document); document.insertString(0, " "); document.deleteString(0, 1); myFile = getPsiManager().findFile(myVFile); assertNotNull("Can't create PsiFile for '" + fileName + "'. Unknown file type most probably.", myFile); assertTrue(myFile.isPhysical()); myEditor = createEditor(myVFile); myVFile.setCharset(CharsetToolkit.UTF8_CHARSET); PsiDocumentManager.getInstance(getProject()).commitAllDocuments(); return document; }
Example #3
Source File: CachesHolder.java From consulo with Apache License 2.0 | 6 votes |
@SuppressWarnings({"HardCodedStringLiteral"}) private File getCachePath(final RepositoryLocation location) { File file = getCacheBasePath(); file.mkdirs(); String s = location.getKey(); try { final byte[] bytes = MessageDigest.getInstance("MD5").digest(CharsetToolkit.getUtf8Bytes(s)); StringBuilder result = new StringBuilder(); for (byte aByte : bytes) { result.append(String.format("%02x", aByte)); } return new File(file, result.toString()); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } }
Example #4
Source File: AbstractVcsTestCase.java From consulo with Apache License 2.0 | 6 votes |
public VirtualFile createFileInCommand(final VirtualFile parent, final String name, @Nullable final String content) { final Ref<VirtualFile> result = new Ref<VirtualFile>(); new WriteCommandAction.Simple(myProject) { @Override protected void run() throws Throwable { try { VirtualFile file = parent.createChildData(this, name); if (content != null) { file.setBinaryContent(CharsetToolkit.getUtf8Bytes(content)); } result.set(file); } catch (IOException e) { throw new RuntimeException(e); } } }.execute(); return result.get(); }
Example #5
Source File: FlutterCommand.java From flutter-intellij with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Creates the command line to run. * <p> * If a project is supplied, it will be used to determine the ANDROID_HOME variable for the subprocess. */ @NotNull public GeneralCommandLine createGeneralCommandLine(@Nullable Project project) { final GeneralCommandLine line = new GeneralCommandLine(); line.setCharset(CharsetToolkit.UTF8_CHARSET); line.withEnvironment(FlutterSdkUtil.FLUTTER_HOST_ENV, FlutterSdkUtil.getFlutterHostEnvValue()); final String androidHome = IntelliJAndroidSdk.chooseAndroidHome(project, false); if (androidHome != null) { line.withEnvironment("ANDROID_HOME", androidHome); } line.setExePath(FileUtil.toSystemDependentName(sdk.getHomePath() + "/bin/" + FlutterSdkUtil.flutterScriptName())); if (workDir != null) { line.setWorkDirectory(workDir.getPath()); } if (!isDoctorCommand()) { line.addParameter("--no-color"); } line.addParameters(type.subCommand); line.addParameters(args); return line; }
Example #6
Source File: BinaryContent.java From consulo with Apache License 2.0 | 6 votes |
@Override @SuppressWarnings({"EmptyCatchBlock"}) @Nullable public Document getDocument() { if (myDocument == null) { if (isBinary()) return null; String text = null; try { Charset charset = ObjectUtil .notNull(myCharset, EncodingProjectManager.getInstance(myProject).getDefaultCharset()); text = CharsetToolkit.bytesToString(myBytes, charset); } catch (IllegalCharsetNameException e) { } // Still NULL? only if not supported or an exception was thrown. // Decode a string using the truly default encoding. if (text == null) text = new String(myBytes); text = LineTokenizer.correctLineSeparators(text); myDocument = EditorFactory.getInstance().createDocument(text); myDocument.setReadOnly(true); } return myDocument; }
Example #7
Source File: URLUtil.java From consulo with Apache License 2.0 | 6 votes |
/** * Extracts byte array from given data:URL string. * data:URL will be decoded from base64 if it contains the marker of base64 encoding. * * @param dataUrl data:URL-like string (may be quoted) * @return extracted byte array or {@code null} if it cannot be extracted. */ @Nullable public static byte[] getBytesFromDataUri(@Nonnull String dataUrl) { Matcher matcher = DATA_URI_PATTERN.matcher(StringUtil.unquoteString(dataUrl)); if (matcher.matches()) { try { String content = matcher.group(4); return ";base64".equalsIgnoreCase(matcher.group(3)) ? Base64.decode(content) : content.getBytes(CharsetToolkit.UTF8_CHARSET); } catch (IllegalArgumentException e) { return null; } } return null; }
Example #8
Source File: ExecUtil.java From consulo with Apache License 2.0 | 6 votes |
@Nonnull public static String loadTemplate(@Nonnull ClassLoader loader, @Nonnull String templateName, @Nullable Map<String, String> variables) throws IOException { @SuppressWarnings("IOResourceOpenedButNotSafelyClosed") InputStream stream = loader.getResourceAsStream(templateName); if (stream == null) { throw new IOException("Template '" + templateName + "' not found by " + loader); } String template = FileUtil.loadTextAndClose(new InputStreamReader(stream, CharsetToolkit.UTF8)); if (variables == null || variables.size() == 0) { return template; } StringBuilder buffer = new StringBuilder(template); for (Map.Entry<String, String> var : variables.entrySet()) { String name = var.getKey(); int pos = buffer.indexOf(name); if (pos >= 0) { buffer.replace(pos, pos + name.length(), var.getValue()); } } return buffer.toString(); }
Example #9
Source File: FlutterCommand.java From flutter-intellij with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Creates the command line to run. * <p> * If a project is supplied, it will be used to determine the ANDROID_HOME variable for the subprocess. */ @NotNull public GeneralCommandLine createGeneralCommandLine(@Nullable Project project) { final GeneralCommandLine line = new GeneralCommandLine(); line.setCharset(CharsetToolkit.UTF8_CHARSET); line.withEnvironment(FlutterSdkUtil.FLUTTER_HOST_ENV, FlutterSdkUtil.getFlutterHostEnvValue()); final String androidHome = IntelliJAndroidSdk.chooseAndroidHome(project, false); if (androidHome != null) { line.withEnvironment("ANDROID_HOME", androidHome); } line.setExePath(FileUtil.toSystemDependentName(sdk.getHomePath() + "/bin/" + FlutterSdkUtil.flutterScriptName())); if (workDir != null) { line.setWorkDirectory(workDir.getPath()); } if (!isDoctorCommand()) { line.addParameter("--no-color"); } line.addParameters(type.subCommand); line.addParameters(args); return line; }
Example #10
Source File: FTManager.java From consulo with Apache License 2.0 | 6 votes |
/** Save template to file. If template is new, it is saved to specified directory. Otherwise it is saved to file from which it was read. * If template was not modified, it is not saved. * todo: review saving algorithm */ private static void saveTemplate(File parentDir, FileTemplateBase template, final String lineSeparator) throws IOException { final File templateFile = new File(parentDir, encodeFileName(template.getName(), template.getExtension())); FileOutputStream fileOutputStream; try { fileOutputStream = new FileOutputStream(templateFile); } catch (FileNotFoundException e) { // try to recover from the situation 'file exists, but is a directory' FileUtil.delete(templateFile); fileOutputStream = new FileOutputStream(templateFile); } OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, CharsetToolkit.UTF8_CHARSET); String content = template.getText(); if (!lineSeparator.equals("\n")){ content = StringUtil.convertLineSeparators(content, lineSeparator); } outputStreamWriter.write(content); outputStreamWriter.close(); fileOutputStream.close(); }
Example #11
Source File: DiffContentFactoryImpl.java From consulo with Apache License 2.0 | 6 votes |
@Nonnull private static DocumentContent createFromBytesImpl(@Nullable Project project, @Nonnull byte[] content, @Nonnull FileType fileType, @Nonnull String fileName, @javax.annotation.Nullable VirtualFile highlightFile, @Nonnull Charset charset) { Charset bomCharset = CharsetToolkit.guessFromBOM(content); boolean isBOM = bomCharset != null; if (isBOM) charset = bomCharset; boolean malformedContent = false; String text = CharsetToolkit.tryDecodeString(content, charset); LineSeparator separator = StringUtil.detectSeparators(text); String correctedContent = StringUtil.convertLineSeparators(text); DocumentContent documentContent = createImpl(project, correctedContent, fileType, fileName, highlightFile, charset, isBOM, true, true); if (malformedContent) { String notificationText = "Content was decoded with errors (using " + "'" + charset.name() + "' charset)"; DiffUtil.addNotification(DiffNotifications.createNotification(notificationText, LightColors.RED), documentContent); } return documentContent; }
Example #12
Source File: HttpRequests.java From consulo with Apache License 2.0 | 6 votes |
private static Charset getCharset(Request request) throws IOException { String contentType = request.getConnection().getContentType(); if (!StringUtil.isEmptyOrSpaces(contentType)) { Matcher m = CHARSET_PATTERN.matcher(contentType); if (m.find()) { try { return Charset.forName(StringUtil.unquoteString(m.group(1))); } catch (IllegalArgumentException e) { throw new IOException("unknown charset (" + contentType + ")", e); } } } return CharsetToolkit.UTF8_CHARSET; }
Example #13
Source File: LoadTextUtil.java From consulo with Apache License 2.0 | 5 votes |
/** * Tries to detect text in the {@code bytes} and call the {@code fileTextProcessor} with the text (if detected) or with null if not */ public static String getTextFromBytesOrNull(@Nonnull byte[] bytes, int startOffset, int endOffset) { Charset defaultCharset = EncodingManager.getInstance().getDefaultCharset(); DetectResult info = guessFromBytes(bytes, startOffset, endOffset, defaultCharset); Charset charset; if (info.hardCodedCharset != null) { charset = info.hardCodedCharset; } else { switch (info.guessed) { case SEVEN_BIT: charset = CharsetToolkit.US_ASCII_CHARSET; break; case VALID_UTF8: charset = StandardCharsets.UTF_8; break; case INVALID_UTF8: case BINARY: // the charset was not detected so the file is likely binary return null; default: throw new IllegalStateException(String.valueOf(info.guessed)); } } byte[] bom = info.BOM; ConvertResult result = convertBytes(bytes, Math.min(startOffset + (bom == null ? 0 : bom.length), endOffset), endOffset, charset); return result.text.toString(); }
Example #14
Source File: EnvironmentUtil.java From consulo with Apache License 2.0 | 5 votes |
private static Map<String, String> setCharsetVar(@Nonnull Map<String, String> env) { if (!isCharsetVarDefined(env)) { Locale locale = Locale.getDefault(); Charset charset = CharsetToolkit.getDefaultSystemCharset(); String language = locale.getLanguage(); String country = locale.getCountry(); String value = (language.isEmpty() || country.isEmpty() ? "en_US" : language + '_' + country) + '.' + charset.name(); env.put(LC_CTYPE, value); LOG.info("LC_CTYPE=" + value); } return env; }
Example #15
Source File: LoadTextUtil.java From consulo with Apache License 2.0 | 5 votes |
@Nonnull private static DetectResult guessFromBytes(@Nonnull byte[] content, int startOffset, int endOffset, @Nonnull Charset defaultCharset) { CharsetToolkit toolkit = new CharsetToolkit(content, defaultCharset); toolkit.setEnforce8Bit(true); Charset charset = toolkit.guessFromBOM(); if (charset != null) { byte[] bom = ObjectUtils.notNull(CharsetToolkit.getMandatoryBom(charset), CharsetToolkit.UTF8_BOM); return new DetectResult(charset, null, bom); } CharsetToolkit.GuessedEncoding guessed = toolkit.guessFromContent(startOffset, endOffset); if (guessed == CharsetToolkit.GuessedEncoding.VALID_UTF8) { return new DetectResult(StandardCharsets.UTF_8, CharsetToolkit.GuessedEncoding.VALID_UTF8, null); //UTF detected, ignore all directives } return new DetectResult(null, guessed, null); }
Example #16
Source File: LoadTextUtil.java From consulo with Apache License 2.0 | 5 votes |
@Nonnull public static Pair.NonNull<Charset, byte[]> charsetForWriting(@Nullable Project project, @Nonnull VirtualFile virtualFile, @Nonnull String text, @Nonnull Charset existing) { Charset specified = extractCharsetFromFileContent(project, virtualFile, text); Pair.NonNull<Charset, byte[]> chosen = chooseMostlyHarmlessCharset(existing, specified, text); Charset charset = chosen.first; // in case of "UTF-16", OutputStreamWriter sometimes adds BOM on it's own. // see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6800103 byte[] bom = virtualFile.getBOM(); Charset fromBom = bom == null ? null : CharsetToolkit.guessFromBOM(bom); if (fromBom != null && !fromBom.equals(charset)) { chosen = Pair.createNonNull(fromBom, text.getBytes(fromBom)); } return chosen; }
Example #17
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 #18
Source File: LoadTextUtil.java From consulo with Apache License 2.0 | 5 votes |
@Nonnull private static DetectResult detectHardCharset(@Nonnull VirtualFile virtualFile, @Nonnull byte[] content, int length, @Nonnull FileType fileType) { String charsetName = fileType.getCharset(virtualFile, content); DetectResult guessed = guessFromContent(virtualFile, content, length); Charset hardCodedCharset = charsetName == null ? guessed.hardCodedCharset : CharsetToolkit.forName(charsetName); if (hardCodedCharset == null && guessed.guessed == CharsetToolkit.GuessedEncoding.VALID_UTF8) { return new DetectResult(StandardCharsets.UTF_8, guessed.guessed, guessed.BOM); } return new DetectResult(hardCodedCharset, guessed.guessed, guessed.BOM); }
Example #19
Source File: URLUtil.java From consulo with Apache License 2.0 | 5 votes |
/** * Encodes a URI component by replacing each instance of certain characters by one, two, three, * or four escape sequences representing the UTF-8 encoding of the character. * Behaves similarly to standard JavaScript build-in function <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent">encodeURIComponent</a>. * * @param s a component of a URI * @return a new string representing the provided string encoded as a URI component */ @Nonnull public static String encodeURIComponent(@Nonnull String s) { try { return URLEncoder.encode(s, CharsetToolkit.UTF8).replace("+", "%20").replace("%21", "!").replace("%27", "'").replace("%28", "(").replace("%29", ")").replace("%7E", "~"); } catch (UnsupportedEncodingException e) { return s; } }
Example #20
Source File: UIUtil.java From consulo with Apache License 2.0 | 5 votes |
@Nullable public static StyleSheet loadStyleSheet(@Nullable URL url) { if (url == null) return null; try { StyleSheet styleSheet = new StyleSheet(); styleSheet.loadRules(new InputStreamReader(url.openStream(), CharsetToolkit.UTF8), url); return styleSheet; } catch (IOException e) { getLogger().warn(url + " loading failed", e); return null; } }
Example #21
Source File: ChooseFileEncodingAction.java From consulo with Apache License 2.0 | 5 votes |
@Nonnull protected DefaultActionGroup createCharsetsActionGroup(@Nullable String clearItemText, @Nullable Charset alreadySelected, @Nonnull Function<? super Charset, String> charsetFilter) { DefaultActionGroup group = new DefaultActionGroup(); List<Charset> favorites = new ArrayList<>(EncodingManager.getInstance().getFavorites()); Collections.sort(favorites); Charset current = myVirtualFile == null ? null : myVirtualFile.getCharset(); favorites.remove(current); favorites.remove(alreadySelected); if (clearItemText != null) { String description = "Clear " + (myVirtualFile == null ? "default" : "file '" + myVirtualFile.getName() + "'") + " encoding."; group.add(new DumbAwareAction(clearItemText, description, null) { @Override public void actionPerformed(@Nonnull AnActionEvent e) { chosen(myVirtualFile, NO_ENCODING); } }); } if (favorites.isEmpty() && clearItemText == null) { fillCharsetActions(group, myVirtualFile, Arrays.asList(CharsetToolkit.getAvailableCharsets()), charsetFilter); } else { fillCharsetActions(group, myVirtualFile, favorites, charsetFilter); DefaultActionGroup more = new DefaultActionGroup("more", true); group.add(more); fillCharsetActions(more, myVirtualFile, Arrays.asList(CharsetToolkit.getAvailableCharsets()), charsetFilter); } return group; }
Example #22
Source File: LoadTextUtil.java From consulo with Apache License 2.0 | 5 votes |
@Nonnull private static Pair.NonNull<Charset, byte[]> getOverriddenCharsetByBOM(@Nonnull byte[] content, @Nonnull Charset charset) { if (charset.name().contains(CharsetToolkit.UTF8) && CharsetToolkit.hasUTF8Bom(content)) { return Pair.createNonNull(charset, CharsetToolkit.UTF8_BOM); } Charset charsetFromBOM = CharsetToolkit.guessFromBOM(content); if (charsetFromBOM != null) { byte[] bom = ObjectUtils.notNull(CharsetToolkit.getMandatoryBom(charsetFromBOM), ArrayUtilRt.EMPTY_BYTE_ARRAY); return Pair.createNonNull(charsetFromBOM, bom); } return Pair.createNonNull(charset, ArrayUtilRt.EMPTY_BYTE_ARRAY); }
Example #23
Source File: URLUtil.java From consulo with Apache License 2.0 | 5 votes |
@Nonnull public static String unescapePercentSequences(@Nonnull String s) { if (s.indexOf('%') == -1) { return s; } StringBuilder decoded = new StringBuilder(); final int len = s.length(); int i = 0; while (i < len) { char c = s.charAt(i); if (c == '%') { TIntArrayList bytes = new TIntArrayList(); while (i + 2 < len && s.charAt(i) == '%') { final int d1 = decode(s.charAt(i + 1)); final int d2 = decode(s.charAt(i + 2)); if (d1 != -1 && d2 != -1) { bytes.add(((d1 & 0xf) << 4 | d2 & 0xf)); i += 3; } else { break; } } if (!bytes.isEmpty()) { final byte[] bytesArray = new byte[bytes.size()]; for (int j = 0; j < bytes.size(); j++) { bytesArray[j] = (byte)bytes.getQuick(j); } decoded.append(new String(bytesArray, CharsetToolkit.UTF8_CHARSET)); continue; } } decoded.append(c); i++; } return decoded.toString(); }
Example #24
Source File: Executor.java From consulo with Apache License 2.0 | 5 votes |
protected static String run(List<String> params) { final ProcessBuilder builder = new ProcessBuilder().command(params); builder.directory(ourCurrentDir()); builder.redirectErrorStream(true); Process clientProcess; try { clientProcess = builder.start(); } catch (IOException e) { throw new RuntimeException(e); } CapturingProcessHandler handler = new CapturingProcessHandler(clientProcess, CharsetToolkit.getDefaultSystemCharset(), StringUtil.join(params, " ")); ProcessOutput result = handler.runProcess(30*1000); if (result.isTimeout()) { throw new RuntimeException("Timeout waiting for the command execution. Command: " + StringUtil.join(params, " ")); } if (result.getExitCode() != 0) { log("{" + result.getExitCode() + "}"); } String stdout = result.getStdout().trim(); if (!StringUtil.isEmptyOrSpaces(stdout)) { log(stdout.trim()); } return stdout; }
Example #25
Source File: ShelveChangesManager.java From consulo with Apache License 2.0 | 5 votes |
private static List<TextFilePatch> loadPatches(Project project, final String patchPath, CommitContext commitContext, boolean loadContent) throws IOException, PatchSyntaxException { char[] text = FileUtil.loadFileText(new File(patchPath), CharsetToolkit.UTF8); PatchReader reader = new PatchReader(new CharArrayCharSequence(text), loadContent); final List<TextFilePatch> textFilePatches = reader.readTextPatches(); ApplyPatchDefaultExecutor.applyAdditionalInfoBefore(project, reader.getAdditionalInfo(null), commitContext); return textFilePatches; }
Example #26
Source File: EncodingUtil.java From consulo with Apache License 2.0 | 5 votes |
@Nonnull static Magic8 isSafeToReloadIn(@Nonnull VirtualFile virtualFile, @Nonnull CharSequence text, @Nonnull byte[] bytes, @Nonnull 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); LoadTextUtil.AutoDetectionReason failReason = LoadTextUtil.getCharsetAutoDetectionReason(virtualFile); if (failReason != null && StandardCharsets.UTF_8.equals(virtualFile.getCharset()) && !StandardCharsets.UTF_8.equals(charset)) { return Magic8.NO_WAY; // can't reload utf8-autodetected file in another charset } byte[] bytesToSave; try { bytesToSave = toSave.getBytes(charset); } // turned out some crazy charsets have incorrectly implemented .newEncoder() returning null catch (UnsupportedOperationException | NullPointerException 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 : StringUtil.equals(loaded, text) ? Magic8.ABSOLUTELY : Magic8.WELL_IF_YOU_INSIST; }
Example #27
Source File: ExecUtil.java From consulo with Apache License 2.0 | 5 votes |
@Nonnull public static File createTempExecutableScript(@Nonnull String prefix, @Nonnull String suffix, @Nonnull String content) throws IOException, ExecutionException { File tempDir = new File(ContainerPathManager.get().getTempPath()); File tempFile = FileUtil.createTempFile(tempDir, prefix, suffix, true, true); FileUtil.writeToFile(tempFile, content.getBytes(CharsetToolkit.UTF8)); if (!tempFile.setExecutable(true, true)) { throw new ExecutionException("Failed to make temp file executable: " + tempFile); } return tempFile; }
Example #28
Source File: EncodingReference.java From consulo with Apache License 2.0 | 5 votes |
@RequiredReadAction @Override @Nullable public PsiElement resolve() { return CharsetToolkit.forName(myCharsetName) == null ? null : myElement; //if (ApplicationManager.getApplication().isUnitTestMode()) return myValue; // tests do not have full JDK //String fqn = charset.getClass().getName(); //return myValue.getManager().findClass(fqn, GlobalSearchScope.allScope(myValue.getProject())); }
Example #29
Source File: LoadTextUtilTest.java From consulo with Apache License 2.0 | 5 votes |
private static void doTest(String source, String expected, String expectedSeparator) { final LightVirtualFile vFile = new LightVirtualFile("test.txt"); final CharSequence real = LoadTextUtil.getTextByBinaryPresentation(source.getBytes(CharsetToolkit.US_ASCII_CHARSET), vFile); assertTrue("content", Comparing.equal(expected, real)); if (expectedSeparator != null) { assertEquals("detected line separator", expectedSeparator, FileDocumentManager.getInstance().getLineSeparator(vFile, null)); } }
Example #30
Source File: FileDocumentManagerImplTest.java From consulo with Apache License 2.0 | 5 votes |
public void testContentChanged_ignoreEventsFromSelf() throws Exception { final VirtualFile file = createFile("test.txt", "test\rtest"); Document document = myDocumentManager.getDocument(file); file.setBinaryContent("xxx".getBytes(CharsetToolkit.UTF8_CHARSET), -1, -1, myDocumentManager); assertNotNull(file.toString(), document); assertEquals("test\ntest", document.getText()); }