<p>I want something that looks like a file handle but is really backed by an in-memory buffer to use for I/O redirects. How can I do this?</p>

[{'answer_id': 65211, 'author': 'Chris Smith', 'author_id': 9073, 'author_profile': 'https://Stackoverflow.com/users/9073', 'pm_score': 1, 'selected': False, 'text': '<p>This may not be possible. <a href="http://en.wikipedia.org/wiki/Glasgow_Haskell_Compiler" rel="nofollow noreferrer">GHC</a>, at least, seems to require a handle to have an OS file descriptor that is used for all read/write/seek operations.</p>\n\n<p>See <code>/libraries/base/IOBase.lhs</code> from the GHC sources.</p>\n\n<p>You may be able to get the same effect by enlisting the OS\'s help: create a temporary file, connect the handle to it and then memory map the file for the I/O redirects. This way, all the handle I/O would become visible in the memory mapped section.</p>\n'}, {'answer_id': 67306, 'author': "Lawrence D'Anna", 'author_id': 10168, 'author_profile': 'https://Stackoverflow.com/users/10168', 'pm_score': -1, 'selected': True, 'text': "<p>It's not possible without modifying the compiler.  This is because Handle is an abstract data type, not a typeclass.</p>\n"}, {'answer_id': 68355, 'author': 'Jason Dagit', 'author_id': 5113, 'author_profile': 'https://Stackoverflow.com/users/5113', 'pm_score': 2, 'selected': False, 'text': "<p>If you can express what you want to do in terms of C or system calls you could use Haskell's Foreign Function Interface (FFI).  I started to suggest using mmap, but on second thought I think mmap might be a mapping the wrong way even if you used it with the anonymous option.</p>\n\n<p>You can find more information about the Haskell FFI at the haskell.org wiki.</p>\n"}, {'answer_id': 1047220, 'author': 'cjs', 'author_id': 107294, 'author_profile': 'https://Stackoverflow.com/users/107294', 'pm_score': 2, 'selected': False, 'text': "<p>This is actually a bug in the library design, and one that's annoyed me, too. I see two approaches to doing what you want, neither of which is terribly attractive.</p>\n\n<ol>\n<li><p>Create a new typeclass, make the current handle an instance of it, write another instance to do the in-memory-data thing, and change all of your programs that need to use this facility. Possibly this is as simple as importing <code>System.SIO</code> (or whatever you want to call it) instead of <code>System.IO</code>. But if you use the custom I/O routines in libraries such as <code>Data.ByteString</code>, there's more work to be done there.</p></li>\n<li><p>Rewrite the I/O libraries to extend them to support this. Not trivial, and a lot of work, but it wouldn't be particularly difficult work to do. However, then you've got a compatibility issue with systems that don't have this library.</p></li>\n</ol>\n"}, {'answer_id': 7700839, 'author': 'John Millikin', 'author_id': 3560, 'author_profile': 'https://Stackoverflow.com/users/3560', 'pm_score': 4, 'selected': False, 'text': '<p>I just wrote a library which provides this, called "knob" [<a href="http://hackage.haskell.org/package/knob">hackage</a>]. You can use it to create <code>Handle</code>s which reference/modify a <code>ByteString</code>:</p>\n\n<pre><code>import Data.ByteString (pack)\nimport Data.Knob\nimport System.IO\n\nmain = do\n    knob &lt;- newKnob (pack [])\n    h &lt;- newFileHandle knob "test.txt" WriteMode\n    hPutStrLn h "Hello world!"\n    hClose h\n    bytes &lt;- Data.Knob.getContents knob\n    putStrLn ("Wrote bytes: " ++ show bytes)\n</code></pre>\n'}, {'answer_id': 48191061, 'author': 'Asa', 'author_id': 1769679, 'author_profile': 'https://Stackoverflow.com/users/1769679', 'pm_score': 1, 'selected': False, 'text': '<p>To add a modern answer to this question, you could use <code>createPipe</code> from <code>System.Process</code>:</p>\n\n<pre><code>createPipe :: IO (Handle, Handle)\n</code></pre>\n\n<p><a href="https://www.stackage.org/haddock/lts-10.3/process-1.6.1.0/System-Process.html#v:createPipe" rel="nofollow noreferrer">https://www.stackage.org/haddock/lts-10.3/process-1.6.1.0/System-Process.html#v:createPipe</a></p>\n'}]
['https://Stackoverflow.com/questions/60569', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5304/']