Java Code Examples for com.intellij.openapi.fileEditor.impl.LoadTextUtil#getTextByBinaryPresentation()

The following examples show how to use com.intellij.openapi.fileEditor.impl.LoadTextUtil#getTextByBinaryPresentation() . 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: EncodingUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
static Magic8 isSafeToConvertTo(@Nonnull VirtualFile virtualFile, @Nonnull CharSequence text, @Nonnull byte[] bytesOnDisk, @Nonnull Charset charset) {
  try {
    String lineSeparator = FileDocumentManager.getInstance().getLineSeparator(virtualFile, null);
    CharSequence textToSave = lineSeparator.equals("\n") ? text : StringUtilRt.convertLineSeparators(text, lineSeparator);

    Pair<Charset, byte[]> chosen = LoadTextUtil.chooseMostlyHarmlessCharset(virtualFile.getCharset(), charset, textToSave.toString());

    byte[] saved = chosen.second;

    CharSequence textLoadedBack = LoadTextUtil.getTextByBinaryPresentation(saved, charset);

    return !StringUtil.equals(text, textLoadedBack) ? Magic8.NO_WAY : Arrays.equals(saved, bytesOnDisk) ? Magic8.ABSOLUTELY : Magic8.WELL_IF_YOU_INSIST;
  }
  catch (UnsupportedOperationException e) { // unsupported encoding
    return Magic8.NO_WAY;
  }
}
 
Example 2
Source File: FileContentImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
@Override
public CharSequence getContentAsText() {
  if (myFileType.isBinary()) {
    throw new IllegalDataException("Cannot obtain text for binary file type : " + myFileType.getDescription());
  }
  final CharSequence content = getUserData(IndexingDataKeys.FILE_TEXT_CONTENT_KEY);
  if (content != null) {
    return content;
  }
  CharSequence contentAsText = myContentAsText;
  if (contentAsText == null) {
    myContentAsText = contentAsText = LoadTextUtil.getTextByBinaryPresentation(myContent, myFile);
    myContent = null; // help gc, indices are expected to use bytes or chars but not both
  }
  return contentAsText;
}
 
Example 3
Source File: LoadTextUtilTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
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 4
Source File: ApplyTextFilePatch.java    From consulo with Apache License 2.0 5 votes vote down vote up
@javax.annotation.Nullable
protected Result applyChange(final Project project, final VirtualFile fileToPatch, final FilePath pathBeforeRename, final Getter<CharSequence> baseContents) throws IOException {
  byte[] fileContents = fileToPatch.contentsToByteArray();
  CharSequence text = LoadTextUtil.getTextByBinaryPresentation(fileContents, fileToPatch);
  final GenericPatchApplier applier = new GenericPatchApplier(text, myPatch.getHunks());
  if (applier.execute()) {
    final Document document = FileDocumentManager.getInstance().getDocument(fileToPatch);
    if (document == null) {
      throw new IOException("Failed to set contents for updated file " + fileToPatch.getPath());
    }
    document.setText(applier.getAfter());
    FileDocumentManager.getInstance().saveDocument(document);
    return new Result(applier.getStatus()) {
      @Override
      public ApplyPatchForBaseRevisionTexts getMergeData() {
        return null;
      }
    };
  }
  applier.trySolveSomehow();
  return new Result(ApplyPatchStatus.FAILURE) {
    @Override
    public ApplyPatchForBaseRevisionTexts getMergeData() {
      return ApplyPatchForBaseRevisionTexts.create(project, fileToPatch, pathBeforeRename, myPatch, baseContents);
    }
  };
}
 
Example 5
Source File: PatchVirtualFileReader.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static PatchReader create(final VirtualFile virtualFile) throws IOException {
  final byte[] patchContents = virtualFile.contentsToByteArray();
  final CharSequence patchText = LoadTextUtil.getTextByBinaryPresentation(patchContents, virtualFile);
  return new PatchReader(patchText);
}