Skip to content

Commit 35ecadc

Browse files
committed
bijections-thrift: Use TArrayByteTransport for thrift deserialization for both compact and binary protocols
1 parent 9181ae6 commit 35ecadc

File tree

2 files changed

+14
-30
lines changed

2 files changed

+14
-30
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Bijection #
22

3+
### 0.9.8
4+
* Use TArrayByteTransport for thrift deserialization for both compact and binary protocols
5+
36
### 0.9.2
47
* Fix issue with `Injection[String, Array[Byte]].invert` for arrays more than `2^(24)` in size: https://github.com/twitter/bijection/pull/243
58

bijection-thrift/src/main/scala/com/twitter/bijection/thrift/ThriftCodecs.scala

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@ import com.twitter.bijection.Inversion.attempt
55
import com.twitter.bijection.macros.Macros
66
import java.io.{ByteArrayInputStream, ByteArrayOutputStream}
77
import 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}
149
import org.apache.thrift.transport.TIOStreamTransport
1510
import org.codehaus.jackson.map.MappingJsonFactory
1611
import java.lang.{Integer => JInt}
1712
import scala.collection.mutable.{Map => MMap}
1813
import scala.util.{Failure, Success}
1914
import 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

5147
class 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

9374
object CompactThriftCodec {
9475
def apply[T <: TBase[_, _]: ClassTag]: Injection[T, Array[Byte]] =

0 commit comments

Comments
 (0)