public static Long readInt(final IBStream stream) {
        char ch = stream.getChar();
        checkChar(BConst.INT_START, ch);

        long val = 0;
        boolean negative = false;
        boolean leadZero = false;

        ch = stream.getChar();
        if (BConst.SIGN == ch) {
            negative = true;
            ch = stream.getChar();
        } else if (BConst.ZERO == ch) {
            leadZero = true;
            ch = stream.getChar();
        }

        if (BConst.COMMON_END == ch) {
            if (leadZero) {
                return 0L;
            } else if (negative) {
                throw new IllegalStateException("Sign without digets not allowed.");
            }
            return null;
        }

        while (isDigit(ch)) {
            if (leadZero && BConst.ZERO != ch) {
                throw new IllegalStateException("Lead zero not allowed.");
            }
            val = val * 10 + (int) ch - (int) BConst.ZERO;
            ch = stream.getChar();
        }

        checkChar(BConst.COMMON_END, ch);
        return negative ? -val : val;
    }