`
收藏列表
标题 标签 来源
mina的功能 mina

MINA是一个简单、但功能丰富的网络程序框架。 提供:

 

    对各类传输协议提供统一的API接口
    TCP/IP 、UDP/IP, 采用NIO
    串行通信(RS232),采用RXTX
    IN-VM pipe 通讯
    你可以实现你自己的
    过滤器作为扩展点,类似于Servlet过滤器
    低级别和高级别的API
    低级别: 采用 ByteBuffers
    高级别: 使用用户定义信息对象和编解码
    高度客户化线程模式
    单线程
    一个线程池
    多余一个线程池(如:SEDA)
    Out-of-the-box SSL、TLS、StartTLS支持,采用 Java5 SSLEngine
    负载保护和交通截留(Overload shielding & traffic throttling)
    单元可测试,采用mock对象
    JMX管理能力
    基于Stream I/O支持,采用StreamIoHandler
    和已知的容器可以很好的集成,如:PicoContainer, Spring
    从Netty平滑的迁移。(Netty:MINA的前身,祖先)

MINA使用SSL的例子 mina MINA使用SSL的例子
如题

http://grepcode.com/file/repo1.maven.org/maven2/org.apache.mina/mina-example/2.0.0-M1/org/apache/mina/example/echoserver/Main.java?av=f
How to disable SslFilter logging in apache MINA? mina How to disable SslFilter logging in apache MINA?
How to disable SslFilter logging in apache MINA?
	

I'am developing application based on apache MINA. For secure data transmission I use SSL. And there appears some annoying thing: when I receive incomming message, SslFilter class appends about 10kbytes to the log-file. How can I prevent this?

----------------
I finally found the solution for this problem. Here is the log4j.xml source:
<?xml version='1.0' encoding='ISO-8859-1' ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
  <appender name="CONSOLE-DEBUG" class="org.apache.log4j.ConsoleAppender">
    <param name="target" value="System.out"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d{ISO8601} [%5p] %m at %l%n"/>
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
      <param name="LevelMin" value="ALL"/>
      <param name="LevelMax" value="INFO"/>
    </filter>
  </appender>
  <appender name="CONSOLE-WARN" class="org.apache.log4j.ConsoleAppender">
    <param name="target" value="System.err"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d{ISO8601} [%5p] %m at %l%n"/>
    </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
      <param name="LevelMin" value="WARN"/>
    </filter>
  </appender>
  <appender name="LOG-FILE-APPENDER" class="org.apache.log4j.FileAppender">
    <param name="file" value="app.log"/>
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d{ISO8601} [%5p] %c %m at %l%n"/>
    </layout>
  </appender>
  <category name="org.apache.mina.filter.ssl.SslHandler" additivity="false">
    <priority value="ERROR"/>
    <appender-ref ref="LOG-FILE-APPENDER"/>
  </category>
  <category name="org.apache.mina.filter.ssl.SslFilter" additivity="false">
    <priority value="ERROR"/>
    <appender-ref ref="LOG-FILE-APPENDER"/>
  </category>
  <root>
    <priority value="DEBUG"/>
    <appender-ref ref="CONSOLE-WARN"/>
    <appender-ref ref="CONSOLE-DEBUG"/>
    <appender-ref ref="LOG-FILE-APPENDER"/>
  </root>
</log4j:configuration>
apache MINA 2 配置 SSL 安全连接 mina Apache Mina – SSL Configuration
	
                                          Apache Mina – SSL Configuration

Introduction
    Quite some time back, I had wrote an article to create a simple client/server application using Apache Mina 2.0.x. In that article the transaction between the client and server is unsecured.  In order to make a secured transaction between the client and the server, SSL should be configured. In this article, Let us see how to configure Secured Socket Layer(SSL) for a sample Client/Server application using 3 easy steps,

   1. Generate SSLContext
   2. Server part
   3. Client part

Step 1 – Generate SSLContext

    SSLContext is a factory for secure socket or SSLEngine. For the sample application, A class named “SSLGenerator” is used to generate the SSLContext. To make a secured transaction, Two types of key files are needed they are “Keystore” and “Truststore” file. The Creation of these two files has been explained in the article “Step by step tutorial to create Keystore and Truststore file (http://www.techbrainwave.com/?p=953)“. The factory classes used in the SSLContextGenerator class is,

    KeyStoreFactory - This factory class is used to create and configures a new Keystore instance.
    SSLContextFactory - This factory class is used to create and configures a new SSLContext.

SSLContextGenerator.java
01	package com.sample.ssl;
02	 
03	import java.io.File;
04	import java.security.KeyStore;
05	import javax.net.ssl.SSLContext;
06	import org.apache.mina.filter.ssl.KeyStoreFactory;
07	import org.apache.mina.filter.ssl.SslContextFactory;
08	 
09	/**
10	* @author giftsam
11	*/
12	public class SSLContextGenerator {
13	
14	    public SSLContext getSslContext(){
15	
16	        SSLContext sslContext = null;
17	        try{
18	
19	             File keyStoreFile = new File("/home/giftsam/Desktop/certificates/keystore");
20	             File trustStoreFile = new File("/home/giftsam/Desktop/certificates/truststore");
21	 
22	             if (keyStoreFile.exists() && trustStoreFile.exists()){
23	
24	                 final KeyStoreFactory keyStoreFactory = new KeyStoreFactory();
25	                 System.out.println("Url is: " + keyStoreFile.getAbsolutePath());
26	                 keyStoreFactory.setDataFile(keyStoreFile);
27	                 keyStoreFactory.setPassword("techbrainwave");
28	 
29	                 final KeyStoreFactory trustStoreFactory = new KeyStoreFactory();
30	                 trustStoreFactory.setDataFile(trustStoreFile);
31	                 trustStoreFactory.setPassword("techbrainwave");
32	 
33	                 final SslContextFactory sslContextFactory = new SslContextFactory();
34	                 final KeyStore keyStore = keyStoreFactory.newInstance();
35	                 sslContextFactory.setKeyManagerFactoryKeyStore(keyStore);
36	 
37	                 final KeyStore trustStore = trustStoreFactory.newInstance();
38	                 sslContextFactory.setTrustManagerFactoryKeyStore(trustStore);
39	                 sslContextFactory.setKeyManagerFactoryKeyStorePassword("techbrainwave");
40	                 sslContext = sslContextFactory.newInstance();
41	                 System.out.println("SSL provider is: " + sslContext.getProvider());
42	              } else {
43	
45	                 System.out.println("Keystore or Truststore file does not exist");
46	              }
47	           } catch (Exception ex){
48		
50	             ex.printStackTrace();
51	           }
52	           return sslContext;
53	       }
54	}

Step 2 – Server part

     For the server part two classes named “SSLServer” and “SSLServerHandler” has been used.  In the SSLServer class,  “SSLFilter” class is used to encrypt and decrypt the data exchanged in the session, Also it triggers the SSLHandshake procedure immediately(If you don’t want the handshake procedure to start immediately, please specify false as autostart parameter in the constructor).

     Note: SSLFilter works only for the TCP/IP connections.

     An interface named “IoAcceptor” is used to accept the incoming connections from the client and that fires the event to the handler. Two filters has been used, the first one is the “LoggingFilter” which logs all the events and requests and the second one is the “ProtocolCodecFilter” which is used to convert an incoming ByteBuffer into message POJO.

SSLServer.java
01	package com.sample.ssl;
02	 
03	import java.io.IOException;
04	import java.net.InetSocketAddress;
05	import java.nio.charset.Charset;
06	import java.security.GeneralSecurityException;
07	import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
08	 
09	import org.apache.mina.core.session.IdleStatus;
10	import org.apache.mina.core.service.IoAcceptor;
11	import org.apache.mina.filter.codec.ProtocolCodecFilter;
12	import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
13	import org.apache.mina.filter.logging.LoggingFilter;
14	import org.apache.mina.filter.ssl.SslFilter;
15	import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
16	 
17	/**
18	* @author giftsam
19	*/
20	public class SSLServer{
21	
22	    private static final int PORT = 5000;
23	 
24	    private static void addSSLSupport(DefaultIoFilterChainBuilder chain){
25	
26	        try{
27	
28	            SslFilter sslFilter = new SslFilter(new SSLContextGenerator().getSslContext());
29	            chain.addLast("sslFilter", sslFilter);
30	            System.out.println("SSL support is added..");
31	        } catch (Exception ex){
34	            ex.printStackTrace();
35	        }
36	    }
37	 
38	    public static void main(String[] args) throws IOException, GeneralSecurityException{
39	
40	        IoAcceptor acceptor = new NioSocketAcceptor();
41	        DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
42	 
43	        addSSLSupport(chain);
44	 
45	        chain.addLast("logger", new LoggingFilter());
46	        chain.addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
47	 
48	        acceptor.setHandler(new SSLServerHandler());
49	        acceptor.getSessionConfig().setReadBufferSize(2048);
50	        acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
51	        acceptor.bind(new InetSocketAddress(PORT));
52	        System.out.println("Server Started..");
53	    }
54	}

    The SSLServerHandler class contains four methods. The first method “sessionOpened” is called when the session is opened and it is used to set the session idle time. The second method “receiveMessage” is used to receive the message sent by the client. The other two methods “sessionIdle” is used to close the session when it was idle for 10 secs and the fourth method “exceptionCaught” is used to close the session when an exception occured.

SSLServerHandler.java

    GeoTrust SSL Certificates -- Try SSL free for 30 days right now. Get SSL up and running in minutes. www.geocerts.com
    SSL Certificates - thawte -- Choose from a complete range of certificates with the strongest SSL. www.thawte.com
    Export Controls Webinars -- Advanced Level EART/ITAR Topics On Demand & Available Now. www.exportcontrolstraining.co

01	package com.sample.ssl;
02	 
03	import org.apache.mina.core.session.IdleStatus;
04	import org.apache.mina.core.service.IoHandlerAdapter;
05	import org.apache.mina.core.session.IoSession;
06	import org.slf4j.Logger;
07	import org.slf4j.LoggerFactory;
08	 
09	/**
10	* @author giftsam
11	*/
12	public class SSLServerHandler extends IoHandlerAdapter{
13	
14	    private final Logger logger = (Logger) LoggerFactory.getLogger(getClass());
15	    private int idleTimeout = 10;
16	 
17	    @Override
18	    public void sessionOpened(IoSession session){
19	
20	        // set idle time to 10 seconds
21	        session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, idleTimeout);
22	 
23	        session.setAttribute("Values: ");
24	    }
25	 
26	    @Override
27	    public void messageReceived(IoSession session, Object message){
28	    
29	        System.out.println("Message received in the server..");
30	        System.out.println("Message is: " + message.toString());
31	    }
32	 
33	    @Override
34	    public void sessionIdle(IoSession session, IdleStatus status){
35	    
36	        logger.info("Transaction is idle for " + idleTimeout + "secs, So disconnecting..");
37	        // disconnect an idle client
38	        session.close();
39	    }
40	 
41	    @Override
42	    public void exceptionCaught(IoSession session, Throwable cause){
43	    
44	    // close the connection on exceptional situation
45	    session.close();
46	    }
47	}

Step 3 – Client part

    For the client part two classes named “SSLClient” and “SSLClientHandler” has been used. In the “MinaClient” class the SSLFilter class is used to encrypt and decrypt the data exchanged in the session and SSLFilter property  UseClientMode should be set as true and that configures the socket to use client mode in its first handshake.

    “IoConnector” interface is used to communicate with the server and that fires the event to the handler. Like the server part, The same “LoggingFilter” and “ProtocolCodecFilter” has been used. An interface named “ConnectFuture” is used to windup the asynchronous connection requests.

SSLClient.java
01	package com.sample.ssl;
02	 
03	import java.io.IOException;
04	import java.net.InetSocketAddress;
05	import java.nio.charset.Charset;
06	import java.security.GeneralSecurityException;
07	import javax.net.ssl.SSLContext;
08	import org.apache.mina.core.future.ConnectFuture;
09	import org.apache.mina.core.service.IoConnector;
10	import org.apache.mina.core.session.IoSession;
11	import org.apache.mina.filter.codec.ProtocolCodecFilter;
12	import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
13	import org.apache.mina.filter.logging.LoggingFilter;
14	import org.apache.mina.filter.ssl.SslFilter;
15	import org.apache.mina.transport.socket.nio.NioSocketConnector;
16	 
17	/**
18	* @author giftsam
19	*/
20	public class SSLClient{
21	
22	    private static final int REMORT_PORT = 5000;
23	 
24	    public static void main(String[] args) throws IOException, InterruptedException, GeneralSecurityException{
25	
26	        IoConnector connector = new NioSocketConnector();
27	        connector.getSessionConfig().setReadBufferSize(2048);
28	 
29	        SSLContext sslContext = new SSLContextGenerator().getSslContext();
30	        System.out.println("SSLContext protocol is: " + sslContext.getProtocol());
31	 
32	        SslFilter sslFilter = new SslFilter(sslContext);
33	        sslFilter.setUseClientMode(true);
34	        connector.getFilterChain().addFirst("sslFilter", sslFilter);
35	 
36	        connector.getFilterChain().addLast("logger", new LoggingFilter());
37	        connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
38	 
39	        connector.setHandler(new SSLClientHandler("Hello Server.."));
40	        ConnectFuture future = connector.connect(new InetSocketAddress("172.108.0.6", REMORT_PORT));
41	        future.awaitUninterruptibly();
42	 
43	        if (!future.isConnected()){
44	
45	            return;
46	        }
47	        IoSession session = future.getSession();
48	        session.getConfig().setUseReadOperation(true);
49	        session.getCloseFuture().awaitUninterruptibly();
50	        System.out.println("After Writing");
51	        connector.dispose();
52	    }
53	}

    For the handler, Like the server part the same methods “sessionOpened”, “messageReceived” and “exceptionCaught” has been used.

SSLClientHandler.java
01	package com.sample.ssl;
02	 
03	import org.apache.mina.core.service.IoHandlerAdapter;
04	import org.apache.mina.core.session.IoSession;
05	import org.slf4j.Logger;
06	import org.slf4j.LoggerFactory;
07	 
08	/**
09	* @author giftsam
10	*/
11	public class SSLClientHandler extends IoHandlerAdapter{
12	
13	    private final Logger logger = (Logger) LoggerFactory.getLogger(getClass());
14	    private final String values;
15	    private boolean finished;
16	 
17	    public SSLClientHandler(String values){
18	
19	        this.values = values;
20	     }
21	 
22	    public boolean isFinished(){
23	
24	        return finished;
25	    }
26	 
27	    @Override
28	    public void sessionOpened(IoSession session){
29	
30	        session.write(values);
31	    }
32	 
33	    @Override
34	    public void messageReceived(IoSession session, Object message){
35	
36	        logger.info("Message received in the client..");
37	        logger.info("Message is: " + message.toString());
38	    }
39	 
40	    @Override
41	    public void exceptionCaught(IoSession session, Throwable cause){
42	
43	        session.close();
44	    }
45	}

    Now its time to test the preceding codes, First the code “SSLServer” should be executed and then execute the “SSLClient”, the outcome of the codes will looks like the below,

Output – Server
01	Url is: /home/giftsam/Desktop/certificates/keystore
02	SSL Provider is: SunJSSE version 1.6
03	SSL support is added..
04	Server Started..
05	Dec 10, 2010 8:37:59 PM org.apache.mina.filter.logging.LoggingFilter log
06	INFO: CREATED
07	Dec 10, 2010 8:37:59 PM org.apache.mina.filter.logging.LoggingFilter log
08	INFO: OPENED
09	Dec 10, 2010 8:37:59 PM org.apache.mina.filter.logging.LoggingFilter log
10	INFO: RECEIVED: HeapBuffer[pos=0 lim=15 cap=36: 48 65 6C 6C 6F 20 53 65 72 76 65 72 2E 2E 0A]
11	Message received in the server..
12	Message is: Hello Server..
13	Dec 10, 2010 8:38:09 PM org.apache.mina.filter.logging.LoggingFilter log
14	INFO: IDLE
15	Dec 10, 2010 8:38:09 PM com.sample.ssl.SSLServerHandler sessionIdle
16	INFO: Transaction is idle for 10secs, So disconnecting..
17	Dec 10, 2010 8:38:09 PM org.apache.mina.filter.logging.LoggingFilter log
18	INFO: CLOSED

Output – client
01	Url is: /home/giftsam/Desktop/certificates/keystore
02	SSL Provider is: SunJSSE version 1.6
03	SSLContext protocol is: TLS
04	Dec 10, 2010 8:37:59 PM org.apache.mina.filter.logging.LoggingFilter log
05	INFO: CREATED
06	Dec 10, 2010 8:37:59 PM org.apache.mina.filter.logging.LoggingFilter log
07	INFO: OPENED
08	Dec 10, 2010 8:37:59 PM org.apache.mina.filter.logging.LoggingFilter log
09	INFO: SENT: HeapBuffer[pos=0 lim=15 cap=16: 48 65 6C 6C 6F 20 53 65 72 76 65 72 2E 2E 0A]
10	Dec 10, 2010 8:37:59 PM org.apache.mina.filter.logging.LoggingFilter log
11	INFO: SENT: HeapBuffer[pos=0 lim=0 cap=0: empty]

    Thats all folks. I hope this article clearly explains the steps to implement SSL for a client/server application using Apache Mina 2.0.x. If you find this article is useful for you, dont forget to leave your valuable comments. Have a joyous code day.


-----  [Reply]
Madhurima vadlamudi says:May 28, 2012 at 5:30 pm	

Hi,
I tryed this example ,its working very well.Problem:

1. if ssl client and server are using different .jks file, then server will through an exception like “Invalid certificate”
2. Another scenario is actually my SSL client is in C++,here Mina SSL Server is able to accept any kind of message from C++ SSL Client even though client is using different(invalid certificate) certificate.

Please help me to resolve this issues,its very urgent for me.

------ [Reply]
Alberto Acevedo says:	April 26, 2011 at 3:34 am	

Excellent article. I was able to reuse your Mina server side code and I successfully was able to do an SSL handshaking against a Flex application using the AS3 Crypto library.

Thanks for sharing.

------ [Reply]
Madhurima says:	May 23, 2012 at 4:45 pm	

Hi ALL,

Please help me.

Is Mina SSL Server accept c++ SSL Client request.
If possible someone can provide me the server.

Thanks,
Madhurima v

------ [Reply]
Adalbert says:	June 6, 2012 at 6:44 pm	

Hey, but is this realife example ?. In Your code client is reading keystore and truststore from the same files as server does. It works only on one machine or what ? Maybe Am I missing something.


------ [Reply]
roop says:	June 17, 2012 at 2:48 am	

Hi,

thanks for this nice tutorial. One thing i’m a bit confused about is the fact that SSLContextGenerator needs keystore-file AND truststore-file. If i got it right then keystore-file contains private/public key. The idea behind this is not to give private-key away, right? So SSLContextGenerator should look different right?

greetings roop
开发工具
“工欲善其事,必先利其器”, 所以顺手的工具,对我们的日常工作是相当有帮助的。这些年积累的工具很多,但沉淀下来就就那几样。

       Total Command, 这个我要强力推荐;简称TC, 据说很多Linux的管理员,因为有了Total Command, 觉得Windows也不是那么可憎。主要功能包括文件管理,FTP, 文件压缩/解压,还有众多的插件支持;基本上你你想要的,它都可以帮你实现,特别是文件管理部分。

       Snagit, 一个截图的工具,也可以做一些图像的处理。我一般就是拿来截图的,支持截图直接存为文件;各种截屏的方式,不如全屏,窗口,区域等。用了就知道,肯定是你想要的。这个不是免费软件,各位可以试用下先,我这里有个免注册的版本,需要的留个言。

       Evernote, 在线笔记,你可以把你的想法随时记录下来,然后会自动同步的云端,你可以在你的家里,或是用手机或是电脑都可以看到;有Chrome,Firefox的插件,你能容易把网页保存到Evernote中;爱收藏的朋友,一定要试下这个。

       Dropbox, 这个就不用说了,关于它的各种神奇的用法,大家可以Google下,我主要还是用它同步文件。我有时候需要在家里写写代码,所以我就直接把SVN库建在DropBox的文件夹中,这样就如同有了一个分布式的代码仓库。点击这里去注册一个吧。

       Notepad++, 号称取代UltraEdit的编辑器;我用它,主要是因为UE用不了了,不过它支持插件,还是不错的。

      Eclipse , 这个就不说了,Java开发人员多用Eclipse, 我也一样。WEB开发用Eclipse的JEE版本(曾经叫它WTP).自从有了Maven后,事情更简单了;Web开发也就装个jetty插件就好。


      如果各位有什么神器,也欢迎分享一下。
Java 下载 HTTP 资源保存到本地 Java 下载 HTTP 资源保存到本地
最近做个新功能,需要用到这个功能,测试无误,发上来备忘 :)

package com.xxx.utility;

/*******************************************************************************
 * <p>Title: xxx</p>
 * <p>Description: xxx</p>
 * <p>Copyright: Copyright (c) 2006 DavidHsing All Rights Reserved</p>
 * <p>Company: xxx</p>
 * <p>WebSite: http://blog.csdn.net/DavidHsing</p>
 *
 * @author   : DavidHsing <DavidHsing(At)163.com>
 * @version  : 1.00
 * @date     : 2007-08-28
 * @direction: 下载 HTTP 网络资源保存到本地
 ******************************************************************************/

import java.io.*;
import java.net.*;
import java.util.*;

//******************************************************************************


public class HttpDownload
{
    //==========================================================================

    //public HttpDownload() {}

    //==========================================================================

    private Vector vRemoteHttpURL = new Vector();
    private Vector vLocalSaveFile = new Vector();

    //==========================================================================

    /**
     * 设置代理服务器
     * @param    String
     * @param    String
     */
    public void setProxy(String sProxyHost, String sProxyPort)
    {
        if (sProxyHost != null && !sProxyHost.trim().equals(""))
        {
            if (sProxyPort == null || sProxyPort.trim().equals(""))
            {
                sProxyPort = "80";
            }
            System.getProperties().put("proxySet", "true");
            System.getProperties().put("proxyHost", sProxyHost);
            System.getProperties().put("proxyPort", sProxyPort);
        }
    }

    //==========================================================================

    /**
     * 添加一个下载任务
     * @param    String
     * @param    String
     * @return   boolean
     */
    public boolean addOneTask(String sRemoteHttpURL, String sLocalSaveFile)
    {
        if (sRemoteHttpURL == null || sRemoteHttpURL.trim().equals("") || !sRemoteHttpURL.trim().substring(0, 7).equalsIgnoreCase("http://"))
        {
            System.out.println(" @> HttpDownload.addOneTask() : 源地址有误,不是一个有效的 http 地址!");
            return false;
        }
        else
        {
            if (sLocalSaveFile == null || sLocalSaveFile.trim().equals(""))
            {
                sLocalSaveFile = "./" + sRemoteHttpURL.substring(sRemoteHttpURL.lastIndexOf("/") + 1);
            }
            vRemoteHttpURL.add(sRemoteHttpURL);
            vLocalSaveFile.add(sLocalSaveFile);
        }

        return true;
    }

    //==========================================================================

    /**
     * 清除下载列表
     */
    public void clearAllTasks()
    {
        vRemoteHttpURL.clear();
        vLocalSaveFile.clear();
    }

    //==========================================================================

    /**
     * 根据列表下载资源
     * @return   boolean
     */
    public boolean downLoadByList()
    {
        for (int i = 0; i < vRemoteHttpURL.size(); i++)
        {
            String sRemoteHttpURL = (String)vRemoteHttpURL.get(i);
            String sLocalSaveFile = (String)vLocalSaveFile.get(i);

            if (!saveToFile(sRemoteHttpURL, sLocalSaveFile))
            {
                System.out.println(" @> HttpDownload.downLoadByList() : 下载远程资源时出现异常!");
                //return false;
            }
        }

        return true;
    }

    //==========================================================================

    /**
     * 将 HTTP 资源保存为文件
     * @param    String
     * @param    String
     * @return   boolean
     */
    private boolean saveToFile(String sRemoteHttpURL, String sLocalSaveFile)
    {
        if (sRemoteHttpURL == null || sRemoteHttpURL.trim().equals(""))
        {
            System.out.println(" @> HttpDownload.saveToFile() : 要下载的远程资源地址不能为空!");
            return false;
        }

        try
        {
            URL tURL = new URL(sRemoteHttpURL);
            HttpURLConnection tHttpURLConnection = (HttpURLConnection)tURL.openConnection();
            tHttpURLConnection.connect();
            BufferedInputStream tBufferedInputStream = new BufferedInputStream(tHttpURLConnection.getInputStream());
            FileOutputStream tFileOutputStream = new FileOutputStream(sLocalSaveFile);

            int nBufferSize = 1024 * 5;
            byte[] bufContent = new byte[nBufferSize];
            int nContentSize = 0;
            while ((nContentSize = tBufferedInputStream.read(bufContent)) != -1)
            {
                tFileOutputStream.write(bufContent, 0, nContentSize);
            }

            tFileOutputStream.close();
            tBufferedInputStream.close();
            tHttpURLConnection.disconnect();

            tURL = null;
            tHttpURLConnection = null;
            tBufferedInputStream = null;
            tFileOutputStream = null;
        }
        catch (Exception ex)
        {
            System.out.println(" @> HttpDownload.saveToFile() : 下载远程资源时出现异常!");
            System.out.println("    远程地址:" + sRemoteHttpURL);
            System.out.println("    本地路径:" + sLocalSaveFile);
            return false;
        }

        return true;
    }

    //==========================================================================

    ///**
    // * 主方法(用于测试)
    // * @param argv String[]
    // */
    public static void main(String argv[])
    {
        HttpDownload tHttpDownload = new HttpDownload();
        tHttpDownload.addOneTask("http://www.baidu.com/test.zip", "C:/test.zip");
        tHttpDownload.downLoadByList();
        tHttpDownload = null;
    }

    //==========================================================================

} //class HttpDownload end
Mina2.0控制读取、写出流量代码(出自red5) mina compression filter ? Mina2.0控制读取、写出流量代码(出自red5)
public class TrafficShapingFilter extends IoFilterAdapter {

 protected static Logger log = LoggerFactory.getLogger(TrafficShapingFilter.class);

 private final AttributeKey STATE = new AttributeKey(getClass(), "state");

 //定时器服务,用来创建定时任务的定时器服务类
 private final ScheduledExecutorService scheduledExecutor;

 //用来估算对象占据JVM内存的大小的类,计算出一个对象实际占用JVM内存的大小
 private final MessageSizeEstimator messageSizeEstimator;

 private volatile int maxReadThroughput;

 private volatile int maxWriteThroughput;

 private volatile int poolSize = 1;

 /**
  * 构造方法
  * @param maxReadThroughput 最大读取字节大小(单位:秒)
  * @param maxWriteThroughput 最大写出的字节大小(单位:秒)
  */
 public TrafficShapingFilter(int maxReadThroughput, int maxWriteThroughput) {
  this(null, null, maxReadThroughput, maxWriteThroughput);
 }

 public TrafficShapingFilter(ScheduledExecutorService scheduledExecutor, int maxReadThroughput,
   int maxWriteThroughput) {
  this(scheduledExecutor, null, maxReadThroughput, maxWriteThroughput);
 }

 public TrafficShapingFilter(ScheduledExecutorService scheduledExecutor, MessageSizeEstimator messageSizeEstimator,
   int maxReadThroughput, int maxWriteThroughput) {

  log.debug("ctor - executor: {} estimator: {} max read: {} max write: {}", new Object[] { scheduledExecutor,
    messageSizeEstimator, maxReadThroughput, maxWriteThroughput });

  if (scheduledExecutor == null) {
   //实例化一个定时器对象,线程池的默认数量是1.
   scheduledExecutor = new ScheduledThreadPoolExecutor(poolSize);
   //throw new NullPointerException("scheduledExecutor");
  }

  if (messageSizeEstimator == null) {
   //实例化默认的估算消息大小的类
   messageSizeEstimator = new DefaultMessageSizeEstimator() {
    @Override
    public int estimateSize(Object message) {
     if (message instanceof IoBuffer) {
      return ((IoBuffer) message).remaining();
     }
     return super.estimateSize(message);
    }
   };
  }

  this.scheduledExecutor = scheduledExecutor;
  this.messageSizeEstimator = messageSizeEstimator;
  //设置最大读取字节长度(单位:秒)
  setMaxReadThroughput(maxReadThroughput);
  //设置最大写出字节长度(单位:秒)
  setMaxWriteThroughput(maxWriteThroughput);
 }

 public ScheduledExecutorService getScheduledExecutor() {
  return scheduledExecutor;
 }

 public MessageSizeEstimator getMessageSizeEstimator() {
  return messageSizeEstimator;
 }

 public int getMaxReadThroughput() {
  return maxReadThroughput;
 }

 public void setMaxReadThroughput(int maxReadThroughput) {
  if (maxReadThroughput < 0) {
   maxReadThroughput = 0;
  }
  this.maxReadThroughput = maxReadThroughput;
 }

 public int getMaxWriteThroughput() {
  return maxWriteThroughput;
 }

 public void setMaxWriteThroughput(int maxWriteThroughput) {
  if (maxWriteThroughput < 0) {
   maxWriteThroughput = 0;
  }
  this.maxWriteThroughput = maxWriteThroughput;
 }

 public int getPoolSize() {
  return poolSize;
 }

 public void setPoolSize(int poolSize) {
  if (poolSize < 1) {
   poolSize = 1;
  }
  this.poolSize = poolSize;
 }

 @Override
 public void onPreAdd(IoFilterChain parent, String name, NextFilter nextFilter) throws Exception {
  if (parent.contains(this)) {
   throw new IllegalArgumentException(
     "You can't add the same filter instance more than once.  Create another instance and add it.");
  }
  //给每一个session添加一个属性 STATE 属性,关联一个State对象。
  parent.getSession().setAttribute(STATE, new State());
  //调节会话sessiion读取buffer大小
  adjustReadBufferSize(parent.getSession());
 }

 @Override
 public void onPostRemove(IoFilterChain parent, String name, NextFilter nextFilter) throws Exception {
  //写完毕,关闭会话移除关联对象State
  parent.getSession().removeAttribute(STATE);
 }

 @Override
 public void messageReceived(NextFilter nextFilter, final IoSession session, Object message) throws Exception {

  int maxReadThroughput = this.maxReadThroughput;
  //process the request if our max is greater than zero
  if (maxReadThroughput > 0) {
   final State state = (State) session.getAttribute(STATE);
   long currentTime = System.currentTimeMillis();

   long suspendTime = 0;
   boolean firstRead = false;
   synchronized (state) {
    //估算当前已经读取字节数组总量
    state.readBytes += messageSizeEstimator.estimateSize(message);
                 //如果读取回话挂起,suspendedRead=true
    if (!state.suspendedRead) {
     if (state.readStartTime == 0) {//表示第一次读取会话的数据
      firstRead = true;
      //设置会话开始读取数据的时间
      state.readStartTime = currentTime - 1000;
     }
                    //估算当前平均每秒读取字节流量大小
     long throughput = (state.readBytes * 1000 / (currentTime - state.readStartTime));
     if (throughput >= maxReadThroughput) {//如果平均读取流量大于设置的数值,
      //计算需要挂起的时间,((state.readBytes/maxReadThroughput)*1000)计算如果按照规定流量读取数据需要多少秒,然后再减去已经读取的时间差,计算出需要挂起的时间
      suspendTime = Math.max(0, (state.readBytes * 1000 / maxReadThroughput)
        - (firstRead ? 0 : currentTime - state.readStartTime));

      state.readBytes = 0;
      state.readStartTime = 0;
      state.suspendedRead = suspendTime != 0;
                        //调整会话状态,设置会话不在读取数据,利用定时器,挂起会话,
      adjustReadBufferSize(session);
     }
    }
   }

   if (suspendTime != 0) {
    session.suspendRead();
    scheduledExecutor.schedule(new Runnable() {
     public void run() {
      synchronized (state) {
       state.suspendedRead = false;
      }
      session.resumeRead();
     }
    }, suspendTime, TimeUnit.MILLISECONDS);
   }
  }

  nextFilter.messageReceived(session, message);

 }

 /**
  *
  * 调整session默认设置最大读取字节数组长度
  * @param session
  */
 private void adjustReadBufferSize(IoSession session) {
  int maxReadThroughput = this.maxReadThroughput;
  if (maxReadThroughput == 0) {//如果不限制读取流量返回
   return;
  }
  IoSessionConfig config = session.getConfig();
  if (config.getReadBufferSize() > maxReadThroughput) {
   config.setReadBufferSize(maxReadThroughput);
  }
  if (config.getMaxReadBufferSize() > maxReadThroughput) {
   config.setMaxReadBufferSize(maxReadThroughput);
  }
 }

 @Override
 public void messageSent(NextFilter nextFilter, final IoSession session, WriteRequest writeRequest) throws Exception {
          //得到当前系统设置最大写出字节
  int maxWriteThroughput = this.maxWriteThroughput;
  //process the request if our max is greater than zero
  if (maxWriteThroughput > 0) {
   final State state = (State) session.getAttribute(STATE);
   //得到系统时间
   long currentTime = System.currentTimeMillis();
            //挂起时间长度0
   long suspendTime = 0;
   boolean firstWrite = false;
   synchronized (state) {
    state.writtenBytes += messageSizeEstimator.estimateSize(writeRequest.getMessage());
    if (!state.suspendedWrite) {
     if (state.writeStartTime == 0) {
      firstWrite = true;
      //初始化写时间
      state.writeStartTime = currentTime - 1000;
     }
                    //计算平均写字节数组流量
     long throughput = (state.writtenBytes * 1000 / (currentTime - state.writeStartTime));
     if (throughput >= maxWriteThroughput) {//写流量超出系统设置,会话挂起操作
      //计算会话需要挂起的时间
      suspendTime = Math.max(0, state.writtenBytes * 1000 / maxWriteThroughput
        - (firstWrite ? 0 : currentTime - state.writeStartTime));
      state.writtenBytes = 0;
      state.writeStartTime = 0;
      state.suspendedWrite = suspendTime != 0;
     }
    }
   }

   if (suspendTime != 0) {
    log.trace("Suspending write");
    //挂起会话
    session.suspendWrite();
    //定时器执行定时挂起操作
    scheduledExecutor.schedule(new Runnable() {
     public void run() {
      synchronized (state) {
       state.suspendedWrite = false;
      }
      //挂起唤醒会话
      session.resumeWrite();
      log.trace("Resuming write");
     }
    }, suspendTime, TimeUnit.MILLISECONDS);
   }
  }
        //串模式,执行下一个过滤器
  nextFilter.messageSent(session, writeRequest);

 }

 /**
  * 状态标志
  * @author Administrator
  *
  */
 private static class State {
  /**
   * 开始读取数据的时间
   */
  private long readStartTime;

  /**
   * 开始写数据的时间
   */
  private long writeStartTime;

  /**
   * 读数据是否已经被挂起,true:挂起,false:未挂起
   */
  private boolean suspendedRead;

  /**
   * 是否写操作被挂起,true:被挂起,false:未被挂起
   */
  private boolean suspendedWrite;

  /**
   * 该会话总共被读取数据字节长度
   */
  private long readBytes;

  /**
   * 总共被写的数据的长度
   */
  private long writtenBytes;
 }
}
用mina传输文件 mina compression filter ? 用mina传输文件
Mina 传输文件,但是没有协议,比较危险,即谁都可以传输等等,什么文件都认等


    package com.rose.common.frame.mina;   
      
    import java.io.BufferedInputStream;   
    import java.io.BufferedOutputStream;   
    import java.io.IOException;   
    import java.io.InputStream;   
    import java.io.OutputStream;   
      
    /**  
     * 用于mina 服务器上传下载  
     * 流处理线程公共类  
     * @author rose  
     *  
     */  
    public class IoStreamThreadWork extends Thread{   
        public static final int BUFFER_SIZE = 1024*2;   
           
        private BufferedInputStream bis;   
        private BufferedOutputStream bos;   
           
           
        public BufferedInputStream getBis() {   
            return bis;   
        }   
      
        public void setBis(BufferedInputStream bis) {   
            this.bis = bis;   
        }   
      
        public BufferedOutputStream getBos() {   
            return bos;   
        }   
      
        public void setBos(BufferedOutputStream bos) {   
            this.bos = bos;   
        }   
      
        public IoStreamThreadWork(InputStream in, OutputStream os){   
            bis = new BufferedInputStream(in);   
            bos = new BufferedOutputStream(os);   
        }   
        public synchronized void run() {   
            byte[] bufferByte = new byte[BUFFER_SIZE];   
            int tempData = 0;   
            try {   
                while((tempData = bis.read(bufferByte)) != -1 ){   
                    bos.write(bufferByte, 0, tempData);   
                }   
                try {   
                    bos.flush();   
                } catch (IOException e) {   
                    e.printStackTrace();   
                }   
            } catch (IOException e) {   
                e.printStackTrace();   
            }finally{   
                try {   
                    bos.close();   
                    bis.close();   
                } catch (IOException e) {   
                    e.printStackTrace();   
                }   
            }   
        }   
    }  

服务端:

    package com.rose.common.frame.mina;   
      
    import java.io.File;   
    import java.io.FileNotFoundException;   
    import java.io.FileOutputStream;   
    import java.io.IOException;   
    import java.io.InputStream;   
    import java.io.OutputStream;   
    import java.net.InetSocketAddress;   
    import java.util.concurrent.ArrayBlockingQueue;   
    import java.util.concurrent.ThreadPoolExecutor;   
    import java.util.concurrent.TimeUnit;   
      
    import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;   
    import org.apache.mina.core.session.IoSession;   
    import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;   
    import org.apache.mina.filter.logging.LoggingFilter;   
    import org.apache.mina.handler.stream.StreamIoHandler;   
    import org.apache.mina.transport.socket.nio.NioSocketAcceptor;   
      
    /**  
     * @author rose  
     * 文件传输服务器  
     */  
    public class MinaFileServer extends StreamIoHandler{   
        public static final int PORT = 8888;   
        @Override  
        public void sessionOpened(IoSession session) {   
            System.out.println("客户端连接了:"+session.getRemoteAddress());   
            super.sessionOpened(session);   
        }   
      
        protected void processStreamIo(IoSession session, InputStream in,OutputStream out) {   
            //设定一个线程池   
            //参数说明:最少数量3,最大数量6 空闲时间 3秒   
            ThreadPoolExecutor threadPool = new ThreadPoolExecutor(3, 6, 3,TimeUnit.SECONDS,    
                    //缓冲队列为3   
                    new ArrayBlockingQueue<Runnable>(3),   
                    //抛弃旧的任务   
                    new ThreadPoolExecutor.DiscardOldestPolicy());   
            FileOutputStream fos = null;   
                       //此处路径如何动态设定。   
            File receiveFile = new File("e:\\test.pdf");   
      
            try {   
                fos = new FileOutputStream(receiveFile);   
            } catch (FileNotFoundException e1) {   
                e1.printStackTrace();   
            }   
            //将线程放入线程池 当连接很多时候可以通过线程池处理   
            threadPool.execute(new IoStreamThreadWork(in,fos));   
            //直接启动线程 连接很少可以选用下面   
    //      new IoStreamThreadWork(in,fos).start();   
        }   
           
        public void createServerStream(){   
            //建立一个无阻塞服务端socket 用nio   
            NioSocketAcceptor acceptor = new NioSocketAcceptor();   
            //创建接收过滤器 也就是你要传送对象的类型   
            DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();   
            //===========过滤器创建好了就开始设定============   
               
            //设定 对象传输工厂   
            ObjectSerializationCodecFactory factory = new ObjectSerializationCodecFactory();   
            //设定传输最大值   
            factory.setDecoderMaxObjectSize(Integer.MAX_VALUE);// 设定后服务器可以接收大数据   
            factory.setEncoderMaxObjectSize(Integer.MAX_VALUE);   
            chain.addLast("logging", new LoggingFilter());//这个用于打印日志 可以不写   
            //设定服务端消息处理器   
            acceptor.setHandler(new MinaFileServer());   
            InetSocketAddress inetSocketAddress = null;   
            try {   
                inetSocketAddress = new InetSocketAddress(8888);   
                acceptor.bind(inetSocketAddress);   
            } catch (IOException e) {   
                // TODO Auto-generated catch block   
                e.printStackTrace();   
            }   
            System.out.println("文件服务器已经开启:"+8888);   
        }   
        public static void main(String[] args) {   
            MinaFileServer server = new MinaFileServer();   
            server.createServerStream();   
        }   
    }  

客户端:

    package com.rose.common.frame.mina;   
      
    import java.io.File;   
    import java.io.FileInputStream;   
    import java.io.FileNotFoundException;   
    import java.io.InputStream;   
    import java.io.OutputStream;   
    import java.net.InetSocketAddress;   
      
    import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;   
    import org.apache.mina.core.future.ConnectFuture;   
    import org.apache.mina.core.session.IoSession;   
    import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;   
    import org.apache.mina.filter.logging.LoggingFilter;   
    import org.apache.mina.handler.stream.StreamIoHandler;   
    import org.apache.mina.transport.socket.nio.NioSocketConnector;   
      
      
    /**  
     * @author rose  
     * 文件传输客户端   
     */  
    public class MinaFileClient extends StreamIoHandler{   
        IoSession session;   
        public void setSession(IoSession session) {   
            this.session = session;   
        }   
        public IoSession getSession() {   
            return session;     
        }   
        @Override  
        protected void processStreamIo(IoSession session, InputStream in,   
                OutputStream out) {   
            //客户端发送文件   
                File sendFile = new File("F:\\ttt.pdf");   
                FileInputStream fis = null;   
                try {   
                    fis = new FileInputStream(sendFile);   
                       
                } catch (FileNotFoundException e) {   
                    e.printStackTrace();   
                }   
                //放入线程让其执行   
                 //客户端一般都用一个线程实现即可 不用线程池   
                new IoStreamThreadWork(fis,out).start();   
                return;   
        }   
           
        public void createClienStream(){   
            int port = 8888;   
            String local = "127.0.0.1";   
               
            NioSocketConnector connector = new NioSocketConnector();   
            DefaultIoFilterChainBuilder chain = connector.getFilterChain();   
            ObjectSerializationCodecFactory factory = new ObjectSerializationCodecFactory();   
            factory.setDecoderMaxObjectSize(Integer.MAX_VALUE);   
            factory.setEncoderMaxObjectSize(Integer.MAX_VALUE);   
            chain.addLast("logging", new LoggingFilter());//用于打印日志可以不写   
            connector.setHandler(new MinaFileClient());   
            ConnectFuture connectFuture = connector.connect(new InetSocketAddress(local,port));   
            connectFuture.awaitUninterruptibly();//写上这句为了得到下面的session 意思是等待连接创建完成 让创建连接由异步变同步   
            //后来表明我开始的想法不行 动态依旧不能做到   
    //      @SuppressWarnings("unused")   
    //      IoSession session = connectFuture.getSession();   
    //      setSession(session);   
        }   
        public static void main(String[] args) {   
            MinaFileClient client = new MinaFileClient();   
            client.createClienStream();   
        }   
    }  
MIna 2 的 CompressionHandler mina compression filter ? Scalable NIO Servers – Part 3 – Features
Scalable NIO Servers – Part 3 – Features
April 13, 2009
Tags: Grizzly, Java, Mina, Netty, NIO
Tweet

We have now analyzed various open source NIO servers for performance and memory consumption.  Per my quick, initial testing, only Grizzly, Mina, and Netty were comparable. Now, let’s analyze features and how each of these frameworks use them.  For my purposes, I am going to be looking into the following features that I personally value most important for my project:

    Intercepting Pattern (ie: Filters)
    Access to high level, yet effcient, buffers rather than lower level byte buffers
    Protocol independence and abstraction
    Socket independence and abstraction
    Custom protocol support
    POJO support for encoding/decoding
    Custom thread model support
    HTTP support
    User documentation (user guide, javadoc, source code, examples

Intercepting Pattern: Filters/Handlers

Let’s look at the first feature:  intercepting pattern.  The intercepting pattern is a J2EE-based pattern such as Filters that are used to take an incoming request and/or outgoing response and perform various logic such as compression, security checks, etc.  They are used to abstract away specific functionality from the true business logic.  This is a very good idea as the code that does your business logic should not have to care about performing compression, performing security, etc.  Intercepting filters provide that abstraction.  For NIO servers, this pattern also fits very well as you can define various protocol stacks using filters.  For example, you may want to have an encryption filter that provides SSL translation, a compression filter that performs GZip compression, an authentication filter that performs authentication, and finally an application handler that performs the business logic.  As each filter is its own implementation with its own purpose, you can easily change them in and out, re-order, and temporarily disable.

Netty provides this functionality through the use of channel handlers via ChannelUpstreamHandler and ChannelDownstreamHandler.  The handlers get added to a particular pipeline.  The order distinguishes how the handlers are applied to incoming or outgoing data.  Handlers in Netty can provide various types of functions.  First, they can merely perform a check on the data, such as authorization, session, etc and merely pass the buffer up or down the stream.  Second, they can remove a portion of the buffer such as a protocol header or codec and then pass the remaining data to subsequent handlers.  Third, it can translate the data into POJOs and pass the POJOs on to higher level handlers.  This allows you to build any number of helpful handlers as we will see in a future post on building protocol stacks.  For example:

ChannelFactory factory = new NioServerSocketChannelFactory
(
    Executors.newCachedThreadPool(),
    Executors.newCachedThreadPool()
);
 
ServerBootstrap bootstrap = new ServerBootstrap(factory);
 
bootstrap.getPipeline().addLast("compressor", new CompressionHandler());
bootstrap.getPipeline().addLast("authenticator", new AuthenticationHandler());
bootstrap.getPipeline().addLast("codec", 
    new DelimiterBasedFrameDecoder(Delimiters.lineDelimiter()));
bootstrap.getPipeline().addLast("handler", new ApplicationHandler());

Mina also provides the intercepting pattern through actual filters. The filters are used to translate and handle data before handlers get invoked. In Netty, everything is a filter/handler. They are the same. In Mina, filters get applied first and the resultant gets passed on to the handler. For example:

NioSocketAcceptor acceptor = new NioSocketAcceptor();
 
acceptor.getFilterChain().addLast("compressor", new CompressionFilter());
acceptor.getFilterChain().addLast("authenticator", new AuthenticationFilter());
acceptor.getFilterChain().addLast("codec", 
    new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
 
acceptor.setHandler(new ApplicationHandler());

If you are familiar with Java web applications and/or J2EE, Mina would feel very comfortable for you. The filters and filter chains directly relate to the Filter and FilterChain in J2EE and the handler directly relates to the Servlet. In other words, in a typical web application, one or more filters process the data first (ie: compression, authentication, etc) and then the resultant stream/data gets passed to the servlet. This same technique is used with Mina.

Grizzly also uses the concept of a protocol filter chain, except that rather than have separate handlers and separate filters, it behaves like Netty in that everything being a filter. In any sense, all three frameworks provide the filtering mechanism to easily build protocol stacks and abstract key functionality from each other.

ProtocolChain protocolChain = pic.poll();
protocolChain.addFilter(new CompressionFilter());
protocolChain.addFilter(new AuthenticationFilter());
protocolChain.addFilter(new ApplicationFilter());

Support Handlers

Netty comes out of the box with handlers for Base64 encoding/decoding, delimiter based codecs, fixed length codecs, HTTP handlers, logging handlers, Java object serialization/deserialization codecs, Google Protocol Buffer codecs, SSL handlers, simple string codecs, and handlers used to control bandwidth, traffic shaping, etc. There are also several utility handlers that may be used to build custom handlers such as a replay handler, timeout handler, frame decoder, etc. Mina comes with support for blacklist filters, compression filters, connection throttling filters, SSL filters, logging filters, protocol codecs such as delimiter based, and HTTP. Grizzly provides support for SSL, custom protocol codecs, logging, and HTTP. They essentially offer similar handlers. However, in my personal preference, I prefer the handlers and architecture of Netty and believe they provide a little better support for custom handlers based on their already existent handlers and their utility handlers.

High Level Buffers

Next, let’s look at how the various frameworks support byte buffers in NIO. Byte buffers are a low level construct in the NIO library and has much complexity involved in order to maintain the proper states, handle de-fragmentation, etc. Most libraries, including these three, provide a custom high level object that wraps one or more byte buffers and provides access and utility methods for obtaining the data.

Netty uses an interface named ChannelBuffer. The ChannelBuffer class wraps multiple ByteBuffer instances and provides transparent zero copy to reduce its memory usage and improve performance. Rather than creating a composite buffer by copying multiple fragmented buffers, Netty maintains references to the fragments and allows access as if they were composite. ChannelBuffers also provide support for marking and resetting reader indexes, which is very helpful in custom protocol codecs. It also provides support for searching, slicing, and reading/writing various data types.

Mina provides an interfaced named IoBuffer that provides a wrapper construct around NIO byte buffers. IoBuffers use an underlying byte buffer instance and provides access and utility methods for interacting with that data. It also supports auto-expansion by re-allocating the buffer. As of version 2, however, it does not appear to support zero-copy operations, which is on the list for upcoming version 3. Othewise, IoBuffers provide similar functionality for access, marking, skipping, etc. One interesting thing to note, however, is that Mina may be moving to more of an InputStream type interface that manages one or more byte buffers.

Socket and Protocol Independence

For socket and protocol independence, all three libraries built their architectures precisely on those principles. For example, both Netty and Mina are not directly built as a NIO framework. Rather, they support the old style Java I/O as well. Mina even supports custom protocols such as RS-232 serial. By not directly relying on NIO, it makes changes in the future easier. For example, JDK 7 will introduce AIO (or NIO.2) for better support for asynchronous I/O. Each of these libraries can easily build support for that in without requiring a complete change of programs. Grizzly is already working on a new NIO.2 framework based on AIO. The only time your program needs to rely on a particular implementation is when it sets up its connection, which is generally only one or two lines of code in terms of selecting which selector you want to use (TCP, UDP, NIO, AIO, OIO, etc). The rest of the program is completely transparent to the underlying selector technology.

Custom Protocols and POJO

In terms of custom protocol support and POJO support, this is easily supported through custom filters/handlers. As filters/handlers pass data up and down the chain, they can translate the data as needed. This includes disposing of header data or translating the data into a POJO and returning the POJO. For example, a simple java application can easily serialize and deserialize objects on the wire by using a handler that reads the incoming bytes and deserializes and serializes an outgoing object and writes the bytes. Each of these frameworks support this by passing Object as the message during the message received and message sent callbacks. It is up to the filters/handlers to define how that Object gets translated and handled.

Threading Models

Threading support is a key to any successful NIO framework. In the old I/O frameworks, there was generally one or more acceptor threads that merely accepted incoming connections and then created a worker thread per connection. However, this failed to scale as it required thousands of concurrent threads. In NIO, everything is asynchronous. As a result, you have to manage data differently and ensure you have enough threads to handle the data without causing long blocks for other clients. As each and every protocol and server is different, the number of acceptor threads and the number of worker threads is vastly different. It is a fine tuning process. As such, it requires an extensible API. Further, as data may jump from one worker thread to another, it requires a very well designed API and architecture. Each of these frameworks have undergone several testing, analysis, and trials to ensure they properly handle threads and the NIO model. Netty provides this API by using java.util.concurrent thread pool libraries to specify the type of threading model. Further, it provides handlers for shaping traffic and controlling bandwidth. Mina provides similar facility by specifying the number of acceptor threads, the thread pool for worker threads and processor threads. It also provides execution filters to handle threads, bandwidth, etc.

Documentation

Finally, documentation. In my opinion, Netty has the best documentation, source code, and developer guide available. However, Mina is closely behind and may actually overtake it. However, I prefer the API style of Netty more, so I am a bit biased I guess. I struggled to find good documentation for Grizzly apart from the JavaDocs, a few examples, and a few blog posting. Netty and Mina both made helpful examples readily available.

Conclusion

Overall, I have come to prefer Netty over both Mina and Grizzly for performance, memory, and feature usage. Note that you should undergo your own analysis to decide which library suits your requirements.
How to write a Compression Filter ? mina compression filter ? How to write a Compression Filter ?
Hi all,

i am trying to write a IoFilter for Googles Snappy compressor /
decompressor. I am using the Mina-LZip -Compressor Filter as a base an
rewirited it for Snappy. The usage of Snappy is quite simpel
(Snappy.compress(byte[]):byte[]  / Snappy.uncompress(byte[]):byte[]) but
Mina give me a headache ! I dont know what i am doing wrong, but i am
loosing messages, even if i disable the compression (see the attached
TestCompressor-class)

This is what i do :
Compression

   1. compress the data
   2. write the length of the compressed data as an int-value(4 byte)
   3. write the data

Decompression

   1. check if i there is enough data for the length field (4byte)
   2. read the length of the appended data
   3. check if there is enough data to read the compressed data
   4. read and decompress the compressed data


Here is my Code

public class CompressionFilter extends WriteRequestFilter {
@Override
public void messageReceived(NextFilter nextFilter, IoSession session, Object
message) throws Exception {
if (!(message instanceof IoBuffer)) {
nextFilter.messageReceived(session, message);
return;
}
IoBuffer inBuffer = (IoBuffer) message;
boolean hasMoreData;
do {
hasMoreData = decode(inBuffer, nextFilter, session);
}
while (hasMoreData);
}
private boolean decode(IoBuffer inBuffer, NextFilter nextFilter, IoSession
session) {
int position = inBuffer.position();
final int remaining = inBuffer.remaining();
if (remaining < 4) {
inBuffer.position(position);
return false;
}
final int size = inBuffer.getInt();
if (size > remaining - 4) {
inBuffer.position(position);
return false;
}
byte[] inBytes = new byte[size];
inBuffer.get(inBytes);
final byte[] uncompress = uncompress(inBytes);
IoBuffer outBuffer = IoBuffer.wrap(uncompress);
nextFilter.messageReceived(session, outBuffer);
return true;
}
private byte[] uncompress(byte[] compressed) {
// return Snappy.uncompress(compressed);
return compressed;
}
@Override
protected Object doFilterWrite(NextFilter nextFilter, IoSession session,
WriteRequest writeRequest) throws IOException {
IoBuffer inBuffer = (IoBuffer) writeRequest.getMessage();
if (!inBuffer.hasRemaining()) {
// Ignore empty buffers
return null;
} else {
byte[] inBytes = new byte[inBuffer.remaining()];
inBuffer.get(inBytes).flip();
byte[] outBytes = compress(inBytes);
IoBuffer outBuf = IoBuffer.allocate(outBytes.length + 4);
outBuf.putInt(outBytes.length);
outBuf.put(outBytes);
outBuf.flip();
return outBuf;
}
}
private byte[] compress(byte[] uncompressed) {
// return Snappy.compress(inBytes);
return uncompressed;
}
}




It seems my attached test case is not displayed, here is it...

public class TestCompressor {

private static final InetSocketAddress LOCAL_ADDRESS = new
InetSocketAddress(5000);
private IoAcceptor acceptor;
private IoConnector connector;

private List<String> receivedMessages;

@Before
public void setUp() throws IOException {
connector = new NioSocketConnector();
connector.getFilterChain().addLast("compression", new CompressionFilter());
connector.getFilterChain().addLast("text", new ProtocolCodecFilter(new
TextLineCodecFactory()));
connector.setHandler(new IoHandlerAdapter());

 acceptor = new NioSocketAcceptor();
acceptor.getFilterChain().addLast("compression", new CompressionFilter());
acceptor.getFilterChain().addLast("text", new ProtocolCodecFilter(new
TextLineCodecFactory()));
acceptor.setHandler(new IoHandlerAdapter() {
@Override
public void messageReceived(IoSession session, Object message) throws
Exception {
System.out.println(message);
receivedMessages.add((String) message);
}
});
acceptor.bind(LOCAL_ADDRESS);
 receivedMessages= new ArrayList<String>();
}

@Test
public void test() throws Throwable {
List<String> sendMessages = new ArrayList<String>();
for (int i = 0; i < 3000; i++)
sendMessages.add("Message " + i);
 ConnectFuture connectFuture = connector.connect(LOCAL_ADDRESS);
connectFuture.await();
final IoSession session = connectFuture.getSession();
 for (String message : sendMessages)
session.write(message).await();
 for (int i = 0; i < sendMessages.size(); i++)
assertEquals(sendMessages.get(i), receivedMessages.get(i));
 session.close(false).await();
}
}

On Thu, Jul 21, 2011 at 8:40 AM, Chris S <chriss.dev@googlemail.com> wrote:

> Hi all,
>
> i am trying to write a IoFilter for Googles Snappy compressor /
> decompressor. I am using the Mina-LZip -Compressor Filter as a base an
> rewirited it for Snappy. The usage of Snappy is quite simpel
> (Snappy.compress(byte[]):byte[]  / Snappy.uncompress(byte[]):byte[]) but
> Mina give me a headache ! I dont know what i am doing wrong, but i am
> loosing messages, even if i disable the compression (see the attached
> TestCompressor-class)
>
> This is what i do :
> Compression
>
>    1. compress the data
>    2. write the length of the compressed data as an int-value(4 byte)
>    3. write the data
>
> Decompression
>
>    1. check if i there is enough data for the length field (4byte)
>    2. read the length of the appended data
>    3. check if there is enough data to read the compressed data
>    4. read and decompress the compressed data
>
>
> Here is my Code
>
> public class CompressionFilter extends WriteRequestFilter {
> @Override
>  public void messageReceived(NextFilter nextFilter, IoSession session,
> Object message) throws Exception {
> if (!(message instanceof IoBuffer)) {
>  nextFilter.messageReceived(session, message);
> return;
> }
>  IoBuffer inBuffer = (IoBuffer) message;
> boolean hasMoreData;
> do {
>  hasMoreData = decode(inBuffer, nextFilter, session);
> }
> while (hasMoreData);
>  }
> private boolean decode(IoBuffer inBuffer, NextFilter nextFilter, IoSession
> session) {
> int position = inBuffer.position();
>  final int remaining = inBuffer.remaining();
> if (remaining < 4) {
> inBuffer.position(position);
>  return false;
> }
> final int size = inBuffer.getInt();
>  if (size > remaining - 4) {
> inBuffer.position(position);
> return false;
>  }
> byte[] inBytes = new byte[size];
> inBuffer.get(inBytes);
>  final byte[] uncompress = uncompress(inBytes);
> IoBuffer outBuffer = IoBuffer.wrap(uncompress);
>  nextFilter.messageReceived(session, outBuffer);
> return true;
> }
>  private byte[] uncompress(byte[] compressed) {
> // return Snappy.uncompress(compressed);
> return compressed;
>  }
> @Override
> protected Object doFilterWrite(NextFilter nextFilter, IoSession session,
> WriteRequest writeRequest) throws IOException {
>  IoBuffer inBuffer = (IoBuffer) writeRequest.getMessage();
> if (!inBuffer.hasRemaining()) {
> // Ignore empty buffers
>  return null;
> } else {
> byte[] inBytes = new byte[inBuffer.remaining()];
>  inBuffer.get(inBytes).flip();
> byte[] outBytes = compress(inBytes);
> IoBuffer outBuf = IoBuffer.allocate(outBytes.length + 4);
>  outBuf.putInt(outBytes.length);
> outBuf.put(outBytes);
> outBuf.flip();
>  return outBuf;
> }
> }
> private byte[] compress(byte[] uncompressed) {
>  // return Snappy.compress(inBytes);
> return uncompressed;
> }
> }
>
>
常用算法小结(附Java实现) 常用算法小结(附Java实现)
常用排序算法小记

排序算法很多地方都会用到,近期又重新看了一遍算法,并自己简单地实现了一遍,特此记录下来,为以后复习留点材料。

废话不多说,下面逐一看看经典的排序算法:

 

1. 选择排序

选择排序的基本思想是遍历数组的过程中,以 i 代表当前需要排序的序号,则需要在剩余的 [i…n-1] 中找出其中的最小值,然后将找到的最小值与 i 指向的值进行交换。因为每一趟确定元素的过程中都会有一个选择最大值的子流程,所以人们形象地称之为选择排序。

举个实例来看看:

初始: [38, 17, 16, 16, 7, 31, 39, 32, 2, 11]

i = 0:  [2 , 17, 16, 16, 7, 31, 39, 32, 38 , 11] (0th [38]<->8th [2])

i = 1:  [2, 7 , 16, 16, 17 , 31, 39, 32, 38, 11] (1st [38]<->4th [17])

i = 2:  [2, 7, 11 , 16, 17, 31, 39, 32, 38, 16 ] (2nd [11]<->9th [16])

i = 3:  [2, 7, 11, 16, 17, 31, 39, 32, 38, 16] ( 无需交换 )

i = 4:  [2, 7, 11, 16, 16 , 31, 39, 32, 38, 17 ] (4th [17]<->9th [16])

i = 5:  [2, 7, 11, 16, 16, 17 , 39, 32, 38, 31 ] (5th [31]<->9th [17])

i = 6:  [2, 7, 11, 16, 16, 17, 31 , 32, 38, 39 ] (6th [39]<->9th [31])

i = 7:  [2, 7, 11, 16, 16, 17, 31, 32, 38, 39] ( 无需交换 )

i = 8:  [2, 7, 11, 16, 16, 17, 31, 32, 38, 39] ( 无需交换 )

i = 9:  [2, 7, 11, 16, 16, 17, 31, 32, 38, 39] ( 无需交换 )

由例子可以看出,选择排序随着排序的进行( i 逐渐增大),比较的次数会越来越少,但是不论数组初始是否有序,选择排序都会从 i 至数组末尾进行一次选择比较,所以给定长度的数组,选择排序的比较次数是固定的: 1 + 2 + 3 + …. + n = n * (n + 1) / 2 ,而交换的次数则跟初始数组的顺序有关,如果初始数组顺序为随机,则在最坏情况下,数组元素将会交换 n 次,最好的情况下则可能 0 次(数组本身即为有序)。

由此可以推出,选择排序的时间复杂度和空间复杂度分别为 O(n2 ) 和 O(1) (选择排序只需要一个额外空间用于数组元素交换)。

实现代码:

 
Java代码  收藏代码

    /** 
     * Selection Sorting 
     */  
    SELECTION(new Sortable() {  
        public <T extends Comparable<T>> void sort(T[] array, boolean ascend) {  
            int len = array.length;  
            for (int i = 0; i < len; i++) {  
                int selected = i;  
                for (int j = i + 1; j < len; j++) {  
                    int compare = array[j].compareTo(array[selected]);  
                    if (compare != 0 && compare < 0 == ascend) {  
                        selected = j;  
                    }  
                }  
      
                exchange(array, i, selected);  
            }  
        }  
    })  

 

 

 

2. 插入排序

插入排序的基本思想是在遍历数组的过程中,假设在序号 i 之前的元素即 [0..i-1] 都已经排好序,本趟需要找到 i 对应的元素 x 的正确位置 k ,并且在寻找这个位置 k 的过程中逐个将比较过的元素往后移一位,为元素 x “腾位置”,最后将 k 对应的元素值赋为 x ,插入排序也是根据排序的特性来命名的。

以下是一个实例,红色 标记的数字为插入的数字,被划掉的数字是未参与此次排序的元素,红色 标记的数字与被划掉数字之间的元素为逐个向后移动的元素,比如第二趟参与排序的元素为 [11, 31, 12] ,需要插入的元素为 12 ,但是 12 当前并没有处于正确的位置,于是我们需要依次与前面的元素 31 、 11 做比较,一边比较一边移动比较过的元素,直到找到第一个比 12 小的元素 11 时停止比较,此时 31 对应的索引 1 则是 12 需要插入的位置。

初始:    [11, 31, 12, 5, 34, 30, 26, 38, 36, 18]

第一趟: [11, 31 , 12, 5, 34, 30, 26, 38, 36, 18] (无移动的元素)

第二趟: [11, 12 , 31, 5, 34, 30, 26, 38, 36, 18] ( 31 向后移动)

第三趟: [5 , 11, 12, 31, 34, 30, 26, 38, 36, 18] ( 11, 12, 31 皆向后移动)

第四趟: [5, 11, 12, 31, 34 , 30, 26, 38, 36, 18] (无移动的元素)

第五趟: [5, 11, 12, 30 , 31, 34, 26, 38, 36, 18] ( 31, 34 向后移动)

第六趟: [5, 11, 12, 26 , 30, 31, 34, 38, 36, 18] ( 30, 31, 34 向后移动)

第七趟: [5, 11, 12, 26, 30, 31, 34, 38 , 36, 18] (无移动的元素)

第八趟: [5, 11, 12, 26, 30, 31, 34, 36 , 38, 18] ( 38 向后移动)

第九趟: [5, 11, 12, 18 , 26, 30, 31, 34, 36, 38] ( 26, 30, 31, 34, 36, 38 向后移动)

插入排序会优于选择排序,理由是它在排序过程中能够利用前部分数组元素已经排好序的一个优势,有效地减少一些比较的次数,当然这种优势得看数组的初始顺序如何,最坏的情况下(给定的数组恰好为倒序)插入排序需要比较和移动的次数将会等于 1 + 2 + 3… + n = n * (n + 1) / 2 ,这种极端情况下,插入排序的效率甚至比选择排序更差。因此插入排序是一个不稳定的排序方法,插入效率与数组初始顺序息息相关。一般情况下,插入排序的时间复杂度和空间复杂度分别为 O(n2 ) 和 O(1) 。

实现代码:

 
Java代码  收藏代码

    /** 
     * Insertion Sorting 
     */  
    INSERTION(new Sortable() {  
        public <T extends Comparable<T>> void sort(T[] array, boolean ascend) {  
            int len = array.length;  
            for (int i = 1; i < len; i++) {  
                T toInsert = array[i];  
                int j = i;  
                for (; j > 0; j--) {  
                    int compare = array[j - 1].compareTo(toInsert);  
                    if (compare == 0 || compare < 0 == ascend) {  
                        break;  
                    }  
                    array[j] = array[j - 1];  
                }  
      
                array[j] = toInsert;  
            }  
        }  
    })  

 

 

 

3. 冒泡排序

冒泡排序可以算是最经典的排序算法了,记得小弟上学时最先接触的也就是这个算法了,因为实现方法最简单,两层 for 循环,里层循环中判断相邻两个元素是否逆序,是的话将两个元素交换,外层循环一次,就能将数组中剩下的元素中最小的元素“浮”到最前面,所以称之为冒泡排序。

照例举个简单的实例吧:

初始状态:   [24, 19, 26, 39, 36, 7, 31, 29, 38, 23]

内层第一趟: [24, 19, 26, 39, 36, 7, 31, 29, 23 , 38 ] ( 9th [23]<->8th [38 )

内层第二趟: [24, 19, 26, 39, 36, 7, 31, 23 , 29 , 38] ( 8th [23]<->7th [29] )

内层第三趟: [24, 19, 26, 39, 36, 7, 23 , 31 , 29, 38] ( 7th [23]<->6th [31] )

内层第四趟: [24, 19, 26, 39, 36, 7, 23, 31, 29, 38] ( 7 、 23 都位于正确的顺序,无需交换)

内层第五趟: [24, 19, 26, 39, 7 , 36 , 23, 31, 29, 38] ( 5th [7]<->4th [36] )

内层第六趟: [24, 19, 26, 7 , 39 , 36, 23, 31, 29, 38] ( 4th [7]<->3rd [39] )

内层第七趟: [24, 19, 7 , 26 , 39, 36, 23, 31, 29, 38] ( 3rd [7]<->2nd [26] )

内层第八趟: [24, 7 , 19 , 26, 39, 36, 23, 31, 29, 38] ( 2nd [7]<->1st [19] )

内层第九趟: [7 , 24 , 19, 26, 39, 36, 23, 31, 29, 38] ( 1st [7]<->0th [24] )

…...

其实冒泡排序跟选择排序比较相像,比较次数一样,都为 n * (n + 1) / 2 ,但是冒泡排序在挑选最小值的过程中会进行额外的交换(冒泡排序在排序中只要发现相邻元素的顺序不对就会进行交换,与之对应的是选择排序,只会在内层循环比较结束之后根据情况决定是否进行交换),所以在我看来,选择排序属于冒泡排序的改进版。

实现代码:

 
Java代码  收藏代码

    /** 
     * Bubble Sorting, it's very similar with Insertion Sorting 
     */  
    BUBBLE(new Sortable() {  
        public <T extends Comparable<T>> void sort(T[] array, boolean ascend) {  
            int length = array.length;  
            int lastExchangedIdx = 0;  
            for (int i = 0; i < length; i++) {  
                // mark the flag to identity whether exchange happened to false  
                boolean isExchanged = false;  
                // last compare and exchange happened before reaching index i  
                int currOrderedIdx = lastExchangedIdx > i ? lastExchangedIdx : i;  
                for (int j = length - 1; j > currOrderedIdx; j--) {  
                    int compare = array[j - 1].compareTo(array[j]);  
                    if (compare != 0 && compare > 0 == ascend) {  
                        exchange(array, j - 1, j);  
                        isExchanged = true;  
                        lastExchangedIdx = j;  
                    }  
                }  
                // if no exchange happen means array is already in order  
                if (isExchanged == false) {  
                    break;  
                }  
            }  
        }  
    })  

 

 

 

4. 希尔排序

希尔排序的诞生是由于插入排序在处理大规模数组的时候会遇到需要移动太多元素的问题。希尔排序的思想是将一个大的数组“分而治之”,划分为若干个小的数组,以 gap 来划分,比如数组 [1, 2, 3, 4, 5, 6, 7, 8] ,如果以 gap = 2 来划分,可以分为 [1, 3, 5, 7] 和 [2, 4, 6, 8] 两个数组(对应的,如 gap = 3 ,则划分的数组为: [1, 4, 7] 、 [2, 5, 8] 、 [3, 6] )然后分别对划分出来的数组进行插入排序,待各个子数组排序完毕之后再减小 gap 值重复进行之前的步骤,直至 gap = 1 ,即对整个数组进行插入排序,此时的数组已经基本上快排好序了,所以需要移动的元素会很小很小,解决了插入排序在处理大规模数组时较多移动次数的问题。

具体实例请参照插入排序。

希尔排序是插入排序的改进版,在数据量大的时候对效率的提升帮助很大,数据量小的时候建议直接使用插入排序就好了。

实现代码:

 
Java代码  收藏代码

    /** 
     * Shell Sorting 
     */  
    SHELL(new Sortable() {  
        public <T extends Comparable<T>> void sort(T[] array, boolean ascend) {  
            int length = array.length;  
            int gap = 1;  
      
            // use the most next to length / 3 as the first gap  
            while (gap < length / 3) {  
                gap = gap * 3 + 1;  
            }  
      
            while (gap >= 1) {  
                for (int i = gap; i < length; i++) {  
                    T next = array[i];  
                    int j = i;  
                    while (j >= gap) {  
                        int compare = array[j - gap].compareTo(next);  
                        // already find its position  
                        if (compare == 0 || compare < 0 == ascend) {  
                            break;  
                        }  
      
                        array[j] = array[j - gap];  
                        j -= gap;  
                    }  
                    if (j != i) {  
                        array[j] = next;  
                    }  
                }  
                gap /= 3;  
            }  
      
        }  
    })  

 

 

 

5. 归并排序

归并排序采用的是递归来实现,属于“分而治之”,将目标数组从中间一分为二,之后分别对这两个数组进行排序,排序完毕之后再将排好序的两个数组“归并”到一起,归并排序最重要的也就是这个“归并”的过程,归并的过程中需要额外的跟需要归并的两个数组长度一致的空间,比如需要规定的数组分别为: [3, 6, 8, 11] 和 [1, 3, 12, 15] (虽然逻辑上被划为为两个数组,但实际上这些元素还是位于原来数组中的,只是通过一些 index 将其划分成两个数组,原数组为 [3, 6, 8, 11, 1, 3, 12, 15 ,我们设置三个指针 lo, mid, high 分别为 0,3,7 就可以实现逻辑上的子数组划分)那么需要的额外数组的长度为 4 + 4 = 8 。归并的过程可以简要地概括为如下:

1) 将两个子数组中的元素复制到新数组 copiedArray 中,以前面提到的例子为例,则 copiedArray = [3, 6, 8, 11, 1, 3, 12, 15] ;

2) 设置两个指针分别指向原子数组中对应的第一个元素,假定这两个指针取名为 leftIdx 和 rightIdx ,则 leftIdx = 0 (对应 copiedArray 中的第一个元素 [3] ), rightIdx = 4 (对应 copiedArray 中的第五个元素 [1] );

3) 比较 leftIdx 和 rightIdx 指向的数组元素值,选取其中较小的一个并将其值赋给原数组中对应的位置 i ,赋值完毕后分别对参与赋值的这两个索引做自增 1 操作,如果 leftIdx 或 rigthIdx 值已经达到对应数组的末尾,则余下只需要将剩下数组的元素按顺序 copy 到余下的位置即可。

下面给个归并的具体实例:

第一趟:

辅助数组 [21 , 28, 39 | 35, 38] (数组被拆分为左右两个子数组,以 | 分隔开)

[21 ,  ,  ,  ,  ] (第一次 21 与 35 比较 , 左边子数组胜出, leftIdx = 0 , i = 0 )

第二趟:

辅助数组 [21, 28 , 39 | 35, 38]

[21 , 28,  ,  ,  ] (第二次 28 与 35 比较,左边子数组胜出, leftIdx = 1 , i = 1 )

第三趟: [21, 28, 39 | 35 , 38]

 [21 , 28 , 35,  ,  ] (第三次 39 与 35 比较,右边子数组胜出, rightIdx = 0 , i = 2 )

第四趟: [21, 28, 39 | 35, 38 ]

 [21 , 28 , 35 , 38,  ] (第四次 39 与 38 比较,右边子数组胜出, rightIdx = 1 , i = 3 )

第五趟: [21, 28, 39 | 35, 38]

 [21 , 28 , 35 , 38 , 39] (第五次时右边子数组已复制完,无需比较 leftIdx = 2 , i = 4 )

以上便是一次归并的过程,我们可以将整个需要排序的数组做有限次拆分(每次一分为二)直到分为长度为 1 的小数组为止,长度为 1 时数组已经不用排序了。在这之后再逆序(由于采用递归)依次对这些数组进行归并操作,直到最后一次归并长度为 n / 2 的子数组,归并完成之后数组排序也完成。

归并排序需要的额外空间是所有排序中最多的,每次归并需要与参与归并的两个数组长度之和相同个元素(为了提供辅助数组)。则可以推断归并排序的空间复杂度为 1 + 2 + 4 + … + n = n * ( n + 2) / 4 (忽略了 n 的奇偶性的判断),时间复杂度比较难估,这里小弟也忘记是多少了(囧)。

实现代码:

 
Java代码  收藏代码

    /** 
     * Merge sorting 
     */  
    MERGE(new Sortable() {  
        public <T extends Comparable<T>> void sort(T[] array, boolean ascend) {  
            this.sort(array, 0, array.length - 1, ascend);  
        }  
      
        private <T extends Comparable<T>> void sort(T[] array, int lo, int hi, boolean ascend) {  
            // OPTIMIZE ONE  
            // if the substring's length is less than 20,  
            // use insertion sort to reduce recursive invocation  
            if (hi - lo < 20) {  
                for (int i = lo + 1; i <= hi; i++) {  
                    T toInsert = array[i];  
                    int j = i;  
                    for (; j > lo; j--) {  
                        int compare = array[j - 1].compareTo(toInsert);  
                        if (compare == 0 || compare < 0 == ascend) {  
                            break;  
                        }  
                        array[j] = array[j - 1];  
                    }  
      
                    array[j] = toInsert;  
                }  
      
                return;  
            }  
      
            int mid = lo + (hi - lo) / 2;  
            sort(array, lo, mid, ascend);  
            sort(array, mid + 1, hi, ascend);  
            merge(array, lo, mid, hi, ascend);  
        }  
      
        private <T extends Comparable<T>> void merge(T[] array, int lo, int mid, int hi, boolean ascend) {  
            // OPTIMIZE TWO  
            // if it is already in right order, skip this merge  
            // since there's no need to do so  
            int leftEndCompareToRigthStart = array[mid].compareTo(array[mid + 1]);  
            if (leftEndCompareToRigthStart == 0 || leftEndCompareToRigthStart < 0 == ascend) {  
                return;  
            }  
      
            @SuppressWarnings("unchecked")  
            T[] arrayCopy = (T[]) new Comparable[hi - lo + 1];  
            System.arraycopy(array, lo, arrayCopy, 0, arrayCopy.length);  
      
            int lowIdx = 0;  
            int highIdx = mid - lo + 1;  
      
            for (int i = lo; i <= hi; i++) {  
                if (lowIdx > mid - lo) {  
                    // left sub array exhausted  
                    array[i] = arrayCopy[highIdx++];  
                } else if (highIdx > hi - lo) {  
                    // right sub array exhausted  
                    array[i] = arrayCopy[lowIdx++];  
                } else if (arrayCopy[lowIdx].compareTo(arrayCopy[highIdx]) < 0 == ascend) {  
                    array[i] = arrayCopy[lowIdx++];  
                } else {  
                    array[i] = arrayCopy[highIdx++];  
                }  
            }  
        }  
    })  

 

 

 

6. 快速排序

快速排序也是用归并方法实现的一个“分而治之”的排序算法,它的魅力之处在于它能在每次 partition (排序算法的核心所在)都能为一个数组元素确定其排序最终正确位置(一次就定位准,下次循环就不考虑这个元素了)。

未完待续. . .
OGNL小应用-动态数学公式计算 OGNL小应用-动态数学公式计算
OGNL解析器:
Java代码  收藏代码

    import ognl.Ognl;  
    import ognl.OgnlContext;  
    import ognl.OgnlException;  
    public class OgnlExpression {  
    /* 表达式 */  
    private Object expression;  
    /* expressionString为OGNL表达式 */  
    public OgnlExpression(String expressionString) throws OgnlException {  
    super();  
    /* Ognl存放表达式,此时还没有解析 */  
    expression = Ognl.parseExpression(expressionString);  
    }  
    /* 获得表达式 */  
    public Object getExpression() {  
    return expression;  
    }  
    /* 获取Ognl结果 */  
    //参数1:上下文.  
    //参数2:对象  
    public Object getValue(OgnlContext context, Object rootObject)throws OgnlException {  
    /* 开始解析 */  
    return Ognl.getValue(getExpression(), context, rootObject);  
    }  
    }  



测试Servlet:
Java代码  收藏代码

    import java.io.IOException;  
    import javax.servlet.ServletException;  
    import javax.servlet.http.HttpServlet;  
    import javax.servlet.http.HttpServletRequest;  
    import javax.servlet.http.HttpServletResponse;  
    import ognl.OgnlContext;  
    public class Test extends HttpServlet {  
    public void doPost(HttpServletRequest request, HttpServletResponse response)  
    throws ServletException, IOException {  
    try {  
    // 创建OGNL上下文  
    OgnlContext context = new OgnlContext();  
    // 需要全类名  
    OgnlExpression expr = new OgnlExpression(request.getParameter("test"));  
    /* 指定表达式在指定上下文和根目录时返回的结果 */  
    System.out.println(expr.getValue(context, context));  
    System.out.println("表达式是:" + expr.getExpression());  
    } catch (Exception e) {  
    throw new RuntimeException();  
    }  
    }  
    }  


测试JSP:
Java代码  收藏代码

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
    <html>  
    <head>  
    <title>My JSP 'index.jsp' starting page</title>  
    </head>  
    <body>  
    <form action="test" method="post">  
    <input type="text" name="test" />  
    <input type="submit" />  
    </form>  
    </body>  
    </html>  


测试用例:
(1.12+13.4)/3.1*1.7
结果:
7.96258064516129 
Global site tag (gtag.js) - Google Analytics