title: JAVA : BufferdInputStream and BufferedOutputStream
body: <p>I have several questions-</p>
  
  <p><strong>1.</strong>  I have two computers connected by socket connection. When the program executes</p>
  
  <pre><code>outputStream.writeInt(value);
  outputStream.flush();
  </code></pre>
  
  <p>what actually happens? Does the program wait until the other computer reads the integer value?</p>
  
  <p><strong>2.</strong> How can I empty the <code>outputStream</code> or <code>inputStream</code>? Meaning, when emptying
  the <code>outputStream</code> or <code>inputStream</code>, whatever is written to that stream gets removed.
  (please don't suggest to do it by closing the connection!)
  I tried to empty the <code>inputStream</code> this way-</p>
  
  <pre><code>byte[] eatup=new byte[20*1024];
  int available=0;
  while(true)
  {
      available=serverInputStream.available();
      if(available==0)
         break;
      serverInputStream.read(eatup,0,available);
  }
  eatup=null;
  
  String fileName=(String)serverInputStream.readObject();
  </code></pre>
  
  <p>Program should not process the line as nothing else is being written on the <code>outputStream</code> .
  But my program executes it anyway and throws a <code>java.io.OptionalDataException</code> error.</p>
  
  <p><strong>Note:</strong> I am working on a client-server file transfer project. The client sends files to
  the server. The second code is for server terminal. If 'cancel button' is pressed on server
  end then it stops reading bytes from the <code>serverInputStream</code> and sends a signal(I used <code>int</code>  -1)
  to the client. When client receieves this signal it stops sending data to the server, but I've
  noticed that <code>serverInputStream</code> is not empty. So I need to empty this <code>serverInputStream</code> so that
  the client computer is able to send the server computer files again(That's why I can't manage a lock
  from <code>read</code> method)</p>