serializer.scala
来自「JAVA 语言的函数式编程扩展」· SCALA 代码 · 共 58 行
SCALA
58 行
/* __ *\** ________ ___ / / ___ Scala API **** / __/ __// _ | / / / _ | (c) 2005-2007, LAMP/EPFL **** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **** /____/\___/_/ |_/____/_/ | | **** |/ **\* */// $Id: Serializer.scala 13032 2007-10-09 16:28:22Z michelou $package scala.actors.remoteimport java.lang.ClassNotFoundExceptionimport java.io.{DataInputStream, DataOutputStream, EOFException, IOException}abstract class Serializer(val service: Service) { def serialize(o: AnyRef): Array[Byte] def deserialize(a: Array[Byte]): AnyRef @throws(classOf[IOException]) def readBytes(inputStream: DataInputStream): Array[Byte] = { try { val length = inputStream.readInt() val bytes = new Array[Byte](length) inputStream.readFully(bytes, 0, length) return bytes } catch { case npe: NullPointerException => throw new EOFException("Connection closed.") } } @throws(classOf[IOException]) @throws(classOf[ClassNotFoundException]) def readObject(inputStream: DataInputStream): AnyRef = { val bytes = readBytes(inputStream) deserialize(bytes) } @throws(classOf[IOException]) def writeBytes(outputStream: DataOutputStream, bytes: Array[Byte]) { val length = bytes.length; // original length outputStream.writeInt(length) outputStream.write(bytes, 0, length) outputStream.flush() } @throws(classOf[IOException]) def writeObject(outputStream: DataOutputStream, obj: AnyRef) { val bytes = serialize(obj) writeBytes(outputStream, bytes) }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?