I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
Commit 4970fd22 authored by zzz's avatar zzz
Browse files

save a little space by avoiding BigInteger when possible

parent ac9392b9
No related branches found
No related tags found
No related merge requests found
...@@ -221,46 +221,51 @@ public class BDecoder ...@@ -221,46 +221,51 @@ public class BDecoder
{ {
c = read(); c = read();
if (c == 'e') if (c == 'e')
return new BEValue(BigInteger.ZERO); return new BEValue(Integer.valueOf(0));
else else
throw new InvalidBEncodingException("'e' expected after zero," throw new InvalidBEncodingException("'e' expected after zero,"
+ " not '" + (char)c + "'"); + " not '" + (char)c + "'");
} }
// XXX - We don't support more the 255 char big integers // XXX - We don't support more the 255 char big integers
char[] chars = new char[256]; StringBuilder chars = new StringBuilder(16);
int off = 0;
if (c == '-') if (c == '-')
{ {
c = read(); c = read();
if (c == '0') if (c == '0')
throw new InvalidBEncodingException("Negative zero not allowed"); throw new InvalidBEncodingException("Negative zero not allowed");
chars[off] = (char)c; chars.append((char)c);
off++;
} }
if (c < '1' || c > '9') if (c < '1' || c > '9')
throw new InvalidBEncodingException("Invalid Integer start '" throw new InvalidBEncodingException("Invalid Integer start '"
+ (char)c + "'"); + (char)c + "'");
chars[off] = (char)c; chars.append((char)c);
off++;
c = read(); c = read();
int i = c - '0'; while(c >= '0' && c <= '9')
while(i >= 0 && i <= 9)
{ {
chars[off] = (char)c; chars.append((char)c);
off++;
c = read(); c = read();
i = c - '0';
} }
if (c != 'e') if (c != 'e')
throw new InvalidBEncodingException("Integer should end with 'e'"); throw new InvalidBEncodingException("Integer should end with 'e'");
String s = new String(chars, 0, off); String s = chars.toString();
return new BEValue(new BigInteger(s)); int len = s.length();
// save a little space if we're sure it will fit
Number num;
if (len < 10)
num = Integer.valueOf(s);
else if (len < 19)
num = Long.valueOf(s);
else if (len > 256)
throw new InvalidBEncodingException("Too many digits: " + len);
else
num = new BigInteger(s);
return new BEValue(num);
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment