You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SerializerHelper.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.util;
  17. import java.io.IOException;
  18. import java.io.ObjectInputStream;
  19. import java.io.ObjectOutputStream;
  20. /**
  21. * Helper class for performing serialization. Most of the methods are here are
  22. * workarounds for problems in Google App Engine. Used internally by Vaadin and
  23. * should not be used by application developers. Subject to change at any time.
  24. *
  25. * @since 6.0
  26. */
  27. public class SerializerHelper {
  28. /**
  29. * Serializes the class reference so {@link #readClass(ObjectInputStream)}
  30. * can deserialize it. Supports null class references.
  31. *
  32. * @param out
  33. * The {@link ObjectOutputStream} to serialize to.
  34. * @param cls
  35. * A class or null.
  36. * @throws IOException
  37. * Rethrows any IOExceptions from the ObjectOutputStream
  38. */
  39. public static void writeClass(ObjectOutputStream out, Class<?> cls)
  40. throws IOException {
  41. if (cls == null) {
  42. out.writeObject(null);
  43. } else {
  44. out.writeObject(cls.getName());
  45. }
  46. }
  47. /**
  48. * Serializes the class references so
  49. * {@link #readClassArray(ObjectInputStream)} can deserialize it. Supports
  50. * null class arrays.
  51. *
  52. * @param out
  53. * The {@link ObjectOutputStream} to serialize to.
  54. * @param classes
  55. * An array containing class references or null.
  56. * @throws IOException
  57. * Rethrows any IOExceptions from the ObjectOutputStream
  58. */
  59. public static void writeClassArray(ObjectOutputStream out,
  60. Class<?>[] classes) throws IOException {
  61. if (classes == null) {
  62. out.writeObject(null);
  63. } else {
  64. String[] classNames = new String[classes.length];
  65. for (int i = 0; i < classes.length; i++) {
  66. classNames[i] = classes[i].getName();
  67. }
  68. out.writeObject(classNames);
  69. }
  70. }
  71. /**
  72. * Deserializes a class references serialized by
  73. * {@link #writeClassArray(ObjectOutputStream, Class[])}. Supports null
  74. * class arrays.
  75. *
  76. * @param in
  77. * {@link ObjectInputStream} to read from.
  78. * @return Class array with the class references or null.
  79. * @throws ClassNotFoundException
  80. * If one of the classes could not be resolved.
  81. * @throws IOException
  82. * Rethrows IOExceptions from the ObjectInputStream
  83. */
  84. public static Class<?>[] readClassArray(ObjectInputStream in)
  85. throws ClassNotFoundException, IOException {
  86. String[] classNames = (String[]) in.readObject();
  87. if (classNames == null) {
  88. return null;
  89. }
  90. Class<?>[] classes = new Class<?>[classNames.length];
  91. for (int i = 0; i < classNames.length; i++) {
  92. classes[i] = resolveClass(classNames[i]);
  93. }
  94. return classes;
  95. }
  96. /**
  97. * List of primitive classes. Google App Engine has problems
  98. * serializing/deserializing these (#3064).
  99. */
  100. private static Class<?>[] primitiveClasses = new Class<?>[] { byte.class,
  101. short.class, int.class, long.class, float.class, double.class,
  102. boolean.class, char.class };
  103. /**
  104. * Resolves the class given by {@code className}.
  105. *
  106. * @param className
  107. * The fully qualified class name.
  108. * @return A {@code Class} reference.
  109. * @throws ClassNotFoundException
  110. * If the class could not be resolved.
  111. */
  112. public static Class<?> resolveClass(String className)
  113. throws ClassNotFoundException {
  114. for (Class<?> c : primitiveClasses) {
  115. if (className.equals(c.getName())) {
  116. return c;
  117. }
  118. }
  119. return Class.forName(className);
  120. }
  121. /**
  122. * Deserializes a class reference serialized by
  123. * {@link #writeClass(ObjectOutputStream, Class)}. Supports null class
  124. * references.
  125. *
  126. * @param in
  127. * {@code ObjectInputStream} to read from.
  128. * @return Class reference to the resolved class
  129. * @throws ClassNotFoundException
  130. * If the class could not be resolved.
  131. * @throws IOException
  132. * Rethrows IOExceptions from the ObjectInputStream
  133. */
  134. public static Class<?> readClass(ObjectInputStream in) throws IOException,
  135. ClassNotFoundException {
  136. String className = (String) in.readObject();
  137. if (className == null) {
  138. return null;
  139. } else {
  140. return resolveClass(className);
  141. }
  142. }
  143. }