Java Code Examples for com.mysql.cj.exceptions.NumberOutOfRange

The following examples show how to use com.mysql.cj.exceptions.NumberOutOfRange. These examples are extracted from open source projects. 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 Project: quarkus   Source File: MySQLJDBCReflections.java    License: Apache License 2.0 5 votes vote down vote up
@BuildStep
void registerExceptionsForReflection(BuildProducer<ReflectiveClassBuildItem> reflectiveClass) {
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJCommunicationsException.class.getName()));
    reflectiveClass
            .produce(new ReflectiveClassBuildItem(false, false, CJConnectionFeatureNotAvailableException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJOperationNotSupportedException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJTimeoutException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJPacketTooBigException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, AssertionFailedException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, CJOperationNotSupportedException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, ClosedOnExpiredPasswordException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, ConnectionIsClosedException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, DataConversionException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, DataReadException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, DataTruncationException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, DeadlockTimeoutRollbackMarker.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, FeatureNotAvailableException.class.getName()));
    reflectiveClass
            .produce(new ReflectiveClassBuildItem(false, false, InvalidConnectionAttributeException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, NumberOutOfRange.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, OperationCancelledException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, PasswordExpiredException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, PropertyNotModifiableException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, RSAException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, SSLParamsException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, StatementIsClosedException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, StreamingNotifiable.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, UnableToConnectException.class.getName()));
    reflectiveClass
            .produce(new ReflectiveClassBuildItem(false, false, UnsupportedConnectionStringException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, WrongArgumentException.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, "com.mysql.cj.jdbc.MysqlXAException"));
    reflectiveClass
            .produce(new ReflectiveClassBuildItem(false, false, StandardLoadBalanceExceptionChecker.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false, NdbLoadBalanceExceptionChecker.class.getName()));
}
 
Example 2
Source Project: lams   Source File: StringUtils.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static int getInt(byte[] buf, int offset, int endpos) throws NumberFormatException {
    long l = getLong(buf, offset, endpos);
    if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
        throw new NumberOutOfRange(Messages.getString("StringUtils.badIntFormat", new Object[] { StringUtils.toString(buf, offset, endpos - offset) }));
    }
    return (int) l;
}
 
Example 3
@Override
public T createFromLong(long l) {
    if (l < this.min || l > this.max) {
        throw new NumberOutOfRange(Messages.getString("ResultSet.NumberOutOfRange", new Object[] { l, this.targetVf.getTargetTypeName() }));
    }
    return this.targetVf.createFromLong(l);
}
 
Example 4
@Override
public T createFromBigInteger(BigInteger i) {
    if (new BigDecimal(i).compareTo(BigDecimal.valueOf(this.min)) < 0 || new BigDecimal(i).compareTo(BigDecimal.valueOf(this.max)) > 0) {
        throw new NumberOutOfRange(Messages.getString("ResultSet.NumberOutOfRange", new Object[] { i, this.targetVf.getTargetTypeName() }));
    }
    return this.targetVf.createFromBigInteger(i);
}
 
Example 5
@Override
public T createFromDouble(double d) {
    if (d < this.min || d > this.max) {
        throw new NumberOutOfRange(Messages.getString("ResultSet.NumberOutOfRange", new Object[] { d, this.targetVf.getTargetTypeName() }));
    }
    return this.targetVf.createFromDouble(d);
}
 
Example 6
@Override
public T createFromBigDecimal(BigDecimal d) {
    if (d.compareTo(BigDecimal.valueOf(this.min)) < 0 || d.compareTo(BigDecimal.valueOf(this.max)) > 0) {
        throw new NumberOutOfRange(Messages.getString("ResultSet.NumberOutOfRange", new Object[] { d, this.targetVf.getTargetTypeName() }));
    }
    return this.targetVf.createFromBigDecimal(d);
}
 
Example 7
Source Project: lams   Source File: IntegerBoundsEnforcer.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public T createFromLong(long l) {
    if (l < this.min || l > this.max) {
        throw new NumberOutOfRange(
                Messages.getString("ResultSet.NumberOutOfRange", new Object[] { Long.valueOf(l).toString(), this.targetVf.getTargetTypeName() }));
    }
    return this.targetVf.createFromLong(l);
}
 
Example 8
Source Project: lams   Source File: IntegerBoundsEnforcer.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public T createFromBigInteger(BigInteger i) {
    if (i.compareTo(BigInteger.valueOf(this.min)) < 0 || i.compareTo(BigInteger.valueOf(this.max)) > 0) {
        throw new NumberOutOfRange(Messages.getString("ResultSet.NumberOutOfRange", new Object[] { i, this.targetVf.getTargetTypeName() }));
    }
    return this.targetVf.createFromBigInteger(i);
}
 
Example 9
Source Project: lams   Source File: IntegerBoundsEnforcer.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public T createFromDouble(double d) {
    if (d < this.min || d > this.max) {
        throw new NumberOutOfRange(Messages.getString("ResultSet.NumberOutOfRange", new Object[] { d, this.targetVf.getTargetTypeName() }));
    }
    return this.targetVf.createFromDouble(d);
}
 
Example 10
Source Project: lams   Source File: IntegerBoundsEnforcer.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public T createFromBigDecimal(BigDecimal d) {
    if (d.compareTo(BigDecimal.valueOf(this.min)) < 0 || d.compareTo(BigDecimal.valueOf(this.max)) > 0) {
        throw new NumberOutOfRange(Messages.getString("ResultSet.NumberOutOfRange", new Object[] { d, this.targetVf.getTargetTypeName() }));
    }
    return this.targetVf.createFromBigDecimal(d);
}
 
Example 11
Source Project: FoxTelem   Source File: StringUtils.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static int getInt(byte[] buf, int offset, int endpos) throws NumberFormatException {
    long l = getLong(buf, offset, endpos);
    if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
        throw new NumberOutOfRange(Messages.getString("StringUtils.badIntFormat", new Object[] { StringUtils.toString(buf, offset, endpos - offset) }));
    }
    return (int) l;
}
 
Example 12
@Override
public T createFromLong(long l) {
    if (l < this.min || l > this.max) {
        throw new NumberOutOfRange(Messages.getString("ResultSet.NumberOutOfRange", new Object[] { l, this.targetVf.getTargetTypeName() }));
    }
    return this.targetVf.createFromLong(l);
}
 
Example 13
@Override
public T createFromBigInteger(BigInteger i) {
    if (new BigDecimal(i).compareTo(BigDecimal.valueOf(this.min)) < 0 || new BigDecimal(i).compareTo(BigDecimal.valueOf(this.max)) > 0) {
        throw new NumberOutOfRange(Messages.getString("ResultSet.NumberOutOfRange", new Object[] { i, this.targetVf.getTargetTypeName() }));
    }
    return this.targetVf.createFromBigInteger(i);
}
 
Example 14
@Override
public T createFromDouble(double d) {
    if (d < this.min || d > this.max) {
        throw new NumberOutOfRange(Messages.getString("ResultSet.NumberOutOfRange", new Object[] { d, this.targetVf.getTargetTypeName() }));
    }
    return this.targetVf.createFromDouble(d);
}
 
Example 15
@Override
public T createFromBigDecimal(BigDecimal d) {
    if (d.compareTo(BigDecimal.valueOf(this.min)) < 0 || d.compareTo(BigDecimal.valueOf(this.max)) > 0) {
        throw new NumberOutOfRange(Messages.getString("ResultSet.NumberOutOfRange", new Object[] { d, this.targetVf.getTargetTypeName() }));
    }
    return this.targetVf.createFromBigDecimal(d);
}
 
Example 16
@Override
public T createFromLong(long l) {
    if (l < this.min || l > this.max) {
        throw new NumberOutOfRange(
                Messages.getString("ResultSet.NumberOutOfRange", new Object[] { Long.valueOf(l).toString(), this.targetVf.getTargetTypeName() }));
    }
    return this.targetVf.createFromLong(l);
}
 
Example 17
@Override
public T createFromBigInteger(BigInteger i) {
    if (i.compareTo(BigInteger.valueOf(this.min)) < 0 || i.compareTo(BigInteger.valueOf(this.max)) > 0) {
        throw new NumberOutOfRange(Messages.getString("ResultSet.NumberOutOfRange", new Object[] { i, this.targetVf.getTargetTypeName() }));
    }
    return this.targetVf.createFromBigInteger(i);
}
 
Example 18
@Override
public T createFromDouble(double d) {
    if (d < this.min || d > this.max) {
        throw new NumberOutOfRange(Messages.getString("ResultSet.NumberOutOfRange", new Object[] { d, this.targetVf.getTargetTypeName() }));
    }
    return this.targetVf.createFromDouble(d);
}
 
Example 19
@Override
public T createFromBigDecimal(BigDecimal d) {
    if (d.compareTo(BigDecimal.valueOf(this.min)) < 0 || d.compareTo(BigDecimal.valueOf(this.max)) > 0) {
        throw new NumberOutOfRange(Messages.getString("ResultSet.NumberOutOfRange", new Object[] { d, this.targetVf.getTargetTypeName() }));
    }
    return this.targetVf.createFromBigDecimal(d);
}
 
Example 20
@Test
public void testIntValues() {
    ValueFactory<String> vf = new StringValueFactory();
    assertEquals(String.valueOf(Integer.MIN_VALUE), this.valueDecoder.decodeInt4(String.valueOf(Integer.MIN_VALUE).getBytes(), 0, 11, vf));
    assertEquals(String.valueOf(Integer.MAX_VALUE), this.valueDecoder.decodeInt4(String.valueOf(Integer.MAX_VALUE).getBytes(), 0, 10, vf));

    assertEquals(String.valueOf(Integer.MAX_VALUE), this.valueDecoder.decodeUInt4(String.valueOf(Integer.MAX_VALUE).getBytes(), 0, 10, vf));
    assertEquals("2147483648",
            this.valueDecoder.decodeUInt4(BigInteger.valueOf(Integer.MAX_VALUE).add(BigInteger.valueOf(1)).toString().getBytes(), 0, 10, vf));
    try {
        this.valueDecoder.decodeInt4(BigInteger.valueOf(Integer.MAX_VALUE).add(BigInteger.valueOf(1)).toString().getBytes(), 0, 10, vf);
        fail("Exception should be thrown for decodeInt4(Integer.MAX_VALUE + 1)");
    } catch (NumberOutOfRange ex) {
        // expected
    }

    byte[] uint8LessThanMaxLong = "8223372036854775807".getBytes();
    ValueFactory<String> fromLongOnly = new DefaultValueFactory<String>() {
        @Override
        public String createFromLong(long l) {
            return Long.valueOf(l).toString();
        }

        public String getTargetTypeName() {
            return null;
        }
    };
    assertEquals("8223372036854775807", this.valueDecoder.decodeUInt8(uint8LessThanMaxLong, 0, uint8LessThanMaxLong.length, fromLongOnly));
    byte[] uint8MoreThanMaxLong1 = "9223372036854775807".getBytes();
    byte[] uint8MoreThanMaxLong2 = "18223372036854775807".getBytes();
    assertEquals("9223372036854775807", this.valueDecoder.decodeUInt8(uint8MoreThanMaxLong1, 0, uint8MoreThanMaxLong1.length, vf));
    assertEquals("18223372036854775807", this.valueDecoder.decodeUInt8(uint8MoreThanMaxLong2, 0, uint8MoreThanMaxLong2.length, vf));
}
 
Example 21
Source Project: lams   Source File: SQLExceptionsMapping.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static SQLException translateException(Throwable ex, ExceptionInterceptor interceptor) {
    if (ex instanceof SQLException) {
        return (SQLException) ex;

    } else if (ex.getCause() != null && ex.getCause() instanceof SQLException) {
        return (SQLException) ex.getCause();

    } else if (ex instanceof CJCommunicationsException) {
        return SQLError.createCommunicationsException(ex.getMessage(), ex, interceptor);

    } else if (ex instanceof CJConnectionFeatureNotAvailableException) {
        return new ConnectionFeatureNotAvailableException(ex.getMessage(), ex);

    } else if (ex instanceof SSLParamsException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_BAD_SSL_PARAMS, 0, false, ex, interceptor);

    } else if (ex instanceof ConnectionIsClosedException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_CONNECTION_NOT_OPEN, ex, interceptor);

    } else if (ex instanceof InvalidConnectionAttributeException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_INVALID_CONNECTION_ATTRIBUTE, ex, interceptor);

    } else if (ex instanceof UnableToConnectException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_UNABLE_TO_CONNECT_TO_DATASOURCE, ex, interceptor);

    } else if (ex instanceof StatementIsClosedException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, ex, interceptor);

    } else if (ex instanceof WrongArgumentException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, ex, interceptor);

    } else if (ex instanceof StringIndexOutOfBoundsException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, ex, interceptor);

    } else if (ex instanceof NumberOutOfRange) {
        // must come before DataReadException as it's more specific
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_NUMERIC_VALUE_OUT_OF_RANGE, ex, interceptor);

    } else if (ex instanceof DataConversionException) {
        // must come before DataReadException as it's more specific
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_INVALID_CHARACTER_VALUE_FOR_CAST, ex, interceptor);

    } else if (ex instanceof DataReadException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, ex, interceptor);

    } else if (ex instanceof DataTruncationException) {
        return new MysqlDataTruncation(((DataTruncationException) ex).getMessage(), ((DataTruncationException) ex).getIndex(),
                ((DataTruncationException) ex).isParameter(), ((DataTruncationException) ex).isRead(), ((DataTruncationException) ex).getDataSize(),
                ((DataTruncationException) ex).getTransferSize(), ((DataTruncationException) ex).getVendorCode());

    } else if (ex instanceof CJPacketTooBigException) {
        return new PacketTooBigException(ex.getMessage());

    } else if (ex instanceof OperationCancelledException) {
        return new MySQLStatementCancelledException(ex.getMessage());

    } else if (ex instanceof CJTimeoutException) {
        return new MySQLTimeoutException(ex.getMessage());

    } else if (ex instanceof CJOperationNotSupportedException) {
        return new OperationNotSupportedException(ex.getMessage());

    } else if (ex instanceof UnsupportedOperationException) {
        return new OperationNotSupportedException(ex.getMessage());

    } else if (ex instanceof CJException) {
        return SQLError.createSQLException(ex.getMessage(), ((CJException) ex).getSQLState(), ((CJException) ex).getVendorCode(),
                ((CJException) ex).isTransient(), interceptor);

    } else {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_GENERAL_ERROR, ex, interceptor);
    }
}
 
Example 22
Source Project: lams   Source File: StringUtils.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static long getLong(byte[] buf, int offset, int endpos) throws NumberFormatException {
    int base = 10;

    int s = offset;

    /* Skip white space. */
    while (s < endpos && Character.isWhitespace((char) buf[s])) {
        ++s;
    }

    if (s == endpos) {
        throw new NumberFormatException(StringUtils.toString(buf));
    }

    /* Check for a sign. */
    boolean negative = false;

    if ((char) buf[s] == '-') {
        negative = true;
        ++s;
    } else if ((char) buf[s] == '+') {
        ++s;
    }

    /* Save the pointer so we can check later if anything happened. */
    int save = s;

    long cutoff = Long.MAX_VALUE / base;
    long cutlim = (int) (Long.MAX_VALUE % base);

    if (negative) {
        cutlim++;
    }

    boolean overflow = false;
    long i = 0;

    for (; s < endpos; s++) {
        char c = (char) buf[s];

        if (c >= '0' && c <= '9') {
            c -= '0';
        } else if (Character.isLetter(c)) {
            c = (char) (Character.toUpperCase(c) - 'A' + 10);
        } else {
            break;
        }

        if (c >= base) {
            break;
        }

        /* Check for overflow. */
        if ((i > cutoff) || ((i == cutoff) && (c > cutlim))) {
            overflow = true;
        } else {
            i *= base;
            i += c;
        }
    }

    // no digits were parsed after a possible +/-
    if (s == save) {
        throw new NumberFormatException(
                Messages.getString("StringUtils.badIntFormat", new Object[] { StringUtils.toString(buf, offset, endpos - offset) }));
    }

    if (overflow) {
        throw new NumberOutOfRange(Messages.getString("StringUtils.badIntFormat", new Object[] { StringUtils.toString(buf, offset, endpos - offset) }));
    }

    /* Return the result of the appropriate sign. */
    return (negative ? (-i) : i);
}
 
Example 23
public static SQLException translateException(Throwable ex, ExceptionInterceptor interceptor) {
    if (ex instanceof SQLException) {
        return (SQLException) ex;

    } else if (ex.getCause() != null && ex.getCause() instanceof SQLException) {
        return (SQLException) ex.getCause();

    } else if (ex instanceof CJCommunicationsException) {
        return SQLError.createCommunicationsException(ex.getMessage(), ex, interceptor);

    } else if (ex instanceof CJConnectionFeatureNotAvailableException) {
        return new ConnectionFeatureNotAvailableException(ex.getMessage(), ex);

    } else if (ex instanceof SSLParamsException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_BAD_SSL_PARAMS, 0, false, ex, interceptor);

    } else if (ex instanceof ConnectionIsClosedException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_CONNECTION_NOT_OPEN, ex, interceptor);

    } else if (ex instanceof InvalidConnectionAttributeException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_INVALID_CONNECTION_ATTRIBUTE, ex, interceptor);

    } else if (ex instanceof UnableToConnectException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_UNABLE_TO_CONNECT_TO_DATASOURCE, ex, interceptor);

    } else if (ex instanceof StatementIsClosedException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, ex, interceptor);

    } else if (ex instanceof WrongArgumentException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, ex, interceptor);

    } else if (ex instanceof StringIndexOutOfBoundsException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, ex, interceptor);

    } else if (ex instanceof NumberOutOfRange) {
        // must come before DataReadException as it's more specific
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_NUMERIC_VALUE_OUT_OF_RANGE, ex, interceptor);

    } else if (ex instanceof DataConversionException) {
        // must come before DataReadException as it's more specific
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_INVALID_CHARACTER_VALUE_FOR_CAST, ex, interceptor);

    } else if (ex instanceof DataReadException) {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_ILLEGAL_ARGUMENT, ex, interceptor);

    } else if (ex instanceof DataTruncationException) {
        return new MysqlDataTruncation(((DataTruncationException) ex).getMessage(), ((DataTruncationException) ex).getIndex(),
                ((DataTruncationException) ex).isParameter(), ((DataTruncationException) ex).isRead(), ((DataTruncationException) ex).getDataSize(),
                ((DataTruncationException) ex).getTransferSize(), ((DataTruncationException) ex).getVendorCode());

    } else if (ex instanceof CJPacketTooBigException) {
        return new PacketTooBigException(ex.getMessage());

    } else if (ex instanceof OperationCancelledException) {
        return new MySQLStatementCancelledException(ex.getMessage());

    } else if (ex instanceof CJTimeoutException) {
        return new MySQLTimeoutException(ex.getMessage());

    } else if (ex instanceof CJOperationNotSupportedException) {
        return new OperationNotSupportedException(ex.getMessage());

    } else if (ex instanceof UnsupportedOperationException) {
        return new OperationNotSupportedException(ex.getMessage());

    } else if (ex instanceof CJException) {
        return SQLError.createSQLException(ex.getMessage(), ((CJException) ex).getSQLState(), ((CJException) ex).getVendorCode(),
                ((CJException) ex).isTransient(), interceptor);

    } else {
        return SQLError.createSQLException(ex.getMessage(), MysqlErrorNumbers.SQL_STATE_GENERAL_ERROR, ex, interceptor);
    }
}
 
Example 24
Source Project: FoxTelem   Source File: StringUtils.java    License: GNU General Public License v3.0 4 votes vote down vote up
public static long getLong(byte[] buf, int offset, int endpos) throws NumberFormatException {
    int base = 10;

    int s = offset;

    /* Skip white space. */
    while (s < endpos && Character.isWhitespace((char) buf[s])) {
        ++s;
    }

    if (s == endpos) {
        throw new NumberFormatException(StringUtils.toString(buf));
    }

    /* Check for a sign. */
    boolean negative = false;

    if ((char) buf[s] == '-') {
        negative = true;
        ++s;
    } else if ((char) buf[s] == '+') {
        ++s;
    }

    /* Save the pointer so we can check later if anything happened. */
    int save = s;

    long cutoff = Long.MAX_VALUE / base;
    long cutlim = (int) (Long.MAX_VALUE % base);

    if (negative) {
        cutlim++;
    }

    boolean overflow = false;
    long i = 0;

    for (; s < endpos; s++) {
        char c = (char) buf[s];

        if (c >= '0' && c <= '9') {
            c -= '0';
        } else if (Character.isLetter(c)) {
            c = (char) (Character.toUpperCase(c) - 'A' + 10);
        } else {
            break;
        }

        if (c >= base) {
            break;
        }

        /* Check for overflow. */
        if ((i > cutoff) || ((i == cutoff) && (c > cutlim))) {
            overflow = true;
        } else {
            i *= base;
            i += c;
        }
    }

    // no digits were parsed after a possible +/-
    if (s == save) {
        throw new NumberFormatException(
                Messages.getString("StringUtils.badIntFormat", new Object[] { StringUtils.toString(buf, offset, endpos - offset) }));
    }

    if (overflow) {
        throw new NumberOutOfRange(Messages.getString("StringUtils.badIntFormat", new Object[] { StringUtils.toString(buf, offset, endpos - offset) }));
    }

    /* Return the result of the appropriate sign. */
    return (negative ? (-i) : i);
}
 
Example 25
@Test(expected = NumberOutOfRange.class)
public void testOutsideMaxBoundsLong() {
    this.enforcing100Vf.createFromLong(101);
}
 
Example 26
@Test(expected = NumberOutOfRange.class)
public void testOutsideMaxBoundsBigInteger() {
    this.enforcing100Vf.createFromBigInteger(BigInteger.valueOf(101));
}
 
Example 27
@Test(expected = NumberOutOfRange.class)
public void testOutsideMaxBoundsDouble() {
    this.enforcing100Vf.createFromDouble(100.5);
}
 
Example 28
@Test(expected = NumberOutOfRange.class)
public void testOutsideMaxBoundsBigDecimal() {
    this.enforcing100Vf.createFromBigDecimal(BigDecimal.valueOf(101));
}
 
Example 29
@Test(expected = NumberOutOfRange.class)
public void testOutsideMinBoundsLong() {
    this.enforcing100Vf.createFromLong(-101);
}
 
Example 30
@Test(expected = NumberOutOfRange.class)
public void testOutsideMinBoundsBigInteger() {
    this.enforcing100Vf.createFromBigInteger(BigInteger.valueOf(-101));
}