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


Scala FileIO类代码示例

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


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

示例1: MovieListPipeline

//设置package包名称以及导入依赖的类
package com.stacktrace.yo.scrapeline.imdb.pipelines

import java.nio.file.Paths

import akka.NotUsed
import akka.stream.scaladsl.{FileIO, Flow, Keep, Sink, Source}
import akka.stream.{ActorMaterializer, IOResult}
import akka.util.ByteString
import com.stacktrace.yo.scrapeline.core.ScrapeClient.jsoup
import com.stacktrace.yo.scrapeline.core._
import com.stacktrace.yo.scrapeline.imdb.Domain.MovieNameAndDetailUrl
import net.ruippeixotog.scalascraper.dsl.DSL._
import net.ruippeixotog.scalascraper.model.Document
import net.ruippeixotog.scalascraper.scraper.ContentExtractors.elementList

import scala.concurrent.Future

class MovieListPipeline(implicit val m: ActorMaterializer) {

  def getPipelineSource: Source[jsoup.DocumentType, NotUsed] = Source.single(ScrapeClient.scrape("http://www.the-numbers.com/movie/budgets/all"))

  def getParseFlow: Flow[Document, MovieNameAndDetailUrl, NotUsed] = {
    Flow[Document]
      .mapConcat(doc => {
        val table = doc >> elementList("table tr")
        val movieLinkTuples = table.flatMap(tr => {
          val name = tr >> elementList("tr b a")
          name.map(
            link => {
              MovieNameAndDetailUrl(link.text, "http://www.the-numbers.com/" + link.attr("href"))
            }
          )
        })
        movieLinkTuples
      })
  }

  def getPipeOut: Sink[MovieNameAndDetailUrl, Future[IOResult]] = Flow[MovieNameAndDetailUrl]
    .map(s => ByteString(s.name + "\n"))
    .toMat(FileIO.toPath(Paths.get("movie.txt")))(Keep.right)

  def buildAndRun: Future[IOResult] = {
    getPipelineSource
      .via(getParseFlow)
      .runWith(getPipeOut)
  }

} 
开发者ID:StackTraceYo,项目名称:scrapeline,代码行数:49,代码来源:MovieListPipeline.scala

示例2: SerializationDemo

//设置package包名称以及导入依赖的类
package com.martinseeler.dtf

import java.io.File
import java.nio.file.StandardOpenOption._

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{FileIO, Framing}
import akka.util.ByteString
import com.martinseeler.dtf.stages.{DeltaToByteStringStage, TickToDeltaStage}

object SerializationDemo extends App {

  implicit val system = ActorSystem("serialization")
  implicit val mat = ActorMaterializer()

  val csvSource = FileIO
    .fromPath(new File("ticks_1MM.csv").toPath)
    .via(Framing.delimiter(ByteString("\n"), 1024))
    .map(_.utf8String.split(','))
    .map(xs => Tick(xs(0).toLong, xs(1).toDouble, xs(2).toDouble))

  val dtffSink = FileIO
    .toPath(new File("ticks_1MM.dtff").toPath, options = Set(CREATE, WRITE, TRUNCATE_EXISTING))

  csvSource
    .via(new TickToDeltaStage())
    .via(new DeltaToByteStringStage())
    .runWith(dtffSink)
    .onComplete(_ => system.terminate())(scala.concurrent.ExecutionContext.global)
} 
开发者ID:MartinSeeler,项目名称:dense-tick-file-format,代码行数:32,代码来源:SerializationDemo.scala

示例3: DeserializationDemo

//设置package包名称以及导入依赖的类
package com.martinseeler.dtf

import java.io.File

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{FileIO, Source}
import com.martinseeler.dtf.stages.{ByteStringToDeltaStage, DeltaToTickStage}

object DeserializationDemo extends App {

  implicit val system = ActorSystem("deserialization")
  implicit val mat = ActorMaterializer()

  val dtffSource = FileIO.fromPath(new File("ticks_1MM.dtff").toPath)

  dtffSource
    .via(new ByteStringToDeltaStage())
    .via(new DeltaToTickStage())
    .runForeach(println)
    .onComplete(_ => system.terminate())(
        scala.concurrent.ExecutionContext.global)
} 
开发者ID:MartinSeeler,项目名称:dense-tick-file-format,代码行数:24,代码来源:DeserializationDemo.scala

示例4: Step0GenerateStatuses

//设置package包名称以及导入依赖的类
package scaladays.akka.stream

import java.io.File

import akka.NotUsed
import akka.actor.ActorSystem
import akka.http.scaladsl.marshalling.Marshal
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{FileIO, Source}
import akka.util.ByteString

import scaladays.akka.domain.Tweet
import scaladays.akka.http.MyJsonProtocol
import scaladays.akka.support.MakingUpData

object Step0GenerateStatuses extends App
  with MakingUpData with MyJsonProtocol {

  implicit val system = ActorSystem()
  implicit val ec = system.dispatcher
  implicit val mat = ActorMaterializer()

  val n = 10000

  Source.repeat(NotUsed).take(n)
    .map(_ => Tweet.random)
    .mapAsync(1)(t => Marshal(t).to[ByteString])
    .intersperse(ByteString("\n"))
    .runWith(FileIO.toPath(new File("tweets.json").toPath))
    .onComplete { res =>
      println(s"Generated $n tweets. ($res)")
      system.terminate()
    }

} 
开发者ID:ktoso,项目名称:scaladays-berlin-akka-streams,代码行数:36,代码来源:Step0GenerateStatuses.scala

示例5: DownloadManager

//设置package包名称以及导入依赖的类
package im.actor.util.http

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

import akka.stream.scaladsl.FileIO

import scala.concurrent._

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.HttpRequest
import akka.stream.Materializer

import scala.util.{ Success, Failure }

class DownloadManager(implicit system: ActorSystem, materializer: Materializer) {
  implicit val ec: ExecutionContext = system.dispatcher

  val http = Http()

  def download(url: String): Future[(Path, Long)] = {
    val tempFileFuture = createTempFile()
    val responseFuture = http.singleRequest(HttpRequest(uri = url))

    for {
      filePath ? tempFileFuture
      response ? responseFuture
      ioRes ? response.entity.dataBytes.runWith(FileIO.toPath(filePath))
    } yield {
      ioRes.status match {
        case Success(_)     ? (filePath, ioRes.count)
        case Failure(cause) ? throw cause
      }
    }
  }

  // FIXME: dispatcher for this
  private def createTempFile(): Future[Path] = {
    Future {
      blocking {
        Files.createTempFile("ActorDownloadManager", "")
      }
    }
  }
} 
开发者ID:wex5,项目名称:dangchat-server,代码行数:46,代码来源:DownloadManager.scala

示例6: MovieNameToIMDBPipeline

//设置package包名称以及导入依赖的类
package com.stacktrace.yo.scrapeline.imdb.pipelines

import java.net.URLEncoder
import java.nio.file.Paths

import akka.stream.scaladsl.{FileIO, Flow, Framing, Source}
import akka.stream.{ActorMaterializer, IOResult}
import akka.util.ByteString
import akka.{Done, NotUsed}
import com.stacktrace.yo.scrapeline.imdb.Domain.MovieNameAndDetailUrl

import scala.concurrent.{ExecutionContext, Future}

class MovieNameToIMDBPipeline(implicit val m: ActorMaterializer, implicit val ec: ExecutionContext) {

  private def getPipelineSource: Source[String, Future[IOResult]] = {
    FileIO.fromPath(Paths.get("movie.txt"))
      .via(Framing.delimiter(ByteString("\n"), 256)
        .map(_.utf8String)
      )
  }

  private def getDetailUrlFlow: Flow[String, MovieNameAndDetailUrl, NotUsed] = {
    Flow[String]
      .mapAsyncUnordered(100)(mapPipeToImdbSearch)
  }


  private def mapPipeToImdbSearch(in: String): Future[MovieNameAndDetailUrl] = Future {
    val encodedString: String = URLEncoder.encode(in, "UTF-8")
    MovieNameAndDetailUrl(in, "http://www.imdb.com/find?ref_=nv_sr_fn&q=" + encodedString + "&s=tt")
  }


  def getOutput: Future[Done] = {
    getPipelineSource
      .via(getDetailUrlFlow)
      .via(new IMDBSearchSubPipe().getSubFlow)
      .runForeach(x => println(x.name + ":" + x.url))
  }

} 
开发者ID:StackTraceYo,项目名称:scrapeline,代码行数:43,代码来源:MovieNameToIMDBPipeline.scala

示例7: validate

//设置package包名称以及导入依赖的类
package csw.services.config.server.files

import java.nio.file.{Path, Paths}

import akka.stream.scaladsl.{FileIO, Keep}
import csw.services.config.api.models.ConfigData
import csw.services.config.server.commons.ConfigServerLogger
import csw.services.config.server.{ActorRuntime, Settings}

import scala.async.Async._
import scala.concurrent.Future


  def validate(id: String, path: Path): Future[Boolean] = async {
    id == await(Sha1.fromPath(path))
  }

  def saveAndSha(configData: ConfigData): Future[(Path, String)] = async {
    val path = await(fileRepo.createTempFile("config-service-overize-", ".tmp"))
    val (resultF, shaF) = configData.source
      .alsoToMat(FileIO.toPath(path))(Keep.right)
      .toMat(Sha1.sink)(Keep.both)
      .run()
    await(resultF).status.get
    (path, await(shaF))
  }

} 
开发者ID:tmtsoftware,项目名称:csw-prod,代码行数:29,代码来源:AnnexFileService.scala

示例8: Sha1

//设置package包名称以及导入依赖的类
package csw.services.config.server.files

import java.nio.file.Path
import java.security.MessageDigest

import akka.stream.Materializer
import akka.stream.scaladsl.{FileIO, Flow, Keep, Sink, Source}
import akka.util.ByteString
import csw.services.config.api.models.ConfigData

import scala.concurrent.Future


object Sha1 {

  private def fromSource(source: Source[ByteString, Any])(implicit mat: Materializer): Future[String] =
    source.runWith(sink)

  def fromConfigData(configData: ConfigData)(implicit mat: Materializer): Future[String] =
    fromSource(configData.source)

  def fromPath(path: Path)(implicit mat: Materializer): Future[String] =
    fromSource(FileIO.fromPath(path))

  //Keep this a def so that the digester is created anew each time.
  def sink: Sink[ByteString, Future[String]] = {
    val sha1Digester = MessageDigest.getInstance("SHA-1")
    Flow[ByteString]
      .fold(sha1Digester) { (digester, bs) =>
        digester.update(bs.toArray)
        digester
      }
      .mapConcat(_.digest().toList)
      .map(_ & 0xFF)
      .map("%02x" format _)
      .toMat(Sink.fold("")(_ + _))(Keep.right)
  }

} 
开发者ID:tmtsoftware,项目名称:csw-prod,代码行数:40,代码来源:Sha1.scala

示例9: FileClient

//设置package包名称以及导入依赖的类
package eu.svez.backpressuredemo.B_http

import java.nio.file.Paths

import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.stream.scaladsl.FileIO
import eu.svez.backpressuredemo.Flows._
import eu.svez.backpressuredemo.StreamDemo

object FileClient extends StreamDemo {

  val byteSource = FileIO
    .fromPath(Paths.get("/tmp/bigfile.zip"))
    .via(valve(sourceRate.get))
    .via(meter("sourceHttp"))

  val host = "localhost"
  val port = 8080

  val request = HttpRequest(
    uri = Uri(s"http://$host:$port/file"),
    entity = HttpEntity(ContentTypes.`application/octet-stream`, byteSource)
  )

  sourceRate.send(5)

  Http().singleRequest(request).onComplete{ _ =>
    println("All sent!")
  }

  readRatesFromStdIn()
} 
开发者ID:svezfaz,项目名称:akka-backpressure-scala-central-talk,代码行数:34,代码来源:FileClient.scala

示例10: extractFormData

//设置package包名称以及导入依赖的类
package com.flipkart.connekt.receptors.directives

import java.io.File

import akka.Done
import akka.http.scaladsl.model.{HttpEntity, MediaTypes, Multipart}
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server._
import akka.stream.Materializer
import akka.stream.scaladsl.FileIO
import com.flipkart.connekt.commons.utils.StringUtils._
import com.flipkart.connekt.receptors.directives.MultiPartFormData._

import scala.concurrent.{ExecutionContext, Future}
import scala.util.Try


  def extractFormData: Directive1[Map[Name,  Either[Value, FileInfo]]] = {
    Directive[Tuple1[Map[Name,  Either[Value, FileInfo]]]] { inner =>
      extractMaterializer { implicit mat =>
        extractExecutionContext { implicit ec =>
          uploadFileImpl(mat, ec) { filesFuture =>
            ctx => {
              filesFuture.map(map => inner(Tuple1(map))).flatMap(route => route(ctx))
            }
          }
        }
      }
    }
  }

  def downloadFile(file: String): Route = {
    val f = new File(file)
    val responseEntity = HttpEntity(
      MediaTypes.`application/octet-stream`,
      f.length,
      FileIO.fromFile(f, chunkSize = 262144))
    complete(responseEntity)
  }
}


object MultiPartFormData {

  //form field name
  type Name = String
  type Value = String

  case class FileInfo(fileName: String, tmpFilePath: String, status: Try[Done])
} 
开发者ID:ayush03agarwal,项目名称:connekt,代码行数:51,代码来源:FileDirective.scala

示例11: ProcessAttachmentService

//设置package包名称以及导入依赖的类
package pl.touk.nussknacker.ui.api

import java.io.File

import akka.stream.Materializer
import akka.stream.scaladsl.{FileIO, Source}
import akka.util.ByteString
import com.typesafe.scalalogging.LazyLogging
import pl.touk.nussknacker.ui.api.ProcessAttachmentService.AttachmentToAdd
import pl.touk.nussknacker.ui.db.entity.AttachmentEntity.AttachmentEntityData
import pl.touk.nussknacker.ui.process.repository.ProcessActivityRepository
import pl.touk.nussknacker.ui.security.LoggedUser
import pl.touk.nussknacker.ui.util.CatsSyntax

import scala.concurrent.{ExecutionContext, Future}
import scala.util.control.NonFatal

class ProcessAttachmentService(attachmentsBasePath: String, processActivityRepository: ProcessActivityRepository) extends LazyLogging {

  def saveAttachment(processId: String, processVersionId: Long, originalFileName: String, byteSource: Source[ByteString, Any])
                    (implicit ec: ExecutionContext, loggedUser: LoggedUser, mat: Materializer): Future[Unit] = {
    val relativeFilePath = s"$processId/${s"${System.currentTimeMillis()}-$originalFileName"}"
    val attachmentFile = getAttachmentFile(relativeFilePath)
    attachmentFile.getParentFile.mkdirs()
    val fileSink = FileIO.toFile(attachmentFile)
    byteSource.runWith(fileSink).flatMap { _ =>
      val attachmentToAdd = AttachmentToAdd(processId, processVersionId, originalFileName, relativeFilePath)
      processActivityRepository.addAttachment(attachmentToAdd).recoverWith { case NonFatal(ex) =>
        logger.warn(s"Failure during writing attachment to db. Removing file ${attachmentFile}", ex)
        attachmentFile.delete()
        Future.failed(ex)
      }
    }
  }

  def readAttachment(attachmentId: Long)
                    (implicit ec: ExecutionContext, loggedUser: LoggedUser): Future[Option[(File, AttachmentEntityData)]] = {
    val attachmentFutureOpt = processActivityRepository.findAttachment(attachmentId)
    CatsSyntax.futureOpt.map(attachmentFutureOpt) { attachment =>
      (getAttachmentFile(attachment.filePath), attachment)
    }
  }

  private def getAttachmentFile(attachmentRelativePath: String): File = {
    new File(attachmentsBasePath, attachmentRelativePath)
  }

}

object ProcessAttachmentService {

  case class AttachmentToAdd(processId: String,
                             processVersionId: Long,
                             fileName: String,
                             relativeFilePath: String
                            )

} 
开发者ID:TouK,项目名称:nussknacker,代码行数:59,代码来源:ProcessAttachmentService.scala

示例12: HomeController

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

import javax.inject._

import akka.stream.scaladsl.{FileIO, Source}
import akka.util.ByteString
import play.api.http.HttpEntity
import play.api.mvc._


@Singleton
class HomeController @Inject()(webJarAssets: WebJarAssets) extends Controller {

  def index = {
    Action {
      Ok(views.html.index("Your new application is ready.", webJarAssets))
    }
  }

  def streaming = {
    Action {
      val file = new java.io.File("conf/vid4.mp4")
      val path: java.nio.file.Path = file.toPath
      val source: Source[ByteString, _] = FileIO.fromPath(path)

      Result(
        header = ResponseHeader(200, Map.empty),
        body = HttpEntity.Streamed(source, Some(file.length), Some("application/mp4"))
      )

      
    }
  }
} 
开发者ID:rahulforallp,项目名称:playu,代码行数:35,代码来源:HomeController.scala

示例13: executor

//设置package包名称以及导入依赖的类
package com.knoldus

import java.io.File

import akka.actor.ActorSystem
import akka.http.scaladsl.model.Multipart.BodyPart
import akka.http.scaladsl.model.{HttpResponse, Multipart, StatusCodes}
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import akka.stream.Materializer
import akka.stream.scaladsl.FileIO

import scala.concurrent.duration._
import scala.concurrent.{ExecutionContextExecutor, Future}

trait MultipartFormDataHandler {

  implicit val system: ActorSystem

  implicit def executor: ExecutionContextExecutor

  implicit val materializer: Materializer

  val routes = processMultiPartData

  def processMultiPartData: Route = path("process" / "multipart" / "data") {
    (post & entity(as[Multipart.FormData])) { formData =>
    complete {
    val extractedData: Future[Map[String, Any]] = formData.parts.mapAsync[(String, Any)](1) {

      case file: BodyPart if file.name == "file" => val tempFile = File.createTempFile("process", "file")
        file.entity.dataBytes.runWith(FileIO.toPath(tempFile.toPath)).map { ioResult=>
        s"file ${file.filename.fold("Unknown")(identity)}" -> s"${ioResult.count} bytes"}

      case data: BodyPart => data.toStrict(2.seconds).map(strict =>data.name -> strict.entity.data.utf8String)
    }.runFold(Map.empty[String, Any])((map, tuple) => map + tuple)

     extractedData.map { data => HttpResponse(StatusCodes.OK, entity = s"Data : ${data.mkString(", ")} has been successfully saved.")}
     .recover {
      case ex: Exception =>HttpResponse(StatusCodes.InternalServerError,entity = s"Error in processing multipart form data due to ${ex.getMessage}")
     }
   }
    }
  }
} 
开发者ID:knoldus,项目名称:akka-http-multipart-form-data.g8,代码行数:46,代码来源:MultipartFormDataHandler.scala

示例14: Demo5

//设置package包名称以及导入依赖的类
package lew.bing.akka.stream

import java.io.ByteArrayInputStream
import java.nio.file.Paths

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{FileIO, StreamConverters}


object Demo5 {

  def main(args: Array[String]): Unit = {
    //????
    implicit val system = ActorSystem("QuickStart")
    implicit val materializer = ActorMaterializer()
    import scala.concurrent.ExecutionContext.Implicits.global
    //??is
    val source = StreamConverters.fromInputStream(() => new ByteArrayInputStream("I am better man!".getBytes))
    //??os
    val sink = StreamConverters.fromOutputStream(() => System.out)
    val result = source.runWith(sink)
    val result2 =FileIO.fromPath(Paths.get("factorial2.txt")).to(FileIO.toPath(Paths.get("factorial3.txt"))).run()
    result.flatMap(f => result2).onComplete(_ => system.terminate())
  }

} 
开发者ID:liuguobing634,项目名称:akka,代码行数:28,代码来源:Demo5.scala

示例15: CaptchaLoader

//设置package包名称以及导入依赖的类
package com.karasiq.nanoboard.server.utils

import java.nio.file.{Files, Path, Paths}

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.headers.Location
import akka.http.scaladsl.model.{HttpRequest, HttpResponse, Uri}
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.{FileIO, Source}
import com.karasiq.nanoboard.captcha.storage.{NanoboardCaptchaFileSource, NanoboardCaptchaSource}
import com.typesafe.config.{Config, ConfigFactory}

import scala.concurrent.{ExecutionContext, Future}

object CaptchaLoader {
  def apply(config: Config = ConfigFactory.load())(implicit am: ActorMaterializer, as: ActorSystem, ec: ExecutionContext): CaptchaLoader = {
    new CaptchaLoader(Paths.get(config.getString("nanoboard.captcha.storage")))
  }

  def load(config: Config = ConfigFactory.load())(implicit am: ActorMaterializer, as: ActorSystem, ec: ExecutionContext): Future[NanoboardCaptchaFileSource] = {
    apply(config).forUrl(config.getString("nanoboard.captcha.download-url"))
  }
}

final class CaptchaLoader(baseDir: Path)(implicit am: ActorMaterializer, as: ActorSystem, ec: ExecutionContext) {
  private val http = Http()

  // TODO: https://github.com/akka/akka/issues/15990
  private def requestWithRedirects(uri: Uri): Future[HttpResponse] = {
    http.singleRequest(HttpRequest(uri = uri)).flatMap { response ?
      val location = response.header[Location]
      if (response.status.isRedirection() && location.isDefined) {
        requestWithRedirects(location.get.uri)
      } else {
        Future.successful(response)
      }
    }
  }

  def forUrl(url: String): Future[NanoboardCaptchaFileSource] = {
    val fileName = baseDir.resolve(s"${Integer.toHexString(url.hashCode)}.nbc")
    if (Files.exists(fileName)) {
      assert(Files.isRegularFile(fileName), s"Not a file: $fileName")
      Future.successful(NanoboardCaptchaSource.fromFile(fileName.toString))
    } else {
      Source
        .fromFuture(requestWithRedirects(url))
        .flatMapConcat(_.entity.dataBytes)
        .toMat(FileIO.toFile(fileName.toFile))((_, r) ? r.map { ioResult ?
          if (ioResult.wasSuccessful) NanoboardCaptchaSource.fromFile(fileName.toString)
          else throw ioResult.getError
        })
        .run()
    }
  }
} 
开发者ID:Karasiq,项目名称:nanoboard,代码行数:58,代码来源:CaptchaLoader.scala


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