PageRenderTime 25ms CodeModel.GetById 12ms app.highlight 10ms RepoModel.GetById 2ms app.codeStats 0ms

/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
 17package com.hazelcast.nio;
 18
 19import com.hazelcast.logging.ILogger;
 20import com.hazelcast.logging.Logger;
 21
 22import java.io.*;
 23import java.util.Date;
 24import java.util.logging.Level;
 25
 26public class SerializationHelper {
 27
 28    final static ILogger logger = Logger.getLogger(SerializationHelper.class.getName());
 29
 30    public static void writeObject(DataOutput out, Object obj) throws IOException {
 31        if (obj == null) {
 32            out.writeByte(0);
 33        } else if (obj instanceof Long) {
 34            out.writeByte(1);
 35            out.writeLong((Long) obj);
 36        } else if (obj instanceof Integer) {
 37            out.writeByte(2);
 38            out.writeInt((Integer) obj);
 39        } else if (obj instanceof String) {
 40            out.writeByte(3);
 41            out.writeUTF((String) obj);
 42        } else if (obj instanceof Double) {
 43            out.writeByte(4);
 44            out.writeDouble((Double) obj);
 45        } else if (obj instanceof Float) {
 46            out.writeByte(5);
 47            out.writeFloat((Float) obj);
 48        } else if (obj instanceof Boolean) {
 49            out.writeByte(6);
 50            out.writeBoolean((Boolean) obj);
 51        } else if (obj instanceof DataSerializable) {
 52            out.writeByte(7);
 53            out.writeUTF(obj.getClass().getName());
 54            ((DataSerializable) obj).writeData(out);
 55        } else if (obj instanceof Date) {
 56            out.writeByte(8);
 57            out.writeLong(((Date) obj).getTime());
 58        } else {
 59            out.writeByte(9);
 60            ByteArrayOutputStream bos = new ByteArrayOutputStream();
 61            ObjectOutputStream oos = new ObjectOutputStream(bos);
 62            oos.writeObject(obj);
 63            oos.close();
 64            byte[] buf = bos.toByteArray();
 65            out.writeInt(buf.length);
 66            out.write(buf);
 67        }
 68    }
 69
 70    public static Object readObject(DataInput in) throws IOException {
 71        byte type = in.readByte();
 72        if (type == 0) {
 73            return null;
 74        } else if (type == 1) {
 75            return in.readLong();
 76        } else if (type == 2) {
 77            return in.readInt();
 78        } else if (type == 3) {
 79            return in.readUTF();
 80        } else if (type == 4) {
 81            return in.readDouble();
 82        } else if (type == 5) {
 83            return in.readFloat();
 84        } else if (type == 6) {
 85            return in.readBoolean();
 86        } else if (type == 7) {
 87            DataSerializable ds;
 88            try {
 89                String className = in.readUTF();
 90                ds = (DataSerializable) Serializer.newInstance(Serializer.loadClass(className));
 91            } catch (Throwable e) {
 92                throw new IOException(e.getMessage());
 93            }
 94            ds.readData(in);
 95            return ds;
 96        } else if (type == 8) {
 97            return new Date(in.readLong());
 98        } else if (type == 9) {
 99            int len = in.readInt();
100            byte[] buf = new byte[len];
101            in.readFully(buf);
102            ObjectInputStream oin = AbstractSerializer.newObjectInputStream(new ByteArrayInputStream(buf));
103            try {
104                return oin.readObject();
105            } catch (ClassNotFoundException e) {
106                logger.log(Level.WARNING, e.getMessage(), e);
107            }
108            oin.close();
109        } else {
110            throw new IOException("Unknown object type=" + type);
111        }
112        return null;
113    }
114
115    public static void writeByteArray(DataOutput out, byte[] value) throws IOException {
116        int size = (value == null) ? 0 : value.length;
117        out.writeInt(size);
118        if (size > 0) {
119            out.write(value);
120        }
121    }
122
123    public static byte[] readByteArray(DataInput in) throws IOException {
124        int size = in.readInt();
125        if (size == 0) {
126            return null;
127        } else {
128            byte[] b = new byte[size];
129            in.readFully(b);
130            return b;
131        }
132    }
133}