本文整理汇总了Scala中java.io.RandomAccessFile类的典型用法代码示例。如果您正苦于以下问题:Scala RandomAccessFile类的具体用法?Scala RandomAccessFile怎么用?Scala RandomAccessFile使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RandomAccessFile类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: OnDiskHeap
//设置package包名称以及导入依赖的类
package offheap
import java.io.ObjectInputStream
import java.nio.channels.Channels
import java.io.RandomAccessFile
import java.io.ObjectOutputStream
class OnDiskHeap[T <: Node[T]](filename : String) extends Heap[T] {
val file = new RandomAccessFile(filename, "rw")
def append(node : T, reserve : Int = 1) : Long = {
file.seek(file.length())
val pointer = file.getFilePointer
val oos = new ObjectOutputStream(Channels.newOutputStream(file.getChannel))
oos.writeObject(node)
oos.flush()
var current = file.getFilePointer()
file.seek(file.length());
while(current < reserve) {
file.writeByte(0)
current += 1
}
pointer
}
def write(pointer : Long, node : T) : Unit = {
file.seek(pointer)
val oos = new ObjectOutputStream(Channels.newOutputStream(file.getChannel))
oos.writeObject(node)
oos.flush()
}
def read(pointer : Long) : T = {
file.seek(pointer)
val ois = new ObjectInputStream(Channels.newInputStream(file.getChannel))
val value = ois.readObject
value.asInstanceOf[T]
}
def commit() = {
file.getFD.sync()
}
}
示例2: Lock
//设置package包名称以及导入依赖的类
package org.pgscala.embedded
import java.io.{File, RandomAccessFile}
import java.nio.ByteBuffer
import com.typesafe.scalalogging.StrictLogging
object Lock extends StrictLogging {
val DefaultBuffer = 32
def apply(file: File, bufferSize: Int = DefaultBuffer): Builder = new Builder(file, bufferSize)
class Builder(file: File, bufferSize: Int) {
def whenFirst[U](whenFirst: => (String, U)): WithFirst[U] = new WithFirst[U](() => whenFirst)
def whenOther[U](whenOther: String => U): WithOther[U] = new WithOther[U](whenOther)
class WithFirst[U] private[Builder] (whenFirst: () => (String, U)) {
def whenOther(whenOther: String => U): WithFirstAndOther[U] = new WithFirstAndOther[U](whenFirst, whenOther)
}
class WithOther[U] private[Builder] (whenOther: String => U) {
def whenFirst(whenFirst: () => (String, U)): WithFirstAndOther[U] = new WithFirstAndOther[U](whenFirst, whenOther)
}
class WithFirstAndOther[U] private[Builder] (whenFirst: () => (String, U), whenOther: String => U) {
def lock(): U = {
if (!file.isFile) {
logger.trace(s"Lock file did not exist - creating a new lock: ${file}")
file.createNewFile()
}
val channel = new RandomAccessFile(file, "rw").getChannel
try {
val fileLock = channel.lock()
try {
val array = new Array[Byte](bufferSize)
val buffer = ByteBuffer.wrap(array)
val read = channel.read(buffer)
if (read <= 0) {
val (firstPayload, firstResult) = whenFirst()
buffer.put(firstPayload.getBytes("ISO-8859-1"))
buffer.flip()
channel.write(buffer)
firstResult
} else {
val lockBody = new String(array, 0, read, "ISO-8859-1")
whenOther(lockBody)
}
} finally {
fileLock.release()
}
} finally {
channel.close()
}
}
}
}
}
示例3: TestAppSupport
//设置package包名称以及导入依赖的类
package scorex.perma.application
import java.io.{File, RandomAccessFile}
import scorex.crypto.authds.merkle.versioned.MvStoreVersionedMerklizedIndexedSeq
import scorex.crypto.authds.storage.{KVStorage, MvStoreStorageType}
import scorex.crypto.encode.Base58
import scorex.crypto.hash.FastCryptographicHash
import scorex.perma.consensus.PermaAuthData
import scorex.perma.settings.{PermaConstants, PermaSettings}
import scorex.perma.storage.AuthDataStorage
import scorex.settings.Settings
import scorex.utils.ScorexLogging
trait TestAppSupport {
implicit val settings = TestAppSupport.settings
val app = TestAppSupport.app
implicit val consensusModule = app.consensusModule
implicit val transactionalModule = app.transactionalModule
}
object TestAppSupport extends ScorexLogging {
implicit val settings = new Settings with PermaSettings {
lazy val rootHash: Array[Byte] = Base58.decode("13uSUANWHG7PaCac7i9QKDZriUNKXCi84UkS3ijGYTm1").get
override lazy val filename = "settings-test.json"
}
log.info("Generating random data set")
val treeDir = new File(settings.treeDir)
treeDir.mkdirs()
val datasetFile = settings.treeDir + "/data.file"
new RandomAccessFile(datasetFile, "rw").setLength(PermaConstants.n * PermaConstants.segmentSize)
log.info("Calculate tree")
val tree = MvStoreVersionedMerklizedIndexedSeq.fromFile(datasetFile, Some(settings.treeDir), PermaConstants.segmentSize, FastCryptographicHash)
log.info("Test tree")
val index = PermaConstants.n - 3
val leaf = tree.elementAndProof(index).get
require(leaf.check(tree.rootHash)(FastCryptographicHash))
log.info("Put ALL data to local storage")
new File(settings.treeDir).mkdirs()
implicit lazy val authDataStorage: KVStorage[Long, PermaAuthData, MvStoreStorageType] =
new AuthDataStorage(Some(settings.authDataStorage))
def addBlock(i: Long): Unit = {
val p = tree.elementAndProof(i).get
authDataStorage.set(i, new PermaAuthData(p.data, p.proof))
if (i > 0) {
addBlock(i - 1)
}
}
addBlock(PermaConstants.n - 1)
val rootHash = tree.rootHash
val app = new TestApp(rootHash, authDataStorage)
}
示例4: UpdateSource
//设置package包名称以及导入依赖的类
package de.sciss.schwaermen
package control
import java.io.RandomAccessFile
import java.nio.ByteBuffer
import de.sciss.file._
import de.sciss.osc
final class UpdateSource(val uid: Int, config: Config, c: OSCClient, val instance: Status, val debFile: File) {
private[this] val raf = new RandomAccessFile(debFile, "r")
val size: Long = raf.length()
private[this] val ch = raf.getChannel
// private[this] val promise = Promise[Unit]
private[this] val target = Network.dotToSocketMap(instance.dot)
private[this] val buf = ByteBuffer.allocate(6 * 1024)
// def result: Future[Unit] = promise.future
private def reply(p: osc.Packet): Unit =
c.tx.send(p, target)
def begin(): Unit = {
reply(Network.oscUpdateInit(uid = uid, size = size))
}
def sendNext(offset: Long): Unit = {
val bytes: ByteBuffer = ch.synchronized {
if (ch.position != offset) ch.position(offset)
buf.clear()
val chunk = math.min(buf.capacity(), size - offset).toInt
buf.limit(chunk)
ch.read(buf)
buf.flip()
buf
}
reply(Network.oscUpdateSet(uid = uid, offset = offset, bytes = bytes))
}
def dispose(): Unit = {
ch.synchronized(ch.close())
}
}
示例5: ImmutableTest
//设置package包名称以及导入依赖的类
package com.github.sorhus.webalytics.cruft
import java.io.{DataOutputStream, File, FileOutputStream, RandomAccessFile}
import java.nio.channels.FileChannel.MapMode
import org.roaringbitmap.RoaringBitmap
import org.roaringbitmap.buffer.ImmutableRoaringBitmap
object ImmutableTest extends App {
val outFile = new File(args(0))
outFile.createNewFile()
val fos = new FileOutputStream(outFile)
val dos = new DataOutputStream(fos)
val x = RoaringBitmap.bitmapOf(1,3,5)
x.runOptimize()
x.serialize(dos)
dos.close()
val inFile = new RandomAccessFile(args(0), "r")
val memoryMapped = inFile.getChannel.map(MapMode.READ_ONLY, 0, inFile.length())
val bb = memoryMapped.slice()
val bitset = new ImmutableRoaringBitmap(bb)
println(ImmutableRoaringBitmap.and(bitset,bitset).getCardinality)
println(ImmutableRoaringBitmap.andCardinality(bitset,bitset))
inFile.close()
}