Java Object Serialize and Deserialize serializeAndDeserialize(T instance)

Here you can find the source of serializeAndDeserialize(T instance)

Description

serialize And Deserialize

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
    public static <T> T serializeAndDeserialize(T instance) throws Exception 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013 EclipseSource.// w w w . ja v  a2 s.c  om
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Ralf Sternberg - initial implementation and API
 ******************************************************************************/

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class Main {
    @SuppressWarnings("unchecked")
    public static <T> T serializeAndDeserialize(T instance) throws Exception {
        return (T) deserialize(serialize(instance));
    }

    public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
        return new ObjectInputStream(inputStream).readObject();
    }

    public static byte[] serialize(Object object) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        new ObjectOutputStream(outputStream).writeObject(object);
        return outputStream.toByteArray();
    }
}

Related

  1. deserializeStream(final String witness)
  2. serializeAndDeserialize(Object o)
  3. serializeAndDeserialize(Object o)
  4. serializeAndDeserialize(Object obj)
  5. serializeAndDeserialize(T input)
  6. serializeAndDeserialize(T obj)
  7. serializeAndDeserialize(T object)
  8. serializeAndDeserialize(T object)
  9. serializeAndDeserialize(T object)