Android examples for Android OS:Bundle
A helper method that attempts to serialize all of the objects inside the Bundle.
/*// ww w. jav a 2s . c o m * Copyright 2011 two forty four a.m. LLC <http://www.twofortyfouram.com> * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the * License. You may obtain a copy of the License at <http://www.apache.org/licenses/LICENSE-2.0> * * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ import android.location.Location; import android.os.Bundle; import android.test.AndroidTestCase; import android.test.suitebuilder.annotation.SmallTest; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.ArrayList; public class Main{ /** * A helper method that attempts to serialize all of the objects inside the Bundle. This helps test that a Bundle only * contains objects that can be serialized and doesn't contain Parcelable objects. */ private static void checkBundle(final Bundle bundle) { ObjectOutputStream objectOut = null; // needs to be closed final ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); // closing has no effect try { try { objectOut = new ObjectOutputStream(byteOut); } catch (final IOException e) { throw new RuntimeException(); } for (final String key : bundle.keySet()) { final Object value = bundle.get(key); if (value instanceof Bundle) { // recursively serialize assertSerializable(bundle); } else if (value instanceof Serializable) // allows objects that implement both parcelable and serializable { try { ClassLoader.getSystemClassLoader().loadClass( value.getClass().getName()); } catch (final ClassNotFoundException e) { throw new RuntimeException( String.format( "Object associated with key %s is not available to the Android ClassLoader", key)); //$NON-NLS-1$ } try { objectOut.writeObject(bundle.get(key)); } catch (final IOException e) { throw new RuntimeException( String.format( "Object associated with key %s couldn't be serialized", key)); //$NON-NLS-1$ } } else if (value == null) { // null values are acceptable } else { throw new RuntimeException( String.format( "Key \"%s\"'s value %s isn't Serializable. Only primitives or objects implementing Serializable can be stored. Parcelable is not stable for long-term storage.", key, bundle.get(key))); //$NON-NLS-1$ .toString())); } } } finally { if (objectOut != null) { try { objectOut.close(); } catch (final IOException e) { throw new RuntimeException( "Internal failure for test case"); //$NON-NLS-1$ } } } } /** * Asserts whether a Bundle is serializable across processes. * * @param bundle bundle to check. Cannot be null. */ public static void assertSerializable(final Bundle bundle) { if (bundle == null) { throw new IllegalArgumentException("bundle cannot be null"); //$NON-NLS-1$ } try { checkBundle(bundle); } catch (final Exception e) { fail(e.getMessage()); } } }