title: java DataInputStream read() stop and do not proceed
body: <ul>
  <li><p>I want sends and receive an object and file the order is</p>
  
  <ol>
  <li><p>client -> server   by ObjectOut(In)putStream </p></li>
  <li><p>client -> server String(file name) by DataIn(Out)putStrean writeUTF</p></li>
  <li><p>client -> server  by BufferedIn(Out)putStream</p></li>
  <li><p>server -> client  by ObjectOut(In)putStream </p></li>
  </ol></li>
  </ul>
  
  <p>but when the code reaches on 3 writing file takes forever (I think...it is waiting) the critical code is</p>
  
  <pre><code>byte[] data = new byte[BUFFER_SIZE];
  while ((length = bis.read(data)) != -1) {
      bos.write(data, 0, length);
      System.out.println(length);
  }
  </code></pre>
  
  <p><code>bis(BufferedInputStream).read()</code> do not proceed when I print the length the output is</p>
  
  <pre><code>4096
  4096
  879
  </code></pre>
  
  <p>then just wait...</p>
  
  <p>is there anyone who knows what is the problem or solution?</p>
  
  <p><strong>Server thread</strong></p>
  
  <pre><code>public void run() {
  
      System.out.println("\n New FileUploadServer thread started");
      msg = (Message) objComm.recvObject(clientSocket);
      System.out.println("server get message");
      if (checkState(msg.getState()))
          System.out.println(clientAddr + " session over");
  
      System.out.println("");
  }
  
  private boolean checkState(int _state) {
      switch (_state) {
          case ST_EXCEL_FILE:
              return receiveExcel();
          default:
              return false;
      }
  }
  
  
  private boolean receiveExcel() {
  
      Message sendMsg = receiveStr();
      System.out.println("receiveStr() success");
      BufferedInputStream bis;
      DataOutputStream dos;
      DataInputStream dis;
      FileOutputStream fos;
      BufferedOutputStream bos;
  
      VoteInfo sendVoteInfo = (VoteInfo) msg.getObj();
  
      try {
          dis = new DataInputStream(clientSocket.getInputStream());
          dos = new DataOutputStream(clientSocket.getOutputStream());
          // check file name extension is "xls" or "xlsx"
          String fName = dis.readUTF();
  
  
          int idx = fName.lastIndexOf(".");
          String extension = fName.substring(idx + 1, fName.length());
          if (!excelFileCheck(extension))
              return false;
  
          dos.writeUTF("read_ok");
          dos.flush();
          System.out.println("File name: " + fName);
          File f = new File(EXCEL_FILE_DIR + fName);
  
          fos = new FileOutputStream(f);
          bos = new BufferedOutputStream(fos);
          bis = new BufferedInputStream(clientSocket.getInputStream());
  
          int length;
          byte[] data = new byte[BUFFER_SIZE];
          while ((length = bis.read(data)) != -1) {
              bos.write(data, 0, length);
              System.out.println(length);
          }
          bos.flush();
          // send success message to web server
          System.out.println("kangji2");
  
          objComm.sendObject(sendMsg, clientSocket);
  
  
          System.out.println("File receive success!");
  
          if (!dataToDB.excelToDB(EXCEL_FILE_DIR + fName, extension)) {
              //delete all db related to this excel file here
              return false;
          } else {
  
  
          }
          bos.close();
          fos.close();
          dis.close();
  
          clientSocket.close();
  
          // send candidates information to DB server
          return makeResult(sendVoteInfo);
  
      } catch (IOException e) {
          System.out.println("ReceiveExcel error");
          e.printStackTrace();
      }
      return false;
  }
  
  
  public class ObjectComm {
  
      private Message receiveMsg = null;
      private ObjectOutputStream out = null;
      private ObjectInputStream in = null;
  
      public Message commObject(Message _sendMsg, Socket _clntSocket) {
  
          if (!_clntSocket.isConnected()) {
              System.out.println("clnt Socket not connected");
              return null;
          }
  
          try {
              out = new ObjectOutputStream(_clntSocket.getOutputStream());
              out.writeObject(_sendMsg);
              out.flush();
  
              System.out.println("kangji1");
              in = new ObjectInputStream(_clntSocket.getInputStream());
              receiveMsg = (Message) in.readObject();
              System.out.println("kangji2");
              return receiveMsg;
          } catch (Exception e) {
              System.out.println("commObject err");
              e.printStackTrace();
              return null;
          }
  
      }
  
      public boolean sendObject(Message _msg, Socket _clntSocket) {
          if (!_clntSocket.isConnected()) {
              System.out.println("clnt Socket not connected");
              return false;
          }
  
          try {
              out = new ObjectOutputStream(_clntSocket.getOutputStream());
              out.writeObject(_msg);
              out.flush();
  
              return true;
          } catch (IOException e) {
              System.out.println("Object comm send err");
              e.printStackTrace();
              return false;
          }
      }
  
      public Message recvObject(Socket _clntSocket) {
          if (!_clntSocket.isConnected()) {
              System.out.println("clnt Socket not connected");
              return null;
          }
  
          try {
              in = new ObjectInputStream(_clntSocket.getInputStream());
              receiveMsg = (Message) in.readObject();
              return receiveMsg;
  
          } catch (Exception e) {
              System.out.println("Object comm recvObject err");
              e.printStackTrace();
              return null;
          }
      }
  
  }
  </code></pre>