Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to calc bytes written into OutputStream  RSS feed

 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a method which writes data into OutputStream. While I do not want to modify method, I need to get the size of data (in bytes) written. OutputStream is given to me by another class and I am not aware of exact implementation given. So situation is: I get OutputStream and pass it to method mentioned above, but I do not have control over what is written to that stream.

I think to use piped streams to maybe read the same stream that was written just to calc the size.

Maybe you would know more efficient method?
Any suggestions?

Thanks!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can "wrap" or "decorate" the output stream with a class that counts bytes. Now you're going to pass this stream to some other methods and they expect OutputStream so you're stuck with extending OutputStream. As many times as we discourage extending concrete classes, sometimes it's the only way.

So your class would look something like:

You have to override all the methods, but most of them will only pass the message (method call) on to the real stream. You could make somebody get the count with the getCount() call I showed, or maybe log the count at close time or whatever you like.

Oh! Wait! FilterOutputStream already does this. You can extend it and override only the methods you care about. If you have the JDK source, look at how CheckedOutputStream did it.

Zat help? Let us know what you wind up with!
[ December 22, 2004: Message edited by: Stan James ]
 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like a right solution! Many thanks! I will try it asap when at work, and report my results here.

As many times as we discourage extending concrete classes


Seems I am missing something big here... Is it possible to get URL to some article or info on the subject. I am not using inheritance that much (at class level) but still sometimes it is very convenient. What are alternatives to subclassing concrete class?

Thank you!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was reluctant to even mention that. There are some risks to extending a concrete class that you don't own unless you trust that concrete class to be VERY stable. Alan Holub wrote an infamous article Why Extends is Evil. Many folks think he's exaggerating to sell articles, but there are some tasty nuggets of goodness in there and a solid example of how changes to a superclass can break an extended class. Oh, and a way around it. In practice all this boils down to "prefer composition over inheritance" without using words like "never" or "evil". Know what could go bad, and use your head. Who could argue with that? Well, scroll down to the UML and OO forum and you'll find somebody.
 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In general solution worked But it turned out that I have to get the size of data written prior to writting actually occurs. I am trying to POST data over HTTP and I can't set content length after I finished writting.
I think I will write data to some virtual file or memory block and only then POST it.

Hope you had a great Christmas!
Vladas

P.S. Still have to read that article about "extends" inheritance.
 
Derc Zbrojvic
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is such an old question Just adding a simple example of a servlet GET method handler (Java8), for reference.
 
Campbell Ritchie
Sheriff
Posts: 53750
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Experience shows that even very old threads can be useful, so thank you for the example. What do the colons (“:”) mean?
 
Derc Zbrojvic
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What do the colons (“:”) mean?

Just placeholders for other lines of code not pertinent to the question (vertical version of the ellipsis "..." ). You'd have statements there to e.g. read data from the DB, construct a BufferedImage, etc.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!