夢幻之旅

          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 閱讀(960) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 玉屏| 万载县| 武冈市| 灵宝市| 怀宁县| 通海县| 西吉县| 米林县| 拉萨市| 潼南县| 即墨市| 襄汾县| 太白县| 富宁县| 通化市| 黔西县| 延吉市| 青海省| 巴彦淖尔市| 克东县| 渑池县| 高邑县| 寿光市| 应城市| 巩留县| 缙云县| 甘洛县| 武定县| 裕民县| 区。| 突泉县| 田林县| 荣昌县| 县级市| 宜城市| 吉水县| 威宁| 商丘市| 五寨县| 贺兰县| 平舆县|