Package org.directmemory.serialization

Source Code of org.directmemory.serialization.StandardSerializer

package org.directmemory.serialization;

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

import org.javasimon.SimonManager;
import org.javasimon.Split;
import org.javasimon.Stopwatch;

public class StandardSerializer implements Serializer {
 
  public byte[] serialize(Object obj, @SuppressWarnings("rawtypes") Class clazz) throws IOException {
        Stopwatch stopWatch = SimonManager.getStopwatch("serializer.javaSerialize");
    Split split = stopWatch.start();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(obj);
    oos.flush();
    oos.close();
    split.stop();
    return baos.toByteArray();   
  }

  public Serializable deserialize(byte[] source, @SuppressWarnings("rawtypes") Class clazz) throws IOException, ClassNotFoundException {
        Stopwatch stopWatch = SimonManager.getStopwatch("serializer.javaDeserialize");
    Split split = stopWatch.start();
    ByteArrayInputStream bis = new ByteArrayInputStream(source);
    ObjectInputStream ois = new ObjectInputStream(bis);
    Serializable obj = (Serializable) ois.readObject();
    ois.close();
    split.stop();
    return obj;
  }
}
TOP

Related Classes of org.directmemory.serialization.StandardSerializer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.