1+ package io .cloudquery .scalar ;
2+
3+ import org .apache .arrow .vector .types .pojo .ArrowType ;
4+ import org .apache .commons .codec .binary .Base64 ;
5+
6+ import java .util .Arrays ;
7+
8+ public class Binary implements Scalar {
9+ protected byte [] value ;
10+ protected boolean valid ;
11+
12+ public Binary () {
13+ }
14+
15+ public Binary (Object value ) throws ValidationException {
16+ this .set (value );
17+ }
18+
19+ @ Override
20+ public String toString () {
21+ if (this .valid ) {
22+ return Base64 .encodeBase64String (this .value );
23+ }
24+ return NULL_VALUE_STRING ;
25+ }
26+
27+ @ Override
28+ public boolean isValid () {
29+ return this .valid ;
30+ }
31+
32+ @ Override
33+ public ArrowType dataType () {
34+ return ArrowType .Binary .INSTANCE ;
35+ }
36+
37+ @ Override
38+ public void set (Object value ) throws ValidationException {
39+ if (value == null ) {
40+ this .valid = false ;
41+ this .value = null ;
42+ return ;
43+ }
44+
45+ if (value instanceof Scalar scalar ) {
46+ if (!scalar .isValid ()) {
47+ this .valid = false ;
48+ this .value = null ;
49+ return ;
50+ }
51+
52+ this .set (scalar .get ());
53+ return ;
54+ }
55+
56+ if (value instanceof byte [] bytes ) {
57+ this .valid = true ;
58+ this .value = bytes ;
59+ return ;
60+ }
61+
62+ if (value instanceof String string ) {
63+ this .valid = true ;
64+ this .value = Base64 .decodeBase64 (string );
65+ return ;
66+ }
67+
68+ if (value instanceof char [] chars ) {
69+ this .valid = true ;
70+ this .value = Base64 .decodeBase64 (new String (chars ));
71+ return ;
72+ }
73+
74+ throw new ValidationException (ValidationException .NO_CONVERSION_AVAILABLE , this .dataType (), value );
75+ }
76+
77+ @ Override
78+ public Object get () {
79+ if (this .valid ) {
80+ return this .value ;
81+ }
82+ return null ;
83+ }
84+
85+ @ Override
86+ public boolean equals (Object other ) {
87+ if (other == null ) {
88+ return false ;
89+ }
90+
91+ if (!(other instanceof Binary o )) {
92+ return false ;
93+ }
94+
95+ return (this .valid && o .valid ) && Arrays .equals (this .value , o .value );
96+ }
97+ }
0 commit comments