Get the total number of bytes loaded into BufferReader before you finish reading from it.

I am reading a large XML file using HttpURLConnection in java, as shown below.

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 a way to get the total number of bytes loaded into the BufferedReader before you finish reading char with char / line by line / char block with char.

+4
source share
2 answers

It looks like you are trying to find out the size of the BufferedReader without consuming it.

You can try using the HttpURLConnection getContentLength() method. This may or may not work. What he certainly would not have done is give you the uncompressed stream size. If this is the last thing you need, you are almost out of luck.

If I misunderstood your question, please specify what exactly you are after.

+2
source

If the content length header is configured, you can access it through the connection. But if the content was compressed, it may not be installed, or it may give a compressed size, where I assume you're looking for an uncompressed size.

0
source

Source: https://habr.com/ru/post/1392916/


All Articles