All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.wangshanhai.power.utils.SerializeUtils Maven / Gradle / Ivy

The newest version!
package com.wangshanhai.power.utils;

import org.springframework.util.StringUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * 自定义序列化类
 * @author Shmily
 */
public class SerializeUtils {
    /**
     * 序列化对象
     */
    public static String serialize(Object object) {
        return toHexString(serializeSource(object));
    }

    /**
     * 序列化对象
     */
    private static byte[] serializeSource(Object object) {
        ObjectOutputStream oos = null;
        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);
            oos.writeObject(object);
            byte[] bytes = baos.toByteArray();
            return bytes;
        } catch (Exception e) {
            Logger.error("[Shanhaipower-serializeSource-error]-msg:{}", e.getMessage());
        }
        return null;
    }

    /**
     * 反序列化对象
     */
    public static Object unserialize(String text) {
        return unserializeSoruce(toByteArray(text));
    }

    /**
     * 反序列化对象
     */
    private static Object unserializeSoruce(byte[] bytes) {
        ByteArrayInputStream bais = null;
        try {
            bais = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(bais);
            return ois.readObject();
        } catch (Exception e) {
            Logger.error("[Shanhaipower-unserializeSoruce-error]-msg:{}", e.getMessage());
        }
        return null;
    }

    /**
     * 字节数组转成16进制表示格式的字符串
     *
     * @param byteArray 需要转换的字节数组
     * @return 16进制表示格式的字符串
     **/
    public static String toHexString(byte[] byteArray) {
        if (byteArray == null || byteArray.length < 1) {
            throw new IllegalArgumentException("this byteArray must not be null or empty");
        }
        final StringBuilder hexString = new StringBuilder();
        for (int i = 0; i < byteArray.length; i++) {
            //0~F前面不零
            if ((byteArray[i] & 0xff) < 0x10) {
                hexString.append("0");
            }
            hexString.append(Integer.toHexString(0xFF & byteArray[i]));
        }
        return hexString.toString().toLowerCase();
    }

    public static byte[] toByteArray(String hexString) {
        if (StringUtils.isEmpty(hexString)) {
            throw new IllegalArgumentException("this hexString must not be empty");
        }
        hexString = hexString.toLowerCase();
        final byte[] byteArray = new byte[hexString.length() / 2];
        int k = 0;
        //因为是16进制,最多只会占用4位,转换成字节需要两个16进制的字符,高位在先
        for (int i = 0; i < byteArray.length; i++) {
            byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
            byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
            byteArray[i] = (byte) (high << 4 | low);
            k += 2;
        }
        return byteArray;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy