Java tutorial
/* * Copyright 1998-2012 360buy.com All right reserved. This software is the confidential and proprietary information of * 360buy.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only * in accordance with the terms of the license agreement you entered into with 360buy.com. */ package com.lxht.emos.data.cache.intf; import com.caucho.hessian.io.HessianInput; import com.caucho.hessian.io.HessianOutput; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import java.io.*; import java.util.*; import java.util.Map.Entry; //import com.lxht.emos.data.publiccommon.service.CommonDbService; /** * HessianUtil.java??hessian????? * * @author liulin 2012-11-29 ?10:55:02 */ @Controller @RequestMapping("/HessianUtil") public class HessianUtil { private static final Logger logger = Logger.getLogger(HessianUtil.class); //@Autowired // public CommonDbService commonDbService; /** * ?. * * @param obj ?? * @param fileName ????. * @throws IOException */ public static void serialize(Object obj, String fileName) throws IOException { if (obj == null) throw new NullPointerException(); File file = new File(fileName); // ?. if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } FileOutputStream fos = new FileOutputStream(file); HessianOutput ho = new HessianOutput(fos); ho.writeObject(obj); ho.flush(); ho.close(); fos.close(); } /** * ??. * * @param obj * @return * @throws IOException */ public static byte[] serialize(Serializable obj) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(1024); HessianOutput ho = new HessianOutput(os); ho.writeObject(obj); return os.toByteArray(); } /** * MapValue?key???? * * @param map * @return * @throws IOException */ public static Map<byte[], byte[]> serialize(Map<String, Serializable> map) throws IOException { if (map == null) { throw new NullPointerException(); } Map<byte[], byte[]> nMap = new HashMap<byte[], byte[]>(); for (Entry<String, Serializable> entry : map.entrySet()) { nMap.put(entry.getKey().getBytes(), serialize(entry.getValue())); } return nMap; } /** * MapValue?key???? * * @param map * @return * @throws IOException */ public static Map<Double, byte[]> serializeZ(Map<String, Serializable> map) throws IOException { if (map == null) { throw new NullPointerException(); } Map<Double, byte[]> nMap = new HashMap<Double, byte[]>(); for (Entry<String, Serializable> entry : map.entrySet()) { nMap.put(Double.parseDouble(entry.getKey()), serialize(entry.getValue())); } return nMap; } /** * ???. * * @param by ?? * @return object ? * @throws IOException */ public static <T extends Object> T deserialize(byte[] by) throws IOException { if (by == null) { throw new NullPointerException(); } ByteArrayInputStream is = new ByteArrayInputStream(by); HessianInput hi = new HessianInput(is); Object obj = hi.readObject(); hi.close(); is.close(); return (T) obj; } /** * ?bean. * * @param rlist * @return * @throws IOException */ public static <T extends Object> List<T> deserialize(List<byte[]> rlist) throws IOException { if (rlist == null) { throw new NullPointerException(); } List<T> list = new ArrayList<T>(); for (byte[] bs : rlist) { if (bs == null) { list.add(null); continue; } T obj = deserialize(bs); list.add(obj); } return list; } /** * ??bean. * * @param * @return * @throws IOException */ public static <T extends Object> List<T> deserializeZ(Set<byte[]> rSet) throws IOException { if (rSet == null) { throw new NullPointerException(); } List<T> list = new ArrayList<T>(); for (byte[] bs : rSet) { if (bs == null) { list.add(null); continue; } T obj = deserialize(bs); list.add(obj); } return list; } /** * ??bean. * * @param * @return * @throws IOException */ public static <T extends Object> List<T> deserializeCollection(Collection<byte[]> rSet) throws IOException { if (rSet == null) { throw new NullPointerException(); } List<T> list = new ArrayList<T>(); for (byte[] bs : rSet) { if (bs == null) { list.add(null); continue; } T obj = deserialize(bs); list.add(obj); } return list; } /** * ???Map * * @param map * @return * @throws IOException */ public static Map<String, Serializable> deserialize(Map<byte[], byte[]> map) throws IOException { if (map == null) { throw new NullPointerException(); } Map<String, Serializable> dMap = new HashMap<String, Serializable>(); for (Entry<byte[], byte[]> entry : map.entrySet()) { Serializable obj = deserialize(entry.getValue()); dMap.put(new String(entry.getKey()), obj); } return dMap; } /** * ????. * * @param fileName ????. * @return object ? * @throws IOException */ public static Object deserialize(String fileName) throws IOException { if (fileName == null) throw new NullPointerException(); FileInputStream fis = new FileInputStream(new File(fileName)); HessianInput hi = new HessianInput(fis); Object obj = hi.readObject(); hi.close(); fis.close(); return obj; } /** * path * * @param path * @return */ public static File createFile(String path) { File file = new File(path); // ? File parent = new File( file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf(File.separator))); // ? if (!parent.exists()) { createFile(parent.getPath()); // parent.mkdirs(); } return file; } /** * ??,?,?????. * * @param objectFileDirectory ? * @param fileName ??????? * @return boolean ??, true:?,false:. */ public static boolean removeSerializeLikeFile(String objectFileDirectory, final String fileName) { if (StringUtils.isBlank(objectFileDirectory) || StringUtils.isBlank(fileName)) { return false; } // ?File. File objDirFile = getObjectFileDir(objectFileDirectory); // ???? File[] objFiles = objDirFile.listFiles(new FilenameFilter() { public boolean accept(File dir, String name) { if (StringUtils.isBlank(name)) { return false; } /** * ??. */ return StringUtils.startsWith(name, fileName); } }); if (ArrayUtils.isEmpty(objFiles)) { return false; } // ?. for (File file : objFiles) { boolean isDelSuc = file.delete(); if (!isDelSuc) { file.deleteOnExit(); logger.warn("removeSerializeLikeFile(),Delete the local cache file failed.[File:" + file + "]"); continue; } } return true; } /** * ??. * * @param strArray * @return */ public static final byte[][] strArrayToByteArray(String[] strArray) { if (ArrayUtils.isEmpty(strArray)) { throw new NullPointerException(); } byte[][] bytes = new byte[strArray.length][]; int i = 0; for (String str : strArray) { bytes[i] = str.getBytes(); i++; } return bytes; } /** * ?map,??K,V = [k,v,k,v] * * @param map * @return * @throws IOException */ public static final byte[][] mapToByteArray(Map<String, Serializable> map) throws IOException { if (map == null || map.isEmpty()) { throw new NullPointerException("mapToByteArray is null."); } byte[][] bytes = new byte[map.size() * 2][]; int i = 0; for (Entry<String, Serializable> entry : map.entrySet()) { bytes[i] = entry.getKey().getBytes(); bytes[i + 1] = serialize(entry.getValue()); i = i + 2; } return bytes; } /** * ?. * * @param objectFileDirectory . * @return file . */ private static final File getObjectFileDir(String objectFileDirectory) { File objDirFile = new File(objectFileDirectory); if (objDirFile == null || !objDirFile.exists()) { return null; } return objDirFile; } public void test() { } }