云中烛火

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

序列化的时候,SimpleByteSource类没有实现Serializable接口,导致序列化失败

SimpleByteSource没有默认构造方法,导致反序列化的时候失败

 

自定义ByteSource的实现类,模仿SimpleByteSource的方法

import org.apache.shiro.codec.Base64;
import org.apache.shiro.codec.CodecSupport;
import org.apache.shiro.codec.Hex;
import org.apache.shiro.util.ByteSource;

import java.io.File;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Arrays;

/**
 * 解决:
 *  shiro 使用缓存时出现:java.io.NotSerializableException: org.apache.shiro.util.SimpleByteSource
 *  序列化后,无法反序列化的问题
 * @author chennan
 * @date 2016/12/6 16:01
 */
public class MySimpleByteSource implements ByteSource, Serializable {
	private static final long serialVersionUID = 5175082362119580768L;

	private  byte[] bytes;
	private String cachedHex;
	private String cachedBase64;

	public MySimpleByteSource(){
	}

	public MySimpleByteSource(byte[] bytes) {
		this.bytes = bytes;
	}

	public MySimpleByteSource(char[] chars) {
		this.bytes = CodecSupport.toBytes(chars);
	}

	public MySimpleByteSource(String string) {
		this.bytes = CodecSupport.toBytes(string);
	}

	public MySimpleByteSource(ByteSource source) {
		this.bytes = source.getBytes();
	}

	public MySimpleByteSource(File file) {
		this.bytes = (new MySimpleByteSource.BytesHelper()).getBytes(file);
	}

	public MySimpleByteSource(InputStream stream) {
		this.bytes = (new MySimpleByteSource.BytesHelper()).getBytes(stream);
	}

	public static boolean isCompatible(Object o) {
		return o instanceof byte[] || o instanceof char[] || o instanceof String || o instanceof ByteSource || o instanceof File || o instanceof InputStream;
	}

	public void setBytes(byte[] bytes) {
		this.bytes = bytes;
	}

	@Override
	public byte[] getBytes() {
		return this.bytes;
	}


	@Override
	public String toHex() {
		if(this.cachedHex == null) {
			this.cachedHex = Hex.encodeToString(this.getBytes());
		}
		return this.cachedHex;
	}

	@Override
	public String toBase64() {
		if(this.cachedBase64 == null) {
			this.cachedBase64 = Base64.encodeToString(this.getBytes());
		}

		return this.cachedBase64;
	}

	@Override
	public boolean isEmpty() {
		return this.bytes == null || this.bytes.length == 0;
	}

	public String toString() {
		return this.toBase64();
	}

	public int hashCode() {
		return this.bytes != null && this.bytes.length != 0? Arrays.hashCode(this.bytes):0;
	}

	public boolean equals(Object o) {
		if(o == this) {
			return true;
		} else if(o instanceof ByteSource) {
			ByteSource bs = (ByteSource)o;
			return Arrays.equals(this.getBytes(), bs.getBytes());
		} else {
			return false;
		}
	}

	private static final class BytesHelper extends CodecSupport {
		private BytesHelper() {
		}

		public byte[] getBytes(File file) {
			return this.toBytes(file);
		}

		public byte[] getBytes(InputStream stream) {
			return this.toBytes(stream);
		}
	}

}

  

 

序列化的工具类:

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

/**
 * 序列化与反序列化
 */
@SuppressWarnings("unchecked")
public class SerializeUtil {

    public static byte[] serialize(Object value) {
        if (value == null) {
            throw new NullPointerException("Can't serialize null");
        }
        byte[] rv = null;
        ByteArrayOutputStream bos = null;
        ObjectOutputStream os = null;
        try {
            bos = new ByteArrayOutputStream();
            os = new ObjectOutputStream(bos);
            os.writeObject(value);
            os.close();
            bos.close();
            rv = bos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("serialize error");
        } finally {
            close(os);
            close(bos);
        }
        return rv;
    }

    
	public static Object deserialize(byte[] in) {
        return deserialize(in, Object.class);
    }

    public static <T> T deserialize(byte[] in, Class<T>...requiredType) {
        Object rv = null;
        ByteArrayInputStream bis = null;
        ObjectInputStream is = null;
        try {
            if (in != null) {
                bis = new ByteArrayInputStream(in);
                is = new ObjectInputStream(bis);
                rv = is.readObject();
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("deserialize error");
        } finally {
            close(is);
            close(bis);
        }
        return (T) rv;
    }

    private static void close(Closeable closeable) {
        if (closeable != null)
            try {
                closeable.close();
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("close stream error");
            }
    }

    public static void main(String[] args) {
        byte[] b = serialize("nanc-shiro-session:703de992-b1f7-4d77-a699-8fba7cdaf4ca");
        System.out.println(deserialize(b));
        System.out.println(b);
    }

}

  

 

posted on 2016-12-07 17:24  云中烛火  阅读(267)  评论(0)    收藏  举报