[Haskell-cafe] Success and one last issue with Data.Binary

John Van Enk vanenkj at gmail.com
Tue Jun 2 12:32:48 EDT 2009


I think getRemainingLazyByteString expects at least one byte (this,
perhaps, is not the appropriate behavior). You'll want to wrap your
call to getRemainingLazyByteString with a call to
Data.Binary.Get.remaining[1] like this:

foo = do
    r <- remaining
    rbs <- case r of
                     0 -> return empty -- Data.ByteString.Lazy.empty
                     _ -> getRemainingLazyByteString

Hope this helps. :)

/jve

1: http://hackage.haskell.org/packages/archive/binary/0.5.0.1/doc/html/Data-Binary-Get.html#v%3Aremaining

On Tue, Jun 2, 2009 at 12:20 PM, David Leimbach <leimy2k at gmail.com> wrote:
> I've got the following "printHex"  string as a response from a 9P server
> running on the Inferno Operating System. (thanks to a friendly mailing list
> contributor who sent a nice example of using Data.Binary)
> 1300000065ffff000400000600395032303030
> This is a little endian encoded ByteString with the following fields in it:
>  Rversion {size :: Word32,
>                 mtype :: Word8,
>                 tag :: Word16,
>                 msize :: Word32,
>                 ssize :: Word16,
>                 version :: ByteString}
> But when I try to use the following implementation of "get" to decode this
> stream, I'm getting the following error:
> "too few bytes. Failed reading at byte position 20"
> Unfortunately, I'm only expecting 19 bytes, and in fact never asked for byte
> 20.  (I am just asking for everything up to ssize, and then
> "getRemainingLazyByteString").
> Is this a bug?    Is it mine or in Data.Binary?  :-)
> Here's my "get" function:
>  get = do s <- getWord32le
>              mtype <- getWord8
>              getSpecific s mtype
>         where
>           getSpecific s mt
>                       | mt == mtRversion = do t <- getWord16le
>                                               ms <- getWord32le
>                                               ss <- getWord16le
>                                               v <-
> getRemainingLazyByteString
>                                               return $ MessageClient $
> Rversion {size=s,
>
>                         mtype=mt,
>
>                         tag=t,
>
>                         msize=ms,
>
>                         ssize=ss,
>
>                         version=v}
>
>
> The good news is I'm talking 9P otherwise, correctly, just having some
> decoding issues.  I hope to have a hackage package eventually for this.
> Dave
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>



-- 
/jve


More information about the Haskell-Cafe mailing list