本文整理汇总了Scala中java.nio.charset.StandardCharsets类的典型用法代码示例。如果您正苦于以下问题:Scala StandardCharsets类的具体用法?Scala StandardCharsets怎么用?Scala StandardCharsets使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了StandardCharsets类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: RequestNormalization
//设置package包名称以及导入依赖的类
package com.lookout.ratelimitingfilter
import java.nio.charset.StandardCharsets
import java.util.UUID
import java.net.URLEncoder
import com.twitter.finagle.http.Request
import shapeless.tag._
import cats.implicits._
import com.lookout.ratelimitingfilter.models._
object RequestNormalization {
def apply(
serviceLookup: Request => Option[String @@ ServiceName],
claimLookup: Request => Option[(UUID @@ EntClaim, UUID @@ SubClaim)],
request: Request
): List[String] = {
val method = request.method
val path = encodePath(request.path)
val serviceBuckets: Option[List[String]] = serviceLookup(request).map {
serviceName => s"$method::$serviceName" :: s"$method::$path::$serviceName" :: Nil
}
val idBuckets: Option[List[String]] = claimLookup(request).map {
case (entUuid, subUuid) =>
s"$method::$path::$entUuid" :: s"$method::$path::$subUuid" :: Nil
}
(serviceBuckets |+| idBuckets).getOrElse(Nil)
}
def encodePath(path: String): String =
URLEncoder.encode(path, StandardCharsets.UTF_8.toString).toLowerCase
}
示例2: parse
//设置package包名称以及导入依赖的类
package parsers
import java.io.{InputStream, InputStreamReader}
import java.nio.charset.StandardCharsets
import java.nio.file.{Files, Path, Paths}
import javax.script.ScriptEngineManager
import com.google.common.base.Charsets
import com.google.common.io.CharStreams
import org.luaj.vm2.{LuaTable, LuaValue}
import scala.collection.breakOut
import scala.io.Source
trait FactorioParser[T] {
import FactorioParser._
def parse(path: String): Seq[T] = commonParse(readAll(path))
def parse(path: Path): Seq[T] = commonParse(readAll(path))
def parse(is: InputStream): Seq[T] = {
val str = CharStreams.toString(new InputStreamReader(is, Charsets.UTF_8))
commonParse(str)
}
def transport(table: LuaTable): Option[T]
private[this] def commonParse(target: String): Seq[T] = {
val dataLua = Source.fromURL(getClass.getResource("/data.lua")).mkString
val lua = dataLua + target
val engine = manager.getEngineByName("luaj")
engine.eval(lua)
val array: LuaTable = engine.get("array").asInstanceOf[LuaTable]
tableToSeq(array)(_.checktable()).flatMap(transport)
}
}
object FactorioParser {
private val manager = new ScriptEngineManager()
def readAll(path: String): String = readAll(Paths.get(path))
def readAll(path: Path): String =
new String(Files.readAllBytes(path), StandardCharsets.UTF_8)
def tableToSeq[T](table: LuaTable)(f: LuaValue => T): Seq[T] = {
table.keys().map(table.get).map(f)(breakOut)
}
def tableToMap[K, V](table: LuaTable)(f: LuaValue => K)(g: LuaValue => V): Map[K, V] = {
table.keys().map { key =>
f(key) -> g(table.get(key))
}(breakOut)
}
}
示例3: PasswordHasherSHA256
//设置package包名称以及导入依赖的类
package controllers
import java.nio.charset.StandardCharsets
import com.mohiva.play.silhouette.api.util.{PasswordHasher, PasswordInfo}
import org.apache.directory.api.ldap.model.constants.LdapSecurityConstants
import org.apache.directory.api.ldap.model.password.PasswordUtil
class PasswordHasherSHA256 extends PasswordHasher{
override def id: String = "SHA-256"
override def hash(plainPassword: String): PasswordInfo = {
PasswordInfo(hasher = id, password = plainPassword)
}
//TODO: Here might be an oversight. It seems to be to complicated.
override def matches(passwordInfo: PasswordInfo, suppliedPassword: String): Boolean = {
val suppliedStoragePW = PasswordUtil.createStoragePassword(
suppliedPassword,
LdapSecurityConstants.getAlgorithm(passwordInfo.hasher)
)
val suppliedPW = new String(PasswordUtil.splitCredentials(suppliedStoragePW).getPassword,StandardCharsets.UTF_8)
val storedPW = passwordInfo.password
suppliedPW.equals(storedPW)
}
}
示例4: ExportModel
//设置package包名称以及导入依赖的类
package com.cloudera.datascience.cdsw.acme
import java.nio.charset.StandardCharsets
import java.nio.file.StandardOpenOption._
import java.nio.file.{Files, Paths}
import javax.xml.transform.stream.StreamResult
import org.dmg.pmml.Application
import org.jpmml.model.JAXBUtil
import org.jpmml.sparkml.ConverterUtil
import acme.ACMEModel
object ExportModel {
def main(args: Array[String]): Unit = {
val training = ACMEData.readData()
val pipeline = ACMEModel.buildModel()
val pmml = ConverterUtil.toPMML(training.schema, pipeline)
pmml.getHeader.setApplication(new Application("ACME Occupancy Detection"))
val modelPath = Paths.get("src", "main", "resources")
if (!Files.exists(modelPath)) {
Files.createDirectory(modelPath)
}
val pmmlFile = modelPath.resolve("model.pmml")
val writer = Files.newBufferedWriter(pmmlFile, StandardCharsets.UTF_8, WRITE, CREATE, TRUNCATE_EXISTING)
try {
JAXBUtil.marshalPMML(pmml, new StreamResult(writer))
} finally {
writer.close()
}
}
}
示例5: MedicineProcess
//设置package包名称以及导入依赖的类
package cn.com.warlock.practice.ml
import java.io.BufferedReader
import java.nio.charset.StandardCharsets
import java.nio.file.{Files, Paths}
import org.apache.spark.ml.UnaryTransformer
import org.apache.spark.ml.param.ParamMap
import org.apache.spark.ml.util.Identifiable
import org.apache.spark.sql.types.{ArrayType, DataType, StringType}
import scala.collection.mutable.Set
class MedicineProcess(override val uid: String, private val dict: String)
extends UnaryTransformer[Seq[String], Seq[String], MedicineProcess] {
def this(dict: String) = this(Identifiable.randomUID("med"), dict)
// ?????????
private val wordsSet = loadDict
// ????
private def loadDict: Set[String] = {
val br: BufferedReader = Files.newBufferedReader(Paths.get(dict), StandardCharsets.UTF_8)
val words = Set[String]()
var count = 0
while (br.ready()) {
words += br.readLine()
count += 1
}
println(s"load med words: $count")
words
}
override protected def createTransformFunc: Seq[String] => Seq[String] = (words: Seq[String]) => {
// ?? "???", arr ?????????, c ??????? word
words.foldLeft(List[String]())((arr, c) => {
val newC = wordsSet.contains(c) match {
case true => List(c, "_MED_")
case false => List(c)
}
arr ++ newC
})
}
override protected def validateInputType(inputType: DataType): Unit = {
require(inputType.isInstanceOf[ArrayType],
s"The input column must be ArrayType, but got $inputType.")
}
override protected def outputDataType: DataType = new ArrayType(StringType, true)
override def copy(extra: ParamMap): MedicineProcess = defaultCopy(extra)
}
示例6: SalesforceCustomerRepoSpec
//设置package包名称以及导入依赖的类
package com.ovoenergy.comms.profiles.salesforce
import java.nio.charset.StandardCharsets
import java.nio.file.{Files, Paths}
import com.ovoenergy.comms.profiles.domain._
import okhttp3._
import org.scalatest.{EitherValues, FlatSpec, Matchers}
class SalesforceCustomerRepoSpec extends FlatSpec with Matchers with EitherValues {
behavior of "SalesforceCustomerRepo"
val request = new Request.Builder().url("http://google.com").get().build()
def buildResponse(code: Int, body: String): (String) => Either[SalesforceError, Response] =
(str: String) =>
Right(
new Response.Builder()
.protocol(Protocol.HTTP_1_1)
.code(code)
.request(request)
.body(ResponseBody.create(MediaType.parse("application/json"), body))
.build()
)
it should "Successfull deserialise a valid customer json body" in {
val json =
new String(Files.readAllBytes(Paths.get("src/test/resources/customer_profile.json")), StandardCharsets.UTF_8)
val makeAuthenticatedRequest = buildResponse(200, json)
val profile = SalesforceCustomerRepo.getCustomerProfile(makeAuthenticatedRequest)("customer123")
profile shouldBe Right(
Customer(
Name(Some("Mr"), "Gary", "Philpott", None),
EmailAddresses(Some("[email protected]"), None),
TelephoneNumbers(Some("+441285112233"), Some("+447834774651"), Some("+441285112233")),
Embedded(
List(
CommunicationPreference("SERVICE", List("Sms", "Email", "POST")),
CommunicationPreference("MARKETING", List.empty),
CommunicationPreference("THIRDPARTY", List.empty)
)
)
)
)
}
it should "display an appropriate message if invalid json is returned" in {
val makeAuthenticatedRequest = buildResponse(200, """{"some": "thing"}""")
val profile = SalesforceCustomerRepo.getCustomerProfile(makeAuthenticatedRequest)("customer123")
profile.left.value.message should startWith("Invalid JSON from salesforce api")
}
}
示例7: ProvisionUser
//设置package包名称以及导入依赖的类
// Copyright (c) 2017 Grier Forensics. All Rights Reserved.
package com.grierforensics.greatdane.connector
import java.net.URI
import java.nio.charset.StandardCharsets
import java.nio.file.{Files, Paths}
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import org.apache.commons.io.IOUtils
import org.apache.http.HttpHeaders
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.HttpClients
object ProvisionUser {
def main(args: Array[String]): Unit = {
def die = {
println("Usage: provision-user <email-address> [<certificate file>]")
sys.exit(1)
}
val (emailAddress, certPem) = args.toList match {
case email :: tail => tail match {
case Nil => (email, "")
case certFile :: Nil => (email, new String(Files.readAllBytes(Paths.get(certFile)), StandardCharsets.UTF_8))
case _ => die
}
case _ => die
}
val client = HttpClients.createDefault()
val uri = new URI(s"http://${Settings.Host}:${Settings.Port}/api/v1/user/$emailAddress")
val post = new HttpPost(uri)
post.addHeader(HttpHeaders.CONTENT_TYPE, "application/json")
post.addHeader(HttpHeaders.AUTHORIZATION, Settings.ApiKey)
println(post.toString)
val req = ProvisionRequest(None, if (certPem.length > 0) Some(Seq(certPem)) else None)
val mapper = new ObjectMapper().registerModule(DefaultScalaModule)
val json = mapper.writeValueAsString(req)
println(json)
post.setEntity(new StringEntity(json))
val resp = client.execute(post)
try {
val entity = resp.getEntity
println(resp.getStatusLine.getStatusCode, resp.getStatusLine.getReasonPhrase)
println(IOUtils.toString(entity.getContent, StandardCharsets.UTF_8))
} finally {
resp.close()
}
}
}
示例8: SimpleKafkaProducerTest
//设置package包名称以及导入依赖的类
package com.example
import java.nio.charset.StandardCharsets
import kafka.consumer.ConsumerConfig
import kafka.utils.TestUtils
import org.scalatest.{FunSpec, Matchers}
//import org.junit.Assert._
import scala.collection.immutable.HashMap
class SimpleKafkaProducerTest extends FunSpec with Matchers{
private val topic = "test"
private val groupId = "group0"
private val kafkaHelpers = new KafkaHelpers()
case class MessageData(a: String, b: String)
describe("The SimpleKafka Api") {
it("Should send data using a producer") {
//Send data to Kafka
val kafkaApi = new SimpleKafkaProducer(kafkaHelpers.kafkaSocket(), topic)
kafkaApi.send[MessageData](new MessageData("Hello", "World"))
//Create consumer
val consumerProperties = TestUtils.createConsumerProperties(kafkaHelpers.zookeeperSocket().toString(), groupId, "consumer0", -1)
val consumer = kafka.consumer.Consumer.create(new ConsumerConfig(consumerProperties))
val topicCountMap = HashMap(topic -> 1)
val consumerMap = consumer.createMessageStreams(topicCountMap)
val stream = consumerMap.get(topic).get(0)
val iterator = stream.iterator()
val msg = new String(iterator.next().message(), StandardCharsets.UTF_8)
assert("{\"a\":\"Hello\",\"b\":\"World\"}" == msg)
// cleanup
consumer.shutdown()
}
}
}
示例9: ScheduleDownloadActor
//设置package包名称以及导入依赖的类
package logic.actors.schedule
import java.nio.charset.StandardCharsets
import javax.inject._
import akka.actor.{Actor, ActorRef}
import helpers.SpiritHelper
import logic.actors.schedule.ScheduleDownloadActor.DownloadSchedule
import logic.actors.schedule.ScheduleParseActor._
import org.fhs.spirit.scheduleparser.enumerations.EScheduleKind
import org.jsoup.Jsoup
import play.api.libs.ws.WSClient
import scala.collection.JavaConversions._
import scala.concurrent.Await
import scala.concurrent.duration._
@Singleton
class ScheduleDownloadActor @Inject()(ws: WSClient, @Named("parseActor") parseActor: ActorRef) extends Actor with SpiritHelper {
override def receive: Receive = {
case DownloadSchedule =>
val baseUrl = configuration.underlying.getString("schedule.baseUrl")
val lectureResults = uncachedCourseNames.map {
courseName =>
val outcome = "s_" + courseName + ".html"
val httpResult = Await.result(ws.url(baseUrl + outcome).get(), 10 seconds)
if (httpResult.status != 404) {
Some((httpResult.bodyAsBytes.decodeString(StandardCharsets.ISO_8859_1.toString), courseName))
} else {
None
}
}.filter(_.nonEmpty).map(rs => (Jsoup.parse(rs.get._1).toString, rs.get._2)).map(rs => (EScheduleKind.REGULAR, rs))
val blockBaseResult = Await.result(ws.url(baseUrl + "bindex.html").get(), 10 seconds)
val bindex = Jsoup.parse(blockBaseResult.bodyAsBytes.decodeString(StandardCharsets.ISO_8859_1.toString))
val blockRefs = bindex.select("a").map(_.attr("href")).toSet
val blockResult = blockRefs.map {
block =>
val httpResult = Await.result(ws.url(baseUrl + block).get(), 10 seconds)
if (httpResult.status != 404) {
Some((httpResult.bodyAsBytes.decodeString(StandardCharsets.ISO_8859_1.toString), block))
} else {
None
}
}.filter(_.nonEmpty).map(rs => (Jsoup.parse(rs.get._1).toString, rs.get._2)).map(rs => (EScheduleKind.BLOCK, rs))
parseActor ! ParseSchedule(lectureResults ++ blockResult)
}
}
示例10: fromXmlGetReportListResponse
//设置package包名称以及导入依赖的类
package com.wegtam.amws.reports.adt
import java.io.ByteArrayInputStream
import java.nio.charset.StandardCharsets
import java.time.OffsetDateTime
import com.wegtam.amws.reports.ReportType
import eu.cdevreeze.yaidom.parse.DocumentParserUsingSax
import eu.cdevreeze.yaidom.queryapi.HasENameApi.withLocalName
import eu.cdevreeze.yaidom.simple.Elem
import scala.collection.immutable.Seq
import scala.util.Try
def fromXmlGetReportListResponse(s: String): Seq[ReportInfo] = {
val eo: Option[Elem] = for {
p <- Try(DocumentParserUsingSax.newInstance()).toOption
d <- Try(p.parse(new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8)))).toOption
e <- d.documentElement.findChildElem(withLocalName("GetReportListResult"))
} yield e
eo.fold(Seq.empty[ReportInfo]) { e =>
val it = for {
c <- e.filterChildElems(withLocalName("ReportInfo"))
r <- fromXmlElement(c)
} yield r
it
}
}
}
示例11: SignRequest
//设置package包名称以及导入依赖的类
package com.wegtam.amws.common
import java.nio.charset.StandardCharsets
import java.util.Base64
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
import com.wegtam.amws.common.Request.{ ParameterName, RequestParameters }
import scala.util.Try
object SignRequest {
// The field name for the query parameter that will hold the signature.
final val SignatureRequestFieldName: ParameterName = "Signature"
// These fields are needed for signed requests and must be added accordingly.
final val SignatureRequestFields: RequestParameters = Map(
"SignatureMethod" -> "HmacSHA256",
"SignatureVersion" -> "2"
)
def sign(key: Array[Byte], data: Array[Byte]): Try[String] = Try {
val algo = "HmacSHA256"
val base = Base64.getEncoder
val hmac = Mac.getInstance(algo)
val skey = new SecretKeySpec(key, algo)
hmac.init(skey)
val sig = hmac.doFinal(data)
new String(base.encode(sig), StandardCharsets.UTF_8)
}
}
示例12: format
//设置package包名称以及导入依赖的类
package akka.stream.alpakka.csv.scaladsl
import java.nio.charset.{Charset, StandardCharsets}
import akka.NotUsed
import akka.stream.alpakka.csv.{javadsl, CsvFormatter}
import akka.stream.scaladsl.{Flow, Source}
import akka.util.ByteString
import scala.collection.immutable
def format[T <: immutable.Iterable[String]](
delimiter: Char = Comma,
quoteChar: Char = DoubleQuote,
escapeChar: Char = Backslash,
endOfLine: String = "\r\n",
quotingStyle: CsvQuotingStyle = CsvQuotingStyle.Required,
charset: Charset = StandardCharsets.UTF_8,
byteOrderMark: Option[ByteString] = None
): Flow[T, ByteString, NotUsed] = {
val formatter =
new CsvFormatter(delimiter, quoteChar, escapeChar, endOfLine, quotingStyle, charset)
byteOrderMark.fold {
Flow[T].map(formatter.toCsv).named("CsvFormatting")
} { bom =>
Flow[T].map(formatter.toCsv).named("CsvFormatting").prepend(Source.single(bom))
}
}
}
示例13: PasswordHasherMD5
//设置package包名称以及导入依赖的类
package controllers
import java.nio.charset.StandardCharsets
import com.mohiva.play.silhouette.api.util.{PasswordHasher, PasswordInfo}
import org.apache.directory.api.ldap.model.constants.LdapSecurityConstants
import org.apache.directory.api.ldap.model.password.PasswordUtil
class PasswordHasherMD5 extends PasswordHasher{
override def id: String = "MD5"
override def hash(plainPassword: String): PasswordInfo = {
PasswordInfo(hasher = id, password = plainPassword)
}
//TODO: Here might be an oversight. It seems to be to complicated.
override def matches(passwordInfo: PasswordInfo, suppliedPassword: String): Boolean = {
val suppliedStoragePW = PasswordUtil.createStoragePassword(
suppliedPassword,
LdapSecurityConstants.getAlgorithm(passwordInfo.hasher)
)
val suppliedPW = new String(PasswordUtil.splitCredentials(suppliedStoragePW).getPassword,StandardCharsets.UTF_8)
val storedPW = passwordInfo.password
suppliedPW.equals(storedPW)
}
}
示例14: encodePacket
//设置package包名称以及导入依赖的类
package roc
import roc.postgresql.transport.{PacketDecoder, PacketDecoderImplicits, Packet, PacketEncoder,
PacketEncoderImplicits}
import java.nio.charset.StandardCharsets
package object postgresql extends PacketEncoderImplicits with PacketDecoderImplicits {
def encodePacket[A <: FrontendMessage: PacketEncoder](a: A): Packet =
implicitly[PacketEncoder[A]].apply(a)
def decodePacket[A <: BackendMessage: PacketDecoder](p: Packet): PacketDecoder.Result[A] =
implicitly[PacketDecoder[A]].apply(p)
def lengthOfCStyleString(str: String): Int = {
val bytes = str.getBytes(StandardCharsets.UTF_8)
bytes.length + 1
}
def lengthOfCStyleStrings(xs: List[String]): Int = xs match {
case h :: t => xs.map(lengthOfCStyleString).reduce(_ + _)
case t => 0
}
}
示例15: MessagesSpec
//设置package包名称以及导入依赖的类
package roc
package postgresql
import java.nio.charset.StandardCharsets
import java.security.MessageDigest
import org.scalacheck.Arbitrary.arbitrary
import org.scalacheck.Prop.forAll
import org.scalacheck.{Arbitrary, Gen}
import org.specs2._
final class MessagesSpec extends Specification with ScalaCheck { def is = s2"""
PasswordMessage
should MD5 encrypt a password with given salt $pmEncrypt
"""
val pmEncrypt = forAll { (user: String, pm: PasswordMessage, salt: Array[Byte]) =>
val md = MessageDigest.getInstance("MD5")
md.update((pm.password+ user).getBytes(StandardCharsets.UTF_8))
val unsaltedHexStr = md.digest().map(x => "%02x".format(x.byteValue)).foldLeft("")(_ + _)
val saltedBytes = unsaltedHexStr.getBytes ++ salt
md.reset()
md.update(saltedBytes)
val passwd = md.digest().map(x => "%02x".format(x.byteValue)).foldLeft("md5")(_ + _)
passwd must_== PasswordMessage.encryptMD5Passwd(user, pm.password, salt)
}
lazy val genByte: Gen[Byte] = arbitrary[Byte]
lazy val genSalt: Gen[Array[Byte]] = Gen.containerOfN[Array, Byte](4, genByte)
lazy val genPasswordMessage: Gen[PasswordMessage] = for {
password <- arbitrary[String]
} yield new PasswordMessage(password)
implicit lazy val implicitPasswordMessage: Arbitrary[PasswordMessage] =
Arbitrary(genPasswordMessage)
}