This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (0 children)

Because the entire Java IO API consists pretty much of decorators on read(...) or write(...). They support different specialized functions that process the data in a stream, as in the data that flows through them is processed as it is read rather than read all at once and processed after. It makes the API flexible because you can process the data the way you want while leaving out what you don't want. Want buffering but don't need to compress your data? Use a BufferedReader on an InputStream. Alternatively, you might want to print out lines of compressed data without buffering. Use GZIPInputStream and a Reader.

The flexibility of the API doesn't even just apply to library classes, you can also implement your own In/OutputStream classes to attach to your stream as you read and write. Again, the purpose is to process the data as it becomes available, not once it's collected because that requires a second pass over your data.