Issue #595: Pluggable serializers - basic implementation

This commit is contained in:
Debasish Ghosh 2011-06-07 06:36:21 +05:30
parent b600d0cf52
commit 40d1ca6da2
22 changed files with 1071 additions and 480 deletions

View file

@ -0,0 +1,39 @@
package akka.serialization
/**
* Copyright (C) 2009-2011 Scalable Solutions AB <http://scalablesolutions.se>
*/
import java.io.{ ObjectOutputStream, ByteArrayOutputStream, ObjectInputStream, ByteArrayInputStream }
import akka.util.ClassLoaderObjectInputStream
import akka.actor.ActorRef
trait Serializer extends scala.Serializable {
def toBinary(o: AnyRef): Array[Byte]
def fromBinary(bytes: Array[Byte], clazz: Option[Class[_]] = None, classLoader: Option[ClassLoader] = None): AnyRef
}
class JavaSerializer extends Serializer {
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
}
}
object JavaSerializer extends JavaSerializer
object Serializer {
val defaultSerializerName = JavaSerializer.getClass.getName
}