コード例 #1
0
ファイル: BytesUtil.java プロジェクト: kalevski/rapidoid
  public static long parseLine(Bytes bytes, Range line, long start, long limit) {
    byte b0 = 0, b1 = 0;
    long ret = -1;

    long i;
    for (i = start; i < limit; i++) {
      b0 = b1;
      b1 = bytes.get(i);

      if (b1 == LF) {
        long len;

        if (b0 == CR) {
          len = i - start - 1;
        } else {
          len = i - start;
        }

        line.set(start, len);
        ret = i + 1;
        break;
      }
    }

    return ret;
  }
コード例 #2
0
ファイル: BytesUtil.java プロジェクト: kalevski/rapidoid
  /**
   * Scans the buffer until a line separator (CRLF or LF) is found, and matches the 4-byte prefix of
   * the scanned selection against the specified search prefix. Returns the position of the
   * separator, or <code>-1</code> if the limit is reached and separator not found. If the prefix is
   * matched, the negative of the position is returned, to mark the prefix match. Duplicated code
   * for performance reasons.
   */
  public static long scanLnAndMatchPrefix(
      Bytes bytes, Range result, long fromPos, long toPos, long searchPrefix) {

    byte b0, b1, b2, b3;

    long p = fromPos;
    if (p <= toPos) {
      b0 = bytes.get(p);
      if (b0 == LF) {
        result.set(fromPos, 0);
        return p + 1;
      }
    } else {
      result.reset();
      return NOT_FOUND;
    }

    p++;
    if (p <= toPos) {
      b1 = bytes.get(p);
      if (b1 == LF) {
        if (b0 == CR) {
          result.set(fromPos, 0);
        } else {
          result.set(fromPos, 1);
        }
        return p + 1;
      }
    } else {
      result.reset();
      return NOT_FOUND;
    }

    p++;
    if (p <= toPos) {
      b2 = bytes.get(p);
      if (b2 == LF) {
        if (b1 == CR) {
          result.set(fromPos, 1);
        } else {
          result.set(fromPos, 2);
        }
        return p + 1;
      }
    } else {
      result.reset();
      return NOT_FOUND;
    }

    p++;
    if (p <= toPos) {
      b3 = bytes.get(p);
      if (b3 == LF) {
        if (b2 == CR) {
          result.set(fromPos, 2);
        } else {
          result.set(fromPos, 3);
        }
        return p + 1;
      }
    } else {
      result.reset();
      return NOT_FOUND;
    }

    long prefix = UTILS.intFrom(b0, b1, b2, b3);

    boolean matchedPrefix = prefix == searchPrefix;

    for (long i = p; i <= toPos; i++) {
      if (bytes.get(i) == LF) {

        if (bytes.get(i - 1) == CR) {
          result.setInterval(fromPos, i - 1);
        } else {
          result.setInterval(fromPos, i);
        }

        long nextPos = i + 1;
        return matchedPrefix ? -nextPos : nextPos;
      }
    }

    result.reset();
    return NOT_FOUND;
  }