本文整理汇总了Scala中java.nio.channels.ReadableByteChannel类的典型用法代码示例。如果您正苦于以下问题:Scala ReadableByteChannel类的具体用法?Scala ReadableByteChannel怎么用?Scala ReadableByteChannel使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ReadableByteChannel类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: StreamingTest
//设置package包名称以及导入依赖的类
import org.scalatest._
class StreamingTest extends FlatSpec with Matchers {
"Hello" should "have tests" in {
import java.nio.ByteBuffer
import java.nio.channels.ReadableByteChannel
import java.nio.file.{FileSystems, Files}
@scala.annotation.tailrec
def countStuff(
buffer: ByteBuffer,
byteChannel: ReadableByteChannel,
oldcount: BigInt
): BigInt = {
val newCount = byteChannel.read(buffer)
if (newCount == -1) {
println("Done reading")
oldcount
} else {
println(s"Read ${newCount + oldcount} bytes!")
buffer.clear()
countStuff(buffer, byteChannel, oldcount + newCount)
}
}
Seq("/i/p/hmrc/attachments/README.md").foreach{(fileWithFullPath: String) =>
val byteChannel =
Files.newByteChannel(FileSystems.getDefault().getPath(fileWithFullPath))
countStuff(ByteBuffer.allocateDirect(1024), byteChannel, 0)
byteChannel.close()
}
}
}
示例2: ReadableByteChannelSource
//设置package包名称以及导入依赖的类
package gv
package isi
package std.io
import java.nio.{ ByteBuffer }
import java.nio.channels.{ ReadableByteChannel }
import isi.io.{ ByteSource ? BS }
import isi.convertible.{ ~?, Convertible }
trait ByteSourceInstances extends AnyRef {
final implicit object ReadableByteChannelSource extends BS[ReadableByteChannel] {
@inline
def readInto(source: ReadableByteChannel, into: ByteBuffer): Int =
source read into
}
final implicit def `T ~=> ReadableByteChanel: ByteSource`[T](implicit conv: T ~? ReadableByteChannel): BS[T] =
(source, into) ? ReadableByteChannelSource readInto (source.convertTo[ReadableByteChannel], into)
final implicit object `ByteBuffer: ByteSource` extends BS[ByteBuffer] {
@inline
def readInto(source: ByteBuffer, into: ByteBuffer): Int = {
val copied: Int = into.remaining
val src: ByteBuffer = {
val slice = source.slice
slice.limit(copied)
slice
}
into.put(src)
copied
}
}
}
示例3: exists
//设置package包名称以及导入依赖的类
package gv
package isi
package io
import java.nio.channels.{ WritableByteChannel, ReadableByteChannel }
import java.nio.file.{ StandardOpenOption ? opt, Files ? JFiles, Path ? JPath }
trait File extends Any {
@inline
final def exists(path: JPath): Boolean =
JFiles exists path
@inline
final def create(path: JPath): WritableByteChannel =
JFiles newByteChannel (path, opt.CREATE_NEW, opt.WRITE)
@inline
final def open(path: JPath): ReadableByteChannel =
JFiles newByteChannel (path, opt.CREATE, opt.READ)
@inline
final def append(path: JPath): WritableByteChannel =
JFiles newByteChannel (path, opt.CREATE, opt.WRITE, opt.APPEND)
@inline
final def truncate(path: JPath): WritableByteChannel =
JFiles newByteChannel (path, opt.CREATE, opt.WRITE, opt.TRUNCATE_EXISTING)
@inline
final def remove(path: JPath): Unit =
JFiles delete path
}
object File extends File
示例4: FileManager
//设置package包名称以及导入依赖的类
package slide
import java.io.{File, FileOutputStream}
import java.net.{URL, URLConnection}
import java.nio.channels.{Channels, ReadableByteChannel}
class FileManager {
var currentFile: String = ""
var numberOfDownloads: Int = 0
def downloadFile(dlsite: String, path: String): Unit = {
val url: URL = new URL(dlsite)
val file: File = new File(path)
if (isConnected(url)) {
currentFile = path
onDownloadStart()
new Thread(new Runnable {
override def run(): Unit = {
try {
val rbc: ReadableByteChannel = Channels.newChannel(url.openStream())
val fos: FileOutputStream = new FileOutputStream(file)
fos.getChannel.transferFrom(rbc, 0, java.lang.Long.MAX_VALUE)
fos.close()
numberOfDownloads += 1
onDownloadFinished()
} catch {
case e: Exception =>
println("Error: Could not download ADB, please run as Administrator")
}
}
}).start()
}
}
def isConnected(site: URL): Boolean = {
try {
// test connection
val conn: URLConnection = site.openConnection()
conn.setConnectTimeout(5000)
conn.getContent
true
} catch {
case e: Exception => false
}
}
def onDownloadStart(): Unit = {}
def onDownloadFinished(): Unit = {}
// var onDownloadStart: () => Unit = null
// var onDownloadFinished: () => Unit = null
}