title: Reading Lines and byte[] from input stream
body: <p>I have a network socket which recieves the following types of messages: (about 24 per second)</p>
  
  <ul>
  <li><p>Commands EG: <code>[Pos,1,2,3,4,1,2,3,4]\n</code></p></li>
  <li><p>Images: <code>[IMG,36000]\nbyte[36000]</code> (byte[36000] is an array of 36000 bytes)</p></li>
  </ul>
  
  <p>So I would need an Reader for that stream that is able to read lines and byte[]. The problem is, that the BufferedReader converts the charset, which is very, very, very bad for images, so reading a string and converting it to byte seems not to be an option.</p>
  
  <p>I tried to connect a DataInputStreamReader and a BufferedReader to the stream but it seems to break after the first change of the reader, but I am not really sure about what caused the problem there.</p>
  
  <p>The obvious solution is:</p>
  
  <pre><code>char c;
  String s = "";
  do{
     c= (char)read.read(); //maybe charset conversion here
     s+=c;
  }while(c!= "\n");
  if(s.startsWith("[IMG")){
    int len = Integer.parseInt(s.split(",")[1]);
    byte[] img = new byte[len];
    read.read(img);
    ...   
  </code></pre>
  
  <p>But I am searching for a better one where I need to do less manually.</p>
  
  <ul>
  <li>What is the recommended way to deal with this problem? </li>
  <li>Is there a way
  to connect two or more readers to an input stream? </li>
  <li>Is there a reader which can read byte[] and strings?</li>
  <li>Is it probably easier to write everything to byte[] and read from that? (How do I know then, when to stop reading?)</li>
  <li>Can I prevent the chartype conversion which messes the byte[] up? (Then i could use the Buffered Reader)</li>
  </ul>
  
  <p>In the easiest case I could write that code: (Which is actually not much more :D)</p>
  
  <pre><code>String s = read.readLine();
  String[] parts = s.split(",");
  if(parts[0].equals("[IMG")){
    byte[] img = new byte[Integer.parseInt(parts[1])];
    read.readByte(img);
    ...
  </code></pre>