Java Class Load from File deserialize(Class expectedType, byte[] data)

Here you can find the source of deserialize(Class expectedType, byte[] data)

Description

deserialize

License

Apache License

Declaration

@SuppressWarnings("unchecked")
    public static <T> T deserialize(Class<? super T> expectedType, byte[] data) 

Method Source Code

//package com.java2s;
/**//ww w  . j  a  v  a2  s.  co m
 * Copyright 2012-2013 Niall Gallagher
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.ByteArrayInputStream;

import java.io.ObjectInputStream;

public class Main {
    @SuppressWarnings("unchecked")
    public static <T> T deserialize(Class<? super T> expectedType, byte[] data) {
        try {
            ObjectInputStream out = new ObjectInputStream(new ByteArrayInputStream(data));
            Object o = out.readObject();
            out.close();
            if (!(expectedType.isAssignableFrom(o.getClass()))) {
                throw new IllegalStateException("Unexpected type: " + o.getClass());
            }
            return (T) o;
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }
}

Related

  1. deserialize(Class classOfT, byte[] bytes)
  2. deserialize(Class clazz, File file)
  3. deserialize(Class clazz, InputStream in)
  4. deserialize(Class type, @Nullable byte[] objectBytes)