本文整理汇总了Scala中java.nio.ByteBuffer类的典型用法代码示例。如果您正苦于以下问题:Scala ByteBuffer类的具体用法?Scala ByteBuffer怎么用?Scala ByteBuffer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ByteBuffer类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Scala代码示例。
示例1: AlgebirdMurmurHash128
//设置package包名称以及导入依赖的类
package sandbox.hashing
import java.nio.ByteBuffer
case class AlgebirdMurmurHash128(seed: Long) extends AnyVal {
def apply(buffer: ByteBuffer, offset: Int, length: Int): (Long, Long) = {
val longs = CassandraMurmurHash.hash3_x64_128(buffer, offset, length, seed)
(longs(0), longs(1))
}
def apply(bytes: Array[Byte]): (Long, Long) = apply(ByteBuffer.wrap(bytes), 0, bytes.length)
def apply(maxBytes: Int, fn: ByteBuffer => Unit): (Long, Long) = {
val buffer = ByteBuffer.allocate(maxBytes)
fn(buffer)
apply(buffer, 0, maxBytes)
}
def apply(array: Array[Char]): (Long, Long) = apply(array.size * 2, { _.asCharBuffer.put(array) })
def apply(array: Array[Short]): (Long, Long) = apply(array.size * 2, { _.asShortBuffer.put(array) })
def apply(array: Array[Int]): (Long, Long) = apply(array.size * 4, { _.asIntBuffer.put(array) })
def apply(array: Array[Float]): (Long, Long) = apply(array.size * 4, { _.asFloatBuffer.put(array) })
def apply(array: Array[Long]): (Long, Long) = apply(array.size * 8, { _.asLongBuffer.put(array) })
def apply(array: Array[Double]): (Long, Long) = apply(array.size * 8, { _.asDoubleBuffer.put(array) })
def apply(value: Char): (Long, Long) = apply(2, { _.asCharBuffer.put(value) })
def apply(value: Short): (Long, Long) = apply(2, { _.asShortBuffer.put(value) })
def apply(value: Int): (Long, Long) = apply(4, { _.asIntBuffer.put(value) })
def apply(value: Float): (Long, Long) = apply(4, { _.asFloatBuffer.put(value) })
def apply(value: Long): (Long, Long) = apply(8, { _.asLongBuffer.put(value) })
def apply(value: Double): (Long, Long) = apply(8, { _.asDoubleBuffer.put(value) })
def apply(string: CharSequence): (Long, Long) = apply(string.length * 2, { buffer =>
val charBuffer = buffer.asCharBuffer
0.to(string.length - 1).foreach{ i => charBuffer.put(string.charAt(i)) }
})
}
开发者ID:alexandrnikitin,项目名称:bloom-filter-scala,代码行数:36,代码来源:AlgebirdMurmurHash128.scala
示例2: PeerExchangeList
//设置package包名称以及导入依赖的类
package com.karasiq.bittorrent.protocol.extensions
import java.net.{InetAddress, InetSocketAddress}
import java.nio.ByteBuffer
import akka.util.ByteString
import com.karasiq.bittorrent.format.{BEncode, BEncodedDictionary, BEncodedString}
import com.karasiq.bittorrent.protocol.{BitTorrentTcpProtocol, TcpMessageProtocol}
trait PeerExchange extends PeerExchangeMessages with PeerExchangeTcp
trait PeerExchangeMessages {
case class PeerExchangeList(addresses: Seq[InetSocketAddress])
}
trait PeerExchangeTcp { self: PeerExchangeMessages ?
implicit object PeerExchangeListTcpProtocol extends TcpMessageProtocol[PeerExchangeList] {
private val ipv4Length: Int = 4
private val ipv6Length: Int = 16
private val portLength: Int = 2
override def toBytes(value: PeerExchangeList): ByteString = {
val (ipv4, ipv6) = value.addresses.partition(_.getAddress.getAddress.length == ipv4Length)
def packAddress(address: InetSocketAddress): ByteString = {
val port = ByteBuffer.allocate(portLength)
port.putShort(address.getPort.toShort)
port.flip()
ByteString(address.getAddress.getAddress) ++ ByteString(port)
}
BEncodedDictionary(Vector(
"dropped" ? BEncodedString(ByteString.empty),
"added" ? BEncodedString(ipv4.map(packAddress).fold(ByteString.empty)(_ ++ _)),
"added.f" ? BEncodedString(ByteString(Array.fill(ipv4.length)(1.toByte))),
"added6" ? BEncodedString(ipv6.map(packAddress).fold(ByteString.empty)(_ ++ _)),
"added6.f" ? BEncodedString(ByteString(Array.fill(ipv6.length)(1.toByte)))
)).toBytes
}
override def fromBytes(bs: ByteString): Option[PeerExchangeList] = {
import com.karasiq.bittorrent.format.BEncodeImplicits._
BEncode.parse(bs.toArray[Byte]).collectFirst {
case BEncodedDictionary(values) ?
val map = values.toMap
val ipv4 = map.byteString("added").fold(Iterator[ByteString]())(_.grouped(ipv4Length + portLength))
val ipv6 = map.byteString("added6").fold(Iterator[ByteString]())(_.grouped(ipv6Length + portLength))
val addresses = (ipv4 ++ ipv6).map { bytes ?
val address = InetAddress.getByAddress(bytes.dropRight(portLength).toArray)
val port = BitTorrentTcpProtocol.int32FromBytes(bytes.takeRight(portLength))
new InetSocketAddress(address, port)
}
PeerExchangeList(addresses.toVector)
}
}
}
}
开发者ID:Karasiq,项目名称:torrentstream,代码行数:56,代码来源:PeerExchange.scala
示例3: acquire
//设置package包名称以及导入依赖的类
package akka.io
import java.nio.ByteBuffer
trait BufferPool {
def acquire(): ByteBuffer
def release(buf: ByteBuffer)
}
private[akka] class DirectByteBufferPool(defaultBufferSize: Int, maxPoolEntries: Int) extends BufferPool {
private[this] val pool: Array[ByteBuffer] = new Array[ByteBuffer](maxPoolEntries)
private[this] var buffersInPool: Int = 0
def acquire(): ByteBuffer =
takeBufferFromPool()
def release(buf: ByteBuffer): Unit =
offerBufferToPool(buf)
private def allocate(size: Int): ByteBuffer =
ByteBuffer.allocateDirect(size)
private final def takeBufferFromPool(): ByteBuffer = {
val buffer = pool.synchronized {
if (buffersInPool > 0) {
buffersInPool -= 1
pool(buffersInPool)
} else null
}
// allocate new and clear outside the lock
if (buffer == null)
allocate(defaultBufferSize)
else {
buffer.clear()
buffer
}
}
private final def offerBufferToPool(buf: ByteBuffer): Unit =
pool.synchronized {
if (buffersInPool < maxPoolEntries) {
pool(buffersInPool) = buf
buffersInPool += 1
} // else let the buffer be gc'd
}
}
开发者ID:love1314sea,项目名称:akka-2.3.16,代码行数:49,代码来源:DirectByteBufferPool.scala
示例4: Key
//设置package包名称以及导入依赖的类
package akka.persistence.journal.leveldb
import java.nio.ByteBuffer
private[leveldb] case class Key(
persistenceId: Int,
sequenceNr: Long,
channelId: Int)
private[leveldb] object Key {
def keyToBytes(key: Key): Array[Byte] = {
val bb = ByteBuffer.allocate(20)
bb.putInt(key.persistenceId)
bb.putLong(key.sequenceNr)
bb.putInt(key.channelId)
bb.array
}
def keyFromBytes(bytes: Array[Byte]): Key = {
val bb = ByteBuffer.wrap(bytes)
val aid = bb.getInt
val snr = bb.getLong
val cid = bb.getInt
new Key(aid, snr, cid)
}
def counterKey(persistenceId: Int): Key = Key(persistenceId, 0L, 0)
def counterToBytes(ctr: Long): Array[Byte] = ByteBuffer.allocate(8).putLong(ctr).array
def counterFromBytes(bytes: Array[Byte]): Long = ByteBuffer.wrap(bytes).getLong
def id(key: Key) = key.channelId
def idKey(id: Int) = Key(1, 0L, id)
def isIdKey(key: Key): Boolean = key.persistenceId == 1
def deletionKey(persistenceId: Int, sequenceNr: Long): Key = Key(persistenceId, sequenceNr, 1)
def isDeletionKey(key: Key): Boolean = key.channelId == 1
}
开发者ID:love1314sea,项目名称:akka-2.3.16,代码行数:39,代码来源:LeveldbKey.scala
示例5: Serializer
//设置package包名称以及导入依赖的类
package org.janzhou.nvmr.storage
import java.io._
import java.nio.ByteBuffer
import scala.reflect.ClassTag
object Serializer {
def serialize(obj:Any): ByteBuffer = {
val bos = new ByteArrayOutputStream()
val out = new ObjectOutputStream(bos)
out.writeObject(obj)
val byteArray = bos.toByteArray()
ByteBuffer.wrap(byteArray)
}
def deserialize[T: ClassTag](buf:ByteBuffer): T = {
buf.rewind()
val array = buf.array()
val inputStream = new ByteArrayInputStream(array)
val objectInputStream = new ObjectInputStream(inputStream)
val obj = objectInputStream.readObject()
objectInputStream.close()
obj.asInstanceOf[T]
}
}
开发者ID:janzhou,项目名称:approxssd,代码行数:27,代码来源:Serializer.scala
示例6: BenchmarkThread
//设置package包名称以及导入依赖的类
package org.janzhou.nvmr
import org.janzhou.nvmr.storage._
import java.nio.ByteBuffer
class BenchmarkThread(id:Int, count:Int, device:String) extends Thread {
val storage = new Driver(device)
val pagesize = 1024*8
val buffer = ByteBuffer.allocate(1024*8) // one page
val offset = 1024*1024*1024*id
override def run() {
for( i <- 0 to count ) {
storage.write(offset+pagesize*i, buffer)
}
}
}
object StorageBenchmark {
def main (args: Array[String]) {
val device = args(0)
val list = List.range(1,2)
val thread_list = list.map( x => new BenchmarkThread(x, 1024*1024, device) )
thread_list.foreach(thread => thread.start())
thread_list.foreach(thread => thread.join())
val thread = new BenchmarkThread(0, 1024, device)
thread.start()
thread.join()
}
}
开发者ID:janzhou,项目名称:approxssd,代码行数:32,代码来源:StorageBenchmark.scala
示例7: Loader
//设置package包名称以及导入依赖的类
import java.io._
import java.nio.ByteBuffer
class Loader(path: String) {
def getPath(filename: String) = path + '/' + filename
val trainImages = Loader.getImages(getPath("train-images-idx3-ubyte"))
val trainLabels = Loader.getLabels(getPath("train-labels-idx1-ubyte"))
val testImages = Loader.getImages(getPath("t10k-images-idx3-ubyte"))
val testLabels = Loader.getLabels(getPath("t10k-labels-idx1-ubyte"))
}
object Loader {
def getImages(filename: String): Array[Array[Float]] = {
val stream = new FileInputStream(filename)
val buf = new Array[Byte](4)
stream.read(buf)
assert(buf.deep == Array[Byte](0, 0, 8, 3).deep)
stream.read(buf)
// data is in big endian, and java likes it
val numImages = ByteBuffer.wrap(buf).getInt()
stream.read(buf)
val imHeight = ByteBuffer.wrap(buf).getInt()
assert(imHeight == 28)
stream.read(buf)
val imWidth = ByteBuffer.wrap(buf).getInt()
assert(imWidth == 28)
val images = new Array[Array[Float]](numImages)
val imageBuffer = new Array[Byte](imWidth * imHeight)
var i = 0
for (i <- 0 until numImages) {
stream.read(imageBuffer)
images(i) = imageBuffer.map(e => ((e & 0xFF).toFloat / 255) - 0.5F)
}
images
}
def getLabels(filename: String): Array[Float] = {
val stream = new FileInputStream(filename)
val buf = new Array[Byte](4)
stream.read(buf)
assert(buf.deep == Array[Byte](0, 0, 8, 1).deep)
stream.read(buf)
val numLabels = ByteBuffer.wrap(buf).getInt()
val labels = new Array[Byte](numLabels)
stream.read(labels)
labels.map(_.toFloat)
}
}
开发者ID:pierric,项目名称:Mnist-Caffe-Spark,代码行数:56,代码来源:Loader.scala
示例8: IpAddress
//设置package包名称以及导入依赖的类
package com.soteradefense.dga.graphx.louvain
import java.net.InetAddress
import java.nio.ByteBuffer
object IpAddress {
def toString(address: Long) = {
val byteBuffer = ByteBuffer.allocate(8)
val addressBytes = byteBuffer.putLong(address)
// The below is needed because we don't have an unsigned Long, and passing a byte array
// with more than 4 bytes causes InetAddress to interpret it as a (bad) IPv6 address
val tmp = new Array[Byte](4)
Array.copy(addressBytes.array, 4, tmp, 0, 4)
InetAddress.getByAddress(tmp).getHostAddress()
}
def toLong(_address: String): Long = {
val address = try {
InetAddress.getByName(_address)
} catch {
case e: Throwable => throw new IllegalArgumentException("Could not parse address: " + e.getMessage)
}
val addressBytes = address.getAddress
val bb = ByteBuffer.allocate(8)
addressBytes.length match {
case 4 =>
bb.put(Array[Byte](0,0,0,0)) // Need a filler
bb.put(addressBytes)
case n =>
throw new IndexOutOfBoundsException("Expected 4 byte address, got " + n)
}
bb.getLong(0)
}
}
开发者ID:kaustubhhiware,项目名称:GraphWise,代码行数:38,代码来源:IpAddress.scala
示例9: FormDataCodecSpec
//设置package包名称以及导入依赖的类
import java.nio.ByteBuffer
import java.nio.charset.StandardCharsets
import korolev.server.FormDataCodec
import org.scalatest.{FlatSpec, Matchers}
class FormDataCodecSpec extends FlatSpec with Matchers {
"decode" should "parse valid multipart/form-data body" in {
val body = """--Asrf456BGe4h
|Content-Disposition: form-data; name="DestAddress"
|
|[email protected]
|--Asrf456BGe4h
|Content-Disposition: form-data; name="MessageTitle"
|
|I'm indignant
|--Asrf456BGe4h
|Content-Disposition: form-data; name="MessageText"
|
|Hello, Vasily! Your hand lion, which you left with me
|last week, tore my whole sofa. Please take it away
|soon! In the attachment, two pictures with consequences.
|--Asrf456BGe4h
|Content-Disposition: form-data; name="AttachedFile1"; filename="horror-photo-1.jpg"
|Content-Type: image/jpeg
|
|<blob1>
|--Asrf456BGe4h
|Content-Disposition: form-data; name="AttachedFile2"; filename="horror-photo-2.jpg"
|Content-Type: image/jpeg
|
|<blob2>
|--Asrf456BGe4h--
""".stripMargin
val bodyBuffer = ByteBuffer.wrap(body.getBytes(StandardCharsets.US_ASCII))
val codec = new FormDataCodec(100500)
val formData = codec.decode(bodyBuffer, "Asrf456BGe4h")
formData.text("DestAddress") should be ("[email protected]")
formData.text("MessageTitle") should be ("I'm indignant")
formData.bytes("AttachedFile2") should be {
ByteBuffer.wrap("<blob2>".getBytes)
}
}
}
开发者ID:techyogillc,项目名称:ServerSideScalaCode,代码行数:48,代码来源:FormDataCodecSpec.scala
示例10: Avro
//设置package包名称以及导入依赖的类
package com.lukecycon.avro
import java.io.ByteArrayOutputStream
import org.apache.avro.io.EncoderFactory
import org.apache.avro.file.BZip2Codec
import java.nio.ByteBuffer
import org.apache.avro.io.DecoderFactory
object Avro {
def schemaFor[T: AvroFormat] = implicitly[AvroFormat[T]].schema
def write[T: AvroFormat](thing: T, compress: Boolean = false): Array[Byte] = {
val out = new ByteArrayOutputStream
val encoder = EncoderFactory.get.binaryEncoder(out, null)
implicitly[AvroFormat[T]].writeValue(thing, encoder)
encoder.flush
if (compress) {
new BZip2Codec().compress(ByteBuffer.wrap(out.toByteArray)).array
} else {
out.toByteArray
}
}
def writeHex[T: AvroFormat](thing: T): String =
byteArrayToHexString(write(thing))
def read[T: AvroFormat](bytes: Array[Byte],
compressed: Boolean = false): Either[String, T] = {
val byts = if (compressed) {
new BZip2Codec().decompress(ByteBuffer.wrap(bytes)).array
} else {
bytes
}
val decoder = DecoderFactory.get.binaryDecoder(byts, null)
implicitly[AvroFormat[T]].decodeValue(Nil, decoder)
}
def readHex[T: AvroFormat](hex: String): Either[String, T] =
read(
hex
.replace(" ", "")
.grouped(2)
.map(Integer.parseInt(_, 16).toByte)
.toArray)
private def byteArrayToHexString(bb: Array[Byte]): String =
bb.map("%02X" format _).mkString.grouped(2).mkString(" ")
}
开发者ID:themattchan,项目名称:Skaro,代码行数:54,代码来源:Avro.scala
示例11: 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
示例12: update
//设置package包名称以及导入依赖的类
package com.spooky.cipher
import javax.crypto.spec.SecretKeySpec
import javax.crypto.Cipher
import java.nio.ByteBuffer
import spooky.util.ByteString
import akka.util.FakeBStrings
sealed trait WriteCipher {
def update(bs: Array[Byte]): Array[Byte]
def update(bb: ByteBuffer): ByteString
def update(bb: ByteString): ByteString
def updateToBytes(bb: ByteBuffer): Array[Byte]
}
class RC4WriteCipher(writeKey: SecretKeySpec) extends RC4Cipher(writeKey, true) with WriteCipher
object WritePlain extends WriteCipher {
def update(bb: ByteBuffer): ByteString = FakeBStrings(bb.duplicate)
def update(bs: Array[Byte]): Array[Byte] = bs
def update(bb: ByteString): ByteString = bb
def updateToBytes(bb: ByteBuffer): Array[Byte] = bb.array
}
开发者ID:zpooky,项目名称:bittorrent,代码行数:27,代码来源:WriteCipher.scala
示例13: ByteBuffers
//设置package包名称以及导入依赖的类
package spooky.util
import java.nio.ByteBuffer
object ByteBuffers {
def allocate(size: Int): ByteBuffer = {
???
}
def shallowWrapBB(buffer: ByteBuffer*): ByteBuffer = {
???
}
def shallowWrapBS(buffer: ByteString*): ByteBuffer = {
???
}
}
开发者ID:zpooky,项目名称:bittorrent,代码行数:16,代码来源:ByteBuffers.scala
示例14: Checksum
//设置package包名称以及导入依赖的类
package com.spooky.bittorrent
import scala.annotation.tailrec
import java.security.MessageDigest
import java.util.Arrays
import org.apache.commons.codec.binary.Hex
import java.nio.ByteBuffer
sealed case class Checksum(sum: Array[Byte], algorithm: Algorithm) {
def check(other: Array[Byte]): Boolean = {
check(other.length, other)
}
def check(length: Int, other: Array[Byte]*): Boolean = {
@tailrec
def rec(length: Int, other: Seq[Array[Byte]], index: Int, digest: MessageDigest): Array[Byte] = {
if (other.length - 1 == index) {
digest.update(other(index), 0, length)
digest.digest
} else {
digest.update(other(index))
rec(length, other, index + 1, digest)
}
}
val digester = MessageDigest.getInstance(algorithm.toString)
MessageDigest.isEqual(sum, rec(length, other, 0, digester))
}
def compare(other: Array[Byte]): Boolean = MessageDigest.isEqual(sum, other)
override def toString: String = Hex.encodeHexString(sum)
override def hashCode: Int = Arrays.hashCode(sum)
override def equals(o: Any): Boolean = o match {
case Checksum(otherHash, Sha1) => MessageDigest.isEqual(otherHash, sum)
case _ => false
}
}
object Checksum {
def parse(raw: ByteBuffer, algorithm: Algorithm): Checksum = {
val checksum = Array.ofDim[Byte](algorithm.bytes)
for (n <- 0 until algorithm.bytes) {
checksum(n) = raw.get
}
Checksum(checksum, algorithm)
}
}
开发者ID:zpooky,项目名称:bittorrent,代码行数:44,代码来源:Checksum.scala
示例15: UuidUtil
//设置package包名称以及导入依赖的类
package com.dbrsn.datatrain.util
import java.nio.ByteBuffer
import java.util.Base64
import java.util.UUID
object UuidUtil {
private val encoder: Base64.Encoder = Base64.getUrlEncoder
private val decoder: Base64.Decoder = Base64.getUrlDecoder
private val lastRedundantCharacters: String = "=="
def toBase64(uuid: UUID): String = {
val uuidBytes = ByteBuffer.wrap(new Array[Byte](16))
uuidBytes.putLong(uuid.getMostSignificantBits)
uuidBytes.putLong(uuid.getLeastSignificantBits)
val result = encoder.encodeToString(uuidBytes.array())
if (result.endsWith(lastRedundantCharacters)) {
result.substring(0, result.length - lastRedundantCharacters.length)
} else {
result
}
}
def toUuid(str: String): UUID = try {
UUID.fromString(str)
} catch {
case e: IllegalArgumentException =>
val uuidBytes = ByteBuffer.wrap(decoder.decode(str))
val result = new UUID(uuidBytes.getLong, uuidBytes.getLong)
if (uuidBytes.hasRemaining) {
throw new IllegalArgumentException("Invalid UUID string: " + str)
}
result
}
}
开发者ID:dborisenko,项目名称:data-train,代码行数:37,代码来源:UuidUtil.scala
示例16: NioSocketChannel
//设置package包名称以及导入依赖的类
package knot.remote.channel
import java.net.SocketAddress
import java.nio.ByteBuffer
import java.nio.channels.SocketChannel
object NioSocketChannel {
def apply(): NioSocketChannel = new NioSocketChannel(SocketChannel.open())
}
class NioSocketChannel(override val channel: SocketChannel) extends NioChannel(channel) {
lazy val incomingBuffer: ByteBuffer = ByteBuffer.allocate(1)
lazy val outgoingBuffer: ByteBuffer = ByteBuffer.allocate(1)
def connect(socketAddress: SocketAddress): Boolean = channel.connect(socketAddress)
def finishConnect: Boolean = channel.finishConnect()
override def bind(address: SocketAddress): Unit = channel.bind(address)
override def readInternal(): Int = {
val r = channel.read(incomingBuffer)
incomingBuffer.flip()
r
}
override def writeInternal(bytes: Array[Byte]): Unit = {
outgoingBuffer.put(bytes)
}
override def flushInternal(): Unit = {
if (!isFlushPending) {
setOpWrite()
outgoingBuffer.flip()
val r = channel.write(outgoingBuffer)
log.debug(s"CLIENT: Write. $r")
if (!outgoingBuffer.hasRemaining) {
outgoingBuffer.clear()
clearOpWrite()
}
}
}
}
开发者ID:defvar,项目名称:knot,代码行数:44,代码来源:NioSocketChannel.scala
示例17: PacketReader
//设置package包名称以及导入依赖的类
package handlers.packets
import java.nio.{ByteBuffer, ByteOrder}
class PacketReader(val data: Array[Byte]) extends Packets {
var cursor: Int = 0
def skip(a: Int): Unit = {
cursor += a
}
def readByte(): Byte = {
val res: Byte = data(cursor)
cursor += byteSize
res
}
def readShort(endian: ByteOrder = ByteOrder.BIG_ENDIAN): Short = {
val res: Short = ByteBuffer.wrap(data.slice(cursor, cursor+shortSize)).order(endian).getShort
cursor += shortSize
res
}
def readInt(endian: ByteOrder = ByteOrder.BIG_ENDIAN): Int = {
val res: Int = ByteBuffer.wrap(data.slice(cursor, cursor+intSize)).order(endian).getInt
cursor += intSize
res
}
def readString(size: Int): String = {
val res: String = PacketsUtils.printableString(new String(data.slice(cursor, cursor+size)))
cursor += size
res
}
}
开发者ID:franblas,项目名称:NAOC,代码行数:39,代码来源:PacketReader.scala
示例18: 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
示例19: NpyFileSpec
//设置package包名称以及导入依赖的类
import java.nio.channels.FileChannel
import java.nio.charset.StandardCharsets
import java.nio.file.{Paths, StandardOpenOption}
import java.nio.{ByteBuffer, ByteOrder}
import sys.process._
import scala.collection.immutable.Range.Inclusive
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import org.scalatest.FlatSpec
import com.indix.ml2npy.NpyFile
class NpyFileSpec extends FlatSpec{
val nosetestspath="nosetests "
val pathToTest = getClass.getResource("/python/Npytest.py").getPath+":"
"NpyFile" should "Convert a float array correctly" in {
val content = NpyFile[Float].addElements(Seq(0.3f, 0.5f))
val channel = FileChannel.open(Paths.get("/tmp/test.npy"), StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE)
channel.write(content)
channel.close()
val command=nosetestspath + pathToTest+"test_0"
val response=command.!
assert(response==0)
}
"NpyFile" should "Convert a sequence of integers correctly" in {
val intContent = NpyFile[Int].addElements(1 to 10)
val intChannel = FileChannel.open(Paths.get("/tmp/inttest.npy"), StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE)
intChannel.write(intContent)
intChannel.close()
val command=nosetestspath + pathToTest+"test_1"
val response=command.!
assert(response==0)
}
"NpyFile" should "write bytes directly" in {
val data: Inclusive = 1 to 10
val intContent3 = NpyFile[Int]
data.foreach(intContent3.addToBuffer)
val bytes = intContent3.getBytes
val intChannel3 = FileChannel.open(Paths.get("/tmp/inttest3.npy"), StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE)
intChannel3.write(ByteBuffer.wrap(bytes))
intChannel3.close()
val command=nosetestspath + pathToTest+"test_3"
val response=command.!
assert(response==0)
}
}
开发者ID:indix,项目名称:ml2npy,代码行数:50,代码来源:NpyFileSpec.scala
示例20: ExpandableByteBuffer
//设置package包名称以及导入依赖的类
package xyz.hyperreal.ramfile
import java.nio.ByteBuffer
class ExpandableByteBuffer
{
private var array: Array[Byte] = null
private var _buffer: ByteBuffer = null
private var _size = 0
array = allocate( 16 )
def buffer = _buffer
def size = _size
def size_=( s: Int ) {
val cur = _buffer.position
sizeHint( s )
_size = s
if (cur > _size)
_buffer.position( _size )
}
def allocate( capacity: Int ) = {
val res = new Array[Byte]( capacity )
val p =
if (_buffer eq null)
0
else
_buffer.position
_buffer = ByteBuffer.wrap( res )
_buffer.position( p )
res
}
def getting( bytes: Int ) = assert( _buffer.position + bytes <= _size, "attempting to read past end of buffer" )
def putting( bytes: Int ) = sizeHint( _buffer.position + bytes )
def sizeHint( len: Int ) {
if (len > array.length && len >= 1) {
val newarray = allocate( len*2 )
compat.Platform.arraycopy( array, 0, newarray, 0, _size )
array = newarray
}
_size = _size max len
}
}
开发者ID:edadma,项目名称:ramfile,代码行数:55,代码来源:ExpandableByteBuffer.scala
注:本文中的java.nio.ByteBuffer类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论