Removing all empty elements from an character array

后端 未结 5 2014
面向向阳花
面向向阳花 2021-01-15 11:07

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 th

5条回答
  •  北荒
    北荒 (楼主)
    2021-01-15 11:58

    Correct way to read from an InputStream in a buffer is as follows.

    public String readBufferUpdate() throws IOException {
        char[] buffer =new char[50000];
        int bytesRead = is.read(buffer, 0, buffer.length);
        return new String(buffer, 0, bytesRead);
    }
    

    Here you is.read() returns how many bytes have been read into bytesRead. Only that many bytes are copied from the buffer to String.

提交回复
热议问题