Two Laptop Bag*
The moose likes Java in General and the fly likes Size of an InputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Size of an InputStream" Watch "Size of an InputStream" New topic
Author

Size of an InputStream

Andr� Latour
Greenhorn

Joined: May 22, 2006
Posts: 15
Good morning everyone,

one (simple) question, regarding these lines of code:

this.in = this.urlConHttp.getInputStream( );
ObjectInputStream oin = new ObjectInputStream( in );
Collection result = ( Collection ) oin.readObject( );

Now, i want to know how "big" the InputStream is, that was transmitted. Is there a simple solution?

Thanks in advance
Andr�
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8834
    
    7

You don't tell us what this.urlConHttp is, so we'll have to speculate. If you have a URLConnection instance, you could try using getHeader() to read the Content-Length header field.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Andr� Latour
Greenhorn

Joined: May 22, 2006
Posts: 15
Hello Joe,

excuse me that was my fault, i thought that the name was self explaining.
You are right, this is a HttpURLConnection. Unfortunately i resolved the following Map after using .getHeaderFields()


{Connection=[Keep-Alive], Content-Language=[en-US], Date=[Fri, 25 Aug 2006 13:39:14 GMT], Keep-Alive=[timeout=15, max=100], Server=[IBM_HTTP_SERVER/1.3.28.1 Apache/1.3.28 (Unix)], Content-Type=[text/html; charset=ISO-8859-1], Transfer-Encoding=[chunked], null=[HTTP/1.1 200 OK]}

I cannot find the "Content-Length" field. :-(

Thank you very much
Andr�
Andr� Latour
Greenhorn

Joined: May 22, 2006
Posts: 15
addendum:

when i use .getContentLength i only receive -1, although i transfer over 20000 Objects within a Collection via Http.... ???

Andr�
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8834
    
    7

Content-Length is set by the server. It is not required. If you are using a servlet then you would have to set the content length.
Why do you want to know the length of an input stream when you are transferring Objects? I like to think we use Objects so we don't have to think about bytes.
Andr� Latour
Greenhorn

Joined: May 22, 2006
Posts: 15
Hello Joe,

i would like to know how much "traffic" is on the net depending on the size of transfered objects.

Thankyou for your answers
Andr�
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8834
    
    7

If you just want a ballpark figure, serialize a known number of objects to a file and read the size. Keep in mind that there will be overhead for serialization housekeeping information and, if you serialize one, the collection itself. The size of the objects themselves may be variable, especially if they contain wildly variable Strings or collections.
If you want an exact figure, you could try wrapping the ServletOutputStream you hook the ObjectOutputStream to and measure the bytes being written.
You could also observe the network traffic with a sniffer, but I have no experience with that.
Dana Bothner-By
Ranch Hand

Joined: Jul 28, 2006
Posts: 37
A general idea: if you want to see how may bytes are read from an input stream, define the obvious decorator and include it in your pipeline:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Size of an InputStream
 
Similar Threads
Creating an ObjectInputStream from a Blob!
InputStream
ProgressMonitorInputStream and ObjectInputStream
StreamCorruptedException
object stream