当前位置: 首页>>代码示例>>Scala>>正文


Scala ByteStreams类代码示例

本文整理汇总了Scala中com.google.common.io.ByteStreams的典型用法代码示例。如果您正苦于以下问题:Scala ByteStreams类的具体用法?Scala ByteStreams怎么用?Scala ByteStreams使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了ByteStreams类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: Zip

//设置package包名称以及导入依赖的类
package se.joham.funrts.util

import java.io.{ByteArrayInputStream, ByteArrayOutputStream}
import java.util.zip.{GZIPInputStream, GZIPOutputStream}

import com.google.common.io.ByteStreams


object Zip {

  def compress(bytes: Array[Byte]): Array[Byte] = {
    val bos = new ByteArrayOutputStream()
    val zs = new GZIPOutputStream(bos)
    zs.write(bytes)
    zs.close()
    bos.toByteArray
  }

  def decompress(bytes: Array[Byte]): Array[Byte] = {
    val bis = new ByteArrayInputStream(bytes)
    val zs = new GZIPInputStream(bis)
    ByteStreams.toByteArray(zs)
  }

} 
开发者ID:GiGurra,项目名称:fun-rts,代码行数:26,代码来源:Zip.scala

示例2: ExampleData

//设置package包名称以及导入依赖的类
package com.kakao.cuesheet.examples.util

import java.io.FileOutputStream

import com.google.common.io.{ByteStreams, Files}

import scala.util.control.NonFatal

object ExampleData {
  lazy val path: String = {
    try {
      val resource = "data.tsv"
      val tmpfile = Files.createTempDir().getAbsolutePath + resource
      val input = getClass.getResourceAsStream(resource)
      val output = new FileOutputStream(tmpfile)
      ByteStreams.copy(input, output)
      input.close()
      output.close()
      tmpfile
    } catch {
      case NonFatal(e) =>
        throw new RuntimeException("Could not copy example data file to temp directory", e)
    }
  }
} 
开发者ID:kakao,项目名称:cuesheet,代码行数:26,代码来源:ExampleData.scala

示例3: IOUtils

//设置package包名称以及导入依赖的类
package core.util
import java.io._
import java.nio.file.{Files, Path}

import com.google.common.io.ByteStreams

object IOUtils {

  def writeString(path: Path, str: String) {
    val s: OutputStream = Files.newOutputStream(path)
    s.write(str.getBytes)
    s.close()
  }

  def readString(path: Path): String = {
    val s: InputStream = Files.newInputStream(path)
    val bytes: Array[Byte] = ByteStreams.toByteArray(s)
    s.close()
    new String(bytes)
  }
} 
开发者ID:citrum,项目名称:storage-server,代码行数:22,代码来源:IOUtils.scala

示例4: atResource

//设置package包名称以及导入依赖的类
package webby.api.controllers

import java.io.InputStream
import java.nio.file.{Files, Path}

import com.google.common.io.ByteStreams
import com.google.common.net.HttpHeaders
import io.netty.handler.codec.http.HttpResponseStatus
import webby.api.libs.MimeTypes
import webby.api.mvc._
import webby.commons.io.Resources
import webby.commons.system.log.PageLog
import webby.mvc.{StdCtl, StdPaths}


  def atResource(subPath: String,
                 resourceBasePath: String = null,
                 fileBasePath: String = null,
                 restriction: ResourceRestriction = NoRestriction) = SimpleAction {req =>
    val valid: Boolean =
      restriction match {
        case NoRestriction => true // always allowed
        case CookieRestriction(cookieName, value) =>
          if (value == null) req.cookies.contains(cookieName)
          else req.cookies.get(cookieName).contains(value)
      }
    if (!valid) {
      Forbidden("Forbidden resource")
    } else {
      def serve(): Result = {
        if (resourceBasePath != null) {
          val stream: InputStream = Resources.classLoader.getResourceAsStream(resourceBasePath + subPath)
          if (stream != null) {
            return filePlainResult(ByteStreams.toByteArray(stream), subPath)
          }
        }
        if (fileBasePath != null) {
          at(fileBasePath, subPath)(req, null)
        } else {
          NotFoundRaw("Not found")
        }
      }
      serve()
    }
  }

  sealed trait ResourceRestriction
  object NoRestriction extends ResourceRestriction
  case class CookieRestriction(cookieName: String, value: String = null) extends ResourceRestriction

  // ------------------------------- Private & protected methods -------------------------------

  private def filePlainResult(body: Array[Byte], fileName: String): PlainResult =
    PlainResult(HttpResponseStatus.OK, body)
      .withHeader(HttpHeaders.CONTENT_TYPE, MimeTypes.forFileName(fileName).getOrElse(MimeTypes.BINARY))
} 
开发者ID:citrum,项目名称:webby,代码行数:57,代码来源:StaticCtl.scala


注:本文中的com.google.common.io.ByteStreams类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。