Java Object Serialize serialize(T t)

Here you can find the source of serialize(T t)

Description

serialize

License

Apache License

Declaration

@SuppressWarnings("unchecked")
    public static <T extends Serializable> T serialize(T t) 

Method Source Code

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

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

import java.io.Serializable;

public class Main {
    @SuppressWarnings("unchecked")
    public static <T extends Serializable> T serialize(T t) {
        try {//  w w  w. ja v a  2s.  c o  m
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(t);
            oos.close();
            final byte[] ba = baos.toByteArray();
            baos.close();
            ByteArrayInputStream bais = new ByteArrayInputStream(ba);
            ObjectInputStream ois = new ObjectInputStream(bais);
            Object returnValue = ois.readObject();
            ois.close();
            return (T) returnValue;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. serialize(T obj)
  2. serialize(T obj)
  3. serialize(T object)
  4. serialize(T object)
  5. serialize(T object)
  6. serialize(T t, String filename)
  7. serialize(T t, String path)
  8. serialize(T toSerialize)
  9. serialized(final Object item)