16
16
17
17
package za .co .absa .cobrix .cobol .parser .decoders
18
18
19
- import scodec .Codec
20
- import scodec .bits .BitVector
21
-
19
+ import java .nio .{ByteBuffer , ByteOrder }
22
20
import scala .util .control .NonFatal
23
21
24
22
object 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
-
30
23
private val BIT_COUNT_MAGIC = 0x000055AFL
31
24
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
+
32
81
/** Decode IEEE754 single precision big endian encoded number. */
33
82
def decodeIeee754SingleBigEndian (bytes : Array [Byte ]): java.lang.Float = {
34
83
try {
35
- floatB.decode( BitVector ( bytes)).require.value
84
+ decodeFloatB( bytes)
36
85
} catch {
37
86
case NonFatal (_) => null
38
87
}
@@ -41,7 +90,7 @@ object FloatingPointDecoders {
41
90
/** Decode IEEE754 double precision big endian encoded number. */
42
91
def decodeIeee754DoubleBigEndian (bytes : Array [Byte ]): java.lang.Double = {
43
92
try {
44
- doubleB.decode( BitVector ( bytes)).require.value
93
+ decodeDoubleB( bytes)
45
94
} catch {
46
95
case NonFatal (_) => null
47
96
}
@@ -50,7 +99,7 @@ object FloatingPointDecoders {
50
99
/** Decode IEEE754 single precision little endian encoded number. */
51
100
def decodeIeee754SingleLittleEndian (bytes : Array [Byte ]): java.lang.Float = {
52
101
try {
53
- floatL.decode( BitVector ( bytes)).require.value
102
+ decodeFloatL( bytes)
54
103
} catch {
55
104
case NonFatal (_) => null
56
105
}
@@ -59,7 +108,7 @@ object FloatingPointDecoders {
59
108
/** Decode IEEE754 double precision little endian encoded number. */
60
109
def decodeIeee754DoubleLittleEndian (bytes : Array [Byte ]): java.lang.Double = {
61
110
try {
62
- doubleL.decode( BitVector ( bytes)).require.value
111
+ decodeDoubleL( bytes)
63
112
} catch {
64
113
case NonFatal (_) => null
65
114
}
0 commit comments