title: convert streamed buffers to utf8-string
body: <p>I want to make a HTTP-request using node.js to load some text from a webserver. Since the response can contain much text (some Megabytes) I want to process each text chunk separately. I can achieve this using the following code:</p>
  
  <pre><code>var req = http.request(reqOptions, function(res) {
      ...
      res.setEncoding('utf8');
      res.on('data', function(textChunk) {
          // process utf8 text chunk
      });
  });
  </code></pre>
  
  <p>This seems to work without problems. However I want to support HTTP-compression, so I use zlib:</p>
  
  <pre><code>var zip = zlib.createUnzip();
  
  // NO res.setEncoding('utf8') here since we need the raw bytes for zlib
  res.on('data', function(chunk) {
      // do something like checking the number of bytes downloaded
      zip.write(chunk); // give the raw bytes to zlib, s.b.
  });
  
  zip.on('data', function(chunk) {
      // convert chunk to utf8 text:
      var textChunk = chunk.toString('utf8');
  
      // process utf8 text chunk
  });
  </code></pre>
  
  <p>This can be a problem for multi-byte characters like <code>'\u00c4'</code> which consists of two bytes: <code>0xC3</code> and <code>0x84</code>. If the first byte is covered by the first chunk (<code>Buffer</code>) and the second byte by the second chunk then <code>chunk.toString('utf8')</code> will produce incorrect characters at the end/beginning of the text chunk. How can I avoid this?</p>
  
  <p>Hint: I still need the buffer (more specifically the number of bytes in the buffer) to limit the number of downloaded bytes. So using <code>res.setEncoding('utf8')</code> like in the first example code above for non-compressed data does not suit my needs.</p>