alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Play Framework/Scala example source code file (GzipSpec.scala)

This example Play Framework source code file (GzipSpec.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Play Framework (and Scala) source code examples by using tags.

All credit for the original source code belongs to Play Framework; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Play Framework tags/keywords

api, array, bytearrayinputstream, bytearrayoutputstream, enumeratee, gzipinputstream, gzipoutputstream, gzipspec, hello, iterate, lib, library, play, play framework, specification, string

The GzipSpec.scala Play Framework example source code

/*
 * Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
 */
package play.filters.gzip

import concurrent.Await
import play.api.libs.iteratee.{Iteratee, Enumeratee, Enumerator}
import concurrent.duration.Duration
import org.specs2.mutable.Specification
import scala.concurrent.ExecutionContext.Implicits._

object GzipSpec extends Specification {

  "gzip" should {

    /**
     * Uses both Java's GZIPOutputStream and GZIPInputStream to verify correctness.
     */
    def test(values: String*) = {
      import java.io._
      import java.util.zip._

      val valuesBytes = values.map(_.getBytes("utf-8"))

      val result: Array[Byte] = Await.result(Enumerator.enumerate(valuesBytes) &> Gzip.gzip() |>>> Iteratee.consume[Array[Byte]](), Duration.Inf)

      // Check that it exactly matches the gzip output stream
      val baos = new ByteArrayOutputStream()
      val os = new GZIPOutputStream(baos)
      valuesBytes.foreach(bytes => os.write(bytes))
      os.close()
      val baosResult = baos.toByteArray

      for (i <- 0 until result.length) {
        if (result(i) != baosResult(i)) {
          result(i) must_== baosResult(i)
        }
      }

      result must_== baos.toByteArray

      // Check that it can be unzipped
      val bais = new ByteArrayInputStream(result)
      val is = new GZIPInputStream(bais)
      val check: Array[Byte] = Await.result(Enumerator.fromStream(is) |>>> Iteratee.consume[Array[Byte]](), Duration.Inf)
      values.mkString("") must_== new String(check, "utf-8")
    }

    "gzip simple input" in {
      test("Hello world")
    }

    "gzip multiple inputs" in {
      test("Hello", " ", "world")
    }

    "gzip large repeating input" in {
      val bigString = Seq.fill(1000)("Hello world").mkString("")
      test(bigString)
    }

    "gzip multiple large repeating inputs" in {
      val bigString = Seq.fill(1000)("Hello world").mkString("")
      test(bigString, bigString, bigString)
    }

    "gzip large random input" in {
      test(scala.util.Random.nextString(10000))
    }

    "gzip multiple large random inputs" in {
      test(scala.util.Random.nextString(10000),
        scala.util.Random.nextString(10000),
        scala.util.Random.nextString(10000))
    }
  }

  "gunzip" should {

    /**
     * Uses the gzip enumeratee to verify correctness.
     */
    def test(value: String, gunzip: Enumeratee[Array[Byte], Array[Byte]] = Gzip.gunzip(), gzip: Enumeratee[Array[Byte], Array[Byte]] = Gzip.gzip()) = {
      val future = Enumerator(value.getBytes("utf-8")) &> gzip &> gunzip |>>> Iteratee.consume[Array[Byte]]()
      val result = new String(Await.result(future, Duration.Inf), "utf-8")
      result must_== value
    }

    "gunzip simple input" in {
      test("Hello world")
    }

    "gunzip simple input in small chunks" in {
      test("Hello world", gzip = Gzip.gzip(5))
    }

    "gunzip large repeating input" in {
      test(Seq.fill(1000)("Hello world").mkString(""))
    }

    "gunzip large repeating input in small chunks" in {
      test(Seq.fill(1000)("Hello world").mkString(""), gzip = Gzip.gzip(100))
    }

    "gunzip large random input" in {
      test(scala.util.Random.nextString(10000))
    }

    "gunzip large random input in small chunks" in {
      test(scala.util.Random.nextString(10000), gzip = Gzip.gzip(100))
    }
  }
}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework GzipSpec.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.