scala enumeratee to get the length

105 views Asked by At

I have a Enumeratee.map[Array[Byte]] with scala

class AccessLog extends EssentialFilter {
  def apply(next: EssentialAction) = new EssentialAction {
    lazy val processor: RequestStatsProcessor = RequestStatsProcessor.getInstance
    def apply(rh: RequestHeader) = {
      val start = System.currentTimeMillis
      def writeableEnumeratee[A](writeable: Writeable[A]): Enumeratee[A, Array[Byte]] = Enumeratee.map(a => writeable.transform(a))
      var size: Long = 0;
      def logTime(response: PlainResult): Result = response match {
        case simple @ SimpleResult(header, body) => {
          val time = System.currentTimeMillis - start

          val en = Enumeratee.map[Array[Byte]] { item =>
            size = size + item.length
            var requestInfo: RequestInfo = new RequestInfo(start, rh, response, size)
            processor.setRequest(requestInfo)
            processor.process
            item
          }
          SimpleResult(header, body &> writeableEnumeratee(simple.writeable) &> en)
        }
        case _ => response
      }
      next(rh).map {
        case plain: PlainResult => logTime(plain)
        case async: AsyncResult => async.transform(logTime)
      }
    }
  }
}

if I write like this, I'll get each length of the Array[Byte] but not total size in the map?

How can I do to get the total length?

0

There are 0 answers