---
title: "MINA SOCKET SERVER学习笔记(一)Writing IoFilter"
author: "luckybat"
date: "2008-12-01T12:08:40Z"
tags: ["Mina", "Servlet", "Socket", "编程", "网络应用"]
slug: "mina-socket-server-writing-iofilter"
draft: false
lastmod: "2008-12-01T17:08:43Z"
id: 283825
url: "https://luckybat.iteye.com/blog/283825"
---

[IoFilter](http://mina.apache.org/iofilter.html)

IoFilter is one of the MINA core constructs that serves a very important role. It filters all I/O events and requests between IoService and IoHandler. If you have an experience with web application programming, you can safely think that it's a cousin of Servlet filter. Many out-of-the-box filters are provided to accelerate network application development pace by simplifying typical cross-cutting concerns using the out-of-the-box filters such as:  
IoFilter是MINA核心构造之一，扮演非常重要的角色。它过滤所有的I/O事件和请求，这些事件和请求由IoService最终到达IoHandler。假如你有web应用编程的经验，你确实可以把它比作Servlet filter。许多具有独特创意的filters被开发出来以加速网络应用的开发速度，简单、典型、横切关注点地使用这些filters，例如:

- LoggingFilter logs all events and requests.  
  LoggingFilter日志记录所有的事件和请求。
- ProtocolCodecFilter converts an incoming ByteBuffer into message POJO and vice versa.  
  ProtocolCodecFilter将接收的字节流转换为消息POJO等等。
- CompressionFilter compresses all data.  
  CompressionFilter压缩所有的数据。
- SSLFilter adds SSL - TLS - StartTLS support.  
  SSLFilter加入SSL-TLS-StartTLS支持。
- and many more!  
  还有很多！

It's easy to implement an `IoFilter` in general, but you might also need to know specifics of MINA internals. Any related internal properties will be explained here.

通常，实现一个IoFilter还是很容易的，但是你可能需要了解MINA的内部细节，以下介绍所有相关的内部内容。

 

## Overriding Events Selectively

You can extend `IoAdapter` instead of implementing `IoFilter` directly. Unless overriden, any received events will be forward to the next filter immediately:  
除了直接实现IoFilter，你也可以扩展IoAdapter。除了重载了的事件，任何接收事件都将立即转发到下一个filter。

## Transforming a Write Request

If you are going to transform an incoming write request via `IoSession.write()`, things can get pretty tricky. For example, let's assume your filter transforms `HighLevelMessage` to `LowLevelMessage` when `IoSession.write()` is invoked with a `HighLevelMessage` object. You could insert appropriate transformation code to your filter's `filterWrite()` method and think that's all. However, you have to note that you also need to take care of `messageSent` event because an `IoHandler` or any filters next to yours will expect `messageSent()` method is called with `HighLevelMessage` as a parameter, because it's irrational for the caller to get notified that `LowLevelMessage` is sent when the caller actually wrote `HighLevelMessage`. Consequently, you have to implement both `filterWrite()` and `messageSent()` if your filter performs transformation.

 

Please also note that you still need to implement similar mechanism even if the types of the input object and the output object are identical (e.g. `CompressionFilter`) because the caller of `IoSession.write()` will expect exactly what he wrote in his or her `messageSent()` handler method.