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 .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); } } }