Netty layer is the encapsulation of Java NIO, before the use of Netty have a general impression, next review Java IO, NIO, for the back of the netty underlying principles lay a little foundation.

The classification of the flow

Node stream: A stream class that is read and written from a specific place, such as a disk or a block of memory. Filter streams: Use node streams as input and output. A filter stream is created using an existing input stream or output stream connection.

IO package InputStream, OutputStream class hierarchy:

I/O flow connection

Decorator pattern

We use streams as follows:

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("text.txt")));
Copy the code

Underlying this usage is the decorative pattern.

Dr. Yan Hong’s “JAVA and Patterns” book on decorative patterns made a good explanation, here to move:

Decorator pattern class diagram:

Code:

Public interface Component {public void sampleOperation(); } Specific component roles: Public class ConcreteComponent implements Component {@override public void sampleOperation() {}  public class Decorator implements Component{ private Component component; public Decorator(Component component){ this.component = component; } @ Override public void sampleOperation () {/ / delegated to component. The component sampleOperation (); }}  public class ConcreteDecoratorA extends Decorator { public ConcreteDecoratorA(Component component) { super(component); } @override public void sampleOperation() {super.sampleOperation(); // Write the relevant business code}} specific decoration role B:  public class ConcreteDecoratorB extends Decorator { public ConcreteDecoratorB(Component component) { super(component); } @override public void sampleOperation() {super.sampleOperation(); // Write relevant business code}}Copy the code