Convert bytes to HashMap - Android java.util

Android examples for java.util:Map

Description

Convert bytes to HashMap

Demo Code


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

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

import java.util.HashMap;
import android.util.Log;

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

    /**//from w  w w  .jav a  2 s.  c o m
     * Convert bytes to HashMap<String, Object>
     * @param blob
     * @return
     */
    @SuppressWarnings("unchecked")
    public static HashMap<String, Object> byte2HashMap(byte[] blob) {

        return (HashMap<String, Object>) 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