Click here to Skip to main content
15,883,883 members
Articles / Programming Languages / Java / Java SE

OpenNxSerialization

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
16 Jun 2009GPL32 min read 20.4K   177   9  
Speed up object serialization in Java.
/*
 * @(#)NxCollectionSerializationSurrogate.java	1.0
 *
 * Created on September 18, 2008, 12:59 PM
 *
 * Copyright 2008 NeXtreme Innovations, Inc. All rights reserved.
 * "NeXtreme Innovations" PROPRIETARY/CONFIDENTIAL. Use is subject
 * to license terms.
 */
package com.nextreme.opennxserialization.standard.io.surrogates;

import com.nextreme.opennxserialization.core.io.NxObjectInput;
import com.nextreme.opennxserialization.core.io.NxObjectOutput;
import com.nextreme.opennxserialization.core.io.surrogates.NxBuiltinSerializationSurrogate;
import com.nextreme.opennxserialization.core.io.surrogates.NxIOException;
import com.nextreme.opennxserialization.core.io.surrogates.NxInstantiationException;
import com.nextreme.opennxserialization.core.io.surrogates.NxSerializationSurrogateImpl;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;

/**
 * NxCollectionSerializationSurrogate<E> is responsible for writing and reading
 * instances of Collection<E> class.
 *
 * @version 1.0, September 18, 2008
 */
public abstract class NxCollectionSerializationSurrogate<E>
        extends NxSerializationSurrogateImpl
        implements NxBuiltinSerializationSurrogate
{
    /** Creates a new instance of NxBooleanSerializationSurrogate */
    public NxCollectionSerializationSurrogate(Class<? extends Collection<E>> cls) {
        super(cls);
    }
    
    /**
     * Read an object of type returned by getRealClass() from the stream reader.
     * A fresh instance of the object is passed as parameter.
     * The surrogate should populate fields in the object from data on the stream
     *
     * @param input stream reader
     * @param graph a fresh instance of the object that the surrogate must deserialize.
     * @exception NxInstantiationException Object creation related exceptions.
     * @exception NxIOException Any of the usual Input/Output related exceptions.
     * @return object read from the stream reader
     */
    public Object readDirect(NxObjectInput input, Object graph)
	throws NxInstantiationException, NxIOException
    {
        Collection<E> object = (Collection<E>)graph;
        try {
            int len = input.readInt();
            for (int i = 0; i < len; i++)
            {
                E item = (E) input.readObject();
                object.add(item);
            }
        } catch (ClassNotFoundException ex) {
            throw new NxIOException(ex);
        } catch (IOException ex) {
            throw new NxIOException(ex);
        }
        return graph;
    }

    /**
     * Write an object of type returned by getRealClass( to the stream writer
     *
     * @param output stream writer
     * @param graph object to be written to the stream reader
     * @exception NxIOException Any of the usual Input/Output related exceptions.
     */
    public void writeDirect(NxObjectOutput output, Object graph)
      throws NxIOException
   {
        Collection<E> object = (Collection<E>)graph;
        int len = object.size();
        
        try {
            output.writeInt(len);
            if(len > 0)
            {
                Iterator<E> iter = object.iterator();
                while(iter.hasNext())
                {
                    output.writeObject(iter.next());
                }
            }
        } catch (IOException ex) {
            throw new NxIOException(ex);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Architect
Pakistan Pakistan
Let a = b ....... (1)
a - b = a - b
a^2 - ab = a^2 - ab
a^2 - ab = a^2 - b^2 (from 1)
a (a - b) = (a + b) (a - b)
a = (a + b) ...... (2)

if a = 1
1 = (1 + 1) (from 1 & 2)
1 = 2 !!

Comments and Discussions