Java Object Serialize and Deserialize serializeAndDeserialize(T obj)

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

Description

serialize And Deserialize

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
    public static <T> T serializeAndDeserialize(T obj) throws IOException, ClassNotFoundException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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 obj) throws IOException, ClassNotFoundException {
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        try (ObjectOutputStream out = new ObjectOutputStream(buf)) {
            out.writeObject(obj);//from   w w  w  .  j a v a2s.  c  om
        }
        try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buf.toByteArray()))) {
            return (T) in.readObject();
        }
    }
}

Related

  1. serializeAndDeserialize(Object o)
  2. serializeAndDeserialize(Object o)
  3. serializeAndDeserialize(Object obj)
  4. serializeAndDeserialize(T input)
  5. serializeAndDeserialize(T instance)
  6. serializeAndDeserialize(T object)
  7. serializeAndDeserialize(T object)
  8. serializeAndDeserialize(T object)
  9. serializeAndDeserializeObject(Object myObject)