Android Open Source - msghandle Image Request 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  w w .j a  v  a 2s .  c o m
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.anlong.msghandle.common.HandleStaticValue;
import com.anlong.msghandle.request.Request100;
import com.anlong.msghandle.socket.InitFileSocketServer;
import com.anlong.msghandle.util.IMLog;
import com.anlong.msghandle.util.Utils;

/**
 * @ClassName: RequestImageFile 
 * @Package: com.anlong.imsghandle.file
 * @company ShenZhen anlong Technology CO.,LTD.  
 * @Description: TODO ???????????????
 * @author anlong 
 * @date 2013-5-23 ????6:55:05 
 * @version V1.0
 */
public class ImageRequestHandle {
  private ByteArrayOutputStream byteOutputStream = null;
  private DataOutputStream  dataOutputStream = null;
  
  public void ImageEncode(Object request){
    OutputStream outputStream = null;
    try {
      if (request == null)
        return;
      
      if(request instanceof Request100){
        // TODO ????????
        outputStream = InitFileSocketServer.getOutputStream();
        if (outputStream == null)
          return;
        // TODO ????????
        Request100 request100 = (Request100)request;
        
        // TODO ?????????????
        byteOutputStream = new ByteArrayOutputStream();
        dataOutputStream = new DataOutputStream(byteOutputStream);
        
        // TODO ????????????????
        byte[] imageBuf = HandleImageDataSteam(request100);
          
        // TODO ?????????????
        writeContent(request100, dataOutputStream);
        
        // TODO ???????????????
        if (imageBuf == null)
          byteOutputStream.write(0);
        else 
          byteOutputStream.write(imageBuf);
              
        // TODO ???????????????
        byte[] buf = byteOutputStream.toByteArray();
        IMLog.anlong("?????:"+buf);
        // TODO ?????????
        outputStream.write(buf);
        outputStream.flush();
        IMLog.anlong("????????!");
      }// end instance of
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      /*// TODO ????
      if(dataOutputStream != null){
        try {
          dataOutputStream.close();
        } catch (Exception e2) {
          e2.printStackTrace();
        }
      }
      if(byteOutputStream != null){
        try {
          byteOutputStream.close();
        } catch (Exception e2) {
          e2.printStackTrace();
        }
      }*/
    }
  }
  
  /**
   * @Title: HandleImageDataSteam 
   * @Description: TODO ????????????
   * @author anlong 
   * @param @param request100
   * @param @return     
   * @return byte[]     
   * @throws
   */
  private byte[] HandleImageDataSteam(Request100 request100){
    try {
      if (Utils.isNull(request100.getFileUrl()))
        return null;
      // TODO ????????????
      byte[] imageSteam = readFromSD(request100);
      
      if (imageSteam == null)
        return null;
      // TODO ?????????
      IMLog.anlong("?????????: " + imageSteam.length);
      request100.setFileSize(imageSteam.length);
      
      return imageSteam;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
  
  /**
   * @Title: readFromSD 
   * @Description: TODO ?SD??????????????????????
   * @author anlong 
   * @param @param dir
   * @param @param fileName
   * @param @return     
   * @return byte[]     
   * @throws
   */
   private byte[] readFromSD(Request100 request100){
    InputStream inputStream = null;
    try {
      File file = new File(request100.getFileUrl());
      if (!file.exists())
        return null;
      // TODO ????SD?????????
      inputStream = new BufferedInputStream(new FileInputStream(file));
      byte[] data = new byte[inputStream.available()];
        inputStream.read(data);
        return data;
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e1) {
      e1.printStackTrace();
    } finally {
      try {
        if( inputStream != null ){
             inputStream.close();
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return null;
   }


  
  /**
   * @Title: writeContent 
   * @Description: TODO ?????????????????
   * @author anlong 
   * @param @param request100
   * @param @param dataOutputStream     
   * @return void     
   * @throws
   */
  private void writeContent(Request100 request100, DataOutputStream dataOutputStream){
    try {
      dataOutputStream.writeByte(Utils.isNotNull(request100.getOperateType())?request100.getOperateType():0);
      wirteString(request100.getFileCode(), dataOutputStream);
      wirteString(request100.getFileType(), dataOutputStream);
      dataOutputStream.writeInt(Utils.isNotNull(request100.getFileSize())?request100.getFileSize():0);
      dataOutputStream.writeByte(Utils.isNotNull(request100.getImageType())?request100.getImageType():0);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  
  /**
   * @Title: wirteString 
   * @Description: TODO ?????????????
   * @author anlong 
   * @param @param str
   * @param @param dataOutputStream
   * @param @throws IOException     
   * @return void     
   * @throws
   */
  private void wirteString(String str, DataOutputStream dataOutputStream) throws IOException{
    if(Utils.isNotNull(str)){
      byte[] byteArr = str.getBytes(HandleStaticValue.CHARSET_NAME);
      dataOutputStream.writeShort((short) byteArr.length);
      dataOutputStream.write(byteArr);
    }else{
      dataOutputStream.writeShort((short) 0);
    }
  }
  
}




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