f1150baeb22334ca4b15e31519adece01ec51b33.svn-base 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package com.toolkit.copy;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.ObjectInputStream;
  6. import java.io.ObjectOutputStream;
  7. public abstract class BeanUtil{
  8. @SuppressWarnings("unchecked")
  9. public static <T> T cloneTo(T src) throws RuntimeException {
  10. ByteArrayOutputStream memoryBuffer = new ByteArrayOutputStream();
  11. ObjectOutputStream out = null;
  12. ObjectInputStream in = null;
  13. T dist = null;
  14. try {
  15. out = new ObjectOutputStream(memoryBuffer);
  16. out.writeObject(src);
  17. out.flush();
  18. in = new ObjectInputStream(new ByteArrayInputStream(memoryBuffer.toByteArray()));
  19. dist = (T) in.readObject();
  20. } catch (Exception e) {
  21. throw new RuntimeException(e);
  22. } finally {
  23. if (out != null)
  24. try {
  25. out.close();
  26. out = null;
  27. } catch (IOException e) {
  28. throw new RuntimeException(e);
  29. }
  30. if (in != null)
  31. try {
  32. in.close();
  33. in = null;
  34. } catch (IOException e) {
  35. throw new RuntimeException(e);
  36. }
  37. }
  38. return dist;
  39. }
  40. }