Determine size of HTTP Response?
Asked Answered
S

5

9

Is there a way to determine the size of the HTTPServletResponse content? I read this get-size-of-http-response-in-java question but sadly where I work I do not have access to CommonsIO :(

The response content consists of a single complex object so I have considered writing it out to a temp file and then checking that file. This is not something I want to be doing as a diagnostic while the application is running in production though so want to avoid it if at all possible.

PS I read erickson's answer but it mentioned input streams I want to know the size of the object being written out... Would be really nice if the writeObject() method returned a number representing bytes written instead of void...

Skylark answered 25/3, 2011 at 16:35 Comment(0)
S
0

I eventually found a way to get what I wanted:

URLConnection con = servletURL.openConnection();
BufferedInputStream bif = new BufferedInputStream(con.getInputStream());
ObjectInputStream input = new ObjectInputStream(bif);
int avail = bif.available();

System.out.println("Response content size = " + avail);

This allowed me to see the response size on the client. I still would like to know what it is on the server side before it is sent but this was the next best thing.

Skylark answered 28/3, 2011 at 19:26 Comment(1)
Odd that I get down voted to my own response to my own question for an answer that does exactly what I want...Skylark
B
15

If you have access to the response header, you can read the Content-Length.

Here is a example of a response header:

(Status-Line):HTTP/1.1 200 OK
Connection:Keep-Alive
Date:Fri, 25 Mar 2011 16:26:56 GMT
Content-Length:728

Check this out: Header Field Definitions

Brick answered 25/3, 2011 at 16:51 Comment(3)
It appears the content length of the response does not have to be set. In my client code I tried URLConnection.getContentLength() and I keep getting -1. So what must I do on the server side to ensure the content length of the response is calculated? But if I could do that on the server side my problem would be solved :)Skylark
You can include it in the response header in your server just before send the response to the client.Brick
Note: This is not present if Transfer-Encoding: chunked.Tomasatomasina
S
0

This seems to be what you're looking for:

DataOutputStream dos = new DataOutputStream(response.getOutputStream());
...
int len = dos.size();
Seafowl answered 25/3, 2011 at 16:44 Comment(3)
I think this is your best option because I assume you want to know the size of the outputstream being sent not being received.Loose
But there is no writeObject() method on the DataOutputStream - I would have to make very significant modifications to the application to reduce everything to primitives.Skylark
I get "java.lang.IllegalStateException: getWriter() has already been called for this response"Wagonlit
S
0

I eventually found a way to get what I wanted:

URLConnection con = servletURL.openConnection();
BufferedInputStream bif = new BufferedInputStream(con.getInputStream());
ObjectInputStream input = new ObjectInputStream(bif);
int avail = bif.available();

System.out.println("Response content size = " + avail);

This allowed me to see the response size on the client. I still would like to know what it is on the server side before it is sent but this was the next best thing.

Skylark answered 28/3, 2011 at 19:26 Comment(1)
Odd that I get down voted to my own response to my own question for an answer that does exactly what I want...Skylark
S
0

Assuming the use of ObjectOutputStream, build it around a java.io.ByteArrayOutputStream:

ByteArrayOutputStream contentBytes = new ByteArrayOutputStream();

ObjectOutputStream objectOut = new ObjectOutputStream(contentBytes);

objectOut.writeObject(content);

int contentLength = contentBytes.size();

And then you can send the content with

contentBytes.writeTo(connection.getOutputStream());

where connection is whatever you're getting your OutputStream from.

Better late than never, right?

Swart answered 13/2, 2013 at 4:21 Comment(0)
C
0

if you use Catalina, this worked for me:

import org.apache.catalina.connector.ResponseFacade
...
long len=((ResponseFacade) response).getContentWritten();
Covering answered 27/9, 2023 at 11:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.