Click here to Skip to main content
15,881,413 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.
/*
 * @(#)NxStringArraySerializationSurrogate.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;

/**
 * NxStringArraySerializationSurrogate is responsible for writing and reading
 * instances of String[] class.
 *
 * @version 1.0, September 18, 2008
 */
public class NxStringArraySerializationSurrogate
        extends NxSerializationSurrogateImpl
        implements NxBuiltinSerializationSurrogate
{

    /** Creates a new instance of NxObjectArraySerializationSurrogate */
    public NxStringArraySerializationSurrogate()
    {
        super(String[].class);
    }

    /**
     * Creates instance of type returned by getRealClass(). This is different from NxSerializationSurrogateBase.createTypeInstance() 
     * in the sense that an NxObjectInput object is passed as parameter that can be used to read creation specific 
     * information from the stream. For example it can be used to read the length of the array before actually 
     * reading the values.
     * 
     * The default implementation simply delegates to super.createTypeInstance().
     *
     * @param input stream reader
     * @exception NxInstantiationException Object creation related exceptions.
     * @return Object that this surrogate must deserialize
     */
    @Override
    public Object instantiate(NxObjectInput input)
        throws NxInstantiationException
    {
        try {
            int length = input.readInt();
            return new String[length];
        } catch (IOException ex) {
            throw new NxInstantiationException(ex);
        }
    }

    /**
     * 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
    {
        try {
            String[] array = (String[])graph;
            int len = array.length;
            for (int i = 0; i < len; i++)
                array[i] = input.readUTF();
            return array;
        } catch (IOException ex) {
            throw new NxIOException(ex);
        }
    }

    /**
     * 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
   {
        try {
            String[] array = (String[])graph;
            int len = array.length;
            output.writeInt(len);
            for (int i = 0; i < len; i++)
                output.writeUTF(array[i]);
        } 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