mimo
mimo

Reputation: 6827

Java StringBuilder doesn't append whole InputStream

I have a weird error in my code. I'm trying to read some characters from InputStreamReader using char buffer and appending it to StringBuilder.

The snippet is here:

char[] buffer = new char[4096];
int numRead = 0;
webResource = new StringBuilder("");
while ( (numRead = reader.read(buffer, 0, 4096)) != -1 )
            {
                System.out.println("Capacity before: "+webResource.capacity());
                webResource.append(buffer, 0, numRead);
                System.out.println("Capacity after: "+webResource.capacity());
            }

The System.out.println() is only for debug purpose.

I'm trying to read InputStream that have 147 781 chars. First two times in while cycle is everything ok, I read characters into buffer and then it is appended to webResource (StringBuilder).

But at the third iteration I read to buffer 4096 characters but only 1446 characters are appended to webResource (StringBuilder).

I've been debugging the code in Eclipse and while watching variable webResource I find out that, after third iteration (after appendding 1446 characters) the content of variable simply ends with "..." and no new characters are added in next iterations.

Weird thing also is, that webResource.count is incementing properly (after four interations its value is 16 384, and so on).

Pleas can anyone help? I really do not see where is the problem of why I can't append whole inputstream to StringBuilder, I can only append the first 9 638 characters.

Thanks in advance.

Upvotes: 1

Views: 518

Answers (1)

antlersoft
antlersoft

Reputation: 14751

You seem to be misinterpreting what you see in the debugger. The debugger is not showing you the whole content of the stringbuilder because it's too long. but it's all there.,

Upvotes: 2

Related Questions