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

📄 serializeutils.java

📁 一个用struts tiles的在线影院web系统
💻 JAVA
字号:
package com.blue.web.common.util;


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * The serialize utils, handle datas's serilize/deserilize operations.
 * @author Lucifer
 *
 */
public class SerializeUtils {

	/**
	 * Do not allow this class to be instantiated
	 *
	 */
	private SerializeUtils() {
	}

	/**
	 * Creates a Map from two string. The first contains the key pattern and the second 
	 * contains the values spaced according to the keys
	 * @param keys : Keys for the the map
	 * @param values : Values for the map 
	 * @return Map
	 */
	static public Map toMap(String keys, String values) {
		Map map = new HashMap();

		if (keys != null && values != null && keys.length() > 0) {
			String[] arrKeys = keys.split(":");
			
			for (int i = 0; i < (arrKeys.length / 4); i ++ ) {
				int start = Integer.parseInt(arrKeys[(i * 4) + 2]);
				int len = Integer.parseInt(arrKeys[(i * 4) + 3]);
				String key = arrKeys[i * 4];
				
				// Future version will support more complex types
				if (((arrKeys[(i * 4) + 1].equals("S")) && (start >= 0)) && (len > 0) && (values.length() >= (start + len))) {
					map.put(key, values.substring(start, start + len));
				}
			}
		}
		return map;
	}

	/**
	 * Create the SerializeDate object from an Map object
	 * @param map
	 * @param keys
	 * @param values
	 * @return
	 */
	static public SerializeData fromMap(Map map) {
		SerializeData data = new SerializeData();
		if (map == null || map.size() == 0)
			return data;
		
		StringBuffer sbKey = new StringBuffer();
		StringBuffer sbValue = new StringBuffer();
		
		int index = 0;
		
		Iterator iter = map.keySet().iterator();
		while (iter.hasNext()) {
			String key = (String) iter.next();
			if (key.indexOf(':') != -1)
				throw new IllegalArgumentException("Key can not contain the character \":\"");
			
			String v = (String) map.get(key);
			
			if (v != null && v.length() > 0) {
				sbKey.append(key + ":S:" + index + ":" + v.length() + ":");
				sbValue.append(v);
				index += v.length();
			}
		}

		data.setKeys(sbKey.toString());
		data.setValues(sbValue.toString());

		return data;
	}
}

⌨️ 快捷键说明

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