Search in sources :

Example 6 with NSData

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;
}
Also used : NSRange(com.webobjects.foundation.NSRange) NSData(com.webobjects.foundation.NSData) NSMutableData(com.webobjects.foundation.NSMutableData)

Aggregations

NSData (com.webobjects.foundation.NSData)6 JSONException (org.json.JSONException)3 JSONObject (org.json.JSONObject)3 NSMutableData (com.webobjects.foundation.NSMutableData)2 InputStream (java.io.InputStream)2 UnmarshallException (org.jabsorb.serializer.UnmarshallException)2 WOActionResults (com.webobjects.appserver.WOActionResults)1 NSRange (com.webobjects.foundation.NSRange)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 MarshallException (org.jabsorb.serializer.MarshallException)1