Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've used the <a href="http://github.com/harrah/sbinary" rel="nofollow noreferrer">sbinary</a> library before and it's very nice. The documentation is a little sparse but I would suggest first looking at the old <a href="http://code.google.com/p/sbinary/wiki/IntroductionToSBinary" rel="nofollow noreferrer">wiki page</a> as that gives you a starting point. Then check out the test specifications, as that gives you some very nice examples.</p> <p>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.</p> <p>As I say below, I'd now recommend people use <a href="http://scodec.org" rel="nofollow noreferrer">scodec</a> 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:</p> <pre><code>struct ST { long long ll; // @ 0 int i; // @ 8 short s; // @ 12 char ch1; // @ 14 char ch2; // @ 15 } ST; </code></pre> <p>A matching Scala case class would be:</p> <pre><code>case class ST(ll: Long, i: Int, s: Short, ch1: String, ch2: String) </code></pre> <p>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.</p> <p>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:</p> <pre><code>val myCodec: Codec[Long ~ Int ~ Short ~ String ~ String] = int64 ~ int32 ~ short16 ~ fixedSizeBits(8L, utf8) ~ fixedSizeBits(8L, utf8) </code></pre> <p>We can then transform this into the ST case class by calling the <code>xmap</code> 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:</p> <pre><code>val stCodec: Codec[ST] = myCodec.xmap[ST]({case ll ~ i ~ s ~ ch1 ~ ch2 =&gt; ST(ll, i, s, ch1, ch2)}, st =&gt; st.ll ~ st.i ~ st.s ~ st.ch1 ~ st.ch2) </code></pre> <p>Now, you can use the codec like so:</p> <pre><code>stCodec.encode(ST(1L, 2, 3.shortValue, "H", "I")) res0: scodec.Attempt[scodec.bits.BitVector] = Successful(BitVector(128 bits, 0x00000000000000010000000200034849)) res0.flatMap(stCodec.decode) =&gt; res1: scodec.Attempt[scodec.DecodeResult[ST]] = Successful(DecodeResult(ST(1,2,3,H,I),BitVector(empty))) </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload