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

Scala ReadableByteChannel类代码示例

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

本文整理汇总了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()
      }
    }

} 
开发者ID:ralreiroe,项目名称:embarcadero,代码行数:40,代码来源:StreamingTest.scala


示例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
    }
  }
} 
开发者ID:mouchtaris,项目名称:jleon,代码行数:38,代码来源:ByteSourceInstances.scala


示例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 
开发者ID:mouchtaris,项目名称:jleon,代码行数:36,代码来源:File.scala


示例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
} 
开发者ID:murizaky,项目名称:dasdasd,代码行数:60,代码来源:FileManager.scala



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Scala AmazonDynamoDBClient类代码示例发布时间:2022-05-23
下一篇:
Scala VerifiableProperties类代码示例发布时间: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