梦幻之旅

DEBUG - 天道酬勤

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  671 随笔 :: 6 文章 :: 256 评论 :: 0 Trackbacks
package org.upeng.mail.net.util;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.CumulativeProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;

public class MessageDecoder extends CumulativeProtocolDecoder
{
    
public static final int MAX_IMAGE_SIZE = 1024 * 1024 * 5;
    
    
private static final String DECODER_STATE_KEY = getKey();
    
    
protected boolean doDecode(IoSession session, IoBuffer in,
            ProtocolDecoderOutput out) 
throws Exception
    
{
        System.out.println(
"decode-开始");
        
        Context context 
= (Context) session.getAttribute(DECODER_STATE_KEY);
        
if(context == null)
        
{
            context 
= new Context();
            session.setAttribute(DECODER_STATE_KEY, context);
        }

        
        
while(true)
        
{
            
if (in.prefixedDataAvailable(4, MAX_IMAGE_SIZE))
            
{
                
int pieceLength = in.getInt();
                
int totalLength = in.getInt();
                context.setTotalLength(totalLength);
                
byte[] bytes = new byte[pieceLength - 4];
                in.get(bytes);
                context.addPiece(bytes);
                
if (context.isComplete())
                
{
                    Message message 
= new Message(context.getTotalData());
                    out.write(message);
                    session.removeAttribute(DECODER_STATE_KEY);
                    
return true;
                }

            }

            
else
            
{
                
return false;
            }

        }

        
//System.out.println("decode-结束");
        
//return false;
    }

    
    
/**
     * <ul>
     * <li>Description:[得到键值]</li>
     * <li>Created by [Huyvanpull] [2011-9-5]</li>
     * <li>Midified by [modifier] [modified time]</li>
     * </ul>
     * 
     * 
@return
     
*/

    
private static String getKey()
    
{
        
return MessageDecoder.class.getName() + ".STATE";
    }

}

2.
package org.upeng.mail.net.util;

import java.util.List;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

public class MessageEncoder extends ProtocolEncoderAdapter
{
    
public void encode(IoSession session, Object message,
            ProtocolEncoderOutput out) 
throws Exception
    
{
        System.out.println(
"encode-开始");
        Message msg 
= (Message) message;
        
        IoBuffer buffer 
= IoBuffer.allocate(1024).setAutoExpand(true);
        
        
// 定义上下文
        Context context = new Context(msg.getDataBody());
        List
<byte[]> pieceLst = context.getPieceLst();
        
for (int i=0; i<pieceLst.size(); i++)
        
{
            
byte[] temp = pieceLst.get(i);
            buffer.putInt(temp.length 
+ 4);
            buffer.putInt(msg.getDataBody().length);
            buffer.put(temp);
        }

        buffer.flip();
        out.write(buffer);
        out.flush();
        System.out.println(
"decode-结束");
    }

    
}



posted on 2011-09-05 19:17 HUIKK 阅读(943) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: