Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ByteArrayOutputStream.flush() necessary ?  RSS feed

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds redundant to flush a ByteArrayOutputStream since there is no underlying stream to flush to. Is it safe to ignore this ? The API says close() is a noop. But it does not mention if flush() is a noop as well.

Pho
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can always look at the source code in src.zip. Yes, flush() is a no-op as well, and you could safely ignore both flush() and close(). If you are certain that you will only ever want to use a ByteArrayOutputStream here. However, in most cases I would recommend you go ahead and do a close(), which also implicitly does a flush(), the same way you would for any other stream. That makes it easier (and safer) if you later need to replace the ByteArrayOutputStream with a FileOutputStream or other stream. It's easier to just always close streams when you're done with them, rather than figuring out which particular classes do not require that treatment.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!