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

com.twitter.finagle.thrift.OutputBuffer.scala Maven / Gradle / Ivy

There is a newer version: 24.2.0
Show newest version
package com.twitter.finagle.thrift

import org.apache.thrift.TBase
import org.apache.thrift.protocol.TProtocolFactory
import org.apache.thrift.transport.TMemoryBuffer

/**
 * OutputBuffers are convenient ways of getting at TProtocols for
 * output to byte arrays
 */
private[finagle] object OutputBuffer {
  def messageToArray(message: TBase[_, _], protocolFactory: TProtocolFactory) = {
    val buffer = new OutputBuffer(protocolFactory)
    message.write(buffer())
    buffer.toArray
  }
}

private[finagle] class OutputBuffer(protocolFactory: TProtocolFactory) {
  private[this] val memoryBuffer = new TMemoryBuffer(512)
  private[this] val oprot = protocolFactory.getProtocol(memoryBuffer)

  def apply() = oprot

  def toArray = {
    oprot.getTransport().flush()
    java.util.Arrays.copyOfRange(memoryBuffer.getArray(), 0, memoryBuffer.length())
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy