title: Java: BufferedReader reads more than a line?
body: <p>I'm making a program in Java with Sockets. I can send commands to the client and from the client to the server. To read the commands I use a <code>BufferedReader</code>. To write them, a <code>PrintWriter</code> But now I want to transfer a file through <strong>that</strong> socket <em>(Not simply create a second connection)</em>.<br>First I write to the outputstream how many bytes the file contains. For example 40000 bytes. So I write the number <code>40000</code> through the socket, but the other side of the connection reads <code>78</code>.</p>
  
  <p>So I was thinking: The <code>BufferedReader</code> reads more than just the line <em>(by calling <code>readLine()</code>)</em> and on that way I lose some bytes from the file-data. Because they are in the buffer from the <code>BufferedReader</code>.<br>
  So the number <code>78</code> is a byte of the file I want to transmit.
  <br><br></p>
  
  <p>Is this way of thinking right, or not. If so, how to sovle this problem.<br>
  I hope I've explained well.</p>
  
  <hr>
  
  <p>Here is my code, but my default language is Dutch. So some variable-name can sound stange.</p>
  
  <pre><code>public void flushStreamToStream(InputStream is, OutputStream os, boolean closeIn, boolean closeOut) throws IOException {
      byte[] buffer = new byte[BUFFERSIZE];
      int bytesRead;
      if ((!closeOut) &amp;&amp; closeIn) { // To Socket from File
          action = "Upload";
          os.write(is.available()); // Here I write 400000
          max = is.available();
          System.out.println("Bytes to send: " + max);
          while ((bytesRead = is.read(buffer)) != -1) {
              startTiming(); // Two lines to compute the speed
              os.write(buffer, 0, bytesRead);
              stopTiming(); // Speed compution
              process += bytesRead;
          }
          os.flush();
          is.close();
          return;
      }
      if ((!closeIn) &amp;&amp; closeOut) { // To File from Socket
          action = "Download";
          int bytesToRead = -1;
          bytesToRead = is.read(); // Here he reads 78.
          System.out.println("Bytes to read: " + bytesToRead);
          max = bytesToRead;
          int nextBufferSize;
          while ((nextBufferSize = Math.min(BUFFERSIZE, bytesToRead)) &gt; 0) {
              startTiming();
              bytesRead = is.read(buffer, 0, nextBufferSize);
              bytesToRead -= bytesRead;
              process += nextBufferSize;
              os.write(buffer, 0, bytesRead);
              stopTiming();
          }
          os.flush();
          os.close();
          return;
      }
      throw new IllegalArgumentException("The only two boolean combinations are: closeOut == false &amp;&amp; closeIn == true AND closeOut == true &amp;&amp; closeIn == false");
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Here is the solution:</strong><br>
  <strong><em>Thanks to James suggestion</em></strong><br>
  <strong>I think <em>laginimaineb</em> anwser was a piece of the solution.</strong></p>
  
  <p>Read the commands.</p>
  
  <pre><code>DataInputStream in = new DataInputStream(is); // Originally a BufferedReader
  // Read the request line
  String str;
  while ((str = in.readLine()) != null) {
      if (str.trim().equals("")) {
         continue;
      }
      handleSocketInput(str);
  }
  </code></pre>
  
  <p>Now the flushStreamToStream:</p>
  
  <pre><code>public void flushStreamToStream(InputStream is, OutputStream os, boolean closeIn, boolean closeOut) throws IOException {
      byte[] buffer = new byte[BUFFERSIZE];
      int bytesRead;
      if ((!closeOut) &amp;&amp; closeIn) { // To Socket from File
          action = "Upload";
          DataOutputStream dos = new DataOutputStream(os);
          dos.writeInt(is.available());
  
          max = is.available();
          System.out.println("Bytes to send: " + max);
          while ((bytesRead = is.read(buffer)) != -1) {
              startTiming();
              dos.write(buffer, 0, bytesRead);
              stopTiming();
              process += bytesRead;
          }
          os.flush();
          is.close();
          return;
      }
      if ((!closeIn) &amp;&amp; closeOut) { // To File from Socket
          action = "Download";
          DataInputStream dis = new DataInputStream(is);
          int bytesToRead = dis.readInt();
          System.out.println("Bytes to read: " + bytesToRead);
          max = bytesToRead;
          int nextBufferSize;
          while ((nextBufferSize = Math.min(BUFFERSIZE, bytesToRead)) &gt; 0) {
              startTiming();
              bytesRead = is.read(buffer, 0, nextBufferSize);
              bytesToRead -= bytesRead;
              process += nextBufferSize;
              os.write(buffer, 0, bytesRead);
              stopTiming();
          }
          os.flush();
          os.close();
          return;
      }
      throw new IllegalArgumentException("The only two boolean combinations are: closeOut == false &amp;&amp; closeIn == true AND closeOut == true &amp;&amp; closeIn == false");
  }
  </code></pre>
  
  <p>Martijn.</p>