Java Code Examples for com.mysql.cj.util.StringUtils#getInt()

The following examples show how to use com.mysql.cj.util.StringUtils#getInt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MysqlTextValueDecoder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public <T> T decodeDate(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    if (length != DATE_BUF_LEN) {
        throw new DataReadException(Messages.getString("ResultSet.InvalidLengthForType", new Object[] { length, "DATE" }));
    }
    int year = StringUtils.getInt(bytes, offset, offset + 4);
    int month = StringUtils.getInt(bytes, offset + 5, offset + 7);
    int day = StringUtils.getInt(bytes, offset + 8, offset + 10);
    return vf.createFromDate(year, month, day);
}
 
Example 2
Source File: MysqlTextValueDecoder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public <T> T decodeTimestamp(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    if (length < TIMESTAMP_NOFRAC_STR_LEN || (length > TIMESTAMP_STR_LEN_MAX && length != TIMESTAMP_STR_LEN_WITH_NANOS)) {
        throw new DataReadException(Messages.getString("ResultSet.InvalidLengthForType", new Object[] { length, "TIMESTAMP" }));
    } else if (length != TIMESTAMP_NOFRAC_STR_LEN) {
        // need at least two extra bytes for fractional, '.' and a digit
        if (bytes[offset + TIMESTAMP_NOFRAC_STR_LEN] != (byte) '.' || length < TIMESTAMP_NOFRAC_STR_LEN + 2) {
            throw new DataReadException(
                    Messages.getString("ResultSet.InvalidFormatForType", new Object[] { StringUtils.toString(bytes, offset, length), "TIMESTAMP" }));
        }
    }

    // delimiter verification
    if (bytes[offset + 4] != (byte) '-' || bytes[offset + 7] != (byte) '-' || bytes[offset + 10] != (byte) ' ' || bytes[offset + 13] != (byte) ':'
            || bytes[offset + 16] != (byte) ':') {
        throw new DataReadException(
                Messages.getString("ResultSet.InvalidFormatForType", new Object[] { StringUtils.toString(bytes, offset, length), "TIMESTAMP" }));
    }

    int year = StringUtils.getInt(bytes, offset, offset + 4);
    int month = StringUtils.getInt(bytes, offset + 5, offset + 7);
    int day = StringUtils.getInt(bytes, offset + 8, offset + 10);
    int hours = StringUtils.getInt(bytes, offset + 11, offset + 13);
    int minutes = StringUtils.getInt(bytes, offset + 14, offset + 16);
    int seconds = StringUtils.getInt(bytes, offset + 17, offset + 19);
    // nanos from MySQL fractional
    int nanos;
    if (length == TIMESTAMP_STR_LEN_WITH_NANOS) {
        nanos = StringUtils.getInt(bytes, offset + 20, offset + length);
    } else {
        nanos = (length == TIMESTAMP_NOFRAC_STR_LEN) ? 0 : StringUtils.getInt(bytes, offset + 20, offset + length);
        // scale out nanos appropriately. mysql supports up to 6 digits of fractional seconds, each additional digit increasing the range by a factor of
        // 10. one digit is tenths, two is hundreths, etc
        nanos = nanos * (int) Math.pow(10, 9 - (length - TIMESTAMP_NOFRAC_STR_LEN - 1));
    }

    return vf.createFromTimestamp(year, month, day, hours, minutes, seconds, nanos);
}
 
Example 3
Source File: MysqlTextValueDecoder.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
public <T> T decodeDate(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    if (length != DATE_BUF_LEN) {
        throw new DataReadException(Messages.getString("ResultSet.InvalidLengthForType", new Object[] { length, "DATE" }));
    }
    int year = StringUtils.getInt(bytes, offset, offset + 4);
    int month = StringUtils.getInt(bytes, offset + 5, offset + 7);
    int day = StringUtils.getInt(bytes, offset + 8, offset + 10);
    return vf.createFromDate(year, month, day);
}
 
Example 4
Source File: MysqlTextValueDecoder.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
public <T> T decodeTimestamp(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    if (length < TIMESTAMP_NOFRAC_STR_LEN || (length > TIMESTAMP_STR_LEN_MAX && length != TIMESTAMP_STR_LEN_WITH_NANOS)) {
        throw new DataReadException(Messages.getString("ResultSet.InvalidLengthForType", new Object[] { length, "TIMESTAMP" }));
    } else if (length != TIMESTAMP_NOFRAC_STR_LEN) {
        // need at least two extra bytes for fractional, '.' and a digit
        if (bytes[offset + TIMESTAMP_NOFRAC_STR_LEN] != (byte) '.' || length < TIMESTAMP_NOFRAC_STR_LEN + 2) {
            throw new DataReadException(
                    Messages.getString("ResultSet.InvalidFormatForType", new Object[] { StringUtils.toString(bytes, offset, length), "TIMESTAMP" }));
        }
    }

    // delimiter verification
    if (bytes[offset + 4] != (byte) '-' || bytes[offset + 7] != (byte) '-' || bytes[offset + 10] != (byte) ' ' || bytes[offset + 13] != (byte) ':'
            || bytes[offset + 16] != (byte) ':') {
        throw new DataReadException(
                Messages.getString("ResultSet.InvalidFormatForType", new Object[] { StringUtils.toString(bytes, offset, length), "TIMESTAMP" }));
    }

    int year = StringUtils.getInt(bytes, offset, offset + 4);
    int month = StringUtils.getInt(bytes, offset + 5, offset + 7);
    int day = StringUtils.getInt(bytes, offset + 8, offset + 10);
    int hours = StringUtils.getInt(bytes, offset + 11, offset + 13);
    int minutes = StringUtils.getInt(bytes, offset + 14, offset + 16);
    int seconds = StringUtils.getInt(bytes, offset + 17, offset + 19);
    // nanos from MySQL fractional
    int nanos;
    if (length == TIMESTAMP_STR_LEN_WITH_NANOS) {
        nanos = StringUtils.getInt(bytes, offset + 20, offset + length);
    } else {
        nanos = (length == TIMESTAMP_NOFRAC_STR_LEN) ? 0 : StringUtils.getInt(bytes, offset + 20, offset + length);
        // scale out nanos appropriately. mysql supports up to 6 digits of fractional seconds, each additional digit increasing the range by a factor of
        // 10. one digit is tenths, two is hundreths, etc
        nanos = nanos * (int) Math.pow(10, 9 - (length - TIMESTAMP_NOFRAC_STR_LEN - 1));
    }

    return vf.createFromTimestamp(year, month, day, hours, minutes, seconds, nanos);
}
 
Example 5
Source File: MysqlTextValueDecoder.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public <T> T decodeTime(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    int pos = 0;
    // used to track the length of the current time segment during parsing
    int segmentLen;

    if (length < TIME_STR_LEN_MIN || length > TIME_STR_LEN_MAX) {
        throw new DataReadException(Messages.getString("ResultSet.InvalidLengthForType", new Object[] { length, "TIME" }));
    }

    boolean negative = false;

    if (bytes[offset] == '-') {
        pos++;
        negative = true;
    }

    // parse hours field
    for (segmentLen = 0; Character.isDigit((char) bytes[offset + pos + segmentLen]); segmentLen++) {
        ;
    }
    if (segmentLen == 0 || bytes[offset + pos + segmentLen] != ':') {
        throw new DataReadException(
                Messages.getString("ResultSet.InvalidFormatForType", new Object[] { "TIME", StringUtils.toString(bytes, offset, length) }));
    }
    int hours = StringUtils.getInt(bytes, offset + pos, offset + pos + segmentLen);
    if (negative) {
        hours *= -1;
    }
    pos += segmentLen + 1; // +1 for ':' character

    // parse minutes field
    for (segmentLen = 0; Character.isDigit((char) bytes[offset + pos + segmentLen]); segmentLen++) {
        ;
    }
    if (segmentLen != 2 || bytes[offset + pos + segmentLen] != ':') {
        throw new DataReadException(
                Messages.getString("ResultSet.InvalidFormatForType", new Object[] { "TIME", StringUtils.toString(bytes, offset, length) }));
    }
    int minutes = StringUtils.getInt(bytes, offset + pos, offset + pos + segmentLen);
    pos += segmentLen + 1;

    // parse seconds field
    for (segmentLen = 0; offset + pos + segmentLen < offset + length && Character.isDigit((char) bytes[offset + pos + segmentLen]); segmentLen++) {
        ;
    }
    if (segmentLen != 2) {
        throw new DataReadException(
                Messages.getString("ResultSet.InvalidFormatForType", new Object[] { StringUtils.toString(bytes, offset, length), "TIME" }));
    }
    int seconds = StringUtils.getInt(bytes, offset + pos, offset + pos + segmentLen);
    pos += segmentLen;

    // parse optional microsecond fractional value
    int nanos = 0;
    if (length > pos) {
        pos++; // skip '.' character

        for (segmentLen = 0; offset + pos + segmentLen < offset + length && Character.isDigit((char) bytes[offset + pos + segmentLen]); segmentLen++) {
            ;
        }
        if (segmentLen + pos != length) {
            throw new DataReadException(
                    Messages.getString("ResultSet.InvalidFormatForType", new Object[] { StringUtils.toString(bytes, offset, length), "TIME" }));
        }
        nanos = StringUtils.getInt(bytes, offset + pos, offset + pos + segmentLen);
        // scale out nanos appropriately. mysql supports up to 6 digits of fractional seconds, each additional digit increasing the range by a factor of
        // 10. one digit is tenths, two is hundreths, etc
        nanos = nanos * (int) Math.pow(10, 9 - segmentLen);
    }

    return vf.createFromTime(hours, minutes, seconds, nanos);
}
 
Example 6
Source File: MysqlTextValueDecoder.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
public <T> T decodeTime(byte[] bytes, int offset, int length, ValueFactory<T> vf) {
    int pos = 0;
    // used to track the length of the current time segment during parsing
    int segmentLen;

    if (length < TIME_STR_LEN_MIN || length > TIME_STR_LEN_MAX) {
        throw new DataReadException(Messages.getString("ResultSet.InvalidLengthForType", new Object[] { length, "TIME" }));
    }

    boolean negative = false;

    if (bytes[offset] == '-') {
        pos++;
        negative = true;
    }

    // parse hours field
    for (segmentLen = 0; Character.isDigit((char) bytes[offset + pos + segmentLen]); segmentLen++) {
        ;
    }
    if (segmentLen == 0 || bytes[offset + pos + segmentLen] != ':') {
        throw new DataReadException(
                Messages.getString("ResultSet.InvalidFormatForType", new Object[] { "TIME", StringUtils.toString(bytes, offset, length) }));
    }
    int hours = StringUtils.getInt(bytes, offset + pos, offset + pos + segmentLen);
    if (negative) {
        hours *= -1;
    }
    pos += segmentLen + 1; // +1 for ':' character

    // parse minutes field
    for (segmentLen = 0; Character.isDigit((char) bytes[offset + pos + segmentLen]); segmentLen++) {
        ;
    }
    if (segmentLen != 2 || bytes[offset + pos + segmentLen] != ':') {
        throw new DataReadException(
                Messages.getString("ResultSet.InvalidFormatForType", new Object[] { "TIME", StringUtils.toString(bytes, offset, length) }));
    }
    int minutes = StringUtils.getInt(bytes, offset + pos, offset + pos + segmentLen);
    pos += segmentLen + 1;

    // parse seconds field
    for (segmentLen = 0; offset + pos + segmentLen < offset + length && Character.isDigit((char) bytes[offset + pos + segmentLen]); segmentLen++) {
        ;
    }
    if (segmentLen != 2) {
        throw new DataReadException(
                Messages.getString("ResultSet.InvalidFormatForType", new Object[] { StringUtils.toString(bytes, offset, length), "TIME" }));
    }
    int seconds = StringUtils.getInt(bytes, offset + pos, offset + pos + segmentLen);
    pos += segmentLen;

    // parse optional microsecond fractional value
    int nanos = 0;
    if (length > pos) {
        pos++; // skip '.' character

        for (segmentLen = 0; offset + pos + segmentLen < offset + length && Character.isDigit((char) bytes[offset + pos + segmentLen]); segmentLen++) {
            ;
        }
        if (segmentLen + pos != length) {
            throw new DataReadException(
                    Messages.getString("ResultSet.InvalidFormatForType", new Object[] { StringUtils.toString(bytes, offset, length), "TIME" }));
        }
        nanos = StringUtils.getInt(bytes, offset + pos, offset + pos + segmentLen);
        // scale out nanos appropriately. mysql supports up to 6 digits of fractional seconds, each additional digit increasing the range by a factor of
        // 10. one digit is tenths, two is hundreths, etc
        nanos = nanos * (int) Math.pow(10, 9 - segmentLen);
    }

    return vf.createFromTime(hours, minutes, seconds, nanos);
}