Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added ThriftCodec.

  • Loading branch information...
commit 4fa9e88117c5c77190c5ebcc2ccee4f9ea7fd019 1 parent 291d309
@sritchie sritchie authored
Showing with 27 additions and 0 deletions.
  1. +27 −0 util-thrift/src/main/scala/com/twitter/util/ThriftCodec.scala
View
27 util-thrift/src/main/scala/com/twitter/util/ThriftCodec.scala
@@ -0,0 +1,27 @@
+package com.twitter.util
+
+import java.io.{InputStream, ByteArrayInputStream, ByteArrayOutputStream}
+import org.apache.thrift.TBase
+import org.apache.thrift.protocol.{TBinaryProtocol, TCompactProtocol, TProtocolFactory }
+import org.apache.thrift.transport.TIOStreamTransport
+
+class ThriftCodec[T <: TBase[_,_]: Manifest, P <: TProtocolFactory:Manifest]
+extends Codec[T, Array[Byte]] with ThriftSerializer {
+
+ protected lazy val prototype: T =
+ manifest[T].erasure.asInstanceOf[Class[T]].newInstance
+
+ override lazy val protocolFactory =
+ manifest[P].erasure.asInstanceOf[Class[P]].newInstance
+
+ override def encode(item: T) = toBytes(item)
+
+ override def decode(bytes: Array[Byte]) = {
+ val obj = prototype.deepCopy
+ fromBytes(obj, bytes)
+ obj.asInstanceOf[T]
+ }
+}
+
+class BinaryThriftCodec[T <: TBase[_,_]: Manifest] extends ThriftCodec[T,TBinaryProtocol.Factory]
+class CompactThriftCodec[T <: TBase[_,_]: Manifest] extends ThriftCodec[T,TCompactProtocol.Factory]
Please sign in to comment.
Something went wrong with that request. Please try again.