Example #1
0
  static void put(final ByteBuffer out, long i) {

    if (i == 0) {
      out.put((byte) '0');
      return;
    }

    if (i == Long.MIN_VALUE) out.put(MIN_VALUE);

    int size = (i < 0) ? Utils.digits(-i) + 1 : Utils.digits(i);

    long q;
    int r;
    byte sign = 0;
    int charPos = size;

    if (i < 0) {
      sign = '-';
      i = -i;
    }

    while (i > Integer.MAX_VALUE) {
      q = i / 100;
      // really: r = i - (q * 100);
      r = (int) (i - ((q << 6) + (q << 5) + (q << 2)));
      i = q;
      buf[--charPos] = Utils.DigitOnes[r];
      buf[--charPos] = Utils.DigitTens[r];
    }

    // Get 2 digits/iteration using ints
    int q2;
    int i2 = (int) i;
    while (i2 >= 65536) {
      q2 = i2 / 100;
      // really: r = i2 - (q * 100);
      r = i2 - ((q2 << 6) + (q2 << 5) + (q2 << 2));
      i2 = q2;
      buf[--charPos] = Utils.DigitOnes[r];
      buf[--charPos] = Utils.DigitTens[r];
    }

    // Fall thru to fast mode for smaller numbers
    // assert(i2 <= 65536, i2);
    for (; ; ) {
      q2 = (i2 * 52429) >>> (16 + 3);
      r = i2 - ((q2 << 3) + (q2 << 1)); // r = i2-(q2*10) ...
      buf[--charPos] = Utils.digits[r];
      i2 = q2;
      if (i2 == 0) break;
    }
    if (sign != 0) {
      buf[--charPos] = sign;
    }

    out.put(buf, 0, size);
  }
Example #2
0
  public static void putFixFloatTag(final ByteBuffer buf, final int tag, final long value) {
    final int length = Utils.digits(value);

    longToFixFloat(digitsBuf, 0, value, length);

    if (tag >= TAGS.length) {
      put(buf, tag);
      buf.put(EQL);
    } else {
      buf.put(TAGS[tag]);
    }

    put(buf, digitsBuf);

    buf.put(SOH);
  }