@Nonnull public static CharSequence loadText(@Nonnull final VirtualFile file) { FileType type = file.getFileType(); if (type.isBinary()) { final BinaryFileDecompiler decompiler = BinaryFileTypeDecompilers.INSTANCE.forFileType(type); if (decompiler != null) { CharSequence text = decompiler.decompile(file); try { StringUtil.assertValidSeparators(text); } catch (AssertionError e) { LOG.error(e); } return text; } throw new IllegalArgumentException("Attempt to load text for binary file which doesn't have a decompiler plugged in: " + file.getPresentableUrl() + ". File type: " + type.getName()); } return loadText(file, UNLIMITED); }
@NotNull public static CharSequence loadText(@NotNull VirtualFile file) { if (file instanceof LightVirtualFile) { CharSequence content = ((LightVirtualFile)file).getContent(); if (StringUtil.indexOf(content, '\r') == -1) return content; CharBuffer buffer = CharBuffer.allocate(content.length()); buffer.append(content); buffer.rewind(); return convertLineSeparators(buffer).first; } if (file.isDirectory()) { throw new AssertionError("'" + file.getPresentableUrl() + "' is directory"); } final FileType fileType = file.getFileType(); if (fileType.isBinary()) { final BinaryFileDecompiler decompiler = BinaryFileTypeDecompilers.INSTANCE.forFileType(fileType); if (decompiler != null) { CharSequence text = decompiler.decompile(file); StringUtil.assertValidSeparators(text); return text; } throw new IllegalArgumentException("Attempt to load text for binary file, that doesn't have decompiler plugged in: "+file.getPresentableUrl()); } try { byte[] bytes = file.contentsToByteArray(); return getTextByBinaryPresentation(bytes, file); } catch (IOException e) { return ArrayUtil.EMPTY_CHAR_SEQUENCE; } }
public String getContent(final String fileUrl) { final VirtualFile fileByUrl = VirtualFileManager.getInstance().findFileByUrl(fileUrl); if (fileByUrl != null) { if (fileByUrl.isDirectory()) { return null; } BinaryFileDecompiler binaryFileDecompiler = null; FileType fileType = fileByUrl.getFileType(); if (fileType.isBinary()) { binaryFileDecompiler = BinaryFileTypeDecompilers.INSTANCE.forFileType(fileType); if (binaryFileDecompiler == null) { return null; } } if (binaryFileDecompiler != null) { return binaryFileDecompiler.decompile(fileByUrl).toString(); } return ApplicationManager.getApplication().runReadAction(new Computable<String>() { public String compute() { return getFileText(getProject(), fileByUrl).toString(); } }); } return null; }
@NotNull public static CharSequence loadText(@NotNull final VirtualFile file) { if (file instanceof LightVirtualFile) { return ((LightVirtualFile)file).getContent(); } if (file.isDirectory()) { throw new AssertionError("'" + file.getPresentableUrl() + "' is a directory"); } FileType fileType = file.getFileType(); if (fileType.isBinary()) { final BinaryFileDecompiler decompiler = BinaryFileTypeDecompilers.INSTANCE.forFileType(fileType); if (decompiler != null) { CharSequence text; Application app = ApplicationManager.getApplication(); if (app != null && app.isDispatchThread() && !app.isWriteAccessAllowed() && !GraphicsEnvironment.isHeadless()) { final Ref<CharSequence> result = Ref.create(ArrayUtil.EMPTY_CHAR_SEQUENCE); final Ref<Throwable> error = Ref.create(); ProgressManager.getInstance().run(new Task.Modal(null, "Decompiling " + file.getName(), true) { @Override public void run(@NotNull ProgressIndicator indicator) { indicator.setIndeterminate(true); try { result.set(ApplicationUtil.runWithCheckCanceled(new Callable<CharSequence>() { @Override public CharSequence call() { return decompiler.decompile(file); } }, indicator)); } catch (Throwable t) { error.set(t); } } }); ExceptionUtil.rethrowUnchecked(error.get()); text = result.get(); } else { text = decompiler.decompile(file); } StringUtil.assertValidSeparators(text); return text; } throw new IllegalArgumentException("Attempt to load text for binary file which doesn't have a decompiler plugged in: " + file.getPresentableUrl() + ". File type: " + fileType.getName()); } try { byte[] bytes = file.contentsToByteArray(); return getTextByBinaryPresentation(bytes, file); } catch (IOException e) { return ArrayUtil.EMPTY_CHAR_SEQUENCE; } }