Java Object Serialize and Deserialize serializeAndDeserialize(Object o)

Here you can find the source of serializeAndDeserialize(Object o)

Description

Serializes and deserializes the given object.

License

Apache License

Parameter

Parameter Description
o an <code>Object</code>.

Exception

Parameter Description
IOException an exception
ClassNotFoundException an exception

Return

an Object.

Declaration

public static Object serializeAndDeserialize(Object o) throws IOException, ClassNotFoundException 

Method Source Code


//package com.java2s;
/*/*from w ww  .j av  a 2  s  . c o m*/
 * #%L
 * JavaUtil
 * %%
 * Copyright (C) 2012 - 2013 Emory University
 * %%
 * 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.
 * #L%
 */

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

public class Main {
    /**
     * Serializes and deserializes the given object.
     * 
     * @param o
     *            an <code>Object</code>.
     * @return an <code>Object</code>.
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static Object serializeAndDeserialize(Object o) throws IOException, ClassNotFoundException {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bytes);
        try {
            out.writeObject(o);
        } finally {
            out.close();
        }

        ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()));
        try {
            Object result = in.readObject();
            return result;
        } finally {
            in.close();
        }
    }
}

Related

  1. deserializeRaw(byte[] array)
  2. deserializeSafe(byte[] buf, T obj)
  3. deserializeShort(byte[] inbuf, int offset)
  4. deserializeStream(final String witness)
  5. serializeAndDeserialize(Object o)
  6. serializeAndDeserialize(Object obj)
  7. serializeAndDeserialize(T input)
  8. serializeAndDeserialize(T instance)
  9. serializeAndDeserialize(T obj)