Java 17 Recipes


STREAMS AND THE DECORATOR PATTERN


Download 3.2 Mb.
Pdf ko'rish
bet150/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   146   147   148   149   150   151   152   153   ...   245
Bog'liq
Java 17 Recipes

STREAMS AND THE DECORATOR PATTERN
I/O streams are the foundation of most Java I/O and include a plethora of ready-made streams 
for just about any occasion, but they are very confusing to use if some context is not provided. 
A stream (like a river) represents an inflow/outflow of data. Think about it this way. You create 
a stream of characters that the system receives (input stream) as you type. When the system 
produces sounds, it sends them to the speaker (output stream). The system could be receiving 
keystrokes and sending sound all day long, and thus the streams can be either processing 
data or waiting for more data.
When a stream doesn’t receive any data, it waits (nothing else to do, right?). As soon as data 
comes in, the stream starts processing this data. The stream then stops and waits for the 
next data item to come. This keeps going until this proverbial river becomes dry (the stream is 
closed).
Like a river, streams can be connected to each other (this is the decorator pattern). For the 
content of this chapter, there are mainly two input streams that you care about. One is the 
file input stream, and the other is the network socket input stream. These two streams are a 
source of data for your I/O programs. Their corresponding output streams are also file output 


294
stream and the network socket output streams (creative, isn’t it?). Like a plumber, you can 
hook them together and create something new. For example, you could weld together a file 
input stream to a network output stream to send the contents of the file through a network 
socket. Or you could do the opposite and connect a network input stream (data coming in) to 
a file output stream (data being written to disk). In I/O parlance, the input streams are called 
sources, while the output streams are called sinks.
There are other input and output streams that can be glued together. For example, there is 
a BufferedInputStream, which allows you to read the data in chunks (it’s more efficient 
than reading it byte by byte), and DataOutputStream allows you to write Java primitives 
to an output stream (instead of just writing bytes). One of the most useful streams is the 
ObjectInputStream and ObjectOutputStream pair, which allow you to serialize/
deserialize objects (there is a recipe for that in this chapter).
The decorator pattern allows you to keep plucking streams together to get many different 
effects. The beauty of this design is that you can create a stream that takes any input and 
produce any output and then can be thrown together with every other stream.

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   146   147   148   149   150   151   152   153   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling