Java Code Examples for com.intellij.openapi.vfs.CharsetToolkit#getMandatoryBom()
The following examples show how to use
com.intellij.openapi.vfs.CharsetToolkit#getMandatoryBom() .
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: ExternalDiffToolUtil.java From consulo with Apache License 2.0 | 5 votes |
@Nonnull private static File createTempFile(@Nonnull final DocumentContent content, @Nonnull FileNameInfo fileName) throws IOException { FileDocumentManager.getInstance().saveDocument(content.getDocument()); LineSeparator separator = content.getLineSeparator(); if (separator == null) separator = LineSeparator.getSystemLineSeparator(); Charset charset = content.getCharset(); if (charset == null) charset = Charset.defaultCharset(); Boolean hasBom = content.hasBom(); if (hasBom == null) hasBom = CharsetToolkit.getMandatoryBom(charset) != null; ThrowableComputable<String,RuntimeException> action = () -> { return content.getDocument().getText(); }; String contentData = AccessRule.read(action); if (separator != LineSeparator.LF) { contentData = StringUtil.convertLineSeparators(contentData, separator.getSeparatorString()); } byte[] bytes = contentData.getBytes(charset); byte[] bom = hasBom ? CharsetToolkit.getPossibleBom(charset) : null; if (bom != null) { bytes = ArrayUtil.mergeArrays(bom, bytes); } return createFile(bytes, fileName); }
Example 2
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; }