namalfernandolk
namalfernandolk

Reputation: 9134

Get the total number of bytes loaded to the BufferReader before finish reading from it

I'm reading a large xml file using HttpURLConnection in java as follows.

StringBuilder responseBuilder = new StringBuilder(1024);
char[] buffer = new char[4096];

BufferedReader  br = new BufferedReader(new InputStreamReader(((InputStream)new DataInputStream(new GZIPInputStream(connection.getInputStream()))),"UTF-8"));

int n = 0;
while(n>=0){
    n=br.read(buffer,0,buffer.length);
    if(n>0) responseBuilder.append(buffer,0,n);

}

Is there any way to get the total number of bytes loaded to the BufferedReader before finish reading it char by char / line by line / char block by char block.

Upvotes: 4

Views: 283

Answers (2)

DaveJohnston
DaveJohnston

Reputation: 10161

If the content-length header has been set then you can access that through the connection. But if the content has been compressed then it might not be set, or might give the compressed size, where I assume you are looking for the uncompressed size.

Upvotes: 0

NPE
NPE

Reputation: 500893

It sounds like you're trying to find out the size of the BufferedReader without consuming it.

You could try using the HttpURLConnection's getContentLength() method. This may or may not work. What it certainly wouldn't do is give you the uncompressed size of the stream. If it's the latter that you're after, you're almost certainly out of luck.

If I have misunderstood your question, please clarify what it is exactly that you're after.

Upvotes: 2

Related Questions