public static int compareToIgnoreSpaces(byte[] byteArray, char[] anotherCharArray) {

if (byteArray == null && anotherCharArray == null) {

return 0;

} else if (byteArray == null) {

return -1;

} else if (anotherCharArray == null) {

return 1;

}

if (byteArray.length == 0 && anotherCharArray.length == 0) {

return 0;

} else if (byteArray.length == 0) {

return -1;

} else if (anotherCharArray.length == 0) {

return 1;

}

// we only ignore end white spaces

int startIndex0 = 0;

int endIndex0 = byteArray.length - 1;

if (startIndex0 < byteArray.length) {

// charArray has non whitespace char

while (endIndex0 >= 0) {

char c = (char) (byteArray[endIndex0] & 0xFF);

if (c == ' ') {

endIndex0--;

} else {

break;

}

}

}

int startIndex1 = 0;

int endIndex1 = anotherCharArray.length - 1;

if (startIndex1 < anotherCharArray.length) {

// anotherCharArray has non whitespace char

while (endIndex1 >= 0) {

char c = anotherCharArray[endIndex1];

if (c == ' ') {

endIndex1--;

} else {

break;

}

}

}

int len1 = endIndex0 - startIndex0 + 1;

int len2 = endIndex1 - startIndex1 + 1;

int n = ((len1 <= len2) ? len1 : len2);

for (int i = 0; i < n; i++) {

char c1 = (char) (byteArray[startIndex0 + i] & 0xFF);

char c2 = anotherCharArray[startIndex1 + i];

if (c1 != c2) {

return c1 - c2;

}

}

return len1 - len2;

}