Using Data.Binary.decodeFile, encountered error "demandInput: not enough bytes"

670 views Asked by At

I'm attempting to use the encodeFile and decodeFile functions in Data.Binary to save a very large datastructure so that I don't have to recompute it every time I run this program. The relevant encoding- and decoding-functions are as follows:

writePlan :: IO ()
writePlan = do (d, _, bs) <- return subjectDomain
               outHandle <- openFile "outputfile" WriteMode
               ((ebsP, aP), cacheData) <- preplanDomain d bs
               putStrLn "Calculated."

               let toWrite = ((map pseudofyOverEBS ebsP, aP),
                              pseudofyOverMap cacheData) :: WrittenData
                 in do encodeFile preplanFilename $ encode toWrite
                    putStrLn "Done."


readPlan :: IO (([EvaluatedBeliefState], [Action]), MVar HeuCache)
readPlan = do (d, _, _) <- return subjectDomain
              inHandle <- openFile "outputfile" ReadMode

              ((ebsP, aP), cacheData) <-  decodeFile preplanFilename :: IO WrittenData

              fancyCache <- newMVar (M.empty, depseudofyOverMap cacheData)
              return $! ((map depseudofyOverEBS ebsP, aP), fancyCache)

The program to calculate and write the file (using writePlan) executes without error, outputting a gigantic binary file. However, when I run the program which takes in this file, executing readPlan results in the error (the program name is "Realtime"):

Realtime: demandInput: not enough bytes

I can't make head nor tail of this, and scouring Google has turned up no substantial documentation or discussion of this message. Any insight would be appreciated!

1

There are 1 answers

0
kvelicka On

I am very late to the party, but found this while looking for help with a similar issue. I'm working with the incremental interface for Data.Binary.Get. As you can see in here, the function is defined in Data.Binary.Get.Internal. Now I am guessing, but your decodeFile function probably does some sort of parsing and the error is thrown because the file does not parse completely (i.e. the parser thinks that there must be something else in the file but it reaches EOF already).

Hope that helps anyone with this/similar issues!