- rn rv;
- }
- public static List<User> deserialize(byte[] in) {
- List<User> list = new ArrayList<User>();
- ByteArrayInputStream bis = null;
- ObjectInputStream is = null;
- try {
- if(in != null) {
- bis=new ByteArrayInputStream(in);
- is=new ObjectInputStream(bis);
- while (true) {
- User user = (User) is.readObject();
- if(user == null){
- break;
- }else{
- list.add(user);
- }
- }
- is.close();
- bis.close();
- }
- } catch (IOException e) {
- logger.warn("Caught IOException decoding %d bytes of data",
- in == null ? 0 : in.length, e);
- } catch (ClassNotFoundException e) {
- logger.warn("Caught CNFE decoding %d bytes of data",
- in == null ? 0 : in.length, e);
- } finally {
- CloseUtil.close(is);
- CloseUtil.close(bis);
- }
- return list;
- }
- }
- static class ListTranscoder{
- 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 (IOException e) {
- throw new IllegalArgumentException("Non-serializable object", e);
- } finally {
- close(os);
- close(bos);
- }
- return rv;
- }
- public static Object deserialize(byte[] in) {
- Object rv=null;
- ByteArrayInputStream bis = null;
- ObjectInputStream is = null;
- try {
- if(in != null) {
- bis=new ByteArrayInputStream(in);
- is=new ObjectInputStream(bis);
- rv=is.readObject();
- is.close();
- bis.close();
- }
- } catch (IOException e) {
- logger.warn("Caught IOException decoding %d bytes of data",
- in == null ? 0 : in.length, e);
- } catch (ClassNotFoundException e) {
- logger.warn("Caught CNFE decoding %d bytes of data",
- in == null ? 0 : in.length, e);
- } finally {
- CloseUtil.close(is);
- CloseUtil.close(bis);
- }
- return rv;
- }
- }
- }
PS:Redsi中存储list没有封装对Object的API,是不是也是倾向于只存储用到的字段,而不是存储Object本身呢?Redis是一个In-Mem的产品,会觉得我们应用的方式。
http://blog.csdn.net/gstormspire/article/details/7653095