com.alachisoft.ncache.serialization.standard.CompactBinaryFormatter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nc-serialization Show documentation
Show all versions of nc-serialization Show documentation
Internal package of Alachisoft.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.alachisoft.ncache.serialization.standard;
import com.alachisoft.ncache.serialization.standard.io.ObjectInputStream;
import com.alachisoft.ncache.serialization.standard.io.ObjectOutputStream;
import java.io.*;
/**
* @author Basit Anwer
*/
public class CompactBinaryFormatter {
/**
* @param graph
* @param cacheContext
* @return
* @throws IOException
*/
public static byte[] toByteBuffer(Object graph, String cacheContext) throws IOException {
try {
ByteArrayOutputStream val = new ByteArrayOutputStream();
ObjectOutput ow = new ObjectOutputStream(val, cacheContext);
ow.writeObject(graph);
ow.flush();
return val.toByteArray();
} catch (IOException iOException) {
try {
ByteArrayOutputStream val = new ByteArrayOutputStream();
ObjectOutput ow = new ObjectOutputStream(val, cacheContext);
ow.writeObject(graph);
ow.flush();
return val.toByteArray();
} catch (IOException iOException2) {
throw iOException2;
}
// throw iOException;
}
}
/**
* @param graph
* @param cacheContext
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
public static Object fromByteBuffer(byte[] graph, String cacheContext) throws IOException, ClassNotFoundException {
try {
ByteArrayInputStream val = new ByteArrayInputStream(graph);
ObjectInput ow = new ObjectInputStream(val, cacheContext);
return ow.readObject();
} catch (IOException iOException) {
try {
ByteArrayInputStream val = new ByteArrayInputStream(graph);
ObjectInput ow = new ObjectInputStream(val, cacheContext);
return ow.readObject();
} catch (IOException iOException1) {
throw iOException;
} catch (ClassNotFoundException classNotFoundException) {
throw classNotFoundException;
}
} catch (ClassNotFoundException classNotFoundException) {
throw classNotFoundException;
}
}
// ///
// /// Serializes an Object into the specified stream.
// ///
// /// specified stream
// /// Object
// public final void Serialize(MemoryStream stream, Object graph, String cacheContext)
// {
// CompactBinaryWriter writer = new CompactBinaryWriter(stream);
// Serialize(writer, graph, cacheContext);
// }
//
// ///
// /// Serializes an Object into the specified stream.
// ///
// /// specified stream
// /// Object
// public final void Serialize(Stream stream, Object graph, String cacheContext, boolean closeStream)
// {
// CompactBinaryWriter writer = new CompactBinaryWriter(stream);
// Serialize(writer, graph, cacheContext);
// writer.Dispose(closeStream);
// }
// ///
// /// Serializes an Object into the specified stream.
// ///
// /// specified stream
// /// Object
//
// public final void Serialize(Stream stream, Object graph, String cacheContext, boolean closeStream, MemoryManager objManager)
// {
// CompactBinaryWriter writer = new CompactBinaryWriter(stream);
// writer.Context.MemManager = objManager;
// Serialize(writer, graph, cacheContext);
// writer.Dispose(closeStream);
// }
// ///
// /// Deserializes an Object from the specified stream.
// ///
// /// specified stream
// /// deserialized Object
//
// public final Object Deserialize(Stream stream, String cacheContext)
// {
// CompactBinaryReader reader = new CompactBinaryReader(stream);
// return Deserialize(reader, cacheContext, false);
// }
// ///
// /// Deserializes an Object from the specified stream.
// ///
// /// specified stream
// /// deserialized Object
//
// public final Object Deserialize(Stream stream, String cacheContext, boolean closeStream)
// {
// Object obj;
// CompactBinaryReader reader = new CompactBinaryReader(stream);
// obj = Deserialize(reader, cacheContext, false);
// reader.Dispose(closeStream);
// return obj;
// }
// ///
// /// Deserializes an Object from the specified stream.
// ///
// /// specified stream
// /// deserialized Object
//
// public final Object Deserialize(Stream stream, String cacheContext, boolean closeStream, MemoryManager memManager)
// {
// Object obj;
// CompactBinaryReader reader = new CompactBinaryReader(stream);
// reader.Context.MemManager = memManager;
// obj = Deserialize(reader, cacheContext, false);
// reader.Dispose(closeStream);
// return obj;
// }
// ///
// /// Serializes an Object into the specified compact binary writer.
// ///
// /// specified compact binary writer
// /// Object
//
// final void Serialize(CompactBinaryWriter writer, Object graph, String cacheContext)
// {
// // Find an appropriate surrogate for the Object
// ISerializationSurrogate surrogate = TypeSurrogateSelector.GetSurrogateForObject(graph, cacheContext);
// // write type handle
// writer.Context.CacheContext = cacheContext;
// writer.Write(surrogate.TypeHandle);
// surrogate.Write(writer, graph);
// }
//
// ///
// /// Deserializes an Object from the specified compact binary writer.
// ///
// /// Stream containing reader
// /// Name of the cache
// /// True to skip the bytes returning null
// final Object Deserialize(CompactBinaryReader reader, String cacheContext, boolean skip)
// {
// // read type handle
// short handle = reader.ReadInt16();
// reader.Context.CacheContext = cacheContext;
// // Find an appropriate surrogate by handle
// ISerializationSurrogate surrogate = TypeSurrogateSelector.GetSurrogateForTypeHandle(handle, cacheContext);
//
// if (surrogate == null)
// {
// surrogate = TypeSurrogateSelector.GetSurrogateForSubTypeHandle(handle, reader.ReadInt16(), cacheContext);
// }
//
// if (surrogate == null)
// {
// //Trace.error("CompactBinaryFormatter.Deserialize"," surrogate not found for handle " + handle);
// throw new CompactSerializationException("Type handle " + handle + "is not registered with Compact Serialization Framework");
// }
// if (!skip)
// {
// return surrogate.Read(reader);
// }
// else
// {
// surrogate.Skip(reader);
// return null;
// }
// }
}