本文整理汇总了Scala中java.nio.charset.StandardCharsets.UTF_8类的典型用法代码示例。如果您正苦于以下问题:Scala UTF_8类的具体用法?Scala UTF_8怎么用?Scala UTF_8使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了UTF_8类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: FileSourceSpec
//设置package包名称以及导入依赖的类
package knot.core.sources
import java.nio.charset.StandardCharsets.UTF_8
import java.nio.file.Files
import knot.core.collectors.Last
import knot.core.{Knot, Workbench}
import knot.data.ByteNode
import knot.testKit.ThroughParser
import org.scalatest.Matchers._
import org.scalatest.{BeforeAndAfter, FunSpec}
import scala.concurrent.Await
import scala.concurrent.duration.Duration
class FileSourceSpec extends FunSpec with BeforeAndAfter {
implicit val wb: Workbench = Workbench.on("test")
private val testText = {
("a" * 513) +
("b" * 513) +
("c" * 513)
}
private val testFile = {
val f = Files.createTempFile("file-source-spec", ".tmp")
Files.newBufferedWriter(f, UTF_8).append(testText).close()
f
}
private def test(): Seq[String] = {
var actual = Seq[String]()
val f = Knot
.from(FileSource(testFile, 512, 5))
.to(ThroughParser[ByteNode](e => actual = actual :+ e.utf8String))
.start(Last[ByteNode]())
Await.result(f, Duration.Inf)
actual
}
after {
Files.delete(testFile)
}
describe("file source") {
it("file") {
val actual = test()
actual should be(Seq(
"a" * 512,
"a" + ("b" * 511),
"bb" + ("c" * 510),
"ccc"
))
}
}
}
示例2: EachLogExpectationBuilder
//设置package包名称以及导入依赖的类
package httpmock
import java.nio.charset.Charset
import java.nio.charset.StandardCharsets.UTF_8
import akka.util.ByteString
import play.api.mvc.Headers
import scala.concurrent.duration._
final case class EachLogExpectationBuilder(
methodOpt: Option[HttpMethod], // accept all method if None
headers: Headers = Headers(),
bodyOpt: Option[ArrayByte] = None, // body for sequential matcher
count: Int = 1,
queue: AccessLogQueue
) {
def method: String = methodOpt.fold("")(_.value)
def body(v: String, charset: Charset = UTF_8): EachLogExpectationBuilder =
copy(bodyOpt = Some(ArrayByte(ByteString(v.getBytes(charset)))))
def count(v: Int): EachLogExpectationBuilder = copy(count = v)
def header(key: String, value: String): EachLogExpectationBuilder = copy(headers = headers.add((key, value)))
def apply(timeout: FiniteDuration = 1.second, interval: Int = 100): Unit = {
new EachLogExecutionContext(this).run(timeout, interval)
}
}
示例3: Main
//设置package包名称以及导入依赖的类
package com.harry0000.kancolle.ac
import java.io.File
import java.nio.charset.StandardCharsets.UTF_8
import java.nio.file.{Files, StandardOpenOption}
object Main {
def main(args: Array[String]): Unit = {
val path = Config.distPath
val areas = PlaceCrawler.crawl()
writeCSV(path, "place.csv", areas)
writeJSON(path, "place.json", areas)
}
val headers = Seq("area", "prefecture_code", "prefecture_name", "place_name", "place_address")
def writeCSV(path: String, name: String, areas: Seq[Area]): Unit = {
import com.github.tototoshi.csv.CSVWriter
val writer = CSVWriter.open(new File(path, name))
try {
writer.writeRow(headers)
for {
area <- areas
pref <- area.prefectures
place <- pref.places
} {
writer.writeRow(Seq(area.name, pref.code, pref.name, place.name, place.address))
}
} finally {
writer.close()
}
}
def writeJSON(path: String, name: String, areas: Seq[Area]): Unit = {
import spray.json._
import PlaceCrawler._
val writer = Files.newBufferedWriter(new File(path, name).toPath, UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)
try {
writer.write(areas.toJson.prettyPrint)
} finally {
writer.close()
}
}
}
示例4: CatClient
//设置package包名称以及导入依赖的类
package ru.ifmo.ctddev.semenov.dkvs.client
import java.io._
import java.net.Socket
import java.nio.charset.StandardCharsets.UTF_8
import java.util.concurrent.LinkedBlockingQueue
import scala.annotation.tailrec
import scala.util.{Failure, Success, Try}
class CatClient(socket: Socket) {
val consoleReader = new BufferedReader(new InputStreamReader(System.in, UTF_8))
val consoleWriter = new PrintWriter(new OutputStreamWriter(System.out, UTF_8))
val socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream, UTF_8))
val socketWriter = new PrintWriter(new OutputStreamWriter(socket.getOutputStream, UTF_8), true)
val requests = new LinkedBlockingQueue[String]()
val responses = new LinkedBlockingQueue[String]()
def start(): Unit = {
def start(action: () => Unit) = {
new Thread(new Runnable() {
override def run() = action()
}).start()
}
start(readRequest)
start(writeRequest)
start(readResponse)
start(writeResponse)
}
private def readRequest() = interact(consoleReader.readLine, requests add _, "exit reading requests")
private def writeRequest() = interact(requests.take, socketWriter.println, "exit writing requests")
private def readResponse() = interact(socketReader.readLine, responses add _, "exit reading responses")
private def writeResponse() = interact(responses.take, consoleWriter.println, "exit writing responses")
@tailrec private def interact(read: () => String, write: String => Unit, exitMessage: String): Unit = {
val line = read()
if (line == null) {
consoleWriter println exitMessage
return
}
write(line)
interact(read, write, exitMessage)
}
}
object Connect {
def main(args: Array[String]) {
if (args.length != 2) println("Usage: Client <host> <port>")
else Try(new Socket(args(0), args(1).toInt)) match {
case Success(socket) => new CatClient(socket).start()
case Failure(exception) =>
println(s"Cannot connect to ${args(0)}:${args(1)}")
exception.printStackTrace()
}
}
}
示例5: StringCodec
//设置package包名称以及导入依赖的类
package com.twitter.finagle.integration
import com.twitter.finagle._
import java.nio.charset.StandardCharsets.UTF_8
import org.jboss.netty.channel.{ChannelPipelineFactory, Channels}
import org.jboss.netty.handler.codec.frame.{DelimiterBasedFrameDecoder, Delimiters}
import org.jboss.netty.handler.codec.string.{StringDecoder, StringEncoder}
object StringCodec extends StringCodec
class StringCodec extends CodecFactory[String, String] {
def server = Function.const {
new Codec[String, String] {
def pipelineFactory = new ChannelPipelineFactory {
def getPipeline = {
val pipeline = Channels.pipeline()
pipeline.addLast("frameDecoder", new DelimiterBasedFrameDecoder(100, Delimiters.lineDelimiter: _*))
pipeline.addLast("stringDecoder", new StringDecoder(UTF_8))
pipeline.addLast("stringEncoder", new StringEncoder(UTF_8))
pipeline
}
}
}
}
def client = Function.const {
new Codec[String, String] {
def pipelineFactory = new ChannelPipelineFactory {
def getPipeline = {
val pipeline = Channels.pipeline()
pipeline.addLast("stringEncode", new StringEncoder(UTF_8))
pipeline.addLast("stringDecode", new StringDecoder(UTF_8))
pipeline
}
}
override def prepareConnFactory(factory: ServiceFactory[String, String], ps: Stack.Params) =
new AddNewlineFilter andThen factory
}
}
class AddNewlineFilter extends SimpleFilter[String, String] {
def apply(request: String, service: Service[String, String]) = service(request + "\n")
}
}
示例6: ResponseTest
//设置package包名称以及导入依赖的类
package com.twitter.finagle.http
import java.nio.charset.StandardCharsets.UTF_8
import org.junit.runner.RunWith
import org.scalatest.FunSuite
import org.scalatest.junit.JUnitRunner
@RunWith(classOf[JUnitRunner])
class ResponseTest extends FunSuite {
test("constructors") {
List(
Response(),
Response(Version.Http11, Status.Ok),
Response()
).foreach { response =>
assert(response.version == Version.Http11)
assert(response.status == Status.Ok)
}
}
test("encode") {
val response = Response()
response.headerMap.set("Server", "macaw")
val expected = "HTTP/1.1 200 OK\r\nServer: macaw\r\n\r\n"
val actual = response.encodeString()
assert(actual == expected)
}
test("decodeString") {
val response = Response.decodeString(
"HTTP/1.1 200 OK\r\nServer: macaw\r\nContent-Length: 0\r\n\r\n")
assert(response.status == Status.Ok)
assert(response.headerMap(Fields.Server) == "macaw")
}
test("decodeBytes") {
val response = Response.decodeBytes(
"HTTP/1.1 200 OK\r\nServer: macaw\r\nContent-Length: 0\r\n\r\n".getBytes(UTF_8))
assert(response.status == Status.Ok)
assert(response.headerMap(Fields.Server) == "macaw")
}
}
示例7: ParserUtilsBenchmark
//设置package包名称以及导入依赖的类
package com.twitter.finagle.memcached.util
import com.twitter.finagle.benchmark.StdBenchAnnotations
import com.twitter.finagle.memcached.util.ParserUtilsBenchmark.Position
import java.nio.charset.StandardCharsets.UTF_8
import org.jboss.netty.buffer.{ChannelBuffer, ChannelBuffers}
import org.openjdk.jmh.annotations.{State, Benchmark, Scope}
import scala.util.Random
// ./sbt 'project finagle-benchmark' 'jmh:run ParserUtilsBenchmark'
class ParserUtilsBenchmark extends StdBenchAnnotations {
@Benchmark
def isDigits(pos: Position): Boolean = {
val idx = pos.i % pos.inputs.length
pos.i += 1
ParserUtils.isDigits(pos.inputs(idx))
}
}
object ParserUtilsBenchmark {
private val size = 100000
private val rnd = new Random(69230L) // just to give us consistent values
private val numbers: Seq[ChannelBuffer] = Seq.fill(size) {
ChannelBuffers.copiedBuffer(rnd.nextInt().toString, UTF_8)
}
private val strings: Seq[ChannelBuffer] = Seq.fill(size) {
ChannelBuffers.copiedBuffer(rnd.nextString(5), UTF_8)
}
private val _inputs =
(numbers ++ strings).toIndexedSeq
@State(Scope.Thread)
class Position {
var i = 0
def inputs: IndexedSeq[ChannelBuffer] = ParserUtilsBenchmark._inputs
}
}
示例8: RichChannelBufferTest
//设置package包名称以及导入依赖的类
package com.twitter.finagle.memcached.unit.util
import com.twitter.finagle.memcached.util.ChannelBufferUtils
import java.nio.charset.StandardCharsets.UTF_8
import org.jboss.netty.buffer.{ChannelBuffers, ChannelBuffer}
import org.junit.runner.RunWith
import org.scalacheck.Gen
import org.scalatest.FunSuite
import org.scalatest.junit.JUnitRunner
import org.scalatest.prop.GeneratorDrivenPropertyChecks
@RunWith(classOf[JUnitRunner])
class RichChannelBufferTest extends FunSuite
with GeneratorDrivenPropertyChecks
{
test("toInt for 0 to Int.MaxValue") {
import ChannelBufferUtils.{stringToChannelBuffer, channelBufferToRichChannelBuffer}
forAll(Gen.chooseNum(0, Int.MaxValue)) { n: Int =>
val cb: ChannelBuffer = n.toString
assert(n == cb.toInt)
}
}
test("toInt for bad input") {
import ChannelBufferUtils.channelBufferToRichChannelBuffer
Seq("", "abc", "123Four", "-1", "1" * 11, "2147483648")
.map(ChannelBuffers.copiedBuffer(_, UTF_8))
.foreach { cb =>
intercept[NumberFormatException] {
cb.toInt
}
}
}
}
示例9: LocalIndexableRootObjectJsonSerializerSpec
//设置package包名称以及导入依赖的类
package org.humanistika.exist.index.algolia
import java.io.StringWriter
import java.nio.file.{Files, Path}
import com.algolia.search.inputs.batch.BatchAddObjectOperation
import com.fasterxml.jackson.databind.ObjectMapper
import org.specs2.Specification
import resource.managed
import java.nio.charset.StandardCharsets.UTF_8
import scala.util.{Failure, Success}
class LocalIndexableRootObjectJsonSerializerSpec extends Specification { def is = s2"""
This is a specification to check the JSON Serialization of IndexableRootObject
The basic JSON serialized result must
must be round-tripable $e1
work in a Batch Operation $e2
"""
def e1 = {
val json = """{"objectID":"86/754/3.5.2.2.6","collection":"/db/t1","dict":"MZ.RGJS.???2","lemma":"???"}"""
val file = createTempJsonFile(json)
serializeJson(LocalIndexableRootObject(file)) mustEqual """{"objectID":"86/754/3.5.2.2.6","collection":"/db/t1","dict":"MZ.RGJS.???2","lemma":"???"}"""
}
def e2 = {
val json = """{"objectID":"86/754/3.5.2.2.6","collection":"/db/t1","dict":"MZ.RGJS.???2","lemma":"???"}"""
val file = createTempJsonFile(json)
val batch = new BatchAddObjectOperation[LocalIndexableRootObject](LocalIndexableRootObject(file))
serializeJson(batch) mustEqual s"""{"body":$json,"action":"addObject"}"""
}
private def createTempJsonFile(json: String) : Path = {
val p = Files.createTempFile("test", "json")
managed(Files.newBufferedWriter(p, UTF_8)).map { writer =>
writer.write(json)
}.tried match {
case Success(_) =>
p
case Failure(t) =>
throw t
}
}
private def serializeJson[T](obj: T): String = {
managed(new StringWriter).map { writer =>
val mapper = new ObjectMapper
mapper.writeValue(writer, obj)
writer.toString
}.tried match {
case Success(s) =>
s
case Failure(t) =>
throw t
}
}
}
示例10: Decompiler
//设置package包名称以及导入依赖的类
package io.github.jamespic.ethereum_tools.decompiler
import java.nio.file.{Files, Paths}
import java.nio.charset.StandardCharsets.UTF_8
import javax.xml.bind.DatatypeConverter
import org.ethereum.util.blockchain.StandaloneBlockchain
import io.github.jamespic.ethereum_tools.Bytecode
import io.github.jamespic.ethereum_tools.decompiler.control_flow.{Block, ControlGraph, GraphRewriteRules, Func}
object Decompiler {
def main(args: Array[String]) = {
val bytecode = args match {
case Array("--solidity", x, name) =>
val code = new String(Files.readAllBytes(Paths.get(x)), UTF_8)
val blockchain = new StandaloneBlockchain().withAutoblock(true)
try {
val contract = blockchain.submitNewContract(code, name)
val contractAddress = contract.getAddress()
blockchain.getBlockchain().getRepository().getCode(contractAddress)
} finally blockchain.getBlockchain().close()
case Array("--bytecode", x) =>
val code = new String(Files.readAllBytes(Paths.get(x)), UTF_8).trim
DatatypeConverter.parseHexBinary(code)
}
println(decompile(bytecode))
}
def decompile(bytes: Array[Byte]) = {
val instructions = Bytecode.parse(bytes)
val blocks = Block.identifyBasicBlocks(instructions)
val controlGraph = ControlGraph(blocks)
val rewritten = GraphRewriteRules.stripUnreachable(controlGraph).getOrElse(controlGraph)
println(Func.identifyFunctionsByReturn(rewritten))
rewritten
}
}
示例11:
//设置package包名称以及导入依赖的类
package com.redbubble.graphql
import java.nio.charset.StandardCharsets.UTF_8
import com.redbubble.graphql.GraphQlSampleQueries._
import com.redbubble.util.io.BufOps
import com.redbubble.util.json.CodecOps
import com.twitter.io.Buf
import org.scalacheck.Gen
import sangria.parser.QueryParser
import scala.util.Success
trait GraphQlGenerators {
private val genValidQueriesWithVariables = for {
qs <- Gen.oneOf(validQueries)
vs <- Gen.oneOf(variableStrings)
} yield queryJsonPayloadStrings(qs, vs)
private val genValidQueriesWithoutVariables = Gen.oneOf(validQueries).map(queryJsonPayload)
val genInvalidQueryStrings: Gen[Buf] = Gen.oneOf(invalidQueries).map(BufOps.stringToBuf(_, UTF_8))
val genValidQueryString: Gen[Buf] = Gen.oneOf(genValidQueriesWithoutVariables, genValidQueriesWithVariables)
val genValidQueries: Gen[GraphQlQuery] = for {
q <- Gen.oneOf(validQueries)
vs <- Gen.oneOf(variableStrings)
} yield {
val Success(parsedQuery) = QueryParser.parse(q.trim)
val Right(parsedVariables) = CodecOps.parse(vs)
GraphQlQuery(parsedQuery, Some(parsedVariables))
}
}
示例12: shares
//设置package包名称以及导入依赖的类
package org.secret_sharing
import java.nio.charset.StandardCharsets.UTF_8
import org.secret_sharing.Validation._
import scala.util.Random
trait SSSSOps[Error, Share] {
// creates the shares or returns an error, if input is invalid
def shares(secret: Array[Byte], requiredParts: Int, totalParts: Int, random: Random): Either[Error, List[Share]]
// creates combined string or returns an error, if shares are from different secrets
def combineToBigInt(shares: List[Share]): Either[Error, BigInt]
// this ensures the encrypted shares for the same secret are always the same
// But opens gate to dictionary attacks
def unsafeShares(secret: Array[Byte], requiredParts: Int, totalParts: Int): Either[Error, List[Share]] =
shares(secret, requiredParts, totalParts, new Random(if(secret.nonEmpty) BigInt(secret).toLong else 0L))
def shares(secret: Array[Byte], requiredParts: Int, totalParts: Int): Either[Error, List[Share]] =
shares(secret, requiredParts, totalParts, Random)
def shares(secret: String, requiredParts: Int, totalParts: Int, random: Random): Either[Error, List[Share]] =
shares(secret.getBytes(UTF_8), requiredParts, totalParts, random)
def shares(secret: String, requiredParts: Int, totalParts: Int): Either[Error, List[Share]] =
shares(secret, requiredParts, totalParts, Random)
def unsafeShares(secret: String, requiredParts: Int, totalParts: Int): Either[Error, List[Share]] =
unsafeShares(secret.getBytes(UTF_8), requiredParts, totalParts)
def shares(secret: BigInt, requiredParts: Int, totalParts: Int, random: Random): Either[Error, List[Share]] =
shares(secret.toByteArray, requiredParts, totalParts, random)
def shares(secret: BigInt, requiredParts: Int, totalParts: Int): Either[Error, List[Share]] =
shares(secret, requiredParts, totalParts, Random)
def unsafeShares(secret: BigInt, requiredParts: Int, totalParts: Int): Either[Error, List[Share]] =
unsafeShares(secret.toByteArray, requiredParts, totalParts)
def combine(shares: List[Share]): Either[Error, String] =
combineToBigInt(shares).map(bigInt => new String(bigInt.toByteArray, UTF_8))
def combineToBytes(shares: List[Share]): Either[Error, Array[Byte]] =
combineToBigInt(shares).map(_.toByteArray)
}
object SSSS extends SSSSOps[ShareError, Share] {
def shares(secret: Array[Byte], requiredParts: Int, totalParts: Int, random: Random): Either[ShareError, List[Share]] =
for {
_ <- validateIn(requiredParts, totalParts, secret)
prime <- BigPrimes.extractPrime(secret)
} yield Shares(secret, requiredParts, totalParts, prime, random).get
def combineToBigInt(shares: List[Share]): Either[ShareError, BigInt] =
for {
_ <- validateShares(shares)
secret = LaGrangeInterpolation.coefficient0(shares)
_ <- validateSecret(secret.toByteArray, shares.head.hash.toArray)
} yield secret
}
示例13: KeyHasherTest
//设置package包名称以及导入依赖的类
package com.twitter.hashing
import com.twitter.io.TempFile
import java.util.Base64
import org.junit.runner.RunWith
import org.scalatest.WordSpec
import org.scalatest.junit.JUnitRunner
import scala.collection.mutable.ListBuffer
import java.nio.charset.StandardCharsets.UTF_8
@RunWith(classOf[JUnitRunner])
class KeyHasherTest extends WordSpec {
def readResource(name: String) = {
var lines = new ListBuffer[String]()
val src = scala.io.Source.fromFile(TempFile.fromResourcePath(getClass, "/"+name))
src.getLines
}
val base64 = Base64.getDecoder
def decode(str: String) = base64.decode(str.getBytes(UTF_8))
def testHasher(name: String, hasher: KeyHasher) = {
val sources = readResource(name + "_source") map { decode(_) }
val hashes = readResource(name + "_hashes")
assert(sources.size > 0)
sources zip hashes foreach { case (source, hashAsString) =>
val hash = BigInt(hashAsString).toLong
assert(hasher.hashKey(source) == hash)
}
}
"KeyHasher" should {
"correctly hash fnv1_32" in {
testHasher("fnv1_32", KeyHasher.FNV1_32)
}
"correctly hash fnv1_64" in {
testHasher("fnv1_64", KeyHasher.FNV1_64)
}
"correctly hash fnv1a_32" in {
testHasher("fnv1a_32", KeyHasher.FNV1A_32)
}
"correctly hash fnv1a_64" in {
testHasher("fnv1a_64", KeyHasher.FNV1A_64)
}
"correctly hash jenkins" in {
testHasher("jenkins", KeyHasher.JENKINS)
}
"correctly hash crc32 itu" in {
testHasher("crc32", KeyHasher.CRC32_ITU)
}
}
}
示例14: ReleaseStore
//设置package包名称以及导入依赖的类
package com.vivint.ceph
import java.util.UUID
import org.slf4j.LoggerFactory
import scala.async.Async.{async, await}
import scala.collection.immutable.Seq
import scala.concurrent.{ExecutionContext, Future}
import java.nio.charset.StandardCharsets.UTF_8
import com.vivint.ceph.kvstore.KVStore
import model._
import play.api.libs.json._
case class ReleaseStore(kvStore: KVStore) {
import PlayJsonFormats._
import ExecutionContext.Implicits.global
val log = LoggerFactory.getLogger(getClass)
val releasesPath = "reservation_releases"
def getReleases: Future[Seq[ReservationRelease]] = async {
val paths = await(kvStore.children(releasesPath)).
map { path =>
(releasesPath + "/" + path)
}
val result = await(kvStore.getAll(paths)).
flatten.
map { bytes => Json.parse(bytes).as[ReservationRelease] }
log.debug("loaded {} reservation releases", result.length)
result
}
def save(release: ReservationRelease): Future[Unit] = {
log.debug("saving release for reservation {}", release.id)
val data = Json.toJson(release).toString
kvStore.createOrSet(s"${releasesPath}/${release.id}", data.getBytes(UTF_8))
}
def delete(reservationId: UUID): Future[Unit] = {
log.debug("deleting release for reservation {}", reservationId)
kvStore.delete(s"${releasesPath}/${reservationId}")
}
}
示例15: JobStore
//设置package包名称以及导入依赖的类
package com.vivint.ceph
import com.vivint.ceph.kvstore.KVStore
import scala.collection.immutable.Seq
import scala.concurrent.{ ExecutionContext, Future }
import scala.async.Async.{async, await}
import play.api.libs.json._
import java.nio.charset.StandardCharsets.UTF_8
case class JobStore(kvStore: KVStore) {
private val tasksPath = "tasks"
import ExecutionContext.Implicits.global
import model._
import PlayJsonFormats._
def getTasks: Future[Seq[PersistentState]] = async {
val paths = await(kvStore.children(tasksPath)).
map { path =>
(tasksPath + "/" + path)
}
await(kvStore.getAll(paths)).
zip(paths).
map {
case (Some(bytes), _) =>
(Json.parse(bytes).as[PersistentState])
case (None, path) =>
throw new RuntimeException(s"Error: empty task state at path ${path}")
}
}
def save(task: PersistentState): Future[Unit] = {
val data = Json.toJson(task).toString
kvStore.createOrSet(s"tasks/${task.role}:" + task.id.toString, data.getBytes(UTF_8))
}
}