Binary buffer in Python
<p>In Python you can use <a href="https://docs.python.org/library/struct.html">StringIO</a> for a file-like buffer for character data. <a href="https://docs.python.org/library/mmap.html">Memory-mapped file</a> basically does similar thing for binary data, but it requires a file that is used as the basis. Does Python have a file object that is intended for binary data and is memory only, equivalent to Java's <a href="http://java.sun.com/javase/6/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream</a>?</p>

<p>The use-case I have is I want to create a ZIP file in memory, and <a href="https://docs.python.org/library/zipfile.html">ZipFile</a> requires a file-like object.</p>

<p>You are probably looking for <a href="http://docs.python.org/release/3.1.3/library/io.html#binary-i-o">io.BytesIO</a> class. It works exactly like StringIO except that it supports binary data:</p>

<pre><code>from io import BytesIO
bio = BytesIO(b"some initial binary data: \x00\x01")
</code></pre>

<p>StringIO will throw TypeError:</p>

<pre><code>from io import StringIO
sio = StringIO(b"some initial binary data: \x00\x01")
</code></pre>
