This way we can reuse the same declaration buffer, and accept uppercase digits as well as lowercase digits. Signed-off-by: Shawn O. Pearce <spearce@xxxxxxxxxxx> --- .../org/spearce/jgit/transport/PacketLineIn.java | 26 ++--------------- .../src/org/spearce/jgit/util/RawParseUtils.java | 30 ++++++++++++++++++++ 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/PacketLineIn.java b/org.spearce.jgit/src/org/spearce/jgit/transport/PacketLineIn.java index 92c7009..8d2cd18 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/transport/PacketLineIn.java +++ b/org.spearce.jgit/src/org/spearce/jgit/transport/PacketLineIn.java @@ -40,7 +40,6 @@ import java.io.IOException; import java.io.InputStream; -import java.util.Arrays; import org.spearce.jgit.errors.PackProtocolException; import org.spearce.jgit.lib.Constants; @@ -51,16 +50,6 @@ class PacketLineIn { static final String END = new String("") /* must not string pool */; - private static final byte fromhex[]; - - static { - fromhex = new byte['f' + 1]; - Arrays.fill(fromhex, (byte) -1); - for (char i = '0'; i <= '9'; i++) - fromhex[i] = (byte) (i - '0'); - for (char i = 'a'; i <= 'f'; i++) - fromhex[i] = (byte) ((i - 'a') + 10); - } static enum AckNackResult { /** NAK */ @@ -127,22 +116,13 @@ String readStringRaw() throws IOException { return RawParseUtils.decode(Constants.CHARSET, raw, 0, len); } - int readLength() throws IOException { NB.readFully(in, lenbuffer, 0, 4); try { - int r = fromhex[lenbuffer[0]] << 4; - - r |= fromhex[lenbuffer[1]]; - r <<= 4; - - r |= fromhex[lenbuffer[2]]; - r <<= 4; - - r |= fromhex[lenbuffer[3]]; - if (r < 0) + final int len = RawParseUtils.parseHexInt16(lenbuffer, 0); + if (len != 0 && len < 4) throw new ArrayIndexOutOfBoundsException(); - return r; + return len; } catch (ArrayIndexOutOfBoundsException err) { throw new IOException("Invalid packet line header: " + (char) lenbuffer[0] + (char) lenbuffer[1] diff --git a/org.spearce.jgit/src/org/spearce/jgit/util/RawParseUtils.java b/org.spearce.jgit/src/org/spearce/jgit/util/RawParseUtils.java index 0554acb..5fb3d27 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/util/RawParseUtils.java +++ b/org.spearce.jgit/src/org/spearce/jgit/util/RawParseUtils.java @@ -255,6 +255,36 @@ public static final long parseLongBase10(final byte[] b, int ptr, } /** + * Parse 4 character base 16 (hex) formatted string to unsigned integer. + * <p> + * The number is read in network byte order, that is, most significant + * nybble first. + * + * @param bs + * buffer to parse digits from; positions {@code [p, p+4)} will + * be parsed. + * @param p + * first position within the buffer to parse. + * @return the integer value. + * @throws ArrayIndexOutOfBoundsException + * if the string is not hex formatted. + */ + public static final int parseHexInt16(final byte[] bs, final int p) { + int r = digits16[bs[p]] << 4; + + r |= digits16[bs[p + 1]]; + r <<= 4; + + r |= digits16[bs[p + 2]]; + r <<= 4; + + r |= digits16[bs[p + 3]]; + if (r < 0) + throw new ArrayIndexOutOfBoundsException(); + return r; + } + + /** * Parse 8 character base 16 (hex) formatted string to unsigned integer. * <p> * The number is read in network byte order, that is, most significant -- 1.6.3.1.333.g3ebba7 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html