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

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

There is a newer version: 5.3.3
Show newest version
/*
 * @(#)MapSerializationSurrogate.java	1.0
 *
 * Created on September 18, 2008, 12:36 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.NCacheObjectInput;
import com.alachisoft.ncache.serialization.core.io.NCacheObjectOutput;
import com.alachisoft.ncache.serialization.core.io.surrogates.BuiltinSerializationSurrogate;
import com.alachisoft.ncache.serialization.core.io.surrogates.NCacheIOException;
import com.alachisoft.ncache.serialization.core.io.surrogates.NCacheInstantiationException;
import com.alachisoft.ncache.serialization.core.io.surrogates.SerializationSurrogateImpl;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

/**
 * MapSerializationSurrogate class.
 *
 * @version 1.0, September 18, 2008
 */
public abstract class MapSerializationSurrogate
        extends SerializationSurrogateImpl
        implements BuiltinSerializationSurrogate {

    /**
     * Creates a new instance of NxBooleanSerializationSurrogate
     */
    public MapSerializationSurrogate(Class> 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.
     * @return object read from the stream reader
     * @throws NCacheInstantiationException Object creation related exceptions.
     * @throws NCacheIOException            Any of the usual Input/Output related exceptions.
     */
    public Object readDirect(NCacheObjectInput input, Object graph)
            throws NCacheInstantiationException, NCacheIOException {
        Map map = (Map) graph;
        try {
            int len = input.readInt();
            for (int i = 0; i < len; i++) {
                K key = (K) input.readObject();
                V value = (V) input.readObject();
                map.put(key, value);
            }
        } catch (ClassNotFoundException ex) {
            throw new NCacheIOException(ex);
        } catch (IOException ex) {
            throw new NCacheIOException(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
     * @throws NCacheIOException Any of the usual Input/Output related exceptions.
     */
    public void writeDirect(NCacheObjectOutput output, Object graph)
            throws NCacheIOException {
        Map map = (Map) graph;
        Set> entries = map.entrySet();
        int len = map.size();

        try {
            output.writeInt(len);
            if (len > 0) {
                Object[] obj = entries.toArray();
                for (int i = 0; i < len; i++) {
                    Map.Entry entry = (Map.Entry) obj[i];
                    output.writeObject(entry.getKey());
                    output.writeObject(entry.getValue());
                }
            }
        } catch (IOException ex) {
            throw new NCacheIOException(ex);
        }
    }

    @Override
    public void skipDirect(NCacheObjectInput input, Object graph) throws NCacheInstantiationException, NCacheIOException {
        try {
            int len = input.readInt();
            for (int i = 0; i < len; i++) {
                input.skipObject();
                input.skipObject();
            }
        } catch (IOException ex) {
            throw new NCacheIOException(ex);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy