Use static 'parseFrom' to create protobuf objects instead of creating a defaultInstance all the time.
This commit is contained in:
parent
c0930bafef
commit
b2d939f2dd
1 changed files with 2 additions and 6 deletions
|
|
@ -39,8 +39,7 @@ class SerializerFactory {
|
|||
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||
*/
|
||||
object Serializer {
|
||||
val EMPTY_CLASS_ARRAY = Array[Class[_]]()
|
||||
val EMPTY_ANY_REF_ARRAY = Array[AnyRef]()
|
||||
val ARRAY_OF_BYTE_ARRAY = Array[Class[_]](classOf[Array[Byte]])
|
||||
|
||||
object NOOP extends NOOP
|
||||
class NOOP extends Serializer {
|
||||
|
|
@ -85,10 +84,7 @@ object Serializer {
|
|||
def fromBinary(bytes: Array[Byte], clazz: Option[Class[_]]): AnyRef = {
|
||||
if (!clazz.isDefined) throw new IllegalArgumentException(
|
||||
"Need a protobuf message class to be able to serialize bytes using protobuf")
|
||||
// TODO: should we cache this method lookup?
|
||||
val message = clazz.get.getDeclaredMethod(
|
||||
"getDefaultInstance", EMPTY_CLASS_ARRAY: _*).invoke(null, EMPTY_ANY_REF_ARRAY: _*).asInstanceOf[Message]
|
||||
message.toBuilder().mergeFrom(bytes).build
|
||||
clazz.get.getDeclaredMethod("parseFrom", ARRAY_OF_BYTE_ARRAY: _*).invoke(null, bytes).asInstanceOf[Message]
|
||||
}
|
||||
|
||||
def fromBinary(bytes: Array[Byte], clazz: Class[_]): AnyRef = {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue