@@ -5,18 +5,14 @@ import com.twitter.bijection.Inversion.attempt
55import com .twitter .bijection .macros .Macros
66import java .io .{ByteArrayInputStream , ByteArrayOutputStream }
77import org .apache .thrift .{TBase , TEnum }
8- import org .apache .thrift .protocol .{
9- TBinaryProtocol ,
10- TCompactProtocol ,
11- TProtocolFactory ,
12- TSimpleJSONProtocol
13- }
8+ import org .apache .thrift .protocol .{TBinaryProtocol , TCompactProtocol , TProtocolFactory , TSimpleJSONProtocol }
149import org .apache .thrift .transport .TIOStreamTransport
1510import org .codehaus .jackson .map .MappingJsonFactory
1611import java .lang .{Integer => JInt }
1712import scala .collection .mutable .{Map => MMap }
1813import scala .util .{Failure , Success }
1914import scala .reflect ._
15+ import scala .util .Try
2016
2117/**
2218 * Codecs for use in serializing and deserializing Thrift structures.
@@ -50,18 +46,17 @@ object ThriftCodec {
5046
5147class ThriftCodec [T <: TBase [_, _], P <: TProtocolFactory ](klass : Class [T ], factory : P )
5248 extends Injection [T , Array [Byte ]] {
53- protected lazy val prototype = klass.newInstance
54- override def apply (item : T ) = {
49+ protected lazy val prototype : T = klass.newInstance
50+ override def apply (item : T ): Array [ Byte ] = {
5551 val baos = new ByteArrayOutputStream
5652 item.write(factory.getProtocol(new TIOStreamTransport (baos)))
5753 baos.toByteArray
5854 }
59- override def invert (bytes : Array [Byte ]) =
60- attempt (bytes) { bytes =>
55+ override def invert (bytes : Array [Byte ]): Try [ T ] =
56+ Macros .fastAttempt (bytes) {
6157 val obj = prototype.deepCopy.asInstanceOf [T ]
62- val stream = new ByteArrayInputStream (bytes)
63- obj.read(factory.getProtocol(new TIOStreamTransport (stream)))
64- obj.asInstanceOf [T ]
58+ obj.read(factory.getProtocol(TArrayByteTransport .apply(bytes)))
59+ obj
6560 }
6661}
6762
@@ -72,23 +67,9 @@ object BinaryThriftCodec {
7267 new BinaryThriftCodec (klass)
7368}
7469
75- class BinaryThriftCodec [T <: TBase [_, _]](klass : Class [T ]) extends Injection [T , Array [Byte ]] {
76- private [this ] val factory = new TBinaryProtocol .Factory
77-
78- protected lazy val prototype = klass.newInstance
79-
80- override def apply (item : T ) = {
81- val baos = new ByteArrayOutputStream
82- item.write(factory.getProtocol(new TIOStreamTransport (baos)))
83- baos.toByteArray
84- }
85- override def invert (bytes : Array [Byte ]) =
86- Macros .fastAttempt(bytes) {
87- val obj = prototype.deepCopy.asInstanceOf [T ]
88- obj.read(TArrayBinaryProtocol (TArrayByteTransport (bytes)))
89- obj.asInstanceOf [T ]
90- }
91- }
70+ class BinaryThriftCodec [T <: TBase [_, _]](klass : Class [T ])
71+ extends ThriftCodec [T , TBinaryProtocol .Factory ](klass, new TBinaryProtocol .Factory ) {
72+ }
9273
9374object CompactThriftCodec {
9475 def apply [T <: TBase [_, _]: ClassTag ]: Injection [T , Array [Byte ]] =
0 commit comments