All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alachisoft.ncache.serialization.standard.io.surrogates.ObjectSerializationSurrogate Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
/*
 * @(#)ObjectSerializationSurrogate.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.alachisoft.ncache.serialization.standard.io.surrogates;

import com.alachisoft.ncache.serialization.core.io.*;
import com.alachisoft.ncache.serialization.core.io.surrogates.*;

import java.io.*;

/**
 * ObjectSerializationSurrogate is responsible for writing and reading
 * instances of unknown classes.
 *
 * @version 1.0, September 18, 2008
 */
public class ObjectSerializationSurrogate
        extends SerializationSurrogateBase
        implements SerializationSurrogate, BuiltinSerializationSurrogate {
    /**
     * Specify a hard handle for this surrogate
     */
    public static final short HARD_HANDLE = TypeSurrogateConstants.FirstTypeHandle + 0x00;

    /**
     * Creates a new instance of NxForbiddenSerializationSurrogate
     */
    public ObjectSerializationSurrogate(Class type) {
        super(type);
    }

    /**
     * Read and return an object. The class that implements this interface
     * defines where the object is "read" from.
     *
     * @param input the stream to read data from in order to restore the object.
     * @return the object read from the stream
     * @throws java.lang.ClassNotFoundException If the class of a serialized
     *                                          object cannot be found.
     * @throws NCacheIOException                If any of the usual Input/Output
     *                                          related exceptions occur.
     */
    public Object readObject(NCacheObjectInput input)
            throws NCacheInstantiationException, NCacheIOException {
        try {
            Integer cookie = input.readInt();
            Object custom = input.getContext().getObject(cookie);
            if (custom == null) {
                int length = input.readInt();
                byte check = input.readByte();

//                ByteArrayInputStream val = new ByteArrayInputStream(;
                //If an object have length zero in it's byte then deserialize the object using native serialization
                if (check == (byte) 1 || (check == (byte) 0 && length == 0)) {
                    ObjectInput nativeReader = new ExtendedObjectInputStream(input.getBaseStream());
                    custom = nativeReader.readObject();
                    input.getContext().rememberForRead(custom);
                } else {
                    input.skipBytes(length);
                }
            }
            return custom;
        } catch (IOException ex) {
            throw new NCacheIOException(ex);
        } catch (ClassNotFoundException ex) {
            throw new NCacheInstantiationException(ex);
        }
    }

    /**
     * Write an object to the underlying storage or stream.  The
     * class that implements this interface defines how the object is
     * written.
     *
     * @param output the stream to write the object to.
     * @param graph  the object to write .
     * @throws NCacheIOException Any of the usual Input/Output related exceptions.
     */
    public void writeObject(NCacheObjectOutput output, Object graph)
            throws NCacheIOException {
        try {
            Integer cookie = output.getContext().getCookie(graph);
            if (cookie != SerializationContext.InvalidCookie) {
                output.writeInt(cookie);
                return;
            }

            cookie = output.getContext().rememberForWrite(graph);
            output.writeInt(cookie);

            ByteArrayOutputStream val = new ByteArrayOutputStream();

            ObjectOutput nativeWriter = new ObjectOutputStream(val);
            nativeWriter.writeObject(graph);
            nativeWriter.flush();

            output.writeInt(val.toByteArray().length);
            output.writeByte((byte) 1);

            output.write(val.toByteArray());
            output.flush();


        } catch (IOException ex) {
            throw new NCacheIOException(ex);
        }
    }

    public void skipObject(NCacheObjectInput input) throws NCacheInstantiationException, NCacheIOException {
        try {
            Integer cookie = input.readInt();
            Object custom = input.getContext().getObject(cookie);
            if (custom == null) {
                int length = input.readInt();
                byte check = input.readByte();

//                ByteArrayInputStream val = new ByteArrayInputStream(;
                //If an object have length zero in it's byte then deserialize the object using native serialization
                if (check == (byte) 1 || (check == (byte) 0 && length == 0)) {
                    ObjectInput nativeReader = new ObjectInputStream(input.getBaseStream());
                    custom = nativeReader.readObject();
                    input.getContext().rememberForRead(custom);
                } else {
                    input.skipBytes(length);
                }
            }
        } catch (IOException ex) {
            throw new NCacheIOException(ex);
        } catch (ClassNotFoundException ex) {
            throw new NCacheInstantiationException(ex);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy