javaserializer.scala

来自「JAVA 语言的函数式编程扩展」· SCALA 代码 · 共 54 行

SCALA
54
字号
/*                     __                                               *\**     ________ ___   / /  ___     Scala API                            ****    / __/ __// _ | / /  / _ |    (c) 2005-2007, LAMP/EPFL             ****  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **** /____/\___/_/ |_/____/_/ | |                                         ****                          |/                                          **\*                                                                      */// $Id: JavaSerializer.scala 13975 2008-02-13 16:19:11Z phaller $package scala.actors.remoteimport java.io.{ByteArrayInputStream, ByteArrayOutputStream,                ObjectInputStream, ObjectOutputStream, InputStream,                ObjectStreamClass}/** *  @author Guy Oliver */class CustomObjectInputStream(os: InputStream, cl: ClassLoader) extends ObjectInputStream(os) {  override def resolveClass(cd: ObjectStreamClass): Class[T] forSome { type T } =    try {      cl.loadClass(cd.getName())    } catch {      case cnf: ClassNotFoundException =>        super.resolveClass(cd)    }}/** *  @author Philipp Haller */class JavaSerializer(serv: Service, cl: ClassLoader) extends Serializer(serv) {  def serialize(o: AnyRef): Array[Byte] = {    val bos = new ByteArrayOutputStream()    val out = new ObjectOutputStream(bos)    out.writeObject(o)    out.flush()    bos.toByteArray()  }  def deserialize(bytes: Array[Byte]): AnyRef = {    val bis = new ByteArrayInputStream(bytes)    // use custom stream only if cl != null    val in = if (cl != null)      new CustomObjectInputStream(bis, cl)    else      new ObjectInputStream(bis)    in.readObject()  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?