zoukankan      html  css  js  c++  java
  • java开发中序列化与反序列化起到的作用

    基本概念:
    序列化是将对象状态转换为可保持或传输的格式的过程。与序列化相对的是反序列化,它将流转换为对象。

    这两个过程结合起来,能够轻松地存储和数据传输。




    特别在网络传输中,它的作用显得尤为重要。我们能够把一个类实现序列化,然后在还有一端通过反序列化能够得到该对象


    比如:我们能够序列化一个对象。只是这个对象要实现序列化方法,并生成序列化号。




    这是对一个对象进行序列化和反序列化的过程:

    public static byte[] serializeObj(Object object){
    		if (object == 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(object);
    			os.writeObject(null);
    			os.close();
    			bos.close();
    			rv = bos.toByteArray();
    		} catch (IOException e) {
    			throw new IllegalArgumentException("Non-serializable object", e);
    		} finally {
    			try {
    				if (os != null) {
    					os.flush();
    					os.close();
    				}
    				if (bos != null) {
    					bos.flush();
    					bos.close();
    				}
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    		return rv;
    	}
    	
    	@SuppressWarnings("unchecked")
    	public static <T>T deserializeObj(byte[] in,Class<T> clazz) {
    		ByteArrayInputStream bis = null;
    		ObjectInputStream is = null;
    		try {
    			if (in != null) {
    				bis = new ByteArrayInputStream(in);
    				is = new ObjectInputStream(bis);
    				while (true) {
    					T o = (T) is.readObject();
    					if (o == null) {
    						break;
    					} else {
    						return o;
    					}
    				}
    
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		} catch (ClassNotFoundException e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				if (is != null) {
    					is.close();
    				}
    				if (bis != null) {
    					bis.close();
    				}
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    		return null;
    	}

    以下是一个扩展(对集合的序列化和反序列化):

    public static <T>byte[] serializeList(List<T> value ,Class<T> clazz) {
    		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);
    			for (T t : value) {
    				os.writeObject(t);
    			}
    			os.writeObject(null);
    			os.close();
    			bos.close();
    			rv = bos.toByteArray();
    		} catch (IOException e) {
    			throw new IllegalArgumentException("Non-serializable object", e);
    		} finally {
    			try {
    				if (os != null) {
    					os.flush();
    					os.close();
    				}
    				if (bos != null) {
    					bos.flush();
    					bos.close();
    				}
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    		return rv;
    	}
    
    	@SuppressWarnings("unchecked")
    	public static <T> List<T> deserializeList(byte[] in,Class<T> clazz) {
    		List<T> list = new ArrayList<T>();
    		ByteArrayInputStream bis = null;
    		ObjectInputStream is = null;
    		try {
    			if (in != null) {
    				bis = new ByteArrayInputStream(in);
    				is = new ObjectInputStream(bis);
    				while (true) {
    					T o = (T) is.readObject();
    					if (o == null) {
    						break;
    					} else {
    						list.add(o);
    					}
    				}
    
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		} catch (ClassNotFoundException e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				if (is != null) {
    					is.close();
    				}
    				if (bis != null) {
    					bis.close();
    				}
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}


  • 相关阅读:
    北京燃气IC卡充值笔记
    随机分析、随机控制等科目在量化投资、计算金融方向有哪些应用?
    量化交易平台大全
    Doctor of Philosophy in Computational and Mathematical Engineering
    Institute for Computational and Mathematical Engineering
    Requirements for the Master of Science in Computational and Mathematical Engineering
    MSc in Mathematical and Computational Finance
    万字长文:详解多智能体强化学习的基础和应用
    数据处理思想和程序架构: 使用Mbedtls包中的SSL,和服务器进行网络加密通信
    31-STM32+W5500+AIR202/302基本控制篇-功能优化-W5500移植mbedtls库以SSL方式连接MQTT服务器(单向忽略认证)
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/7040223.html
Copyright © 2011-2022 走看看