Back to project page msghandle.
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.
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; } }