Exemple #1
0
  public static long fixFloatValueOf(final byte[] s, int length) {
    int start = 0;
    boolean negative = false;
    int decimal = -1;
    long value;

    final byte c = s[start];
    if (c == '-') {
      ++start;
      --length;
      negative = true;
    } /*
       * else if (c == '+') { ++start; --length; }
       */
    if (length == 0) throw new NumberFormatException("to short number");

    for (decimal = length - 1; decimal >= 0; decimal--) if (s[decimal] == (byte) '.') break;

    final int decimals = start + length - 1 - decimal;

    if (decimal > -1) {
      value = Utils.longValueOf(s, start, decimal - start) * FIX_FLOAT_NUMBER_OF_DECIMALS;
      final int dec =
          FIX_FLOAT_NUMBER_OF_DECIMALS_DIGITS < decimals
              ? FIX_FLOAT_NUMBER_OF_DECIMALS_DIGITS
              : decimals;
      value +=
          Utils.intValueOf(s, decimal + 1 < length ? decimal + 1 : length - 1, dec)
              * Utils.multiplier(FIX_FLOAT_NUMBER_OF_DECIMALS_DIGITS - dec);
    } else value = Utils.longValueOf(s, start, decimals) * FIX_FLOAT_NUMBER_OF_DECIMALS;

    if (negative) return -1 * value;
    return value;
  }
Exemple #2
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);
  }
Exemple #3
0
 public static void findEndOfMessage(final ByteBuffer buf) {
   while (buf.hasRemaining()) {
     int pos;
     if ((pos = Utils.scan(buf, SOH)) < 0) return;
     buf.position(pos + 1);
     if (buf.get() != (byte) '1') continue;
     if (buf.get() != (byte) '0') continue;
     if (buf.get() != EQL) continue;
     if ((pos = Utils.scan(buf, SOH)) < 0) return;
     buf.position(pos + 1);
     return;
   }
 }
Exemple #4
0
  public static byte[] getTagStringValue(
      final byte[] msgType, final int tag, final ByteBuffer src, final byte[] dst)
      throws FixSessionException {
    int start = 0;
    final int end = dst.length;
    byte c = SOH;

    Utils.fillNul(dst);

    while ((c = src.get()) != SOH) {

      if (start >= end) {
        throw new FixSessionException(
            SessionRejectReason.VALUE_IS_INCORRECT_OUT_OF_RANGE_FOR_THIS_TAG,
            ("Value length exceeds maximum of " + end).getBytes(),
            tag,
            msgType);
      } else {
        dst[start] = c;
        start++;
      }
    }

    if (start == 0)
      throw new FixSessionException(
          SessionRejectReason.TAG_SPECIFIED_WITHOUT_A_VALUE,
          "Tag specified without a value".getBytes(),
          tag,
          msgType);

    return dst;
  }
Exemple #5
0
  public static int getTagIntValue(final byte[] msgType, final int tag, final ByteBuffer buf)
      throws FixSessionException {
    byte c = SOH;
    int start = 0;
    final int end = FIX_MAX_DIGITS;

    while ((c = buf.get()) != SOH) {

      digitsBuf[start++] = c;

      if (start == end) {
        throw new FixSessionException(
            SessionRejectReason.VALUE_IS_INCORRECT_OUT_OF_RANGE_FOR_THIS_TAG,
            ("Value length exceeds maximum number of digits " + FIX_MAX_DIGITS).getBytes(),
            tag,
            msgType);
      }
    }

    if (start == 0)
      throw new FixSessionException(
          SessionRejectReason.TAG_SPECIFIED_WITHOUT_A_VALUE,
          "Tag specified without a value".getBytes(),
          tag,
          msgType);

    if (c != SOH)
      throw new FixSessionException(
          SessionRejectReason.TAG_SPECIFIED_WITHOUT_A_VALUE,
          "Message not terminated by SOH".getBytes(),
          tag,
          msgType);

    return Utils.intValueOf(digitsBuf, 0, start);
  }
Exemple #6
0
  public static long getTagFloatValue(final byte[] msgType, final int tag, final ByteBuffer buf)
      throws FixSessionException {
    byte c;
    int start = 0;
    final int end = FIX_MAX_DIGITS - 2;

    Utils.fillNul(digitsBuf);

    while ((c = buf.get()) != SOH) {

      digitsBuf[start++] = c;

      if (start == end) {
        throw new FixSessionException(
            SessionRejectReason.VALUE_IS_INCORRECT_OUT_OF_RANGE_FOR_THIS_TAG,
            ("Value is incorrect or out of range for this tag " + FIX_MAX_DIGITS).getBytes(),
            tag,
            msgType);
      }
    }
    try {
      final long val = fixFloatValueOf(digitsBuf, start);
      return val;
    } catch (final NumberFormatException n) {
      throw new FixSessionException(
          SessionRejectReason.INCORRECT_DATA_FORMAT_FOR_VALUE,
          "Incorrect data format for value".getBytes(),
          tag,
          msgType);
    }
  }
Exemple #7
0
  public static ByteBuffer getTagValue(final ByteBuffer data) {
    final int tagSOH = Utils.scan(data, SOH);
    if (tagSOH < 0) return null;

    final ByteBuffer value = data.slice();
    value.limit(tagSOH);

    data.position(tagSOH + 1);

    return value;
  }
Exemple #8
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);
  }
Exemple #9
0
  public static void longToFixFloat(final byte out[], final int offset, long l, int length) {
    final boolean negative = l < 0;
    final int radix = 10;
    int pos = offset + length;
    final int missingDigits = -1 * (length - FIX_FLOAT_NUMBER_OF_DECIMALS_DIGITS);
    int decimal;

    if (missingDigits > 0) decimal = offset + 2;
    else if (missingDigits < 0) decimal = pos - FIX_FLOAT_NUMBER_OF_DECIMALS_DIGITS + 1;
    else decimal = offset + 2;
    pos += length <= FIX_FLOAT_NUMBER_OF_DECIMALS_DIGITS ? decimal + missingDigits : 1;

    Utils.fill(out, offset, pos - offset, (byte) '0');

    if (l == 0) {
      out[offset] = (byte) '0';
      return;
    }

    int count = 2;
    long j = l;
    if (!negative) {
      count = 1;
      j = -l;
    } else {
      pos++;
      decimal++;
    }
    while ((l /= radix) != 0) count++;

    do {
      int ch = 0 - (int) (j % radix);
      if (ch > 9) ch = ch - 10 + (byte) 'a';
      else ch += (byte) '0';
      out[--pos] = (byte) ch;
      if (pos == decimal) --pos;

    } while ((j /= radix) != 0);

    out[decimal - 1] = (byte) '.';

    if (negative) out[0] = (byte) '-';
  }