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...

Community
  • 1
  • 1
BigMac66
  • 1,528
  • 5
  • 19
  • 35

4 Answers4

14

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

Rafael Colucci
  • 6,018
  • 4
  • 52
  • 121
  • 1
    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 :) – BigMac66 Mar 25 '11 at 18:16
  • You can include it in the response header in your server just before send the response to the client. – Rafael Colucci Mar 25 '11 at 20:28
  • 9
    Note: This is not present if `Transfer-Encoding: chunked`. – Andy Hayden May 15 '17 at 06:38
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?

gdejohn
  • 7,451
  • 1
  • 33
  • 49
0

This seems to be what you're looking for:

DataOutputStream dos = new DataOutputStream(response.getOutputStream());
...
int len = dos.size();
David Weiser
  • 5,190
  • 4
  • 28
  • 35
  • I think this is your best option because I assume you want to know the size of the outputstream being sent not being received. – Amir Raminfar Mar 25 '11 at 16:59
  • 1
    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. – BigMac66 Mar 25 '11 at 17:51
  • I get "java.lang.IllegalStateException: getWriter() has already been called for this response" – user3217883 Jun 12 '19 at 03:43
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.

BigMac66
  • 1,528
  • 5
  • 19
  • 35
  • Odd that I get down voted to my own response to my own question for an answer that does exactly what I want... – BigMac66 Aug 20 '19 at 12:55