本文整理汇总了Scala中org.apache.commons.io.IOUtils类的典型用法代码示例。如果您正苦于以下问题:Scala IOUtils类的具体用法?Scala IOUtils怎么用?Scala IOUtils使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了IOUtils类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Scala代码示例。
示例1: BEncodeTest
//设置package包名称以及导入依赖的类
import java.time.Instant
import com.karasiq.bittorrent.format.{Torrent, TorrentFile, TorrentPiece}
import org.apache.commons.codec.binary.Hex
import org.apache.commons.io.IOUtils
import org.scalatest.{FlatSpec, Matchers}
class BEncodeTest extends FlatSpec with Matchers {
val testFile = IOUtils.toByteArray(getClass.getResource("ubuntu-15.10-desktop-amd64.iso.torrent"))
"BEncode parser" should "parse torrent file" in {
val torrent = Torrent(testFile)
Hex.encodeHexString(torrent.infoHash.toArray).toUpperCase shouldBe "3F19B149F53A50E14FC0B79926A391896EABAB6F"
torrent.announce shouldBe "http://torrent.ubuntu.com:6969/announce"
torrent.announceList shouldBe Vector(Vector("http://torrent.ubuntu.com:6969/announce"), Vector("http://ipv6.torrent.ubuntu.com:6969/announce"))
torrent.comment shouldBe Some("Ubuntu CD releases.ubuntu.com")
torrent.date shouldBe Some(Instant.parse("2015-10-22T09:48:19Z"))
torrent.data.pieceLength shouldBe 524288L
torrent.data.pieces.length shouldBe 44960
torrent.data.files.headOption shouldBe Some(TorrentFile("ubuntu-15.10-desktop-amd64.iso", 1178386432L))
}
"Torrent pieces" should "be constructed" in {
val torrent = Torrent(testFile)
val pieces = TorrentPiece.pieces(torrent.data)
pieces.length shouldBe (torrent.data.pieces.length / 20)
pieces.map(_.size).sum shouldBe torrent.size
pieces.head.sha1.length shouldBe 20
}
"Torrent piece blocks" should "be constructed" in {
val torrent = Torrent(testFile)
val pieces = TorrentPiece.pieces(torrent.data)
val blocks = TorrentPiece.blocks(pieces.head, 10000)
blocks.map(_.size).sum shouldBe pieces.head.size
}
}
开发者ID:Karasiq,项目名称:torrentstream,代码行数:38,代码来源:BEncodeTest.scala
示例2: 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()
}
}
}
开发者ID:grierforensics,项目名称:Great-DANE-Connector,代码行数:59,代码来源:ProvisionUser.scala
示例3: Jade4jCompiler
//设置package包名称以及导入依赖的类
package com.karasiq.scalajsbundler.compilers
import java.io.InputStreamReader
import com.karasiq.scalajsbundler.ScalaJSBundler.{FileAsset, PageTypedContent}
import de.neuland.jade4j.Jade4J
import org.apache.commons.io.IOUtils
import scala.collection.JavaConversions._
import scala.util.control.Exception
class Jade4jCompiler extends AssetCompiler {
override def compile(contents: Seq[PageTypedContent]): String = {
val compiled = contents.map(_.asset match {
case FileAsset(file) ?
Jade4J.render(file, Map.empty[String, AnyRef], false)
case a ?
val reader = new InputStreamReader(a.content(), "UTF-8")
Exception.allCatch.andFinally(IOUtils.closeQuietly(reader)) {
Jade4J.render(reader, "input.jade", Map.empty[String, AnyRef], false)
}
})
HtmlConcatCompiler.concat(compiled)
}
}
开发者ID:Karasiq,项目名称:sbt-scalajs-bundler,代码行数:28,代码来源:Jade4jCompiler.scala
示例4: WebAssetsCache
//设置package包名称以及导入依赖的类
package com.karasiq.scalajsbundler
import java.io._
import java.net.URL
import org.apache.commons.io.IOUtils
import scala.util.Try
import scala.util.control.Exception
import scalaj.http.{Http, HttpOptions}
object WebAssetsCache {
private val cacheDir = "target/cached-webassets"
def inputStream(url: String): InputStream = {
val cachedName: String = {
val (host, file) = new URL(url) match { case u ?
u.getHost ? u.getFile
}
s"${Integer.toHexString(host.hashCode)}/${Integer.toHexString(file.hashCode)}"
}
val cached: Option[File] = {
Try(new File(s"$cacheDir/$cachedName"))
.filter(f ? f.exists() && f.isFile && f.length() > 0)
.toOption
}
cached match {
case Some(file) ?
new FileInputStream(file)
case None ?
val http = Http(url).options(HttpOptions.connTimeout(10000), HttpOptions.readTimeout(10000)).asBytes
assert(http.isSuccess, s"Web asset download failed: $url")
val bytes = http.body
val outputStream = Try {
val file = new File(s"$cacheDir/$cachedName")
require(file.getParentFile.isDirectory || file.getParentFile.mkdirs(), s"Not a directory: ${file.getParentFile}")
new FileOutputStream(file)
}
outputStream.foreach { stream ?
Exception.allCatch.andFinally(IOUtils.closeQuietly(stream)) {
IOUtils.write(bytes, stream)
}
}
new ByteArrayInputStream(bytes)
}
}
def text(url: String): String = {
val inputStream = WebAssetsCache.inputStream(url)
Exception.allCatch.andFinally(IOUtils.closeQuietly(inputStream)) {
IOUtils.toString(inputStream, "UTF-8")
}
}
}
开发者ID:Karasiq,项目名称:sbt-scalajs-bundler,代码行数:59,代码来源:WebAssetsCache.scala
示例5: EntityDataFilter
//设置package包名称以及导入依赖的类
package org.dele.misc
import java.io.InputStream
import org.apache.commons.io.IOUtils
import org.dele.misc.EntityData.EntDetail
object EntityDataFilter extends App {
import tgz.TgzUtil._
val defaultEncoding = "UTF-8"
def extractOne(in:InputStream):Map[String,EntDetail] = {
val instr = IOUtils.toString(in, defaultEncoding)
val entData = EntityData.Ser.p(instr)
entData.entity_details.entMap.filter(_._2.curated == 1)
}
def extract(path:String):Map[String, EntDetail] = processAllFiles(path, extractOne).reduce(_ ++ _)
private val datePartLength = 10
def processGroupByDate(em:Map[String,EntDetail], days:Int) = {
val dateGroups = em.groupBy(_._2.created.map(_.substring(0,datePartLength)))
val sortedGroups = dateGroups.toIndexedSeq.sortBy(_._1)(Ordering[Option[String]].reverse).take(days)
sortedGroups.foreach{ g =>
println(s"${g._1} (${g._2.size})")
val sorted = sortByCreatedDesc(g._2.values.toSeq)
sorted.foreach(e => println(s"\t$e"))
}
}
def sortByCreatedDesc(seq:Seq[EntDetail]):Seq[EntDetail] = seq.sortBy(_.created)(Ordering[Option[String]].reverse)
def processBatch(em:Map[String,EntDetail], tag:String, latestCount:Int) = {
val checkedEntities = em.toList.filter(_._2.curated == 1).toMap
println("=====================================================\n")
println(s"\n\n================== batch tag $tag ===================\n\n")
println("=====================================================\n")
println(s"Checked entity count: ${checkedEntities.size}")
//val checkedByDate = checkedEntities.sortBy(_._2.created)(Ordering[Option[String]].reverse).take(20)
processGroupByDate(checkedEntities, latestCount)
//val uncheckedByDate = em.toIndexedSeq.sortBy(_._2.created)(Ordering[Option[String]].reverse).take(30)
//println(checkedByDate.map(_._2).mkString("\n"))
println("\n\n=====================================================\n\n")
//println(uncheckedByDate.map(_._2).mkString("\n"))
processGroupByDate(em, latestCount)
}
def checked(path:String) = {
val entMap = extract(path)
println(entMap.keys.mkString("[\"", "\", \"", "\"]"))
}
checked(
"E:\\VMShare\\facility-161129-21.tgz"
)
}
开发者ID:new2scala,项目名称:text-util,代码行数:60,代码来源:EntityDataFilter.scala
示例6: ResourceScraperService
//设置package包名称以及导入依赖的类
package pl.mojepanstwo.sap.toakoma
import pl.mojepanstwo.sap.toakoma.services.Scraper
import org.jsoup.nodes.Document
import org.jsoup.Jsoup
import scala.io.Source
import java.io.File
import java.nio.file.Files
import org.apache.commons.io.IOUtils
import java.io.FileOutputStream
class ResourceScraperService extends Scraper {
def get(url: String) : Document = {
val pattern = ".*id=(.*)&type=([0-9]+).*".r
val pattern(id, docType) = url
Jsoup.parse(Source.fromResource("isap/" + id + "/" + docType + ".html").mkString)
}
def dowloadFile(fileUrl:String, filePath:String) : String = {
val pattern = ".*id=(.*)&type=([0-9]+).*".r
val pattern(id, docType) = fileUrl
val src = getClass.getResourceAsStream("/isap/" + id + "/" + docType + ".pdf")
val dest = new File(filePath)
val out = new FileOutputStream(dest)
IOUtils.copy(src, out)
src.close()
out.close()
dest.getAbsolutePath
}
}
开发者ID:PrawoPolskie,项目名称:toakoma,代码行数:33,代码来源:ResourceScraperService.scala
示例7: MultipartUploadSpec
//设置package包名称以及导入依赖的类
package io.peregrine
import io.peregrine.test.FlatSpecHelper
import com.twitter.finagle.http.{Request => FinagleRequest}
import org.apache.commons.io.IOUtils
class MultipartUploadSpec extends FlatSpecHelper {
class ExampleApp extends Controller {
post("/groups_file") { request =>
val groupsParam = request.multiParams.get("groups")
val typeParam = request.multiParams.get("type")
render
.header("X-Content-Type", groupsParam.get.contentType.toString)
.header("X-Filename", groupsParam.get.filename.toString)
.header("X-Type-Text", typeParam.get.value)
.plain("ok").toFuture
}
}
val server = new PeregrineServer
server.register(new ExampleApp)
"Multi part uploads with text and file fields" should "work" in {
val s = getClass.getResourceAsStream("/upload.bytes")
val b = IOUtils.toByteArray(s)
val r = FinagleRequest.decodeBytes(b)
send(r)
response.code should equal (200)
response.getHeader("X-Content-Type") should equal("Some(image/gif)")
response.getHeader("X-Filename") should equal("Some(dealwithit.gif)")
response.getHeader("X-Type-Text") should equal("text")
}
}
开发者ID:pairi,项目名称:pairi,代码行数:42,代码来源:MultipartUploadSpec.scala
示例8: Launcher
//设置package包名称以及导入依赖的类
import org.slf4j.LoggerFactory
import com.argcv.spark.utils.SparkHelper.sc
import com.argcv.spark.utils.SparkHelper.ss
import org.apache.spark.rdd.RDD
import org.apache.commons.io.IOUtils
import java.net.URL
import java.nio.charset.Charset
object Launcher extends App {
import ss.implicits._
//
lazy val logger = LoggerFactory.getLogger(Launcher.getClass)
val timeStart = System.currentTimeMillis()
// Zeppelin creates and injects sc (SparkContext) and sqlContext (HiveContext or SqlContext)
// So you don't need create them manually
// load bank data
val bankText: RDD[String] = sc.parallelize(
IOUtils.toString(
new URL("https://s3.amazonaws.com/apache-zeppelin/tutorial/bank/bank.csv"),
Charset.forName("utf8")).split("\n"))
val bank = bankText.map(s => s.split(";")).filter(s => s(0) != "\"age\"").map(
s => Bank(s(0).toInt,
s(1).replaceAll("\"", ""),
s(2).replaceAll("\"", ""),
s(3).replaceAll("\"", ""),
s(5).replaceAll("\"", "").toInt
)
).toDF()
case class Bank(age: Integer, job: String, marital: String, education: String, balance: Integer)
bank.createOrReplaceTempView("bank")
ss.sql("select age, job from bank where age < 35 order by age desc limit 10").show()
logger.info(s"finished , all time cost : ${System.currentTimeMillis() - timeStart}")
}
开发者ID:yuikns,项目名称:my-spark,代码行数:42,代码来源:Launcher.scala
示例9: ReportEngineSpec
//设置package包名称以及导入依赖的类
package au.id.tmm.senatedb.core.engine
import au.id.tmm.senatedb.core.fixtures._
import au.id.tmm.senatedb.core.model.SenateElection
import au.id.tmm.senatedb.core.model.parsing.Party.RegisteredParty
import au.id.tmm.senatedb.core.reporting.ExhaustedVotesReportBuilder
import au.id.tmm.senatedb.core.tallies._
import au.id.tmm.utilities.geo.australia.State
import au.id.tmm.utilities.testing.ImprovedFlatSpec
import org.apache.commons.io.IOUtils
import scala.concurrent.Await
import scala.concurrent.duration.Duration
class ReportEngineSpec extends ImprovedFlatSpec {
"the report engine" should "construct a report as expected" in {
val ballotMaker = BallotMaker(Candidates.ACT)
import ballotMaker.group
val countFormalBallots = TallierBuilder.counting(BallotCounter.FormalBallots)
val countExhaustedVotes = TallierBuilder.counting(BallotCounter.ExhaustedVotes)
val tallyEngine = MockTallyEngine.thatReturns(TallyBundle(
countFormalBallots.overall() -> Tally0(42),
countFormalBallots.groupedBy(BallotGrouping.FirstPreferencedPartyNationalEquivalent) -> Tally1(RegisteredParty("Oranges") -> 22, RegisteredParty("Apples") -> 20),
countFormalBallots.groupedBy(BallotGrouping.State) -> Tally1(State.ACT -> 42d),
countFormalBallots.groupedBy(BallotGrouping.Division) -> Tally1(Divisions.ACT.CANBERRA -> 42d),
countFormalBallots.groupedBy(BallotGrouping.State, BallotGrouping.FirstPreferencedGroup) -> Tally2(State.ACT -> Tally1(group("C") -> 23, group("I") -> 19)),
countExhaustedVotes.overall() -> Tally0(32),
countExhaustedVotes.groupedBy(BallotGrouping.FirstPreferencedPartyNationalEquivalent) -> Tally1(RegisteredParty("Oranges") -> 17, RegisteredParty("Apples") -> 15),
countExhaustedVotes.groupedBy(BallotGrouping.State) -> Tally1(State.ACT -> 32d),
countExhaustedVotes.groupedBy(BallotGrouping.Division) -> Tally1(Divisions.ACT.CANBERRA -> 32d),
countExhaustedVotes.groupedBy(BallotGrouping.State, BallotGrouping.FirstPreferencedGroup) -> Tally2(State.ACT -> Tally1(group("C") -> 23, group("I") -> 9))
))
val reportFuture = ReportEngine.runFor(
MockParsedDataStore,
tallyEngine,
SenateElection.`2016`,
Set(State.ACT),
Set(ExhaustedVotesReportBuilder)
)
val actualReport = Await.result(reportFuture, Duration.Inf).head
val expectedMarkdown = IOUtils.toString(getClass.getResourceAsStream("report_engine_spec_expected_markdown.md"), "UTF-8")
assert(actualReport.asMarkdown === expectedMarkdown)
}
}
开发者ID:tmccarthy,项目名称:SenateDB,代码行数:54,代码来源:ReportEngineSpec.scala
示例10: Client
//设置package包名称以及导入依赖的类
package pl.writeonly.babel.beans
import com.weiglewilczek.slf4s.Logging
import java.io.IOException
import org.apache.commons.httpclient.methods.GetMethod
import org.apache.commons.httpclient.HttpClient
import org.apache.commons.httpclient.HttpStatus
import org.apache.commons.io.IOUtils
class Client extends Logging{
val client = new HttpClient
val method = new GetMethod("http://structureddata.wikispaces.com/Test");
try {
if (HttpStatus.SC_OK == client.executeMethod(method)) {
logger.info(IOUtils.toString(method.getResponseBodyAsStream()));
} else {
throw new IOException("Unable to load page, error " + method.getStatusLine());
}
} finally {
method.releaseConnection();
}
}
开发者ID:writeonly,项目名称:babel,代码行数:23,代码来源:Client.scala
示例11: CompressionUtils
//设置package包名称以及导入依赖的类
package com.flipkart.connekt.commons.utils
import java.io.{ByteArrayInputStream, ByteArrayOutputStream}
import java.util.Base64
import java.util.zip.{GZIPInputStream, GZIPOutputStream}
import com.flipkart.connekt.commons.core.Wrappers._
import org.apache.commons.io.IOUtils
import scala.util.Try
import scala.util.control.NoStackTrace
object CompressionUtils {
def inflate(deflatedTxt: String): Try[String] = Try_ {
val bytes = Base64.getUrlDecoder.decode(deflatedTxt)
try {
val zipInputStream = new GZIPInputStream(new ByteArrayInputStream(bytes))
IOUtils.toString(zipInputStream)
} catch {
case ex:java.util.zip.ZipException =>
throw new Exception(ex.getMessage) with NoStackTrace
}
}
def deflate(txt: String): Try[String] = Try_ {
val arrOutputStream = new ByteArrayOutputStream()
val zipOutputStream = new GZIPOutputStream(arrOutputStream)
zipOutputStream.write(txt.getBytes)
zipOutputStream.close()
Base64.getUrlEncoder.encodeToString(arrOutputStream.toByteArray)
}
implicit class StringCompress(val s: String) {
def compress: Try[String] = deflate(s)
def decompress: Try[String] = inflate(s)
}
}
开发者ID:ayush03agarwal,项目名称:connekt,代码行数:40,代码来源:CompressionUtils.scala
示例12: TwitterInfoClient
//设置package包名称以及导入依赖的类
package clients
import config.ConfigValues
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer
import org.apache.commons.io.IOUtils
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.DefaultHttpClient
object TwitterInfoClient {
def fetchInfo(userId: String): Either[String,String] = {
val consumer = new CommonsHttpOAuthConsumer(ConfigValues.twitterConsumerKey, ConfigValues.twitterConsumerSecret)
consumer.setTokenWithSecret(ConfigValues.twitterAccessToken, ConfigValues.twitterAccessTokenSecret)
val request = new HttpGet(s"https://api.twitter.com/1.1/users/show.json?screen_name=$userId")
consumer.sign(request)
val client = new DefaultHttpClient()
val response = client.execute(request)
println(response.getStatusLine.getStatusCode)
response.getStatusLine.getStatusCode match {
case 200 =>
val responseString = IOUtils.toString(response.getEntity.getContent)
println(s"\n\n$responseString")
Right(responseString)
case 400 =>
Left("Not Found")
}
}
}
开发者ID:dbolene,项目名称:whiltest,代码行数:33,代码来源:TwitterInfoClient.scala
示例13: ImageUtils
//设置package包名称以及导入依赖的类
import java.awt.image.BufferedImage
import java.io.{ByteArrayInputStream, ByteArrayOutputStream}
import javax.imageio.ImageIO
import org.apache.commons.io.IOUtils
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.{FileSystem, Path}
object ImageUtils {
def readImage(hdfsPath: String): BufferedImage = {
val configuration = new Configuration()
configuration.addResource(new Path(Path.localConfPath + "/core-site.xml"))
configuration.addResource(new Path(Path.localConfPath + "/hadoop/hdfs-site.xml"))
configuration.addResource(new Path(Path.localConfPath + "/hadoop/yarn-site.xml"))
val fs = FileSystem.get(configuration)
val is = fs.open(new Path(hdfsPath))
val buffer = IOUtils.toByteArray(is)
val out = new ByteArrayOutputStream()
out.write(buffer, 0, buffer.length)
val b = out.toByteArray
out.close()
val picture = ImageIO.read(new ByteArrayInputStream(b))
is.close()
picture
}
def writeImage(image: BufferedImage, hdfsPath: String): Unit = {
val configuration = new Configuration()
configuration.addResource(new Path(Path.localConfPath + "/core-site.xml"))
configuration.addResource(new Path(Path.localConfPath + "/hdfs-site.xml"))
configuration.addResource(new Path(Path.localConfPath + "/yarn-site.xml"))
val fs = FileSystem.get(configuration)
val os = fs.create(new Path(hdfsPath))
val baos = new ByteArrayOutputStream()
val ios = ImageIO.createImageOutputStream(baos)
ImageIO.write(image, "png", ios)
val is = new ByteArrayInputStream(baos.toByteArray)
IOUtils.copy(is, os)
is.close()
ios.close()
baos.close()
os.close()
}
}
开发者ID:yuanzhaokang,项目名称:ParallelizeHeatmap,代码行数:48,代码来源:ImageUtils.scala
示例14: ApplicationStartupS3DAO
//设置package包名称以及导入依赖的类
package daos
import java.io.{File, FileOutputStream, InputStream}
import javax.inject.{Inject, Singleton}
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain
import com.amazonaws.regions.Regions
import com.amazonaws.services.s3.{AmazonS3, AmazonS3ClientBuilder}
import org.apache.commons.io.IOUtils
import play.api.Configuration
@Singleton
class ApplicationStartupS3DAO @Inject()(config: Configuration) {
private val s3: AmazonS3 = AmazonS3ClientBuilder.standard()
.withCredentials(new DefaultAWSCredentialsProviderChain())
.withRegion(Regions.EU_WEST_2)
.build()
private val bucketName = config.underlying.getString("s3-static")
def downloadImageToTempFile(key: String): File = {
val inputStream: InputStream = s3.getObject(bucketName, key).getObjectContent
val tempFile = File.createTempFile(s"temp-file-$key", ".tmp")
tempFile.deleteOnExit()
val out = new FileOutputStream(tempFile)
IOUtils.copy(inputStream, out)
inputStream.close()
out.close()
tempFile
}
}
开发者ID:muhsinali,项目名称:picture-gallery-scala,代码行数:33,代码来源:ApplicationStartupS3DAO.scala
示例15: AntlrRawFileType
//设置package包名称以及导入依赖的类
package com.atomist.rug.kind.grammar
import java.nio.charset.StandardCharsets
import com.atomist.source.FileArtifact
import com.atomist.tree.content.text.PositionedTreeNode
import com.atomist.tree.content.text.grammar.antlr.{AntlrGrammar, AstNodeCreationStrategy}
import com.atomist.util.Utils.withCloseable
import org.apache.commons.io.IOUtils
import org.springframework.core.io.DefaultResourceLoader
abstract class AntlrRawFileType(
topLevelProduction: String,
nodeCreationStrategy: AstNodeCreationStrategy,
grammars: String*
)
extends TypeUnderFile {
private val g4s: Seq[String] = {
val cp = new DefaultResourceLoader()
val resources = grammars.map(grammar => cp.getResource(grammar))
resources.map(r => withCloseable(r.getInputStream)(is => IOUtils.toString(is, StandardCharsets.UTF_8)))
}
private[kind] def parser = antlrGrammar
private lazy val antlrGrammar = new AntlrGrammar(topLevelProduction, nodeCreationStrategy, g4s: _*)
override def fileToRawNode(f: FileArtifact): Option[PositionedTreeNode] = {
antlrGrammar.parse(f.content)
}
}
开发者ID:atomist,项目名称:rug,代码行数:34,代码来源:AntlrRawFileType.scala
示例16: readConfigFromArgs
//设置package包名称以及导入依赖的类
package pl.touk.nussknacker.engine.process.runner
import java.io.{File, FileReader}
import cats.data.Validated.{Invalid, Valid}
import com.typesafe.config.{Config, ConfigFactory}
import org.apache.commons.io.IOUtils
import pl.touk.nussknacker.engine.api.process.ProcessConfigCreator
import pl.touk.nussknacker.engine.canonicalgraph.CanonicalProcess
import pl.touk.nussknacker.engine.canonize.ProcessCanonizer
import pl.touk.nussknacker.engine.graph.EspProcess
import pl.touk.nussknacker.engine.marshall.ProcessMarshaller
import pl.touk.nussknacker.engine.util.ThreadUtils
trait FlinkRunner {
private val ProcessMarshaller = new ProcessMarshaller
protected def readConfigFromArgs(args: Array[String]): Config = {
val optionalConfigArg = if (args.length > 1) Some(args(1)) else None
readConfigFromArg(optionalConfigArg)
}
protected def loadCreator(config: Config): ProcessConfigCreator =
ThreadUtils.loadUsingContextLoader(config.getString("processConfigCreatorClass")).newInstance().asInstanceOf[ProcessConfigCreator]
protected def readProcessFromArg(arg: String): EspProcess = {
val canonicalJson = if (arg.startsWith("@")) {
IOUtils.toString(new FileReader(arg.substring(1)))
} else {
arg
}
ProcessMarshaller.fromJson(canonicalJson).toValidatedNel[Any, CanonicalProcess] andThen { canonical =>
ProcessCanonizer.uncanonize(canonical)
} match {
case Valid(p) => p
case Invalid(err) => throw new IllegalArgumentException(err.toList.mkString("Unmarshalling errors: ", ", ", ""))
}
}
private def readConfigFromArg(arg: Option[String]): Config =
arg match {
case Some(name) if name.startsWith("@") =>
ConfigFactory.parseFile(new File(name.substring(1)))
case Some(string) =>
ConfigFactory.parseString(string)
case None =>
ConfigFactory.load()
}
}
开发者ID:TouK,项目名称:nussknacker,代码行数:54,代码来源:FlinkRunner.scala
示例17: ImageResponse
//设置package包名称以及导入依赖的类
package demo
import java.io.InputStream
import java.nio.charset.StandardCharsets
import java.util.Base64
import com.amazonaws.services.lambda.runtime.Context
import io.github.jousby.lambda.http._
import io.github.jousby.lambda.http.model.{HttpRequest, HttpResponse}
import org.apache.commons.io.IOUtils
import scala.util.Try
class ImageResponse extends HttpRequestStreamHandler {
import ImageResponse._
def handleHttpRequest(request: HttpRequest, context: Context): Try[HttpResponse] = Try {
val encodedImage: String = encodeImage("add-128.png")
val response = HttpResponse(
body = Some(encodedImage),
isBase64Encoded = true)
context.getLogger.log(s"$response")
response
}
}
object ImageResponse {
def encodeImage(filename: String): String = {
// load image
val imageStream: InputStream = this.getClass.getClassLoader.getResourceAsStream(filename)
// to base64 string
val bytes = IOUtils.toByteArray(imageStream)
new String(Base64.getEncoder().encode(bytes), StandardCharsets.UTF_8)
}
}
开发者ID:jousby,项目名称:scala-aws-lambda,代码行数:41,代码来源:ImageResponse.scala
示例18: NormalizationHandler
//设置package包名称以及导入依赖的类
package com.fustigatedcat.heystk.engine.normalization
import java.io.ByteArrayInputStream
import java.util.zip.GZIPInputStream
import com.fustigatedcat.heystk.common.normalization.Normalization
import com.fustigatedcat.heystk.engine.queue.RabbitQueue
import org.apache.commons.codec.binary.Base64
import org.apache.commons.codec.digest.DigestUtils
import org.apache.commons.io.IOUtils
import org.json4s.native.JsonMethods.parse
import org.slf4j.LoggerFactory
object NormalizationHandler {
val logger = LoggerFactory.getLogger(this.getClass)
implicit val formats = org.json4s.DefaultFormats
def checksum(string : String) : String = {
DigestUtils.sha256Hex(string)
}
def unzip(string : String) : String = {
val arr = Base64.decodeBase64(string)
val gis = new GZIPInputStream(new ByteArrayInputStream(arr))
IOUtils.toString(gis, "UTF-8")
}
def handle(string : String, chksm : String) = {
val body = unzip(string)
if(checksum(body) == chksm) {
// write normalization to temp DB and post to Queue for needs processing
parse(unzip(string)).extract[List[Normalization]].par.foreach(RabbitQueue.postToProcess)
} else {
logger.error(s"Invalid checksum ${checksum(body)} != $chksm")
}
}
}
开发者ID:fustigatedcat,项目名称:heystk,代码行数:42,代码来源:NormalizationHandler.scala
示例19: RegistrationApiHandler
//设置package包名称以及导入依赖的类
package com.donoroncall.server.rest.undertow.handlers.authentication
import com.donoroncall.server.models.User
import com.donoroncall.server.rest.controllers.authentication.AuthenticationController
import com.donoroncall.server.utils.STATUS_CODES
import com.google.inject.Inject
import io.undertow.server.{HttpHandler, HttpServerExchange}
import org.apache.commons.io.IOUtils
import spray.json._
class RegistrationApiHandler @Inject()(authenticationController: AuthenticationController) extends HttpHandler {
override def handleRequest(exchange: HttpServerExchange): Unit = {
if (exchange.isInIoThread) {
exchange.dispatch(this)
} else {
try {
exchange.startBlocking()
val request = new String(IOUtils.toByteArray(exchange.getInputStream))
val requestJson = request.parseJson.asJsObject
val registrationTuple = User.registerUser(requestJson)
if (registrationTuple._1 != null) {
//TODO add logic for Successful Registration
exchange.getResponseSender.send(JsObject(
"status" -> JsString("ok"),
"message" -> JsString("Registration successful")
).prettyPrint)
} else {
//TODO add logic for Failed Registration
exchange.setStatusCode(STATUS_CODES.BAD_REQUEST)
exchange.getResponseSender.send(JsObject(
"status" -> JsString("failed"),
"message" -> JsString("Registration Failed"),
"comments" -> JsArray(registrationTuple._2.map(JsString(_)).toVector)
).prettyPrint)
}
} catch {
case e: Exception => {
exchange.setStatusCode(STATUS_CODES.BAD_REQUEST)
exchange.getResponseSender.send(JsObject(
"status" -> JsString("failed"),
"message" -> JsString("Registration Failed")
).prettyPrint)
}
}
}
}
}
开发者ID:donorcall01,项目名称:DonorOnCall_Server,代码行数:55,代码来源:RegistrationApiHandler.scala
示例20: ProcessCompletion
//设置package包名称以及导入依赖的类
package com.donoroncall.server.rest.undertow.handlers.authentication
import com.donoroncall.server.rest.controllers.authentication.AuthenticationController
import com.donoroncall.server.utils.STATUS_CODES
import com.google.inject.Inject
import io.undertow.server.{HttpHandler, HttpServerExchange}
import org.apache.commons.io.IOUtils
import spray.json._
class ProcessCompletion @Inject()(authenticationController: AuthenticationController) extends HttpHandler {
override def handleRequest(exchange: HttpServerExchange): Unit = {
if (exchange.isInIoThread) {
exchange.dispatch(this)
} else {
try {
exchange.startBlocking()
val request = new String(IOUtils.toByteArray(exchange.getInputStream))
val requestJson = request.parseJson.asJsObject
val username = requestJson.getFields("username").head.asInstanceOf[JsString].value
val donationStatus = requestJson.getFields("donationStatus").head.asInstanceOf[JsString].value
val donorUserName = requestJson.getFields("donorUserName").head.asInstanceOf[JsString].value
val noOfUnits = requestJson.getFields("noOfUnits").head.asInstanceOf[JsString].value.toInt
val date = requestJson.getFields("date").head.asInstanceOf[JsString].value
val blood_group = requestJson.getFields("date").head.asInstanceOf[JsString].value
val userId = authenticationController.processComplete(username, donationStatus, donorUserName, noOfUnits, date, blood_group)
if (userId) {
exchange.getResponseSender.send(JsObject(
"status" -> JsString("Complete"),
"message" -> JsString("Donation Process Complete.")
).prettyPrint)
} else {
//TODO add logic for Failed Registration
exchange.setStatusCode(STATUS_CODES.BAD_REQUEST)
exchange.getResponseSender.send(JsObject(
"status" -> JsString("failed"),
"message" -> JsString("Process Completion Failed")
).prettyPrint)
}
} catch {
case e: Exception => {
exchange.setStatusCode(STATUS_CODES.BAD_REQUEST)
exchange.getResponseSender.send(JsObject(
"status" -> JsString("failed"),
"message" -> JsString("Process Completion Failed")
).prettyPrint)
}
}
}
}
}
开发者ID:donorcall01,项目名称:DonorOnCall_Server,代码行数:61,代码来源:ProcessCompletion.scala
注:本文中的org.apache.commons.io.IOUtils类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论