1616
1717package za .co .absa .cobrix .cobol .parser .decoders
1818
19- import scodec .Codec
20- import scodec .bits .BitVector
21-
19+ import java .nio .{ByteBuffer , ByteOrder }
2220import scala .util .control .NonFatal
2321
2422object FloatingPointDecoders {
25- private val floatB : Codec [Float ] = scodec.codecs.float
26- private val floatL : Codec [Float ] = scodec.codecs.floatL
27- private val doubleB : Codec [Double ] = scodec.codecs.double
28- private val doubleL : Codec [Double ] = scodec.codecs.doubleL
29-
3023 private val BIT_COUNT_MAGIC = 0x000055AFL
3124
25+ /**
26+ * A decoder for IEEE-754 32 bit big endian floats
27+ *
28+ * @param bytes A byte array that represents the binary data
29+ * @return A boxed float
30+ */
31+ def decodeFloatB (bytes : Array [Byte ]): Float = {
32+ require(bytes.length == 4 , " Input must be exactly 4 bytes for a 32-bit float" )
33+
34+ val byteBuffer = ByteBuffer .wrap(bytes)
35+ byteBuffer.order(ByteOrder .BIG_ENDIAN )
36+ byteBuffer.getFloat
37+ }
38+
39+ /**
40+ * A decoder for IEEE-754 32 bit little endian floats
41+ *
42+ * @param bytes A byte array that represents the binary data
43+ * @return A boxed float
44+ */
45+ def decodeFloatL (bytes : Array [Byte ]): Float = {
46+ require(bytes.length == 4 , " Input must be exactly 4 bytes for a 32-bit float" )
47+
48+ val byteBuffer = ByteBuffer .wrap(bytes)
49+ byteBuffer.order(ByteOrder .LITTLE_ENDIAN )
50+ byteBuffer.getFloat
51+ }
52+
53+ /**
54+ * A decoder for IEEE-754 64 bit big endian floats
55+ *
56+ * @param bytes A byte array that represents the binary data
57+ * @return A boxed float
58+ */
59+ def decodeDoubleB (bytes : Array [Byte ]): Double = {
60+ require(bytes.length == 8 , " Input must be exactly 8 bytes for a 64-bit float" )
61+
62+ val byteBuffer = ByteBuffer .wrap(bytes)
63+ byteBuffer.order(ByteOrder .BIG_ENDIAN )
64+ byteBuffer.getDouble
65+ }
66+
67+ /**
68+ * A decoder for IEEE-754 64 bit little endian floats
69+ *
70+ * @param bytes A byte array that represents the binary data
71+ * @return A boxed float
72+ */
73+ def decodeDoubleL (bytes : Array [Byte ]): Double = {
74+ require(bytes.length == 8 , " Input must be exactly 8 bytes for a 64-bit float" )
75+
76+ val byteBuffer = ByteBuffer .wrap(bytes)
77+ byteBuffer.order(ByteOrder .LITTLE_ENDIAN )
78+ byteBuffer.getDouble
79+ }
80+
3281 /** Decode IEEE754 single precision big endian encoded number. */
3382 def decodeIeee754SingleBigEndian (bytes : Array [Byte ]): java.lang.Float = {
3483 try {
35- floatB.decode( BitVector ( bytes)).require.value
84+ decodeFloatB( bytes)
3685 } catch {
3786 case NonFatal (_) => null
3887 }
@@ -41,7 +90,7 @@ object FloatingPointDecoders {
4190 /** Decode IEEE754 double precision big endian encoded number. */
4291 def decodeIeee754DoubleBigEndian (bytes : Array [Byte ]): java.lang.Double = {
4392 try {
44- doubleB.decode( BitVector ( bytes)).require.value
93+ decodeDoubleB( bytes)
4594 } catch {
4695 case NonFatal (_) => null
4796 }
@@ -50,7 +99,7 @@ object FloatingPointDecoders {
5099 /** Decode IEEE754 single precision little endian encoded number. */
51100 def decodeIeee754SingleLittleEndian (bytes : Array [Byte ]): java.lang.Float = {
52101 try {
53- floatL.decode( BitVector ( bytes)).require.value
102+ decodeFloatL( bytes)
54103 } catch {
55104 case NonFatal (_) => null
56105 }
@@ -59,7 +108,7 @@ object FloatingPointDecoders {
59108 /** Decode IEEE754 double precision little endian encoded number. */
60109 def decodeIeee754DoubleLittleEndian (bytes : Array [Byte ]): java.lang.Double = {
61110 try {
62- doubleL.decode( BitVector ( bytes)).require.value
111+ decodeDoubleL( bytes)
63112 } catch {
64113 case NonFatal (_) => null
65114 }
0 commit comments