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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright 2000-2016 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.v7.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. * @deprecated Only used for compatibility-server
  27. */
  28. @Deprecated
  29. public class SerializerHelper {
  30. /**
  31. * Serializes the class reference so {@link #readClass(ObjectInputStream)}
  32. * can deserialize it. Supports null class references.
  33. *
  34. * @param out
  35. * The {@link ObjectOutputStream} to serialize to.
  36. * @param cls
  37. * A class or null.
  38. * @throws IOException
  39. * Rethrows any IOExceptions from the ObjectOutputStream
  40. */
  41. public static void writeClass(ObjectOutputStream out, Class<?> cls)
  42. throws IOException {
  43. if (cls == null) {
  44. out.writeObject(null);
  45. } else {
  46. out.writeObject(cls.getName());
  47. }
  48. }
  49. /**
  50. * Serializes the class references so
  51. * {@link #readClassArray(ObjectInputStream)} can deserialize it. Supports
  52. * null class arrays.
  53. *
  54. * @param out
  55. * The {@link ObjectOutputStream} to serialize to.
  56. * @param classes
  57. * An array containing class references or null.
  58. * @throws IOException
  59. * Rethrows any IOExceptions from the ObjectOutputStream
  60. */
  61. public static void writeClassArray(ObjectOutputStream out,
  62. Class<?>[] classes) throws IOException {
  63. if (classes == null) {
  64. out.writeObject(null);
  65. } else {
  66. String[] classNames = new String[classes.length];
  67. for (int i = 0; i < classes.length; i++) {
  68. classNames[i] = classes[i].getName();
  69. }
  70. out.writeObject(classNames);
  71. }
  72. }
  73. /**
  74. * Deserializes a class references serialized by
  75. * {@link #writeClassArray(ObjectOutputStream, Class[])}. Supports null
  76. * class arrays.
  77. *
  78. * @param in
  79. * {@link ObjectInputStream} to read from.
  80. * @return Class array with the class references or null.
  81. * @throws ClassNotFoundException
  82. * If one of the classes could not be resolved.
  83. * @throws IOException
  84. * Rethrows IOExceptions from the ObjectInputStream
  85. */
  86. public static Class<?>[] readClassArray(ObjectInputStream in)
  87. throws ClassNotFoundException, IOException {
  88. String[] classNames = (String[]) in.readObject();
  89. if (classNames == null) {
  90. return null;
  91. }
  92. Class<?>[] classes = new Class<?>[classNames.length];
  93. for (int i = 0; i < classNames.length; i++) {
  94. classes[i] = resolveClass(classNames[i]);
  95. }
  96. return classes;
  97. }
  98. /**
  99. * List of primitive classes. Google App Engine has problems
  100. * serializing/deserializing these (#3064).
  101. */
  102. private static final Class<?>[] primitiveClasses = new Class<?>[] {
  103. byte.class, short.class, int.class, long.class, float.class,
  104. double.class, boolean.class, char.class };
  105. /**
  106. * Resolves the class given by {@code className}.
  107. *
  108. * @param className
  109. * The fully qualified class name.
  110. * @return A {@code Class} reference.
  111. * @throws ClassNotFoundException
  112. * If the class could not be resolved.
  113. */
  114. public static Class<?> resolveClass(String className)
  115. throws ClassNotFoundException {
  116. for (Class<?> c : primitiveClasses) {
  117. if (className.equals(c.getName())) {
  118. return c;
  119. }
  120. }
  121. return Class.forName(className);
  122. }
  123. /**
  124. * Deserializes a class reference serialized by
  125. * {@link #writeClass(ObjectOutputStream, Class)}. Supports null class
  126. * references.
  127. *
  128. * @param in
  129. * {@code ObjectInputStream} to read from.
  130. * @return Class reference to the resolved class
  131. * @throws ClassNotFoundException
  132. * If the class could not be resolved.
  133. * @throws IOException
  134. * Rethrows IOExceptions from the ObjectInputStream
  135. */
  136. public static Class<?> readClass(ObjectInputStream in)
  137. throws IOException, ClassNotFoundException {
  138. String className = (String) in.readObject();
  139. if (className == null) {
  140. return null;
  141. } else {
  142. return resolveClass(className);
  143. }
  144. }
  145. private SerializerHelper() {
  146. }
  147. }