Convert byte array to Serializable object. - Android java.lang

Android examples for java.lang:array convert

Description

Convert byte array to Serializable object.

Demo Code

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;

import android.util.Log;

public class Main {

  private static final String TAG = "ArrayUtil";

  /**//from   w ww .j av  a 2 s .  com
   * Convert byte array to Serializable object.
   * 
   * @param data
   * @return
   */
  public static Object byteArrayToObject(byte[] data) {
    Object result = null;
    try {
      ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
      ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
      result = objectInputStream.readObject();

      byteArrayInputStream.close();
      objectInputStream.close();
    } catch (Exception e) {
      Log.e(TAG, "byteArrayToObject() error: " + e);
    }
    return result;
  }

}

Related Tutorials