⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socketwraphelper.java

📁 短信发送
💻 JAVA
字号:
/**
 * Created at Nov 20, 2008
 */
package com.jdev.net.protocol;

import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.Map;

import com.jdev.net.config.ServerCfg;
import com.jdev.util.Debug;

/**
 * <p>Title: SocketWrapHelper</p>
 * <p>Description: </p>
 * @author Lawrence
 * @version 1.0
 */
public class SocketWrapHelper {

	private final static String module = SocketWrapHelper.class.getName();

	public final static String ENCODING = "UTF-8";
	private final static String SPLIT = " Split_index";
	private final static String START = " Start_index";
	private final static String STOP = " Stop_index";
	private static byte[] BEGIN;
	private static byte[] END;
	
	/**
	 * 
	 */
	public SocketWrapHelper() {
		ServerCfg cfg = new ServerCfg();
		BEGIN = cfg.getBeginFlag();
		END = cfg.getEndFlag();
	}

	public static byte[] assembleContent(byte[] bytes) {
		byte[] result = null;
		try {
			int length = bytes.length + BEGIN.length + END.length;

			result = new byte[length];
			System.arraycopy(BEGIN, 0, result, 0, BEGIN.length);
			System.arraycopy(bytes, 0, result, BEGIN.length, bytes.length);
			System.arraycopy(END, 0, result, BEGIN.length + bytes.length, END.length);
			//
			
		} catch (Exception e) {
			Debug.logError(e, module);
		}
		return result;
	}

	public static byte[] getContent(byte[] bytes) throws Exception {
		try {

			// 自定义
			Map<Object, Object> map = parse(bytes);
			int start = ( (Integer) map.get(START)).intValue();
			int end = ((Integer)map.get(STOP)).intValue();
			int length = end - start;
			byte[] result = new byte[length];
			System.arraycopy(bytes, start, result, 0, length);
			//
			return result;
		} catch (Exception e) {
			throw new Exception(module + "getContent" + e);
		}
	}

	public static Map<Object, Object> parse(byte[] bytes) throws Exception {
		try {
			Map<Object, Object> map = new HashMap<Object, Object>();
			ByteArrayInputStream in = new ByteArrayInputStream(bytes);
			int count = 0;
			int index = 0;
			int start = 0;
			int end = 0;
			int word = -1;
			boolean begin = false;
			while ( (word = in.read()) != -1) {
				index++;
				if (word == (char)3|| word == (char)35) {
					start = index;
				}else if(word == (char)4 || word == (char)38){
					end = index;
					break;
				}
			}
			
			// 记录下第几个开始正文
			map.put(START, new Integer(start));
			map.put(STOP, new Integer(end));
			
			return map;
		} catch (Exception e) {
			throw new Exception(module + " parse() " + e);
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -