"Hello, Amigo! Today, Ellie told you about the adapter pattern."

Most classes related to I/O streams are implemented as adapters. They either convert equivalent interfaces or they connect them, starting from the simple and moving up to the complex.

"Are InputStreamReader and BufferedReader also adapters? At they very least, they are very similar to adapters in the way they are used: After an object is created, it is passed to the constructor of another class."

"Yes, InputStreamReader converts the InputStream interface to the Reader interface. BufferedReader is not an adapter in its purest form, because Java's creators decided not to give its methods their own separate interface. But it is kindred spirit."

Instead of writing a bazillion different classes, Java's creators wrote two dozen adapters and allowed them to connect to each other however a programmer might want.

This approach is very convenient. A programmer can always write her class and/or adapter, have it implement a standard interface, and include it in the chain of adapter objects she is building.

"So that's how it all works. Instead of big complex classes, we make chains of simple objects and adapters. And then you just create them and combine them in the right order!"

"And you implement whatever is missing."

"Yes, I get it."

"But actually I wanted to tell you about Reader and Writer today. These are two abstract classes that are very similar to the InputStream and OutputStream classes. But unlike those classes, these two classes work with characters. They read and write characters. They are very convenient when working with textual information. Let's take a look at the methods they have:"

Reader methods What the method does
int read(char[] cbuf);
"This method immediately reads several characters into the buffer (char array), until the buffer is full or until the source doesn't have any more characters to read."
The method returns the number of characters actually read (which can be less than the length of the array)
int read();
"This method reads one character and returns it. The result is widened to an int for looks. If there are no available characters, then the method returns -1."
boolean ready();
This method returns true if there are any unread characters for the read methods
void close();
This method «closes» the stream. You call this when you're done working with the stream.
The object then performs the housekeeping operations needed to close the file, etc.
At this point, you can't read any more data from the stream.

"It turns out that Reader's read(char [] cbuf) method lets us read whole blocks of characters, rather than one character at a time. So it's faster and more convenient."

"Exactly. And now let's see what methods Writer has:"

Method What the method does
void write(int c);
This method writes one character. The int type is narrowed to a char. The extra part is simply discarded.
void write(char[] cbuff);
This method writes an array of characters.
void write(String s);
This method writes a string. It is simply converted into an array of characters and then the second method is called.
void flush();
If the stream is internally storing any data that has not yet been written, this method forces it to be written.
void close();
This method «closes» the stream. You call this when you're done working with the stream.
The object then performs the housekeeping operations needed to close the file, etc. You can't write data to the stream any longer, and flush is called automatically.

It's important to understand that Reader and Writer are abstract classes. They don't do anything and contain virtually no code. All their methods will need to be implemented in the classes that inherit them. Their job is to standardize how classes interact. Developers don't need to invent their own standards to interact with each other. It is much more convenient for everyone to maintain a few basic standards. This allows classes written by different programmers to easily interact not only with classes written by Java's creators, but also with classes written by other programmers.

Standards are powerful.

"I agree. Supporting common standards is beneficial to everyone."