From dc24383b6be8bd1e90a6754ea4791af13abb2fc7 Mon Sep 17 00:00:00 2001 From: Han-Wen NIenhuys Date: Tue, 24 Oct 2017 11:25:50 -0400 Subject: [PATCH] Revert "Throw BinaryBlobException from RawParseUtils#lineMap." This reverts commit f2e64cd895a6aa4f18ab3b876f13b7814fb98f04. The newly added throws clause breaks backward compatibility. Change-Id: Ifa76a1b95935e52640b81cd53c171eb17da175c2 Signed-off-by: Han-Wen Nienhuys --- .../org/eclipse/jgit/diff/RawTextTest.java | 10 ------- .../jgit/util/RawParseUtils_LineMapTest.java | 18 ++++++------- .../src/org/eclipse/jgit/diff/RawText.java | 27 ++----------------- .../org/eclipse/jgit/util/RawParseUtils.java | 15 ++++++++--- 4 files changed, 22 insertions(+), 48 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextTest.java index 3b563b3afb..93ea9a7ab5 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RawTextTest.java @@ -64,16 +64,6 @@ public class RawTextTest { assertEquals(0, r.size()); } - @Test - public void testBinary() { - String input = "foo-a\nf\0o-b\n"; - byte[] data = Constants.encodeASCII(input); - final RawText a = new RawText(data); - assertEquals(a.content, data); - assertEquals(a.size(), 1); - assertEquals(a.getString(0, 1, false), input); - } - @Test public void testEquals() { final RawText a = new RawText(Constants.encodeASCII("foo-a\nfoo-b\n")); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/RawParseUtils_LineMapTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/RawParseUtils_LineMapTest.java index fe070c80af..6efdce6d77 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/RawParseUtils_LineMapTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/RawParseUtils_LineMapTest.java @@ -48,45 +48,45 @@ import static org.junit.Assert.assertNotNull; import java.io.UnsupportedEncodingException; -import org.eclipse.jgit.errors.BinaryBlobException; import org.junit.Test; public class RawParseUtils_LineMapTest { @Test - public void testEmpty() throws Exception { + public void testEmpty() { final IntList map = RawParseUtils.lineMap(new byte[] {}, 0, 0); assertNotNull(map); assertArrayEquals(new int[]{Integer.MIN_VALUE, 0}, asInts(map)); } @Test - public void testOneBlankLine() throws Exception { + public void testOneBlankLine() { final IntList map = RawParseUtils.lineMap(new byte[] { '\n' }, 0, 1); assertArrayEquals(new int[]{Integer.MIN_VALUE, 0, 1}, asInts(map)); } @Test - public void testTwoLineFooBar() throws Exception { + public void testTwoLineFooBar() throws UnsupportedEncodingException { final byte[] buf = "foo\nbar\n".getBytes("ISO-8859-1"); final IntList map = RawParseUtils.lineMap(buf, 0, buf.length); assertArrayEquals(new int[]{Integer.MIN_VALUE, 0, 4, buf.length}, asInts(map)); } @Test - public void testTwoLineNoLF() throws Exception { + public void testTwoLineNoLF() throws UnsupportedEncodingException { final byte[] buf = "foo\nbar".getBytes("ISO-8859-1"); final IntList map = RawParseUtils.lineMap(buf, 0, buf.length); assertArrayEquals(new int[]{Integer.MIN_VALUE, 0, 4, buf.length}, asInts(map)); } - @Test(expected = BinaryBlobException.class) - public void testBinary() throws Exception { + @Test + public void testBinary() throws UnsupportedEncodingException { final byte[] buf = "xxxfoo\nb\0ar".getBytes("ISO-8859-1"); - RawParseUtils.lineMap(buf, 3, buf.length); + final IntList map = RawParseUtils.lineMap(buf, 3, buf.length); + assertArrayEquals(new int[]{Integer.MIN_VALUE, 3, buf.length}, asInts(map)); } @Test - public void testFourLineBlanks() throws Exception { + public void testFourLineBlanks() throws UnsupportedEncodingException { final byte[] buf = "foo\n\n\nbar\n".getBytes("ISO-8859-1"); final IntList map = RawParseUtils.lineMap(buf, 0, buf.length); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/diff/RawText.java b/org.eclipse.jgit/src/org/eclipse/jgit/diff/RawText.java index ef3d78bc5f..656319dd7c 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/diff/RawText.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/diff/RawText.java @@ -93,29 +93,7 @@ public class RawText extends Sequence { */ public RawText(final byte[] input) { content = input; - IntList map; - try { - map = RawParseUtils.lineMap(content, 0, content.length); - } catch (BinaryBlobException e) { - map = new IntList(3); - map.add(Integer.MIN_VALUE); - map.add(0); - map.add(content.length); - } - lines = map; - } - - /** - * Construct a new RawText if the line map is already known. - * - * @param data - * the blob data. - * @param lineMap - * Indices of line starts, with indexed by base-1 linenumber. - */ - private RawText(final byte[] data, final IntList lineMap) { - content = data; - lines = lineMap; + lines = RawParseUtils.lineMap(content, 0, content.length); } /** @@ -379,8 +357,7 @@ public class RawText extends Sequence { System.arraycopy(head, 0, data, 0, head.length); IO.readFully(stream, data, off, (int) (sz-off)); - IntList lineMap = RawParseUtils.lineMap(data, 0, data.length); - return new RawText(data, lineMap); + return new RawText(data); } } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java index 0d6a8d9612..ad138bbf18 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java @@ -63,7 +63,6 @@ import java.util.HashMap; import java.util.Map; import org.eclipse.jgit.annotations.Nullable; -import org.eclipse.jgit.errors.BinaryBlobException; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.PersonIdent; @@ -619,6 +618,9 @@ public final class RawParseUtils { *

* The last element (index map.size()-1) always contains * end. + *

+ * If the data contains a '\0' anywhere, the whole region is considered binary + * and a LineMap corresponding to a single line is returned. *

* * @param buf @@ -629,9 +631,10 @@ public final class RawParseUtils { * @param end * 1 past the end of the content within buf. * @return a line map indexing the start position of each line. - * @throws BinaryBlobException if any '\0' is found. */ - public static final IntList lineMap(final byte[] buf, int ptr, int end) throws BinaryBlobException { + public static final IntList lineMap(final byte[] buf, int ptr, int end) { + int start = ptr; + // Experimentally derived from multiple source repositories // the average number of bytes/line is 36. Its a rough guess // to initially size our map close to the target. @@ -644,7 +647,11 @@ public final class RawParseUtils { } if (buf[ptr] == '\0') { - throw new BinaryBlobException(); + // binary data. + map = new IntList(3); + map.add(Integer.MIN_VALUE); + map.add(start); + break; } foundLF = (buf[ptr] == '\n'); -- 2.39.5