/hazelcast/src/main/java/com/hazelcast/nio/SerializationHelper.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 133 lines · 109 code · 9 blank · 15 comment · 54 complexity · 8af8665a69fa2a616419fc39858b1627 MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of 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,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.hazelcast.nio;
  17. import com.hazelcast.logging.ILogger;
  18. import com.hazelcast.logging.Logger;
  19. import java.io.*;
  20. import java.util.Date;
  21. import java.util.logging.Level;
  22. public class SerializationHelper {
  23. final static ILogger logger = Logger.getLogger(SerializationHelper.class.getName());
  24. public static void writeObject(DataOutput out, Object obj) throws IOException {
  25. if (obj == null) {
  26. out.writeByte(0);
  27. } else if (obj instanceof Long) {
  28. out.writeByte(1);
  29. out.writeLong((Long) obj);
  30. } else if (obj instanceof Integer) {
  31. out.writeByte(2);
  32. out.writeInt((Integer) obj);
  33. } else if (obj instanceof String) {
  34. out.writeByte(3);
  35. out.writeUTF((String) obj);
  36. } else if (obj instanceof Double) {
  37. out.writeByte(4);
  38. out.writeDouble((Double) obj);
  39. } else if (obj instanceof Float) {
  40. out.writeByte(5);
  41. out.writeFloat((Float) obj);
  42. } else if (obj instanceof Boolean) {
  43. out.writeByte(6);
  44. out.writeBoolean((Boolean) obj);
  45. } else if (obj instanceof DataSerializable) {
  46. out.writeByte(7);
  47. out.writeUTF(obj.getClass().getName());
  48. ((DataSerializable) obj).writeData(out);
  49. } else if (obj instanceof Date) {
  50. out.writeByte(8);
  51. out.writeLong(((Date) obj).getTime());
  52. } else {
  53. out.writeByte(9);
  54. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  55. ObjectOutputStream oos = new ObjectOutputStream(bos);
  56. oos.writeObject(obj);
  57. oos.close();
  58. byte[] buf = bos.toByteArray();
  59. out.writeInt(buf.length);
  60. out.write(buf);
  61. }
  62. }
  63. public static Object readObject(DataInput in) throws IOException {
  64. byte type = in.readByte();
  65. if (type == 0) {
  66. return null;
  67. } else if (type == 1) {
  68. return in.readLong();
  69. } else if (type == 2) {
  70. return in.readInt();
  71. } else if (type == 3) {
  72. return in.readUTF();
  73. } else if (type == 4) {
  74. return in.readDouble();
  75. } else if (type == 5) {
  76. return in.readFloat();
  77. } else if (type == 6) {
  78. return in.readBoolean();
  79. } else if (type == 7) {
  80. DataSerializable ds;
  81. try {
  82. String className = in.readUTF();
  83. ds = (DataSerializable) Serializer.newInstance(Serializer.loadClass(className));
  84. } catch (Throwable e) {
  85. throw new IOException(e.getMessage());
  86. }
  87. ds.readData(in);
  88. return ds;
  89. } else if (type == 8) {
  90. return new Date(in.readLong());
  91. } else if (type == 9) {
  92. int len = in.readInt();
  93. byte[] buf = new byte[len];
  94. in.readFully(buf);
  95. ObjectInputStream oin = AbstractSerializer.newObjectInputStream(new ByteArrayInputStream(buf));
  96. try {
  97. return oin.readObject();
  98. } catch (ClassNotFoundException e) {
  99. logger.log(Level.WARNING, e.getMessage(), e);
  100. }
  101. oin.close();
  102. } else {
  103. throw new IOException("Unknown object type=" + type);
  104. }
  105. return null;
  106. }
  107. public static void writeByteArray(DataOutput out, byte[] value) throws IOException {
  108. int size = (value == null) ? 0 : value.length;
  109. out.writeInt(size);
  110. if (size > 0) {
  111. out.write(value);
  112. }
  113. }
  114. public static byte[] readByteArray(DataInput in) throws IOException {
  115. int size = in.readInt();
  116. if (size == 0) {
  117. return null;
  118. } else {
  119. byte[] b = new byte[size];
  120. in.readFully(b);
  121. return b;
  122. }
  123. }
  124. }