* default value if object is null
* @return NSData evaluation of the given object
*
*/
public static NSData dataValueWithDefault(Object obj, NSData def) {
NSData value = def;
if (!ERXValueUtilities.isNull(obj)) {
if (obj instanceof NSData) {
value = (NSData) obj;
} else if (obj instanceof byte[]) {
byte[] byteValue = (byte[]) obj;
value = new NSData(byteValue, new NSRange(0, byteValue.length), true);
} else if (obj instanceof String) {
String strValue = ((String) obj).trim();
if (strValue.length() > 0) {
Object objValue = NSPropertyListSerialization.propertyListFromString(strValue); // MS:
// Encoding?
if (objValue == null || !(objValue instanceof NSData)) {
throw new IllegalArgumentException("Failed to parse data from the value '" + obj + "'.");
}
value = (NSData) objValue;
if (value instanceof NSMutableData) {
// AK: we need NSData if we want to use it for a PK, but
// we get NSMutableData
value = new NSData(value);
}
}
} else {
throw new IllegalArgumentException("Failed to parse data from the value '" + obj + "'.");
}