Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.io.ByteArrayInputStream;

import java.io.IOException;

import java.io.ObjectInput;
import java.io.ObjectInputStream;

public class Main {
    /**
     * Convert bytes array to object.
     *
     * @param bytes object bytes
     * @param <T> object class
     * @return object
     */
    public static <T> T fromBytes(byte[] bytes) {
        Object result = null;
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        ObjectInput in = null;
        try {
            in = new ObjectInputStream(bis);
            result = in.readObject();
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (Exception e) {
                }
            }
            if (bis != null) {
                try {
                    bis.close();
                } catch (Exception e) {
                }
            }
        }
        return (T) result;
    }
}