Java Class Load from File deserialize(Class type, @Nullable byte[] objectBytes)

Here you can find the source of deserialize(Class type, @Nullable byte[] objectBytes)

Description

Turns a byte array into an object.

License

Open Source License

Return

deserialized object or null if objectBytes is null

Declaration

@Nullable
public static <T> T deserialize(Class<T> type, @Nullable byte[] objectBytes) 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.io.BaseEncoding.base16;
import java.io.ByteArrayInputStream;

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

import javax.annotation.Nullable;

public class Main {
    /**/*from w  w w.  j a v a2  s.com*/
     * Turns a byte array into an object.
     *
     * @return deserialized object or {@code null} if {@code objectBytes} is {@code null}
     */
    @Nullable
    public static <T> T deserialize(Class<T> type, @Nullable byte[] objectBytes) {
        checkNotNull(type);
        if (objectBytes == null) {
            return null;
        }
        try {
            return type.cast(new ObjectInputStream(new ByteArrayInputStream(objectBytes)).readObject());
        } catch (ClassNotFoundException | IOException e) {
            throw new IllegalArgumentException("Unable to deserialize: objectBytes=" + base16().encode(objectBytes),
                    e);
        }
    }
}

Related

  1. deserialize(Class expectedType, byte[] data)
  2. deserialize(Class classOfT, byte[] bytes)
  3. deserialize(Class clazz, File file)
  4. deserialize(Class clazz, InputStream in)