2011-06-07 06:36:21 +05:30
|
|
|
package akka.serialization
|
|
|
|
|
|
|
|
|
|
/**
|
2012-01-19 18:21:06 +01:00
|
|
|
* Copyright (C) 2009-2012 Typesafe Inc. <http://www.typesafe.com>
|
2011-06-07 06:36:21 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import java.io.{ ObjectOutputStream, ByteArrayOutputStream, ObjectInputStream, ByteArrayInputStream }
|
|
|
|
|
import akka.util.ClassLoaderObjectInputStream
|
2011-07-19 14:20:18 +02:00
|
|
|
|
2011-07-19 19:28:17 +02:00
|
|
|
/**
|
|
|
|
|
* A Serializer represents a bimap between an object and an array of bytes representing that object
|
|
|
|
|
*/
|
2011-06-07 06:36:21 +05:30
|
|
|
trait Serializer extends scala.Serializable {
|
2011-07-19 14:20:18 +02:00
|
|
|
/**
|
2011-12-29 16:11:56 +01:00
|
|
|
* Completely unique value to identify this implementation of Serializer, used to optimize network traffic
|
2011-07-19 14:20:18 +02:00
|
|
|
* Values from 0 to 16 is reserved for Akka internal usage
|
|
|
|
|
*/
|
2011-12-30 22:00:49 +01:00
|
|
|
def identifier: Int
|
2011-07-19 19:28:17 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Serializes the given object into an Array of Byte
|
|
|
|
|
*/
|
2011-06-07 06:36:21 +05:30
|
|
|
def toBinary(o: AnyRef): Array[Byte]
|
2011-07-19 19:28:17 +02:00
|
|
|
|
2011-12-29 16:11:56 +01:00
|
|
|
/**
|
|
|
|
|
* Returns whether this serializer needs a manifest in the fromBinary method
|
|
|
|
|
*/
|
|
|
|
|
def includeManifest: Boolean
|
|
|
|
|
|
2011-12-30 22:00:49 +01:00
|
|
|
/**
|
|
|
|
|
* Deserializes the given Array of Bytes into an AnyRef
|
|
|
|
|
*/
|
|
|
|
|
def fromBinary(bytes: Array[Byte]): AnyRef = fromBinary(bytes, None, None)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Deserializes the given Array of Bytes into an AnyRef with an optional type hint
|
|
|
|
|
*/
|
|
|
|
|
def fromBinary(bytes: Array[Byte], manifest: Option[Class[_]]): AnyRef = fromBinary(bytes, manifest, None)
|
|
|
|
|
|
2011-07-19 19:28:17 +02:00
|
|
|
/**
|
|
|
|
|
* Produces an object from an array of bytes, with an optional type-hint and a classloader to load the class into
|
|
|
|
|
*/
|
2011-12-30 22:00:49 +01:00
|
|
|
def fromBinary(bytes: Array[Byte], manifest: Option[Class[_]], classLoader: Option[ClassLoader]): AnyRef
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Java API for creating a Serializer
|
|
|
|
|
*/
|
|
|
|
|
abstract class JSerializer extends Serializer {
|
|
|
|
|
def fromBinary(bytes: Array[Byte], manifest: Option[Class[_]] = None, classLoader: Option[ClassLoader] = None): AnyRef =
|
|
|
|
|
fromBinary(bytes, manifest.orNull, classLoader.orNull)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This method should be overridden,
|
|
|
|
|
* manifest and classLoader may be null.
|
|
|
|
|
*/
|
|
|
|
|
def fromBinary(bytes: Array[Byte], manifest: Class[_], classLoader: ClassLoader): AnyRef
|
2011-06-07 06:36:21 +05:30
|
|
|
}
|
|
|
|
|
|
2011-07-19 14:20:18 +02:00
|
|
|
object JavaSerializer extends JavaSerializer
|
|
|
|
|
object NullSerializer extends NullSerializer
|
|
|
|
|
|
2011-12-29 16:17:19 +01:00
|
|
|
/**
|
|
|
|
|
* This Serializer uses standard Java Serialization
|
|
|
|
|
*/
|
2011-06-07 06:36:21 +05:30
|
|
|
class JavaSerializer extends Serializer {
|
2011-07-19 14:20:18 +02:00
|
|
|
|
2011-12-29 16:11:56 +01:00
|
|
|
def includeManifest: Boolean = false
|
|
|
|
|
|
2011-12-30 22:00:49 +01:00
|
|
|
def identifier = 1
|
2011-07-19 14:20:18 +02:00
|
|
|
|
2011-06-07 06:36:21 +05:30
|
|
|
def toBinary(o: AnyRef): Array[Byte] = {
|
|
|
|
|
val bos = new ByteArrayOutputStream
|
|
|
|
|
val out = new ObjectOutputStream(bos)
|
|
|
|
|
out.writeObject(o)
|
|
|
|
|
out.close()
|
|
|
|
|
bos.toByteArray
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def fromBinary(bytes: Array[Byte], clazz: Option[Class[_]] = None,
|
|
|
|
|
classLoader: Option[ClassLoader] = None): AnyRef = {
|
|
|
|
|
val in =
|
|
|
|
|
if (classLoader.isDefined) new ClassLoaderObjectInputStream(classLoader.get, new ByteArrayInputStream(bytes)) else
|
|
|
|
|
new ObjectInputStream(new ByteArrayInputStream(bytes))
|
|
|
|
|
val obj = in.readObject
|
|
|
|
|
in.close()
|
|
|
|
|
obj
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-12-29 16:17:19 +01:00
|
|
|
/**
|
|
|
|
|
* This is a special Serializer that Serializes and deserializes nulls only
|
|
|
|
|
*/
|
2011-07-19 14:20:18 +02:00
|
|
|
class NullSerializer extends Serializer {
|
|
|
|
|
val nullAsBytes = Array[Byte]()
|
2011-12-29 16:11:56 +01:00
|
|
|
def includeManifest: Boolean = false
|
2011-12-30 22:00:49 +01:00
|
|
|
def identifier = 0
|
2011-07-19 14:20:18 +02:00
|
|
|
def toBinary(o: AnyRef) = nullAsBytes
|
|
|
|
|
def fromBinary(bytes: Array[Byte], clazz: Option[Class[_]] = None, classLoader: Option[ClassLoader] = None): AnyRef = null
|
2011-06-07 06:36:21 +05:30
|
|
|
}
|