All Downloads are FREE. Search and download functionalities are using the official Maven repository.

scalapb.LiteParser.scala Maven / Gradle / Ivy

There is a newer version: 1.0.0-alpha.1
Show newest version
package scalapb

import java.io.InputStream

import com.google.protobuf.{CodedOutputStream, CodedInputStream}

object LiteParser {
  def readMessage[A <: GeneratedMessage](input: CodedInputStream, message: A)(
      implicit cmp: GeneratedMessageCompanion[A]
  ): A = {
    val length    = input.readRawVarint32()
    val oldLimit  = input.pushLimit(length)
    val result: A = cmp.merge(message, input)
    input.checkLastTagWas(0)
    input.popLimit(oldLimit)
    result
  }

  def readMessage[A <: GeneratedMessage](input: CodedInputStream)(
      implicit cmp: GeneratedMessageCompanion[A]
  ): A = {
    val length    = input.readRawVarint32()
    val oldLimit  = input.pushLimit(length)
    val result: A = cmp.parseFrom(input)
    input.checkLastTagWas(0)
    input.popLimit(oldLimit)
    result
  }

  def parseDelimitedFrom[A <: GeneratedMessage](
      input: InputStream
  )(implicit cmp: GeneratedMessageCompanion[A]): Option[A] = {
    val b = input.read()
    if (b < 0) None
    else {
      val size = CodedInputStream.readRawVarint32(b, input)
      Some(cmp.parseFrom(CodedInputStream.newInstance(new LimitedInputStream(input, size))))
    }
  }

  def parseDelimitedFrom[A <: GeneratedMessage](
      input: CodedInputStream
  )(implicit cmp: GeneratedMessageCompanion[A]): Option[A] = {
    if (input.isAtEnd) None
    else Some(readMessage(input))
  }

  @inline
  def preferredCodedOutputStreamBufferSize(dataLength: Int) =
    dataLength min CodedOutputStream.DEFAULT_BUFFER_SIZE
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy