使用 try-with-resources 重写此代码。

huangapple go评论104阅读模式
英文:

Rewrite this code using try-with-resources

问题

  1. /**
  2. * Clone an instance through java serialization using try-with-resources
  3. * @return
  4. */
  5. public DeepConcretePrototype deepCloneBySerializable() {
  6. DeepConcretePrototype clone = null;
  7. try (
  8. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  9. ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
  10. ) {
  11. objectOutputStream.writeObject(this);
  12. objectOutputStream.flush();
  13. try (
  14. ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
  15. ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
  16. ) {
  17. clone = (DeepConcretePrototype) objectInputStream.readObject();
  18. } catch (ClassNotFoundException e) {
  19. e.printStackTrace();
  20. }
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }
  24. return clone;
  25. }
英文:

This code is used to copy an instance through java serialization. It uses the traditional try-catch-finally writing method. Can it be changed to try-with-resources form?(The DeepConcretePrototype in the code is an ordinary java object)

  1. /**
  2. * Clone an instance through java serialization
  3. * @return
  4. */
  5. public DeepConcretePrototype deepCloneBySerializable() {
  6. DeepConcretePrototype clone = null;
  7. ByteArrayOutputStream byteArrayOutputStream = null;
  8. ObjectOutputStream objectOutputStream = null;
  9. ByteArrayInputStream byteArrayInputStream = null;
  10. ObjectInputStream objectInputStream = null;
  11. try {
  12. //Output an instance to memory
  13. byteArrayOutputStream = new ByteArrayOutputStream();
  14. objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
  15. objectOutputStream.writeObject(this);
  16. objectOutputStream.flush();
  17. //Read instance from memory
  18. byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
  19. objectInputStream = new ObjectInputStream(byteArrayInputStream);
  20. clone = (DeepConcretePrototype)objectInputStream.readObject();
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. } catch (ClassNotFoundException e) {
  24. e.printStackTrace();
  25. } finally {
  26. if (byteArrayOutputStream != null) {
  27. try {
  28. byteArrayOutputStream.close();
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. if (objectOutputStream != null) {
  34. try {
  35. objectOutputStream.close();
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. if (byteArrayInputStream != null) {
  41. try {
  42. byteArrayInputStream.close();
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. if (objectInputStream != null) {
  48. try {
  49. objectInputStream.close();
  50. } catch (IOException e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. }
  55. return clone;
  56. }

答案1

得分: 0

是的,您可以使用try-with-resources,但这有点棘手,因为read的成功取决于write的成功。您可以使用嵌套的try语句来编写它:

  1. public DeepConcretePrototype deepCloneBySerializable() {
  2. DeepConcretePrototype clone = null;
  3. try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  4. ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream)) {
  5. // 将实例输出到内存
  6. objectOutputStream.writeObject(this);
  7. objectOutputStream.flush();
  8. try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
  9. ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream)) {
  10. // 从内存中读取实例
  11. clone = (DeepConcretePrototype) objectInputStream.readObject();
  12. }
  13. } catch (IOException e) {
  14. e.printStackTrace();
  15. } catch (ClassNotFoundException e) {
  16. e.printStackTrace();
  17. }
  18. return clone;
  19. }
英文:

Yes you can use try-with-resources, but it's a little tricky because the success of read depends on the success of write. One way you can write it is with a nested try:

  1. public DeepConcretePrototype deepCloneBySerializable() {
  2. DeepConcretePrototype clone = null;
  3. try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  4. ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream)) {
  5. //Output an instance to memory
  6. objectOutputStream.writeObject(this);
  7. objectOutputStream.flush();
  8. try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
  9. ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream)) {
  10. //Read instance from memory
  11. clone = (DeepConcretePrototype) objectInputStream.readObject();
  12. }
  13. } catch (IOException e) {
  14. e.printStackTrace();
  15. } catch (ClassNotFoundException e) {
  16. e.printStackTrace();
  17. }
  18. return clone;
  19. }

huangapple
  • 本文由 发表于 2020年8月19日 23:10:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63490006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定