// Path: core/src/main/java/org/jeeventstore/EventSerializer.java
// public interface EventSerializer {
//     
//     /**
//      * Serializes a list of events into a {@link String} representation.
//      * 
//      * @param events  the list of events to be serialized, not null
//      * @return  the {@link String} representation of the events
//      */
//     String serialize(List<? extends Serializable> events);
// 
//     /**
//      * Deserializes the String provided and reconstructs the corresponding 
//      * list of events.
//      * @param body  the {@link String} from which the list of events will be reconstructed, not null
//      * @return  the reconstructed list
//      */
//     List<? extends Serializable> deserialize(String body);
// 
// }


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 java.util.List;
import javax.xml.bind.DatatypeConverter;
import org.jeeventstore.EventSerializer;
/*
 * Copyright (c) 2013 Red Rainbow IT Solutions GmbH, Germany
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package org.jeeventstore.serialization;


/**
 * A simple serializer for tests using the Object*Stream api.
 * Encodes the serialized stream as Base64, which makes
 * the output hard to parse.
 */
public class JavaSerializer implements EventSerializer {

    @Override
    public String serialize(List<? extends Serializable> events) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(events);
            oos.flush();
            return DatatypeConverter.printHexBinary(baos.toByteArray());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<? extends Serializable> deserialize(String body) {
        try {
            byte bodybytes[] = DatatypeConverter.parseHexBinary(body);
            ByteArrayInputStream bais = new ByteArrayInputStream(bodybytes);
            ObjectInputStream si = new ObjectInputStream(bais);
// Path: core/src/main/java/org/jeeventstore/EventSerializer.java
// public interface EventSerializer {
//     
//     /**
//      * Serializes a list of events into a {@link String} representation.
//      * 
//      * @param events  the list of events to be serialized, not null
//      * @return  the {@link String} representation of the events
//      */
//     String serialize(List<? extends Serializable> events);
// 
//     /**
//      * Deserializes the String provided and reconstructs the corresponding 
//      * list of events.
//      * @param body  the {@link String} from which the list of events will be reconstructed, not null
//      * @return  the reconstructed list
//      */
//     List<? extends Serializable> deserialize(String body);
// 
// }


// Path: testhelpers/src/main/java/org/jeeventstore/serialization/JavaSerializer.java
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 java.util.List;
import javax.xml.bind.DatatypeConverter;
import org.jeeventstore.EventSerializer;

/*
 * Copyright (c) 2013 Red Rainbow IT Solutions GmbH, Germany
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package org.jeeventstore.serialization;


/**
 * A simple serializer for tests using the Object*Stream api.
 * Encodes the serialized stream as Base64, which makes
 * the output hard to parse.
 */
public class JavaSerializer implements EventSerializer {

    @Override
    public String serialize(List<? extends Serializable> events) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(events);
            oos.flush();
            return DatatypeConverter.printHexBinary(baos.toByteArray());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<? extends Serializable> deserialize(String body) {
        try {
            byte bodybytes[] = DatatypeConverter.parseHexBinary(body);
            ByteArrayInputStream bais = new ByteArrayInputStream(bodybytes);
            ObjectInputStream si = new ObjectInputStream(bais);
            return (List<? extends Serializable>) si.readObject();