@@ -56,12 +56,16 @@ public static UTF8String fromString(String str) {
56
56
* Updates the UTF8String with String.
57
57
*/
58
58
public UTF8String set (final String str ) {
59
- try {
60
- bytes = str .getBytes ("utf-8" );
61
- } catch (UnsupportedEncodingException e ) {
62
- // Turn the exception into unchecked so we can find out about it at runtime, but
63
- // don't need to add lots of boilerplate code everywhere.
64
- PlatformDependent .throwException (e );
59
+ if (str == null ) {
60
+ bytes = new byte [0 ];
61
+ } else {
62
+ try {
63
+ bytes = str .getBytes ("utf-8" );
64
+ } catch (UnsupportedEncodingException e ) {
65
+ // Turn the exception into unchecked so we can find out about it at runtime, but
66
+ // don't need to add lots of boilerplate code everywhere.
67
+ PlatformDependent .throwException (e );
68
+ }
65
69
}
66
70
return this ;
67
71
}
@@ -70,7 +74,7 @@ public UTF8String set(final String str) {
70
74
* Updates the UTF8String with byte[], which should be encoded in UTF-8.
71
75
*/
72
76
public UTF8String set (final byte [] bytes ) {
73
- this .bytes = bytes ;
77
+ this .bytes = ( bytes != null ) ? bytes : new byte [ 0 ] ;
74
78
return this ;
75
79
}
76
80
@@ -185,6 +189,7 @@ public UTF8String clone() {
185
189
186
190
@ Override
187
191
public int compareTo (final UTF8String other ) {
192
+ if (other == null ) return 1 ;
188
193
final byte [] b = other .getBytes ();
189
194
for (int i = 0 ; i < bytes .length && i < b .length ; i ++) {
190
195
int res = bytes [i ] - b [i ];
0 commit comments