Android Byte Array to Object Convert getObject(byte[] bytes)

Here you can find the source of getObject(byte[] bytes)

Description

get Object

License

Open Source License

Declaration

public static Object getObject(byte[] bytes) throws IOException,
            ClassNotFoundException 

Method Source Code

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

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

import java.nio.ByteBuffer;

public class Main {
    public static Object getObject(byte[] bytes) throws IOException,
            ClassNotFoundException {
        ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
        ObjectInputStream oi = new ObjectInputStream(bi);
        Object obj = oi.readObject();
        bi.close();//from w  w w  .  j ava 2 s . c  o m
        oi.close();
        return obj;
    }

    public static Object getObject(ByteBuffer byteBuffer)
            throws ClassNotFoundException, IOException {
        InputStream input = new ByteArrayInputStream(byteBuffer.array());
        ObjectInputStream oi = new ObjectInputStream(input);
        Object obj = oi.readObject();
        input.close();
        oi.close();
        byteBuffer.clear();
        return obj;
    }
}

Related

  1. ByteToObject(byte[] bytes)
  2. byteToObject(byte[] bytes)