本文整理汇总了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)
}
}
示例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)
}
}
}
示例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)
}
}
示例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))
}