title: Removing all empty elements from an character array
body: <p>I have a character array that can hold maximum 50000 characters at a time. Contents of this array are coming through socket connections. However it is not guaranteed that this character buffer is not going to have any empty elements. Then I need to convert this character array into a String (e.g. new String(buffer);). My question is, whenever I receive a buffer from socket that is not 50000 long, how do you remove left over or empty elements from this char array or this String? If not, what is the most efficient way you would suggest?</p>
  
  <p>/<em>Here is the current implementation</em>/</p>
  
  <pre><code>private BufferedReader is;
  
  //Other code    
  
  public String readBufferUpdate() throws IOException {
      char[] buffer =new char[50000];
      is.read(buffer);
      return new String(buffer);
  }
  </code></pre>
  
  <p>////// This is an implementation using ByteArrayOutputStream</p>
  
  <pre><code>public String readAuth() throws IOException {
          int s;
          ByteArrayOutputStream bao = new ByteArrayOutputStream();
          while ((s = is.read()) != -1) {
              bao.write(s);
              if (!is.ready())
                  break;
          }
          if (s == -1) {
              return null;
          }
          String result = bao.toString();
          return result;
      }
  </code></pre>
  
  <p>This implementation is extremely slow when I need to read huge amount of data.</p>
  
  <p>-----------------edit</p>
  
  <p>(for example, server sends me data but it might not always be 50000 long bytes but 32822 long bytes then I need to remove left over array elements)</p>