1. /*
  2. * @(#)SerializationTester.java 1.5 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt.dnd;
  8. import java.io.OutputStream;
  9. import java.io.ObjectOutputStream;
  10. import java.io.IOException;
  11. import java.io.Serializable;
  12. /**
  13. * Tests if an object can truly be serialized by serializing it to a null
  14. * OutputStream.
  15. *
  16. * @version 1.5, 12/19/03
  17. * @since 1.4
  18. */
  19. final class SerializationTester {
  20. private static ObjectOutputStream stream;
  21. static {
  22. try {
  23. stream = new ObjectOutputStream(new OutputStream() {
  24. public void write(int b) {}
  25. });
  26. } catch (IOException cannotHappen) {
  27. }
  28. }
  29. static boolean test(Object obj) {
  30. if (!(obj instanceof Serializable)) {
  31. return false;
  32. }
  33. try {
  34. stream.writeObject(obj);
  35. } catch (IOException e) {
  36. return false;
  37. } finally {
  38. // Fix for 4503661.
  39. // Reset the stream so that it doesn't keep a reference to the
  40. // written object.
  41. try {
  42. stream.reset();
  43. } catch (IOException e) {
  44. // Ignore the exception.
  45. }
  46. }
  47. return true;
  48. }
  49. private SerializationTester() {}
  50. }