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 ww w.j ava2 s.co m*/ import java.io.InputStream; import java.util.Timer; import java.util.TimerTask; import com.anlong.msghandle.common.HandleStaticValue; import com.anlong.msghandle.request.Request100; import com.anlong.msghandle.response.Response1000; import com.anlong.msghandle.socket.InitFileSocketServer; import com.anlong.msghandle.util.ByteAndInt; import com.anlong.msghandle.util.IMLog; import com.anlong.msghandle.util.Utils; public class ImageResponseHandle { // ??? private static Timer timer = null; // ?????????????? private static boolean timerCancel = false; // ?????????? private static long startTime = 0; // ?????????? private static long endTime = 0; // ?????? private static boolean timeOut = false; InputStream inputStream = null; // ??????????? int isSize = 0; public void ImageDecode(Object request){ try { if (request == null) return; if(request instanceof Request100){ IMLog.anlong("??????????..."); inputStream = InitFileSocketServer.getInputStream(); if (inputStream == null) return; // TODO ???????? Request100 request100 = (Request100)request; // ?????????,4??? int msgSize = 0; // ??????? getInstanceTimer(inputStream); while (!timerCancel) { //IMLog.anlong("?????????.."); // ??0.5? Thread.sleep(500); } // TODO ?????? if (timeOut){ IMLog.anlong("???????????!"); // TODO ???????????? try { Object obj = setResponse1000(request100, msgSize, (short)1); Utils.notifyMessage(obj,HandleStaticValue.BCODE1002); } catch (Exception e) { IMLog.anlong("?????????? " + HandleStaticValue.BCODE1002 + "!"); IMLog.anlong("???????????????????:" + HandleStaticValue.BCODE1002 + "!"); } // ??Socket?? closeFileSocket(); return; } // TODO ????????????????? byte[] dataSize = new byte[HandleStaticValue.PROTOCOL_SIZE]; if(inputStream.read(dataSize) == HandleStaticValue.PROTOCOL_SIZE){ msgSize = ByteAndInt.byteArray2Int(dataSize); IMLog.anlong("?????????????:" + msgSize); } dataSize = null; // TODO ?????? try { Object obj = setResponse1000(request100, msgSize, (short)0); Utils.notifyMessage(obj,HandleStaticValue.BCODE1002); } catch (Exception e) { IMLog.anlong("?????????? " + HandleStaticValue.BCODE1002 + "!"); IMLog.anlong("???????????????????:" + HandleStaticValue.BCODE1002 + "!"); } // ??Socket?? closeFileSocket(); }// end instance of } catch (Exception e) { e.printStackTrace(); } finally { if(InitFileSocketServer.getInstance() != null){ try { // TODO ??Socket InitFileSocketServer.closeSocketConnection(); } catch (Exception e2) { e2.printStackTrace(); } } } } /** * @Title: closeFileSocket * @Description: TODO ??Socket?? * @author anlong * @param * @return void * @throws */ private void closeFileSocket(){ try { if(InitFileSocketServer.getInstance() != null){ try { // TODO ??Socket InitFileSocketServer.closeSocketConnection(); } catch (Exception e2) { e2.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } } /** * @Title: setResponse1000 * @Description: TODO ????? * @author anlong * @param @param request100 * @param @param msgSize * @param @param rtCode * @param @return * @return Response1000 * @throws */ private Response1000 setResponse1000(Request100 request100,Integer msgSize,short rtCode){ try { Response1000 response1000 = new Response1000(); response1000.setFileCode(request100.getFileCode()); response1000.setFileSize(msgSize); response1000.setRtCode(rtCode); response1000.setSendType(request100.getSendType()); response1000.setImageType(request100.getImageType()); return response1000; } catch (Exception e) { e.printStackTrace(); } return null; } /** * @Title: notifyMessage * @Description: TODO ??????? * @author anlong * @throws */ private static void getInstanceTimer(InputStream inputStream){ if ( timer == null ){ timer = new Timer(); // ?????????? startTime = System.currentTimeMillis(); // ?1?????????,?????2? timer.schedule(new RunTask(inputStream), 1000, 2000); //timer.cancel(); } } /** * @Title: notifyMessage * @Description: TODO ??? * @author anlong * @throws */ static class RunTask extends TimerTask{ private InputStream inputStream1 = null; public RunTask(InputStream inputStream){ inputStream1 = inputStream; // ???????? endTime = System.currentTimeMillis(); // ??20??? if((endTime - startTime) > HandleStaticValue.SERVER_CONNECTION_TIMEOUT){ timeOut = true; timerCancel = true; timer.cancel(); } } @Override public void run(){ try { if ( inputStream1.available() != 0 ){ timerCancel = true; timer.cancel(); } } catch (Exception e) { e.printStackTrace(); } } } }