Convert byte array To Object via ObjectInputStream - Android java.io

Android examples for java.io:ObjectInputStream

Description

Convert byte array To Object via ObjectInputStream

Demo Code

import android.content.Context;
import android.text.TextUtils;
import android.widget.TextView;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    public static Object bytesToObject(byte[] bytes) {
        if (bytes == null) {
            return null;
        }/* w w w .  ja v a2 s .com*/
        Object obj = null;
        try {
            // bytearray to object
            ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
            ObjectInputStream oi = new ObjectInputStream(bi);
            obj = oi.readObject();
            bi.close();
            oi.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }

}

Related Tutorials