Why is PrintStream extending FilterOutputStream and not OutputStream?

System.out and System.err are PrintStream s; and FilterOutputStream continues to FilterOutputStream .

From FilterOutputStream javadoc:

This class is the superclass of all classes that filter output streams. These streams are located on top of an existing output stream (the main output stream), which it uses as the main data stream, but may convert the data along the way or provide additional functionality.

The FilterOutputStream class itself simply overrides all OutputStream methods with versions that pass all requests to the underlying output stream . Subclasses of FilterOutputStream can optionally override some of these methods, as well as provide additional methods and fields.

(my emphasis)

FilterOutputStream itself extends OutputStream .

I'm here in difficulty. Is there a reason FilterOutputStream needs to extend FilterOutputStream instead of OutputStream ?

Sample code appreciated ...

+4
source share
3 answers

Any higher-level stream that should filter the input or output of a nested lower-level stream must first extend the FilteredOutputStream, and the same is true for InputStreams. Therefore, it makes sense that everyone for a higher-level OutputStreams extends the FilteredOutputStream class, in other words, all streams that allow and actually require the embedding of other streams to work should extend this class.

I do not know what the class does internally, but I believe that it somehow massages the data so that higher-level streams can be understood from them. To learn more, I assume that you will have to delve into the source code.

0
source

FilterOutputStream applies a composition template where it delegates all calls to its out instance variable:

 /* The underlying output stream to be filtered. */ protected OutputStream out; 

FilterOutputStream also has standard implementations for the abstract class OutputStream :

 public void write(int b) throws IOException { out.write(b); } public void write(byte b[]) throws IOException { write(b, 0, b.length); } public void write(byte b[], int off, int len) throws IOException { if ((off | len | (b.length - (len + off)) | (off + len)) < 0) throw new IndexOutOfBoundsException(); for (int i = 0 ; i < len ; i++) { write(b[off + i]); } } public void flush() throws IOException { out.flush(); } public void close() throws IOException { try { flush(); } catch (IOException ignored) { } out.close(); } 

Now, any class that includes FilterOutputStream can extend FilterOutputStream and override the appropriate methods. Note that they still need to delegate their calls to out . For example PrintStream#flush() :

 public void flush() { synchronized (this) { try { ensureOpen(); out.flush(); } catch (IOException x) { trouble = true; } } } 
+2
source

PrintStream must implement various filters, process character encodings, and ultimately print non-character data as characters.

The general FilterOutputStream contract is best suited for this, so this is the class used.

+1
source

Source: https://habr.com/ru/post/1492501/


All Articles