title: Java: Getting multiple lines from socket
body: <p>I have a Java application that consists of a client and a server. The client sends encrypted commands to the server, and the server executes them.</p>
  
  <p>The problem that I am having right now is that, with my encryption algorithm, sometimes the encrypted command contains "\n" or "\r" characters, which mess up my server code. This is because I am using the readLine() method, which stops when it finds a line terminator. What I need is a way to read all the characters the client sends into one string.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public void run(){            
          System.out.println("Accepted Client!");
  
          try{                                
              in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(), "ISO8859_1"));
              out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream(), "ISO8859_1"));
  
              String clientCommand = null;
  
              while(RunThread){                    
                  // read incoming stream
                  do{
                      clientCommand = in.readLine();
                  }while(clientCommand == null);
  
                  //decrypt the data
                  System.out.println("Client: " + clientCommand);
  
                  if(clientCommand.equalsIgnoreCase("quit")){
                      RunThread = false;
                  }else{
                      //do something
                      out.flush();
                  }
              }
          }catch(Exception e){
              e.printStackTrace();
          }
    }
  </code></pre>
  
  <p>Everything I've tried (various forms of nested loops using the read() function) hasn't worked. I would welcome any help or suggestions. Thanks, everyone!</p>