use of com.webobjects.foundation.NSData in project wonder-slim by undur.
the class ERXValueUtilities method dataValueWithDefault.
/**
* Basic utility method for reading <code>NSData</code> values which also
* works with serialized NSData. The default value is used if the object is
* null.
*
* @param obj
* object to be evaluated
* @param def
* 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) {
// MS:
Object objValue = NSPropertyListSerialization.propertyListFromString(strValue);
// 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 + "'.");
}
}
return value;
}
Aggregations