Package org.jboss.serial.data

Source Code of org.jboss.serial.data.LocalMarshalledValue

/*
* JBoss, Home of Professional Open Source
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/

package org.jboss.serial.data;

import org.jboss.serial.objectmetamodel.DataContainer;
import org.jboss.serial.util.StringUtil;

import java.io.*;

/**
* This class is just a test to emulate the MarshalledValue behavior
* $Id: LocalMarshalledValue.java 294 2006-05-20 01:56:07Z csuconic $
*
* @author <a href="mailto:clebert.suconic@jboss.com">Clebert Suconic</a>
*/
public class LocalMarshalledValue implements Externalizable
{

    public LocalMarshalledValue()
    {
    }

    public static LocalMarshalledValue createTestInstance() throws Exception
    {
        return new LocalMarshalledValue(TestParent.createTestInstance());
    }

    DataContainer container;

    public LocalMarshalledValue (Object obj) throws IOException
    {
        container = new DataContainer(true,null);
        ObjectOutput output = container.getOutput();
        output.writeObject(obj);
    }

    /**
     * The object has to be unserialized only when the first get is executed.
     */
    public Object get() throws IOException, ClassNotFoundException {
        try
        {
            container.getCache().setLoader(Thread.currentThread().getContextClassLoader());
            return container.getInput().readObject();
        }
        catch (RuntimeException e)
        {
            e.printStackTrace();
            throw e;
        }
    }

    public boolean equals(Object obj)
    {
        LocalMarshalledValue otherValue = (LocalMarshalledValue)obj;
        try
        {
            Object obj1 = this.get();
            Object obj2 = otherValue.get();
            return obj1.equals(obj2);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
    }

    public void writeExternal(ObjectOutput out) throws IOException {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        BufferedOutputStream buffOut = new BufferedOutputStream(byteOut);
        DataOutputStream dataOut = new DataOutputStream(buffOut);
        container.saveData(dataOut);
        dataOut.flush();
        byte[] arrayBytes = byteOut.toByteArray();
        out.writeInt(arrayBytes.length);
        out.write(arrayBytes);
        out.flush();
    }

    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        byte[] bytes = new byte[in.readInt()];
        in.readFully(bytes);
        ByteArrayInputStream byteInput = new ByteArrayInputStream(bytes);
        DataInputStream input = new DataInputStream(byteInput);
        container = new DataContainer(true,null);
        container.loadData(input);
    }

}
TOP

Related Classes of org.jboss.serial.data.LocalMarshalledValue

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.