Java Byte Array Decode decode(byte[] bytes)

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

Description

decode

License

Apache License

Declaration

@SuppressWarnings("unchecked")
    public static final <T extends Serializable> T decode(byte[] bytes) 

Method Source Code


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

import java.io.ByteArrayInputStream;

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

import java.io.Serializable;

public class Main {
    @SuppressWarnings("unchecked")
    public static final <T extends Serializable> T decode(byte[] bytes) {
        if (bytes == null) {
            return null;
        }/*w  w  w  .  j  av a2  s.  c om*/
        T t = null;
        Exception thrown = null;
        try {
            ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(bytes));
            t = (T) oin.readObject();
        } catch (IOException e) {
            e.printStackTrace();
            thrown = e;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            thrown = e;
        } catch (ClassCastException e) {
            e.printStackTrace();
            thrown = e;
        } finally {
            if (null != thrown)
                throw new RuntimeException("Error decoding byte[] data to instantiate java object - "
                        + "data at key may not have been of this type or even an object", thrown);
        }
        return t;
    }
}

Related

  1. decode(byte[] b)
  2. decode(byte[] bytes)
  3. decode(byte[] bytes)
  4. decode(byte[] msgToDecode)
  5. decode(byte[] source)