(refs #180)Fix a problem about multi-byte characters.

This commit is contained in:
takezoe
2013-10-30 13:19:25 +09:00
parent a1dc19fa26
commit 9e5a302ab1

View File

@@ -3,9 +3,11 @@ package util;
import org.eclipse.jgit.api.errors.PatchApplyException; import org.eclipse.jgit.api.errors.PatchApplyException;
import org.eclipse.jgit.diff.RawText; import org.eclipse.jgit.diff.RawText;
import org.eclipse.jgit.internal.JGitText; import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.patch.FileHeader; import org.eclipse.jgit.patch.FileHeader;
import org.eclipse.jgit.patch.HunkHeader; import org.eclipse.jgit.patch.HunkHeader;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.ArrayList; import java.util.ArrayList;
@@ -16,7 +18,7 @@ import java.util.List;
*/ */
public class PatchUtil { public class PatchUtil {
public static String apply(String source, FileHeader fh) public static String apply(String source, String patch, FileHeader fh)
throws IOException, PatchApplyException { throws IOException, PatchApplyException {
RawText rt = new RawText(source.getBytes("UTF-8")); RawText rt = new RawText(source.getBytes("UTF-8"));
List<String> oldLines = new ArrayList<String>(rt.size()); List<String> oldLines = new ArrayList<String>(rt.size());
@@ -24,10 +26,9 @@ public class PatchUtil {
oldLines.add(rt.getString(i)); oldLines.add(rt.getString(i));
List<String> newLines = new ArrayList<String>(oldLines); List<String> newLines = new ArrayList<String>(oldLines);
for (HunkHeader hh : fh.getHunks()) { for (HunkHeader hh : fh.getHunks()) {
StringBuilder hunk = new StringBuilder(); ByteArrayOutputStream out = new ByteArrayOutputStream();
for (int j = hh.getStartOffset(); j < hh.getEndOffset(); j++) out.write(patch.getBytes("UTF-8"), hh.getStartOffset(), hh.getEndOffset() - hh.getStartOffset());
hunk.append((char) hh.getBuffer()[j]); RawText hrt = new RawText(out.toByteArray());
RawText hrt = new RawText(hunk.toString().getBytes("UTF-8"));
List<String> hunkLines = new ArrayList<String>(hrt.size()); List<String> hunkLines = new ArrayList<String>(hrt.size());
for (int i = 0; i < hrt.size(); i++) for (int i = 0; i < hrt.size(); i++)
hunkLines.add(hrt.getString(i)); hunkLines.add(hrt.getString(i));