本文整理汇总了Scala中java.nio.ByteBuffer类的典型用法代码示例。如果您正苦于以下问题:Scala ByteBuffer类的具体用法?Scala ByteBuffer怎么用?Scala ByteBuffer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ByteBuffer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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)) }
})
}
示例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)
}
}
}
}
示例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
}
}
示例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
}
示例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]
}
}
示例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()
}
}
示例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)
}
}
示例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)
}
}
示例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)
}
}
}
示例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(" ")
}
示例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)
}
}
示例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
}
示例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 = {
???
}
}
示例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)
}
}
示例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
}
}