Skip to content

Commit

Permalink
[SPARK-8301] added additional null checks
Browse files Browse the repository at this point in the history
  • Loading branch information
tarekbecker committed Jun 15, 2015
1 parent 79cb55b commit d2fb05f
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions unsafe/src/main/java/org/apache/spark/unsafe/types/UTF8String.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,16 @@ public static UTF8String fromString(String str) {
* Updates the UTF8String with String.
*/
public UTF8String set(final String str) {
try {
bytes = str.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
// Turn the exception into unchecked so we can find out about it at runtime, but
// don't need to add lots of boilerplate code everywhere.
PlatformDependent.throwException(e);
if (str == null) {
bytes = new byte[0];
} else {
try {
bytes = str.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
// Turn the exception into unchecked so we can find out about it at runtime, but
// don't need to add lots of boilerplate code everywhere.
PlatformDependent.throwException(e);
}
}
return this;
}
Expand All @@ -70,7 +74,7 @@ public UTF8String set(final String str) {
* Updates the UTF8String with byte[], which should be encoded in UTF-8.
*/
public UTF8String set(final byte[] bytes) {
this.bytes = bytes;
this.bytes = (bytes != null) ? bytes : new byte[0];
return this;
}

Expand Down Expand Up @@ -185,6 +189,7 @@ public UTF8String clone() {

@Override
public int compareTo(final UTF8String other) {
if (other == null) return 1;
final byte[] b = other.getBytes();
for (int i = 0; i < bytes.length && i < b.length; i++) {
int res = bytes[i] - b[i];
Expand Down

0 comments on commit d2fb05f

Please sign in to comment.