We're trying to use a custom Kryo serializer so we...
# troubleshooting
m
We're trying to use a custom Kryo serializer so we can use generated scalapb classes without incurring generic serializer performance pain, but it seems like reading from the serializer's input parameter produces weird behavior: Trying to read all the bytes hangs indefinitely, and passing the InputStream to the scalapb parser seems to result in it reading `0`s and blowing up. Are there any tricky things to watch out for here? (Code snippet in thread)
✅ 1
Copy code
case class PBSerializer[T <: GeneratedMessage](companion: GeneratedMessageCompanion[T])
    extends Serializer[PBMessage[T]] {
  setAcceptsNull(false)
  setImmutable(true)

  def read(kryo: Kryo, input: Input, typ: Class[PBMessage[T]]): PBMessage[T] = {
    new PBMessage(companion.parseFrom(input))
  }

  def write(kryo: Kryo, output: Output, obj: PBMessage[T]): Unit = {
    output.write(obj.message.toByteArray)
  }
}
In this case,
companion
is the companion object ScalaPB provides to handle [de]serialization. I get
com.google.protobuf.InvalidProtocolBufferException: Protocol message contained an invalid tag (zero).
which smells like it's either getting unexpectedly empty data, or getting empty data after the end of the expected data.
I wanted to inspect the contents, so when I added a call to
input.readAllBytes()
to
read
so I could log some info out, that call runs forever.
Solved by writing the byte array length first in
write
and then only reading that many more bytes in
read
- apparently the input stream here doesn't have a fixed length