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 w w.ja v a 2 s .com import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.List; import com.anlong.msghandle.common.HandleStaticValue; import com.anlong.msghandle.request.BaseRequest; import com.anlong.msghandle.socket.InitMsgSocketServer; import com.anlong.msghandle.util.ByteAndInt; import com.anlong.msghandle.util.IMLog; import com.anlong.msghandle.util.ReflectionUtil; /** * @ClassName: RequestHandle * @Package: com.anlong.imsghandle.common * @company ShenZhen anlong Technology CO.,LTD. * @Description: TODO ?????????? * @author anlong * @date 2013-4-25 ????5:30:25 * @version V1.0 */ public class MsgRequestHandle{ private ByteArrayOutputStream byteOutputStream = null; private DataOutputStream dataOutputStream = null; private static MsgRequestHandle instance = null; public MsgRequestHandle(){} public static MsgRequestHandle getInstance(){ if ( instance == null ) instance = new MsgRequestHandle(); return instance; } /** * @Title: encode * @Description: TODO ???? * @author anlong * @param request * @param Exception * @throws */ public void encode(Object request) throws Exception { if(request instanceof BaseRequest){ // TODO ???????? OutputStream outputStream = InitMsgSocketServer.getOutputStream(); if (outputStream == null) return ; // ???????? BaseRequest baseRequest=(BaseRequest)request; baseRequest.setMsgSerial(0); // ?????? IMLog.anlong(baseRequest.toString()); byteOutputStream = new ByteArrayOutputStream(); dataOutputStream = new DataOutputStream(byteOutputStream); // TODO ??????? this.writeHead(baseRequest,dataOutputStream); // TODO ???????? this.wirteData(request,dataOutputStream); byte[] buf = byteOutputStream.toByteArray();//??????????????? IMLog.anlong("[??]??????"+buf.length); byte[] buff = toHH( buf.length ); for ( int i = 0 ; i < 4 ; i++ ){ buf[i] = buff[i]; } dataOutputStream.close(); byteOutputStream.close(); // TODO ????????????? outputStream.write(buf); outputStream.flush(); IMLog.anlong("?????????????!"); } else { IMLog.anlong("?????????"); } } /** * @Title: setRequestDataHead * @Description: TODO ?????????? * @author anlong * @param @param dataOutputStream * @param @param baseMessage * @param @return * @param @throws IOException ???? * @return DataOutputStream ???? * @throws */ public void writeHead(BaseRequest baseRequest, DataOutputStream dataOutputStream) throws IOException { // ???????????? ,??4 dataOutputStream.writeInt(baseRequest.getMsgSize()); // ????????? ,??4 dataOutputStream.writeShort(baseRequest.getBCode()); // ?????? ,??4 dataOutputStream.writeInt(baseRequest.getKey()); // ??????ID,??4 dataOutputStream.writeInt(baseRequest.getUid()); // ????????? dataOutputStream.writeByte(baseRequest.getApId()); // ?????????? dataOutputStream.writeInt(baseRequest.getMsgSerial()); } /** * @Title: WriteValue * @Description: TODO ??IO?????? * @author anlong * @param @param obj ???? * @return void ???? * @throws */ @SuppressWarnings({ "unchecked" }) private void wirteData(Object obj, DataOutputStream dataOutputStream){ try { // ????????? Object instance = obj.getClass().newInstance(); Field arrField = obj.getClass().getDeclaredField("fieldArr"); arrField.setAccessible(true); String[] fieldList = (String[]) arrField.get(instance); // ???? String fieldType = null; // ?????????? String fieldNameUpper = null; // ??????? Object result = null; for ( String fieldName : fieldList ) { Field field= obj.getClass().getDeclaredField(fieldName); fieldType = field.getType().getSimpleName(); fieldNameUpper = Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1); result = ReflectionUtil.invokeMethod(obj, "get"+fieldNameUpper); if( fieldType.equals("Byte") ){ if ( result != null ) dataOutputStream.writeByte((Byte)result); else dataOutputStream.writeByte((byte) 0); } else if ( fieldType.equals("Short") ){ if ( result != null ) dataOutputStream.writeShort((Short)result); else dataOutputStream.writeShort((short) 0); } else if ( fieldType.equals("Integer") ){ if ( result != null ) dataOutputStream.writeInt((Integer) result); else dataOutputStream.writeInt(0); } else if ( fieldType.equals("String") ){ if ( result != null ) { String strResult = (String)result; byte[] byteArr = strResult.getBytes(HandleStaticValue.CHARSET_NAME); dataOutputStream.writeShort((short) byteArr.length); dataOutputStream.write(byteArr); } else { dataOutputStream.writeShort((short) 0); } } else if (fieldType.equals("List")){ Type fc = field.getGenericType(); // ??????????? if( fc instanceof ParameterizedType ){ ParameterizedType pt = (ParameterizedType) fc; // ??????class????? Class<Object> genericClazz = (Class<Object>)pt.getActualTypeArguments()[0]; List<Object> list = (List<Object>) result; if (list != null) { // ????????? dataOutputStream.writeShort((short) list.size()); for(int i=0;i<list.size();i++){ // ???????? if( genericClazz.getSimpleName().equals("Byte") ){ dataOutputStream.writeByte((Byte) list.get(i)); }else if( genericClazz.getSimpleName().equals("Short") ){ dataOutputStream.writeShort((Short) list.get(i)); }else if( genericClazz.getSimpleName().equals("Integer") ){ dataOutputStream.writeInt((Integer) list.get(i)); }else if( genericClazz.getSimpleName().equals("String") ){ String str = (String) list.get(i); byte[] strByteArr = str.getBytes(HandleStaticValue.CHARSET_NAME); dataOutputStream.writeShort((short) strByteArr.length); dataOutputStream.write(strByteArr); }else{ // ??????? ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream(); DataOutputStream dataOutputStream2 = new DataOutputStream(byteArrayOutputStream2); // ??????? dataOutputStream2.writeShort((short) 0); wirteData(list.get(i), dataOutputStream2); // ???????? byte[] buf1 = byteArrayOutputStream2.toByteArray(); // ??????? //IMLog.anlong("???????????" + (buf1.length - 2)); byte[] buff = ByteAndInt.short2ByteArray((short) ( buf1.length - 2 )); for(int j=0;j<2;j++){ buf1[j] = buff[j]; } dataOutputStream.write(buf1, 0, buf1.length); dataOutputStream2.close(); byteArrayOutputStream2.close(); } } } else { dataOutputStream.writeShort((short) 0); } } }else{ //????????? wirteData(result, dataOutputStream); } } } catch (IOException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } } public static byte[] toHH(int n) { byte[] b = new byte[4]; b[3] = (byte) (n & 0xff); b[2] = (byte) (n >> 8 & 0xff); b[1] = (byte) (n >> 16 & 0xff); b[0] = (byte) (n >> 24 & 0xff); return b; } /*@Override public void handleMessageEvent(MessageEvent event) { // TODO ??????????????????? if (event != null && event.getbCode() == HandleStaticValue.BCODE1000){ IMLog.anlong("????,?????" + event.getbCode()); socket = null; isConn = true; } } static class getSocketTimer extends TimerTask { @Override public void run() { socket = InitServerManagerImpl.getInstance().getSocketIntance(); IMLog.anlong("[Request]Socket Instance?"+socket); } }*/ }