我使用scala-io
,scala-arm
来编写Long
-s 的二进制流。库本身应该是一种 Scala 的做事方式,但这些不在 Scalamaster
分支中——也许有人知道为什么?我不时使用它们。
1)克隆scala-io
:
git clone https://github.com/scala-incubator/scala-io.git
去scala-io/package
换成你Build.scala
的val scalaVersion
sbt package
2)克隆scala-arm
:
git clone https://github.com/jsuereth/scala-arm.git
去scala-arm/package
换成你build.scala
的scalaVersion :=
sbt package
3)在不太远的地方复制:
scala-io/core/target/scala-xxx/scala-io-core_xxx-0.5.0-SNAPSHOT.jar
scala-io/file/target/scala-xxx/scala-io-file_xxx-0.5.0-SNAPSHOT.jar
scala-arm/target/scala-xxx/scala-arm_xxx-1.3-SNAPSHOT.jar
4)启动REPL:
scala -classpath "/opt/scala-io/scala-io-core_2.10-0.5.0-SNAPSHOT.jar:
/opt/scala-io/scala-io-file_2.10-0.5.0-SNAPSHOT.jar:
/opt/scala-arm/scala-arm_2.10-1.3-SNAPSHOT.jar"
5):paste
实际代码:
import scalax.io._
// create data stream
val EOData = Vector(0xffffffffffffffffL)
val data = List(
(0, Vector(0L,1L,2L,3L))
,(1, Vector(4L,5L))
,(2, Vector(6L,7L,8L))
,(3, Vector(9L))
)
var it = Iterator[Long]()
for (rec <- data) {
it = it ++ Vector(rec._1).iterator.map(_.toLong)
it = it ++ rec._2.iterator
it = it ++ EOData.iterator
}
// write data at once
val out: Output = Resource.fromFile("/tmp/data")
out.write(it)(OutputConverter.TraversableLongConverter)