Does an OutputStreamWriter without buffering exist?
<p>I need to convert a stream of <code>char</code> into a stream of <code>byte</code>s, i.e. I need an adapter from a <code>java.io.Writer</code> interface to a <code>java.io.OutputStream</code>, supporting any valid <code>Charset</code> which I will have as a configuration parameter.</p>

<p>However, the <code>java.io.OutputStreamWriter</code> class has a hidden secret: the <code>sun.nio.cs.StreamEncoder</code> object it delegates to underneath creates an 8192 byte (8KB) buffer, even if you don't ask it to.</p>

<p>The problem is, at the <code>OutputStream</code> end I have inserted a wrapper that needs to <em>count the amount of bytes being written</em>, so that it immediately stops execution of the source system once a specific amount of bytes has been output. And if <code>OutputStreamWriter</code> is creating an 8K buffer, I simply get notified of the amount of bytes generated <strong>too late</strong> because they will only reach my counter when the buffer is <em>flushing</em> (so there will be already more than 8,000 already-generated bytes waiting for me at the <code>OutputStreamWriter</code> buffer).</p>

<p>So the question is, <strong>is there anywhere in the Java runtime a <code>Writer</code> -> <code>OutputStream</code> bridge that can run unbuffered</strong>?</p>

<p>I would really, really hate to have to write this myself :(...</p>

<p><strong>NOTE</strong>: hitting <em>flush()</em> on the <code>OutputStreamWriter</code> for each write is not a valid alternative. That brings a large performance penalty (there's a <code>synchronized</code> block involved at the <code>StreamEncoder</code>).</p>

<p><strong>NOTE 2</strong>: I understand it might be necessary to keep a small char overflow at the bridge in order to compute surrogates. It's not that I need to stop the execution of the source system in the very moment it generates the n-th byte (that would not be possible given bytes can come to me in the form of a larger <code>byte[]</code> in a <code>write</code> call). But I need to stop it <em>asap</em>, and waiting for an 8K, 2K or even 200-byte buffer to flush would simply be too late.</p>