本文整理汇总了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;未经允许,请勿转载。 |
请发表评论