Android Open Source - msghandle Msg Response Handle






From Project

Back to project page msghandle.

License

The source code is released under:

GNU General Public License

If you think the Android project msghandle listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.anlong.msghandle.handle;
/*from  w  ww.  j  av a 2  s .c  o m*/
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import android.util.Log;

import com.anlong.msghandle.common.HandleStaticValue;
import com.anlong.msghandle.event.MessageEvent;
import com.anlong.msghandle.event.MessageEventSource;
import com.anlong.msghandle.util.ByteAndInt;
import com.anlong.msghandle.util.IMLog;
import com.anlong.msghandle.util.ReflectionUtil;
import com.anlong.msghandle.util.Utils;

/**
 * @ClassName: ResponseHandle 
 * @Package: com.anlong.imsghandle.common
 * @company ShenZhen anlong Technology CO.,LTD.  
 * @Description: TODO  ??????????? 
 * @author anlong 
 * @date 2013-4-25 ????5:29:44 
 * @version V1.0
 */
public class MsgResponseHandle {
  private DataInputStream dataInputStream = null;
  private InputStream inputStream = null;
    private final static String TAG="MsgHandle";
  /**
   * @Title: decode 
   * @Description: TODO ???? 
   * @author anlong 
   * @throws
   */
  @SuppressWarnings("static-access")
  public void decode( InputStream stream ) throws Exception {
    try {
      // ??IO??
      inputStream = stream;
      if( inputStream == null ) {
        IMLog.anlong("IO????????????!");
        throw  new IOException();
      }

      // ??????????,??????????4???
      int limit = 0;

      // ?????????,????????4???
      int msgSize = 0;

      // ????????
      byte[] data = null;

      // ????IO???????????? 4???
      byte[] dataSize = new byte[HandleStaticValue.PROTOCOL_SIZE];

      if (inputStream.read(dataSize) == HandleStaticValue.PROTOCOL_SIZE){
        msgSize = ByteAndInt.byteArray2Int(dataSize);
        IMLog.anlong("??????????:" + msgSize);

        // ??????????????
        limit = msgSize - HandleStaticValue.PROTOCOL_SIZE;
        IMLog.anlong("?????????:" + limit + "=" + Utils.getFileSizeString((long)limit));

        // TODO ??10M??
        if (limit > 10000000) {
          IMLog.anlong("???????? " + limit + "??????!");
          
           throw  new IOException();
        }

        // ?????????????
        data = new byte[limit];
      } 

      // ????????????
      int len = 0;
      while(len < limit){
        len += inputStream.read(data,len,(limit - len));
        IMLog.anlong("???????:"+len);
      }

      if (data == null)
        throw new IOException();

      IMLog.anlong("??????????byte[]????"+data.length);
      dataInputStream = new DataInputStream(new ByteArrayInputStream(data));

      if(dataInputStream != null ){
        // ?????
        short bCode = dataInputStream.readShort();
        // ??
        int key = dataInputStream.readInt();
        // ??ID
        int uid = dataInputStream.readInt();
        // ?????
        short rtCode = dataInputStream.readShort();
        // ?????
        String rtMsg = dataInputStream.readUTF();
        // ??????????
        int msgSerial = dataInputStream.readInt();

        // ?????????
        IMLog.anlong("limit size:"+limit+" bCode:"+bCode+" key:"+key+" uid:"+uid+" rtCode:"+rtCode +" rtMsg:"+rtMsg+" msgSerial:"+msgSerial);

        if (bCode == 0) {
          IMLog.anlong("???????!");
          throw new IOException();
        }

        // ??????
        String requestPath = HandleStaticValue.RESPONSE_PACKEAGE +".Response"+bCode;
        Class requestClass = Class.forName(requestPath);

        // ????????
        Object obj = ReadValue(requestClass,dataInputStream);

        // ???????????
        ReflectionUtil.invokeMethod(obj, "init", msgSize,bCode,key,uid,rtCode,rtMsg,msgSerial);

        // ??????
        try {
          if (bCode != 0 && obj != null){
            MessageEvent event = new MessageEvent(bCode,obj);
            new MessageEventSource().getSingleton().notifyMessageEvent(event);
          }
        } catch (Exception e) {
          IMLog.anlong("?????????? " + bCode + "!");
          IMLog.anlong("???????????????????:" + bCode + "!");
          throw e;
        }
        
      } else {
        IMLog.anlong("?????????!");
        throw new Exception();
      }
      
    } catch (Exception e) {
      throw e;
    }
  }
 
  
  /**
   * ?????????IO?????????????
   * */
  @SuppressWarnings("unchecked")
  private Object ReadValue(Class<Object> clz,DataInputStream dataInputStream){
   
      // ??????
      Object obj = null;
      try {
        obj = clz.newInstance();
      } catch (InstantiationException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      } catch (IllegalAccessException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
      // ??????????List????
      Field arrField = null;
      try {
        arrField = obj.getClass().getDeclaredField("fieldArr");
      } catch (NoSuchFieldException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
      arrField.setAccessible(true);
      String[] fieldList = null;
      try {
        fieldList = (String[]) arrField.get(obj);
      } catch (IllegalArgumentException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      } catch (IllegalAccessException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }

      // ????
      String fieldType = null;
      // ??????????
      String fieldNameUpper = null;

      for ( String fieldName : fieldList ) {
        Field field = null;
        try {
          field = obj.getClass().getDeclaredField(fieldName);
        } catch (NoSuchFieldException e1) {
           Log.e(TAG, "??????????------------->"+fieldName);
          continue;
        }
        fieldType = field.getType().getSimpleName();
        fieldNameUpper = Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);

        if ( fieldType.equals("Byte") ){   
          /*byte val = dataInputStream.readByte();
          IMLog.anlong(fieldNameUpper + ": " + val);
          ReflectionUtil.invokeMethod(obj, "set"+fieldNameUpper, val);*/

          try {
            ReflectionUtil.invokeMethod(obj, "set"+fieldNameUpper, dataInputStream.readByte());
          } catch (Exception e) {
             Log.e(TAG, "?????--Byte--??->set"+fieldNameUpper);
              continue;
          }
        } else if ( fieldType.equals("Short") ){
          //          short val = dataInputStream.readShort();
          //          IMLog.anlong(fieldNameUpper + ": " + val);
          //          ReflectionUtil.invokeMethod(obj, "set"+fieldNameUpper, val);

          try {
            ReflectionUtil.invokeMethod(obj, "set"+fieldNameUpper, dataInputStream.readShort());
          } catch (Exception e) {
             Log.e(TAG, "?????--Short--??->set"+fieldNameUpper);
             continue;
          }
        } else if ( fieldType.equals("Integer") ){
          //          int val = dataInputStream.readInt();
          //          IMLog.anlong(fieldNameUpper + ": " + val);
          //          ReflectionUtil.invokeMethod(obj, "set"+fieldNameUpper, val);

          try {
            ReflectionUtil.invokeMethod(obj, "set"+fieldNameUpper, dataInputStream.readInt());
          } catch (Exception e) {
             Log.e(TAG, "?????--Integer--??->set"+fieldNameUpper);
               continue;
          }
        } else if ( fieldType.equals("String") ){
          String value = null;
          try {
            value = dataInputStream.readUTF();
          } catch (IOException e1) {
             Log.e(TAG, "?????--readUTF--??->set"+fieldNameUpper);
               continue;
          }  
          IMLog.anlong(fieldNameUpper + ": " + value);
          if(value.equals("????")){
            Log.e("debug", "-------------???????");
          }
          try {
            ReflectionUtil.invokeMethod(obj, "set"+fieldNameUpper,value);
          } catch (Exception e) {
             Log.e(TAG, "?????--String--??->set"+fieldNameUpper);
               continue;
          }

        } else if (fieldType.equals("List") ){  // ??????????
          // ????
          short arrSize = 0;
          try {
            arrSize = dataInputStream.readShort();
          } catch (IOException e) {
             Log.e(TAG, "???????????????--List--??->");
               continue;
          }
          Type fc = field.getGenericType(); 
          List<Object> list= new ArrayList<Object>();

          // ???????????
          if(fc instanceof ParameterizedType){
            ParameterizedType pt = (ParameterizedType) fc;
            // ??????class?????
            Class<Object> genericClazz = (Class<Object>)pt.getActualTypeArguments()[0]; 

            // ?????????????
            for (int i = 0; i < arrSize; i++) {
              
              try {
                // ???????? 
                if ( genericClazz.getSimpleName().equals("Byte") ){    
                  list.add(dataInputStream.readByte());

                } else if ( genericClazz.getSimpleName().equals("Short") ){
                  list.add(dataInputStream.readShort());

                } else if ( genericClazz.getSimpleName().equals("Integer") ){
                  list.add(dataInputStream.readInt());

                } else if ( genericClazz.getSimpleName().equals("String") ){
                  String str = dataInputStream.readUTF();
                  list.add(str);
                } else {  // ?????????
                  // ?????????
                  int byteCount = dataInputStream.readShort();
                  IMLog.anlong("[???]?????:" + byteCount);

                  byte[] objdata = new byte[byteCount];
                  dataInputStream.readFully(objdata, 0, byteCount);
                  DataInputStream dataInputStream1 = new DataInputStream(new ByteArrayInputStream(objdata));

                  // ????????????
                  list.add(ReadValue(genericClazz,dataInputStream1));
                }
              } catch (IOException e) {
                 Log.e(TAG, "???????????????--List--??->");
                 continue;
              }
              
            }

            // ??????list????
            Method method2 = null;
            try {
              method2 = clz.getMethod("set"+fieldNameUpper, List.class);
            } catch (NoSuchMethodException e1) {
               Log.e(TAG, "???????????list????--List--??->  set"+fieldNameUpper);
                 continue;
            }
            try {
              method2.invoke(obj,list);
            } catch (Exception e) {
              e.printStackTrace();
               continue;
            } 
          }    
        }else{  // ?????????
          String requestPath = HandleStaticValue.RESPONSE_VO+"."+fieldType;
          Class requestClass = null;
          try {
            requestClass = Class.forName(requestPath);
          } catch (ClassNotFoundException e) {
            Log.e(TAG, "??????????????----??->"+requestPath);
            e.printStackTrace();
             continue;
          }
          try {
            ReflectionUtil.invokeMethod(obj, "set"+fieldNameUpper, ReadValue(requestClass,dataInputStream));
          } catch (Exception e) {
              Log.e(TAG, "????????????????===???????->"+requestPath);
            e.printStackTrace();
             continue;
          }
        }
      }

      return obj;
    
  }

}




Java Source Code List

com.anlong.msghandle.common.BaseMessage.java
com.anlong.msghandle.common.HandleStaticValue.java
com.anlong.msghandle.common.HandleTimer.java
com.anlong.msghandle.common.MessageTimer.java
com.anlong.msghandle.event.MessageEventListener.java
com.anlong.msghandle.event.MessageEventSource.java
com.anlong.msghandle.event.MessageEvent.java
com.anlong.msghandle.file.InitImageFileServer.java
com.anlong.msghandle.handle.ImageRequestHandle.java
com.anlong.msghandle.handle.ImageResponseHandle.java
com.anlong.msghandle.handle.MsgRequestHandle.java
com.anlong.msghandle.handle.MsgResponseHandle.java
com.anlong.msghandle.impl.AppParamImpl.java
com.anlong.msghandle.impl.AppSystemMessageReportImpl.java
com.anlong.msghandle.impl.AuthenticationImpl.java
com.anlong.msghandle.impl.BroadcastImpl.java
com.anlong.msghandle.impl.CommonContactImpl.java
com.anlong.msghandle.impl.DepImpl.java
com.anlong.msghandle.impl.DepInfoListImpl.java
com.anlong.msghandle.impl.EditGroupImpl.java
com.anlong.msghandle.impl.EditGroupUserImpl.java
com.anlong.msghandle.impl.EditUserInfoImpl.java
com.anlong.msghandle.impl.ExitPreventGroupImpl.java
com.anlong.msghandle.impl.GetGroupInfoImpl.java
com.anlong.msghandle.impl.GetHistoryMessageImpl.java
com.anlong.msghandle.impl.GetUserInfoImpl105.java
com.anlong.msghandle.impl.GroupInfoListImpl.java
com.anlong.msghandle.impl.GroupShareImpl.java
com.anlong.msghandle.impl.HeartImpl103.java
com.anlong.msghandle.impl.LocationImpl.java
com.anlong.msghandle.impl.LoginImpl101.java
com.anlong.msghandle.impl.MessageStateReportImpl.java
com.anlong.msghandle.impl.OnlineStateImpl102.java
com.anlong.msghandle.impl.OnlineUserListImpl110.java
com.anlong.msghandle.impl.SendImageFile.java
com.anlong.msghandle.impl.SendMessageImpl203.java
com.anlong.msghandle.impl.SystemParaImpl.java
com.anlong.msghandle.impl.UnreadMessageImpl.java
com.anlong.msghandle.impl.UpdatePasswordImpl.java
com.anlong.msghandle.impl.UserInfoListImpl.java
com.anlong.msghandle.interfac.AbstractMsgHandle.java
com.anlong.msghandle.interfac.BaseActivity.java
com.anlong.msghandle.interfac.MessageHandleActivity.java
com.anlong.msghandle.interfac.MsgHandle.java
com.anlong.msghandle.message.InitRequMessageServer.java
com.anlong.msghandle.message.InitRespMessageServer.java
com.anlong.msghandle.request.BaseRequest.java
com.anlong.msghandle.request.Request100.java
com.anlong.msghandle.request.Request101.java
com.anlong.msghandle.request.Request102.java
com.anlong.msghandle.request.Request103.java
com.anlong.msghandle.request.Request105.java
com.anlong.msghandle.request.Request110.java
com.anlong.msghandle.request.Request203.java
com.anlong.msghandle.request.Request301.java
com.anlong.msghandle.response.BaseResponse.java
com.anlong.msghandle.response.Response1000.java
com.anlong.msghandle.response.Response1010.java
com.anlong.msghandle.response.Response1020.java
com.anlong.msghandle.response.Response1030.java
com.anlong.msghandle.response.Response1050.java
com.anlong.msghandle.response.Response1100.java
com.anlong.msghandle.response.Response2030.java
com.anlong.msghandle.response.Response3010.java
com.anlong.msghandle.service.InitServerManager.java
com.anlong.msghandle.service.ServerManager.java
com.anlong.msghandle.socket.InitFileSocketServer.java
com.anlong.msghandle.socket.InitMsgSocketServer.java
com.anlong.msghandle.util.ByteAndInt.java
com.anlong.msghandle.util.FormatTransfer.java
com.anlong.msghandle.util.IMLog.java
com.anlong.msghandle.util.ReflectionUtil.java
com.anlong.msghandle.util.Utils.java
com.anlong.msghandle.vo.AppMessage.java
com.anlong.msghandle.vo.Application.java
com.anlong.msghandle.vo.BroadcastMessage.java
com.anlong.msghandle.vo.DepInfo.java
com.anlong.msghandle.vo.GroupInfo.java
com.anlong.msghandle.vo.GroupShare.java
com.anlong.msghandle.vo.OnlineUser.java
com.anlong.msghandle.vo.ReportState.java
com.anlong.msghandle.vo.SimpleUserInfo.java
com.anlong.msghandle.vo.UserInfo.java
com.anlong.msghandle.vo.UserMessage.java