[关闭]
@weixin 2014-08-11T05:17:35.000000Z 字数 2250 阅读 1244

Apache common source code reading 1

java open-source code


SerializationUtils

  1. public static void serialize(final Serializable obj, final OutputStream outputStream) {
  2. if (outputStream == null) {
  3. throw new IllegalArgumentException("The OutputStream must not be null");
  4. }
  5. ObjectOutputStream out = null;
  6. try {
  7. // stream closed in the finally
  8. out = new ObjectOutputStream(outputStream);
  9. out.writeObject(obj);
  10. } catch (final IOException ex) {
  11. throw new SerializationException(ex);
  12. } finally {
  13. try {
  14. if (out != null) {
  15. out.close();
  16. }
  17. } catch (final IOException ex) { // NOPMD
  18. // ignore close exception
  19. }
  20. }
  21. }
  1. public static <T> T deserialize(final InputStream inputStream) {
  2. if (inputStream == null) {
  3. throw new IllegalArgumentException("The InputStream must not be null");
  4. }
  5. ObjectInputStream in = null;
  6. try {
  7. // stream closed in the finally
  8. in = new ObjectInputStream(inputStream);
  9. @SuppressWarnings("unchecked") // may fail with CCE if serialised form is incorrect
  10. final T obj = (T) in.readObject();
  11. return obj;
  12. } catch (final ClassCastException ex) {
  13. throw new SerializationException(ex);
  14. } catch (final ClassNotFoundException ex) {
  15. throw new SerializationException(ex);
  16. } catch (final IOException ex) {
  17. throw new SerializationException(ex);
  18. } finally {
  19. try {
  20. if (in != null) {
  21. in.close();
  22. }
  23. } catch (final IOException ex) { // NOPMD
  24. // ignore close exception
  25. }
  26. }
  27. }
  1. public void serializationUtilsDemo() {
  2. System.out.println ("*SerializationUtils**");
  3. Date date = new Date();
  4. byte[] bytes = SerializationUtils.serialize (date);
  5. System.out.println (ArrayUtils.toString (bytes) );
  6. System.out.println (date);
  7. Date reDate = (Date) SerializationUtils.deserialize (bytes);
  8. System.out.println (reDate);
  9. System.out.println (ObjectUtils.equals (date, reDate) );
  10. System.out.println (date == reDate);
  11. FileOutputStream fos = null;
  12. FileInputStream fis = null;
  13. try {
  14. fos = new FileOutputStream (new File ("./serialize_test.txt") );
  15. fis = new FileInputStream (new File ("./serialize_test.txt") );
  16. SerializationUtils.serialize (date, fos);
  17. Date reDate2 = (Date) SerializationUtils.deserialize (fis);
  18. System.out.println (date.equals (reDate2) );
  19. } catch (FileNotFoundException e) {
  20. e.printStackTrace();
  21. } finally {
  22. try {
  23. fos.close();
  24. fis.close();
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. }

the result is :

SerializationUtils*
{-84,-19,0,5,115,114,0,14,106,97,118,97,46,117,116,105,108,46,68,97,116,101,104,106,-127,1,75,89,116,25,3,0,0,120,112,119,8,0,0,1,71,-61,123,-20,-66,120}
Sun Aug 10 22:13:04 PDT 2014
Sun Aug 10 22:13:04 PDT 2014
true
false
true

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注