[Mercurial] +getContentAsBytes in HgContentRevision.
authorKirill Likhodedov <kirill.likhodedov@jetbrains.com>
Fri, 6 Aug 2010 11:27:44 +0000 (15:27 +0400)
committerKirill Likhodedov <kirill.likhodedov@jetbrains.com>
Fri, 6 Aug 2010 11:27:44 +0000 (15:27 +0400)
plugins/hg4idea/src/org/zmlx/hg4idea/HgContentRevision.java

index 9d48ad73ca28ecf5441bd8d2994bef6fff57cb83..104fc067b382ffc8aa05f2798211feb68f636e12 100644 (file)
@@ -16,12 +16,17 @@ import com.intellij.openapi.project.Project;
 import com.intellij.openapi.vcs.FilePath;
 import com.intellij.openapi.vcs.VcsException;
 import com.intellij.openapi.vcs.changes.ContentRevision;
+import com.intellij.openapi.vfs.VirtualFile;
+import com.intellij.vcsUtil.VcsUtil;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang.builder.EqualsBuilder;
 import org.apache.commons.lang.builder.HashCodeBuilder;
 import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
 import org.zmlx.hg4idea.command.HgCatCommand;
 
+import java.io.UnsupportedEncodingException;
+
 public class HgContentRevision implements ContentRevision {
 
   private final Project project;
@@ -38,6 +43,7 @@ public class HgContentRevision implements ContentRevision {
     this.revisionNumber = revisionNumber;
   }
 
+  @Nullable
   public String getContent() throws VcsException {
     if (StringUtils.isBlank(content)) {
       content = new HgCatCommand(project).execute(hgFile, revisionNumber, getFile().getCharset());
@@ -45,6 +51,23 @@ public class HgContentRevision implements ContentRevision {
     return content;
   }
 
+  @Nullable
+  public byte[] getContentAsBytes() throws VcsException {
+    String content = getContent();
+    if (content == null) {
+      return null;
+    }
+    try {
+      VirtualFile vf = VcsUtil.getVirtualFile(hgFile.getFile());
+      if (vf == null) {
+        return null;
+      }
+      return content.getBytes(vf.getCharset().name());
+    } catch (UnsupportedEncodingException e) {
+      throw new VcsException("Couldn't retrieve file content due to a UnsupportedEncodingException", e);
+    }
+  }
+
   @NotNull
   public FilePath getFile() {
     if (filePath == null) {