title: Filter ASCII stream in Java
body: <p>I want to write a class to be used in comunicaion between Java and C++</p>
  
  <p>There are a lot of discussion about Java and C++ communication. Here I'm not requesting how to communicate, I konw an i know how.</p>
  
  <p>I create a </p>
  
  <pre><code> OutputStream os = socketChannel.socket().getOutputStream();
   OutputStreamWriter osw = new OutputStreamWriter( os);
   BufferedWriter wr = new BufferedWriter( osw );
  </code></pre>
  
  <p>then I write</p>
  
  <pre><code> String data= dataArea.getText();
  
   wr.write( dataArea.getText() );
   wr.write('\0');//here is the wire with C++
   wr.flush(); // flushes the stream
  </code></pre>
  
  <hr>
  
  <p>I send some text:</p>
  
  <pre><code>Hi this is a test
  i send also 
  character as dollar $ ...
  or other stange é*"£$%&amp;/(()=?'^ìì[]@!&lt;&gt;#òùèé+*
  and so on
  </code></pre>
  
  <p>So reading raw text send between two system i see:</p>
  
  <pre><code>00000000  48 69 20 74 68 69 73 20  69 73 20 61 20 74 65 73   Hi this  is a tes 
  00000010  74 0A 69 20 73 65 6E 64  20 61 6C 73 6F 20 0A 63   t.i send  also .c 
  00000020  68 61 72 61 63 74 65 72  20 61 73 20 64 6F 6C 6C   haracter  as doll 
  00000030  61 72 20 24 20 2E 2E 2E  0A 6F 72 20 6F 74 68 65   ar $ ... .or othe 
  00000040  72 20 73 74 61 6E 67 65  20 C3 A9 2A 22 C2 A3 24   r stange  ..*"..$ 
  00000050  25 26 2F 28 28 29 3D 3F  27 5E C3 AC C3 AC 5B 5D   %&amp;/(()=? '^....[] 
  00000060  40 21 3C 3E 23 C3 B2 C3  B9 C3 A8 C3 A9 2B 2A 0A   @!&lt;&gt;#... .....+*. 
  00000070  61 6E 64 20 73 6F 20 6F  6E 00                     and so o n.
  </code></pre>
  
  <p>This is near the solution.</p>
  
  <p>changing CHARSET "US-ASCII" I obtain</p>
  
  <pre><code>Hi this is a test
      i send also 
      character as dollar $ ...
      or other stange é*"£$%&amp;/(()=?'^ìì[]@!&lt;&gt;#òùèé+*
      and so on
  
  
  00000000  48 69 20 74 68 69 73 20  69 73 20 61 20 74 65 73   Hi this  is a tes 
  00000010  74 0A 20 20 20 20 69 20  73 65 6E 64 20 61 6C 73   t.    i  send als 
  00000020  6F 20 0A 20 20 20 20 63  68 61 72 61 63 74 65 72   o .    c haracter 
  00000030  20 61 73 20 64 6F 6C 6C  61 72 20 24 20 2E 2E 2E    as doll ar $ ... 
  00000040  0A 20 20 20 20 6F 72 20  6F 74 68 65 72 20 73 74   .    or  other st 
  00000050  61 6E 67 65 20 3F 2A 22  3F 24 25 26 2F 28 28 29   ange ?*" ?$%&amp;/(() 
  00000060  3D 3F 27 5E 3F 3F 5B 5D  40 21 3C 3E 23 3F 3F 3F   =?'^??[] @!&lt;&gt;#??? 
  00000070  3F 2B 2A 0A 20 20 20 20  61 6E 64 20 73 6F 20 6F   ?+*.     and so o 
  00000080  6E 00 
  </code></pre>
  
  <p>This is near my purpose.</p>
  
  <hr>
  
  <p>I need to transfer only ASCII with extended characters "è"-->E8</p>
  
  <p>Then I need to put '\0' at the end of the string.</p>
  
  <p>'\0' is message terminator.</p>
  
  <p>Now the question: using Java i like to create a Writer or stream writer to output the specific binary data format.</p>
  
  <p><strong>At what level is correct to work with <code>OutputStreamWriter</code> or <code>BufferedWriter</code> and should I rewrite from scratch extend or use some library?
  Are some example all around about what I like to do?</strong>
  I'll do the same for input</p>
  
  <p>I like to write it in this form because tomorrow when I rewrite C++ server in Java I use on the both side DataOutputStream.</p>
  
  <p>Using a <code>while</code> is not a nice solution I prefer a more Java integrated solution.</p>