Class SafeFloatParser

java.lang.Object
org.jruby.util.SafeFloatParser

public final class SafeFloatParser extends Object
A safer way to parse float values

Prevents brute force attacks using the famous Java bug.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected static final BigDecimal
    Lower allowed value
    protected static final BigDecimal
    The middle of the bad interval - used for rounding bad values
    protected static final BigDecimal
    Constant 2
    protected static final BigDecimal
    Upper allowed value
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected static final double
    Safe way of getting the double value
    prevents BigDecimal from calling Double.parseDouble()
    protected static final double
    Safe way of getting the double value
    Prevents BigDecimal from calling Double.parseDouble()
    protected static final Double
    Safe parsing of a String into a Double
    static float
    Safe way of getting the float value
    prevents BigDecimal from calling Float.parseFloat()
    static float
    floatValue(BigDecimal bigDecimal)
    Safe way of getting the float value
    Prevents BigDecimal from calling Float.parseFloat()
    protected static final boolean
    Heuristic test if we should look closer at the value
    static Float
    Safe way of parsing a Float value from a String
    static Float
    Safe way of parsing a Float value from a String

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • TWO

      protected static final BigDecimal TWO
      Constant 2
    • LOWER

      protected static final BigDecimal LOWER
      Lower allowed value
    • UPPER

      protected static final BigDecimal UPPER
      Upper allowed value
    • MIDDLE

      protected static final BigDecimal MIDDLE
      The middle of the bad interval - used for rounding bad values
  • Constructor Details

    • SafeFloatParser

      public SafeFloatParser()
  • Method Details

    • valueOf

      public static Float valueOf(String s)
      Safe way of parsing a Float value from a String
      Parameters:
      s - The input String
      Returns:
      the Float value
    • parseFloat

      public static Float parseFloat(String s)
      Safe way of parsing a Float value from a String
      Parameters:
      s - The input String
      Returns:
      the Float value
    • floatValue

      public static float floatValue(Number number)
      Safe way of getting the float value
      prevents BigDecimal from calling Float.parseFloat()
      Parameters:
      number -
      Returns:
      the float value
    • floatValue

      public static float floatValue(BigDecimal bigDecimal)
      Safe way of getting the float value
      Prevents BigDecimal from calling Float.parseFloat()
      Parameters:
      bigDecimal -
      Returns:
      the float value
    • isSuspicious

      protected static final boolean isSuspicious(String s)
      Heuristic test if we should look closer at the value
      Parameters:
      s - The non-null input String
      Returns:
      true if the value is suspicious, false otherwise
    • decimalValueOf

      protected static final Double decimalValueOf(String s)
      Safe parsing of a String into a Double
      Parameters:
      s - The input String, can be null
      Returns:
      The Double value
    • decimalValue

      protected static final double decimalValue(Number number)
      Safe way of getting the double value
      prevents BigDecimal from calling Double.parseDouble()
      Parameters:
      number -
      Returns:
      the double value
    • decimalValue

      protected static final double decimalValue(BigDecimal bigDecimal)
      Safe way of getting the double value
      Prevents BigDecimal from calling Double.parseDouble()
      Parameters:
      bigDecimal -
      Returns:
      the double value