score:8
I've used the sbinary library before and it's very nice. The documentation is a little sparse but I would suggest first looking at the old wiki page as that gives you a starting point. Then check out the test specifications, as that gives you some very nice examples.
The primary benefit of sbinary is that it gives you a way to describe the wire format of each object as a Format object. You can then encapsulate those formatted types in a higher level Format object and Scala does all the heavy lifting of looking up that type as long as you've included it in the current scope as an implicit object.
As I say below, I'd now recommend people use scodec instead of sbinary. As an example of how to use scodec, I'll implement how to read a binary representation in memory of the following C struct:
struct ST
{
long long ll; // @ 0
int i; // @ 8
short s; // @ 12
char ch1; // @ 14
char ch2; // @ 15
} ST;
A matching Scala case class would be:
case class ST(ll: Long, i: Int, s: Short, ch1: String, ch2: String)
I'm making things a bit easier for myself by just saying we're storing Strings instead of Chars and I'll say that they are UTF-8 characters in the struct. I'm also not dealing with endian details or the actual size of the long and int types on this architecture and just assuming that they are 64 and 32 respectively.
Scodec parsers generally use combinators to build higher level parsers from lower level ones. So for below, we'll define a parser which combines a 8 byte value, a 4 byte value, a 2 byte value, a 1 byte value and one more 1 byte value. The return of this combination is a Tuple codec:
val myCodec: Codec[Long ~ Int ~ Short ~ String ~ String] =
int64 ~ int32 ~ short16 ~ fixedSizeBits(8L, utf8) ~ fixedSizeBits(8L, utf8)
We can then transform this into the ST case class by calling the xmap
function on it which takes two functions, one to turn the Tuple codec into the destination type and another function to take the destination type and turn it into the Tuple form:
val stCodec: Codec[ST] = myCodec.xmap[ST]({case ll ~ i ~ s ~ ch1 ~ ch2 => ST(ll, i, s, ch1, ch2)}, st => st.ll ~ st.i ~ st.s ~ st.ch1 ~ st.ch2)
Now, you can use the codec like so:
stCodec.encode(ST(1L, 2, 3.shortValue, "H", "I"))
res0: scodec.Attempt[scodec.bits.BitVector] = Successful(BitVector(128 bits, 0x00000000000000010000000200034849))
res0.flatMap(stCodec.decode)
=> res1: scodec.Attempt[scodec.DecodeResult[ST]] = Successful(DecodeResult(ST(1,2,3,H,I),BitVector(empty)))
I'd encourage you to look at the Scaladocs and not at the Guide as there's much more detail in the Scaladocs. The guide is a good start at the very basics but it doesn't get into the composition part much but the Scaladocs cover that pretty well.
score:1
If you are looking for a Java based solution, then I will shamelessly plug Preon. You just annotate the in memory Java data structure, and ask Preon for a Codec, and you're done.
score:1
Byteme is a parser combinators library for doing binary. You can try to use it for your tasks.
score:4
I don't know what you mean with "by hand" but using a simple DataInputStream
(apidoc here) is quite concise and clear:
val dis = new DataInputStream(yourSource)
dis.readFloat()
dis.readDouble()
dis.readInt()
// and so on
Taken from another SO question: http://preon.sourceforge.net/, it should be a framework to do binary encoding/decoding.. see if it has the capabilities you need
score:5
Scala itself doesn't have a binary data input library, but the java.nio
package does a decent job. It doesn't explicitly handle unsigned data--neither does Java, so you need to figure out how you want to manage it--but it does have convenience "get" methods that take byte order into account.
Source: stackoverflow.com
Related Query
- Parsing of binary data with scala
- Parsing multipart HTTP form data with file upload content using Scala
- Parsing matrix data with Scala
- Issue with parsing JSON data stored as string in Scala variable
- Parsing JSON in Play2 and Scala without Data Type
- Which is best data access options available for Play framework with Scala and PostgreSQL?
- Multiple constructors with the same number of parameters exception while transforming data in spark using scala
- Parsing a file with BodyParser in Scala Play20 with new lines
- How do I use Scala to parse CSV data with empty columns?
- Scala option parsing with scopt
- Handle chunked data sent by Play scala with AngularJs
- Json parsing in Scala with scala.util.parsing.json
- Genaric type parsing in Scala with default value
- Scala Array with different data types
- Scala functional way of processing large scala data with lazy collections
- How can I get random data generated for scala case classes with the ability to "change some values" for unit testing?
- Alternative to parsing json with option [ either [A,B ] ] in scala
- Insert data into a Hive table with HiveContext using Spark Scala
- Unable to choose a correct combinator for parsing and deal with it in Scala
- Spark Streaming Saving data to MySQL with foreachRDD() in Scala
- scala is using akka shipped with binary distribution instead of sbt 's one
- How to access binary data in mongoDB from scala (casbah)
- Spark - Scala - Replacing value in dataframe with lookup value from another data frame
- Spark - Csv data split with scala
- Data preprocessing with apache spark and scala
- Parsing JSON with Scala lift
- How to read from textfile(String type data) map and load data into parquet format(multiple columns with different datatype) in Spark scala dynamically
- Parse Data with Many Attributes in Scala
- Parsing recursive JSON structure with Lists in Scala
- Scala with cats exercise Data validation on Kleisli, why my code fails fast instead of accumulating errors?
More Query from same tag
- Memory issue while building spark
- Scala: using recursion for an interest rate calculator
- I want the index of each user in Gatling rampUsers
- Scala Immutable MultiMap
- Gatling Report non-html Request Customization
- How do you Nest different Spray Directives into 1 directive
- What is the use case of an expression inside a class without storing it in a value/variable?
- Spark read partition columns showing up null
- Creating a new scala class that relies on GraphFrames without serialization issues
- How much of a jar file gets loaded into the memory
- Custom Accumulator Spark error using Java class type in mix Scala and Java Spark project
- Multiplication algorithm of nxm and mxp matrices in Scala
- Merging 3 immutable maps in scala
- Akka and Actor behavior interface
- Integration tests in Akka in Scala only pass intermittently
- Play2.1 JSON Format with Empty String Validation
- Futures for blocking calls in Scala
- Handling properties in Scala
- sbt: Module not found: sbt-buildinfo
- Convert difficult json to object (scala, jackson)
- How to return early without return statement?
- Disambiguate constructor parameter with same name as class field of superclass
- Scala undeclared variable in case statement
- Handling exceptions in preStart in actors
- Scala - val a: AnyVal = true, but a.getClass is still Boolean?
- How to tell sbt-proguard to include java *.jars?
- How to case match a View in scala
- How to implement a custom DataType in Spark?
- How to edit existing JSON object with sprayJSON
- Not able to get rid of quotation marks in my regex