• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala FileChannel类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: TorrentFileStream

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

import scala.collection.JavaConversions._
import java.io.File
import java.nio.channels.FileChannel
import java.nio.file.StandardOpenOption
import java.nio.channels.FileChannel.MapMode
import java.nio.ByteBuffer
import scala.Range

class TorrentFileStream(channel: FileChannel, buffer: ByteBuffer) extends BStream {
  def headChar: Char = buffer.duplicate.get.asInstanceOf[Char]
  def headByte: Byte = buffer.duplicate.get
  def tail = {
    val tail = buffer.duplicate
    tail.get
    new TorrentFileStream(channel, tail)
  }
  def isEmpty = !buffer.hasRemaining
  def close: Unit = channel.close
  override def toString: String = {
    val buff = buffer.duplicate
    val builder = StringBuilder.newBuilder
    while (buff.hasRemaining) {
      if (builder.endsWith("6:pieces")) {
        val bah = StringBuilder.newBuilder
        var chaaa = buff.get.asInstanceOf[Char]
        while ("0123456789".contains(chaaa)) {
          bah.append(chaaa)
          chaaa = buff.get.asInstanceOf[Char]
        }
        var i = bah.toString.toInt
        while(i >= 0){
          buff.get
          i = i-1
        }
      }
      builder += buff.get.asInstanceOf[Char]
    }
    builder.toString
  }
}
object TorrentFileStream {
  def apply(torrent: File) = {
    val channel = FileChannel.open(torrent.toPath, StandardOpenOption.READ)
    new TorrentFileStream(channel, channel.map(MapMode.READ_ONLY, 0, channel.size).load)
  }
} 
开发者ID:zpooky,项目名称:bittorrent,代码行数:49,代码来源:TorrentFileStream.scala


示例2: RandomFile

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

import java.nio.channels.FileChannel
import java.io.File
import java.nio.file.StandardOpenOption
import scala.util.Random
import java.nio.ByteBuffer
import com.spooky.bittorrent.u.GigaByte

object RandomFile {
  def main(args: Array[String]): Unit = {
    val channel = FileChannel.open(new File("O:\\tmp\\file.dump").toPath, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
    val r = new Random(0)
    val buffer = ByteBuffer.allocate(1024 * 4)
    val bytes = GigaByte(50).toBytes
    println(bytes.capacity.toLong)
    for (_ <- 0l to(bytes.capacity.toLong, buffer.capacity.toLong)) {
      r.nextBytes(buffer.array())
      buffer.limit(buffer.capacity)
      buffer.position(0)
      channel.write(buffer)
    }
    channel.close()
  }
} 
开发者ID:zpooky,项目名称:bittorrent,代码行数:26,代码来源:RandomFile.scala


示例3: FileIO

//设置package包名称以及导入依赖的类
package swave.core.io.files

import java.io.File
import java.nio.channels.FileChannel
import java.nio.file.{FileSystems, Files, Path, StandardOpenOption}

import scala.util.control.NonFatal
import com.typesafe.config.Config
import swave.core.impl.util.SettingsCompanion
import swave.core.io.Bytes
import swave.core.macros._

object FileIO extends SpoutFromFiles with DrainToFiles {

  lazy val userHomePath: Path = FileSystems.getDefault.getPath(System getProperty "user.home")

  def resolveFileSystemPath(pathName: String): Path =
    if (pathName.length >= 2 && pathName.charAt(0) == '~' && pathName.charAt(1) == File.separatorChar) {
      userHomePath.resolve(pathName substring 2)
    } else FileSystems.getDefault.getPath(pathName)

  val WriteCreateOptions: Set[StandardOpenOption] = {
    import StandardOpenOption._
    Set(CREATE, TRUNCATE_EXISTING, WRITE)
  }

  final case class Settings(defaultFileReadingChunkSize: Int, defaultFileWritingChunkSize: Int) {
    requireArg(defaultFileReadingChunkSize > 0, "`defaultFileChunkSize` must be > 0")
    requireArg(defaultFileWritingChunkSize >= 0, "`defaultFileWritingChunkSize` must be >= 0")

    def withDefaultFileReadingChunkSize(defaultFileReadingChunkSize: Int) =
      copy(defaultFileReadingChunkSize = defaultFileReadingChunkSize)
    def withDefaultFileWritingChunkSize(defaultFileWritingChunkSize: Int) =
      copy(defaultFileWritingChunkSize = defaultFileWritingChunkSize)
  }

  object Settings extends SettingsCompanion[Settings]("swave.core.file-io") {
    def fromSubConfig(c: Config): Settings =
      Settings(
        defaultFileReadingChunkSize = c getInt "default-file-reading-chunk-size",
        defaultFileWritingChunkSize = c getInt "default-file-writing-chunk-size")
  }

  def writeFile[T: Bytes](fileName: String, data: T): Unit = writeFile(resolveFileSystemPath(fileName), data)
  def writeFile[T: Bytes](file: File, data: T): Unit       = writeFile(file.toPath, data)
  def writeFile[T: Bytes](path: Path, data: T, options: StandardOpenOption*): Unit = {
    implicit def decorator(value: T): Bytes.Decorator[T] = Bytes.decorator(value)
    Files.write(path, data.toArray, options: _*)
    ()
  }

  def readFile[T: Bytes](fileName: String): T = readFile(resolveFileSystemPath(fileName))
  def readFile[T: Bytes](file: File): T       = readFile(file.toPath)
  def readFile[T: Bytes](path: Path): T       = implicitly[Bytes[T]].apply(Files.readAllBytes(path))

  private[io] def quietClose(channel: FileChannel): Unit =
    try channel.close()
    catch { case NonFatal(_) ? }
} 
开发者ID:sirthias,项目名称:swave,代码行数:60,代码来源:FileIO.scala


示例4: FolderLock

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

import java.nio.channels.{FileChannel, FileLock}
import java.nio.file.{Files, OpenOption, Paths, StandardOpenOption}


class FolderLock  (folder: => String) extends Lock {
  var isLocked = false
  lazy val lockFolder = Paths.get(folder)
  lazy val lockFilePath = Paths.get(folder, ".lock")
  var channel: FileChannel = null
  var flock: FileLock = null
  override def lock(): Lock = {
    try {
      this synchronized {
        if(isLocked) return this
        if(Files.notExists(lockFolder)) Files.createDirectories(lockFolder)
        if(Files.notExists(lockFilePath)) Files.createFile(lockFilePath)
        logger.debug(s"created file $lockFilePath")
        channel = FileChannel.open(lockFilePath, StandardOpenOption.CREATE, StandardOpenOption.WRITE)
        flock = channel.lock()
        logger.debug(s"acquired lock")
        isLocked = true
        this
      }
    } catch {
      case ex: Exception => {
        logger.error(s"exception while locking: $ex")
        if(isLocked) release()
        throw ex
      }
    }
  }

  override def tryLock(): Boolean =  {
    try {
      lock()
      true
    } catch {
      case ex: Exception => false
    }
  }

  override def release(): Unit = {
    try {
      this synchronized {
        if(!isLocked) return
        if (flock != null) flock.release()
        if (channel != null && channel.isOpen) channel.close()
        if (Files.exists(lockFilePath)) Files.delete(lockFilePath)
        logger.debug(s"lock released $lockFilePath")
        isLocked = false
      }
    } catch {
      case ex: Exception => {
        logger.error(s"exception while releasing lock: $ex")
      } //TODO: handle exception
    }
  }
} 
开发者ID:ksuhail7,项目名称:eCabinet,代码行数:61,代码来源:FolderLock.scala



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala AuditFilter类代码示例发布时间:2022-05-23
下一篇:
Scala LogManager类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap