Changeset 4970fd2


Ignore:
Timestamp:
Jun 23, 2013 5:52:36 PM (7 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
f525685
Parents:
ac9392b
Message:

save a little space by avoiding BigInteger? when possible

File:
1 edited

Legend:

Unmodified
Added
Removed
  • apps/i2psnark/java/src/org/klomp/snark/bencode/BDecoder.java

    rac9392b r4970fd2  
    222222        c = read();
    223223        if (c == 'e')
    224           return new BEValue(BigInteger.ZERO);
     224          return new BEValue(Integer.valueOf(0));
    225225        else
    226226          throw new InvalidBEncodingException("'e' expected after zero,"
     
    229229
    230230    // XXX - We don't support more the 255 char big integers
    231     char[] chars = new char[256];
    232     int off = 0;
     231    StringBuilder chars = new StringBuilder(16);
    233232
    234233    if (c == '-')
     
    237236        if (c == '0')
    238237          throw new InvalidBEncodingException("Negative zero not allowed");
    239         chars[off] = (char)c;
    240         off++;
     238        chars.append((char)c);
    241239      }
    242240
     
    244242      throw new InvalidBEncodingException("Invalid Integer start '"
    245243                                          + (char)c + "'");
    246     chars[off] = (char)c;
    247     off++;
     244    chars.append((char)c);
    248245
    249246    c = read();
    250     int i = c - '0';
    251     while(i >= 0 && i <= 9)
    252       {
    253         chars[off] = (char)c;
    254         off++;
     247    while(c >= '0' && c <= '9')
     248      {
     249        chars.append((char)c);
    255250        c = read();
    256         i = c - '0';
    257251      }
    258252
     
    260254      throw new InvalidBEncodingException("Integer should end with 'e'");
    261255
    262     String s = new String(chars, 0, off);
    263     return new BEValue(new BigInteger(s));
     256    String s = chars.toString();
     257    int len = s.length();
     258    // save a little space if we're sure it will fit
     259    Number num;
     260    if (len < 10)
     261        num = Integer.valueOf(s);
     262    else if (len < 19)
     263        num = Long.valueOf(s);
     264    else if (len > 256)
     265        throw new InvalidBEncodingException("Too many digits: " + len);
     266    else
     267        num = new BigInteger(s);
     268    return new BEValue(num);
    264269  }
    265270
Note: See TracChangeset for help on using the changeset viewer.