title: "available" of DataInputStream from Socket
body: <p>I have this code on the client side :</p>
  
  <pre><code>DataInputStream dis = new DataInputStream(socketChannel.socket().getInputStream());
  while(dis.available()){
       SomeOtherClass.method(dis);
  }
  </code></pre>
  
  <p>But <a href="http://docs.oracle.com/javase/6/docs/api/java/io/FilterInputStream.html#available%28%29" rel="nofollow"><code>available()</code></a> keeps returning <code>0</code>, although there is readable data in the stream. So after the actual data to be read is finished, empty data is passed to the other class to be read and this causes corruption.</p>
  
  <p>After a little search; I found that <code>available()</code> is not reliable when using with sockets, and that I should be reading first few bytes from stream to actually see if data is available to parse.</p>
  
  <p>But in my case; I have to pass the <a href="http://docs.oracle.com/javase/6/docs/api/java/io/DataInputStream.html" rel="nofollow"><code>DataInputStream</code></a> reference I get from the socket to some other class that I cannot change.</p>
  
  <p>Is it possible to read a few bytes from <code>DataInputStream</code> without corrupting it, or any other suggestions ?</p>