• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

A few questions about Java Streams

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. I wanna ask to you some questions about Java streams.

- Is it bad idea to read data from same InputStream using multiple wrapper Streams (BufferedInputStream, DataInputStream, and another subclass of FilteredInputStream) ? If it is, why ?

- Is it bad idea to call reset, mark methods from base InputStream that passed to wrapper InputStream (BufferedInputStream, ...) ? I think, we should call these methods from only wrapper InputStream.

Thanks.
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Leonardo Nash wrote:
- Is it bad idea to read data from same InputStream using multiple wrapper Streams (BufferedInputStream, DataInputStream, and another subclass of FilteredInputStream) ? If it is, why ?


If you mean chain wrappers together (ie wrap a FileInputStream in a BufferedInputStream which is wrapped in a DataInputStream) then this is fine but if you mean to wrap one InputStream in 2 or more different wrappers then this is not a good idea. The reason is if you read from one wrapper it will consume and return values from the underlying stream then when you read from another wrapper it will consume and return the next available values from the underlying stream and so the second read will be missing the values already read in via the first stream. Unless you are extremely careful you will have no idea what the data being read in actually means as it will be be starting from some unknown point in the stream.

Leonardo Nash wrote:
- Is it bad idea to call reset, mark methods from base InputStream that passed to wrapper InputStream (BufferedInputStream, ...) ? I think, we should call these methods from only wrapper InputStream.


The reset, mark methods from InputStream don't do anything (they are null implementations) so there is no point in calling them. As to the general principal of calling methods in base classes of wrapped classes I would say it is a dangerous policy if the methods change the state of the object. For example if you called an InputStream subclass that implemented reset() and then called the wrapping classes reset() method, if the wrapping class provides it's own implementation of reset and mark, then the result is unlikely to be resetting of the file position back to the marked point.
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although mark is a no-op in InputStream, reset throws an exception if the method is not overridden:

Conclusion: use markSupported to check if you can use mark and reset.
 
Leonardo Nash
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Tony Docherty

Thanks.
 
Bartender
Posts: 15743
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reading from and writing to one stream using different wrapper streams is fine under these conditions:

1) Neither wrapper stream may buffer input or output.
2) You must not (indirectly) call close() on the wrappers. For instance, don't declare them in a try-with-resources header.
3) You must not (indirectly) pass them to methods that you don't control to prevent points 1 and 2.

This is roughly what it looks like:
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic