I'd like to use Buffy to interpret binary data starting from the middle of an array. I also need to find out how many bytes of the array have been consumed by Buffy.
Let's say I have a dynamic buffer definition like this:
(ns foo.core
(:refer-clojure :exclude [read])
(:use [byte-streams])
(:require [clojurewerkz.buffy.core :refer :all]
[clojurewerkz.buffy.frames :refer :all]
[clojurewerkz.buffy.types.protocols :refer :all])
(:import [io.netty.buffer Unpooled ByteBuf]))
(def dynbuf
(let [string-encoder (frame-encoder [value]
length (short-type) (count value)
string (string-type (count value)) value)
string-decoder (frame-decoder [buffer offset]
length (short-type)
string (string-type (read length buffer offset)))]
(dynamic-buffer (frame-type string-encoder string-decoder second))))
I hoped I could use a Netty ByteBuf
to parse a bunch of bytes using dynbuf
starting at an offset:
(def buf
(let [bytes (concat [0 0 0 4] (map #(byte %) "Foobar"))
offset 2]
(Unpooled/wrappedBuffer (byte-array bytes) offset (- (count bytes) offset))))
At this point, I can parse buf
per dynbuf
:
user> (decompose dynbuf buf)
["Foob"]
At this point, I was hoping that reading the short-type
and the string-type
from buf
has moved its readerIndex
by 6, but alas, it is not so:
user> (.readerIndex buf)
0
Is this because buffy/decompose
makes some kind of shallow copy of the stream for its internal use, so the readerIndex
of the outer buf
is not updated? Or am I misunderstanding what readerIndex
is supposed to be?
How can I achieve my original goal of passing a (byte-array)
at a given offset to Buffy and learning how many bytes it has consumed?