Skip to content

Instantly share code, notes, and snippets.

@aprock
Created March 14, 2012 17:00
Show Gist options
  • Save aprock/2037883 to your computer and use it in GitHub Desktop.
Save aprock/2037883 to your computer and use it in GitHub Desktop.
manually serialize/deserialize android bundle
private String serializeBundle(final Bundle bundle) {
String base64 = null;
final Parcel parcel = Parcel.obtain();
try {
parcel.writeBundle(bundle);
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
final GZIPOutputStream zos = new GZIPOutputStream(new BufferedOutputStream(bos));
zos.write(parcel.marshall());
zos.close();
base64 = Base64.encodeToString(bos.toByteArray(), 0);
} catch(IOException e) {
e.printStackTrace();
base64 = null;
} finally {
parcel.recycle();
}
return base64;
}
private Bundle deserializeBundle(final String base64) {
Bundle bundle = null;
final Parcel parcel = Parcel.obtain();
try {
final ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
final byte[] buffer = new byte[1024];
final GZIPInputStream zis = new GZIPInputStream(new ByteArrayInputStream(Base64.decode(base64, 0)));
int len = 0;
while ((len = zis.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
zis.close();
parcel.unmarshall(byteBuffer.toByteArray(), 0, byteBuffer.size());
parcel.setDataPosition(0);
bundle = parcel.readBundle();
} catch (IOException e) {
e.printStackTrace();
bundle = null;
} finally {
parcel.recycle();
}
return bundle;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment