Convert bytes to ArrayList - Android File Input Output

Android examples for File Input Output:Byte Array Convert

Description

Convert bytes to ArrayList

Demo Code


//package com.book2s;
import java.io.ByteArrayInputStream;

import java.io.IOException;
import java.io.ObjectInputStream;

import java.util.ArrayList;

import android.util.Log;

public class Main {
    public static final String LOGERR = "ERR";

    /**//from   w  w  w  .  ja va 2  s.  c  o m
     * Convert bytes to ArrayList<long>
     * @param blob
     * @return
     */
    @SuppressWarnings("unchecked")
    public static ArrayList<Long> byte2ArrayList(byte[] blob) {

        return (ArrayList<Long>) byte2Object(blob);
    }

    /**
     * Convert bytes to Object
     * @param blob
     * @return
     */
    public static Object byte2Object(byte[] blob) {
        Object obj = new Object();

        ObjectInputStream bin;
        try {
            bin = new ObjectInputStream(new ByteArrayInputStream(blob));
            obj = bin.readObject();
        } catch (IOException e) {
            Log.e(LOGERR, "Unable to convert bytes to ArrayList<String> ",
                    e);
        } catch (ClassNotFoundException e) {
            Log.e(LOGERR, "Unable to convert bytes to ArrayList<String> ",
                    e);
        }

        return obj;
    }
}

Related Tutorials