com.alachisoft.ncache.serialization.standard.io.surrogates.ObjectArraySerializationSurrogate Maven / Gradle / Ivy
Show all versions of nc-serialization Show documentation
/*
* @(#)ObjectArraySerializationSurrogate.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.NCacheObjectInput;
import com.alachisoft.ncache.serialization.core.io.NCacheObjectOutput;
import com.alachisoft.ncache.serialization.core.io.TypeSurrogateConstants;
import com.alachisoft.ncache.serialization.core.io.surrogates.*;
import com.alachisoft.ncache.serialization.standard.io.TypeSurrogateSelectorImpl;
import java.io.IOException;
import java.lang.reflect.Array;
/**
* ObjectArraySerializationSurrogate is responsible for writing and reading instances of Object[] classe.
*
* @version 1.0, September 18, 2008
*/
public class ObjectArraySerializationSurrogate
extends SerializationSurrogateImpl
implements BuiltinSerializationSurrogate {
/**
* Specify a hard handle for this surrogate
*/
public static final short HARD_HANDLE = TypeSurrogateConstants.FirstTypeHandle + 0x03;
/**
* Creates a new instance of ObjectArraySerializationSurrogate
*/
public ObjectArraySerializationSurrogate() {
super(Object[].class);
}
/**
* Creates instance of type returned by getRealClass(). This is different from NxSerializationSurrogateBase.createTypeInstance() in the sense that an NCacheObjectInput 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
* @return Object that this surrogate must deserialize
* @throws NCacheInstantiationException Object creation related exceptions.
*/
@Override
public Object instantiate(NCacheObjectInput input)
throws NCacheInstantiationException {
try {
int length = input.readInt();
return new Object[length];
} catch (IOException ex) {
throw new NCacheInstantiationException(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.
* @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 {
try {
Object[] array = (Object[]) graph;
TypeSurrogateSelectorImpl mSelector = TypeSurrogateSelectorImpl.getDefault();
short typeHandle = input.readShort();
SerializationSurrogate surrogate = mSelector.getSurrogateForTypeHandle(typeHandle, input.getCacheContext());
if (surrogate == null) {
surrogate = mSelector.GetSurrogateForSubTypeHandle(typeHandle, input.readShort(), input.getCacheContext());
}
Class className = surrogate.getRealClass();
Object obj = Array.newInstance(className, array.length);
int len = array.length;
for (int i = 0; i < len; i++) {
Array.set(obj, i, input.readObject());
}
return obj;
} catch (ClassNotFoundException ex) {
throw new NCacheInstantiationException(ex);
} catch (IOException ex) {
throw new NCacheIOException(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
* @throws NCacheIOException Any of the usual Input/Output related exceptions.
*/
public void writeDirect(NCacheObjectOutput output, Object graph)
throws NCacheIOException {
try {
Object[] array = (Object[]) graph;
int len = array.length;
//length is read in SerializationSurrogateImpl ... which is wrong ... needs to be fixed for understanding and flow
output.writeInt(len);
TypeSurrogateSelectorImpl mSelector = TypeSurrogateSelectorImpl.getDefault();
// SerializationSurrogate surrogate = mSelector.getSurrogateForObject(graph, output.getCacheContext());
Object[] o = (Object[]) graph;
Object[] tempObject = new Object[2];
if (graph.getClass() != tempObject.getClass()) {
Object obj = null;
for (int i = 0; i < o.length; i++) {
if (o[i] != null) {
obj = o[i];
break;
}
}
SerializationSurrogate surrogateTemp = mSelector.getSurrogateForObject(obj, output.getCacheContext());
surrogateTemp.writeHandle(output, obj);
if (surrogateTemp.getSubHandle() > 0) {
surrogateTemp.writeSubHandle(output, obj);
}
} else {
SerializationSurrogate surrogateTemp = mSelector.getDefaultSurrogate();
surrogateTemp.writeHandle(output, new Object());
}
for (int i = 0; i < len; i++) {
try {
output.writeObject(array[i]);
} catch (IOException ex) {
throw new NCacheIOException(ex);
}
}
} catch (IOException ex) {
throw new NCacheIOException(ex);
}
}
@Override
public void skipDirect(NCacheObjectInput input, Object graph) throws NCacheInstantiationException, NCacheIOException {
try {
Object[] array = (Object[]) graph;
TypeSurrogateSelectorImpl mSelector = TypeSurrogateSelectorImpl.getDefault();
short typeHandle = input.readShort();
SerializationSurrogate surrogate = mSelector.getSurrogateForTypeHandle(typeHandle, input.getCacheContext());
if (surrogate == null) {
surrogate = mSelector.GetSurrogateForSubTypeHandle(typeHandle, input.readShort(), input.getCacheContext());
}
Class className = surrogate.getRealClass();
int len = array.length;
for (int i = 0; i < len; i++) {
input.skipObject();
}
} catch (IOException ex) {
throw new NCacheIOException(ex);
}
}
}