root/trunk/LogicMail/src/org/logicprobe/LogicMail/util/SerializableVector.java

Revision 351, 6.7 kB (checked in by octorian, 4 weeks ago)

Comment scrubbing

Line 
1/*-
2 * Copyright (c) 2007, Derek Konigsberg
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the project nor the names of its
15 *    contributors may be used to endorse or promote products derived
16 *    from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32package org.logicprobe.LogicMail.util;
33
34import java.io.DataInputStream;
35import java.io.DataOutputStream;
36import java.io.IOException;
37import java.util.Vector;
38
39/**
40 * Provides an implementation of java.util.Vector that implements
41 * the LogicMail.util.Serializable interface.
42 * The maximum number of items that can be stored is 1000.
43 * This limit exists so that the deserialization code can quickly
44 * handle data corruption that could result in a bad size value.
45 */
46public class SerializableVector extends Vector implements Serializable {
47    final private static int TYPE_NULL    = 0;
48    final private static int TYPE_BOOLEAN = 1;
49    final private static int TYPE_BYTE    = 2;
50    final private static int TYPE_CHAR    = 3;
51    final private static int TYPE_STRING  = 4;
52    final private static int TYPE_DOUBLE  = 5;
53    final private static int TYPE_FLOAT   = 6;
54    final private static int TYPE_INT     = 7;
55    final private static int TYPE_LONG    = 8;
56    final private static int TYPE_SHORT   = 9;
57
58    final private static int MAX_ITEMS = 1000;
59   
60    private long uniqueId;
61   
62    /**
63     * Creates a new instance of SerializableVector.
64     * This class only supports vectors containing objects which
65     * wrap the various primitive types supported by DataOutputStream
66     * and DataInputStream.
67     */
68    public SerializableVector() {
69        super();
70        uniqueId = UniqueIdGenerator.getInstance().getUniqueId();
71    }
72
73    /**
74     * Creates a new instance of SerializableVector.
75     * This class only supports vectors containing objects which
76     * wrap the various primitive types supported by DataOutputStream
77     * and DataInputStream.
78     *
79     * @param initialCapacity Initial capacity of the vector.
80     */
81    public SerializableVector(int initialCapacity) {
82        super(initialCapacity);
83        uniqueId = UniqueIdGenerator.getInstance().getUniqueId();
84    }
85
86    private static void writeObject(DataOutputStream output, Object item) throws IOException {
87        if(item instanceof Boolean) {
88            output.write(TYPE_BOOLEAN);
89            output.writeBoolean(((Boolean)item).booleanValue());
90        }
91        else if(item instanceof Byte) {
92            output.write(TYPE_BYTE);
93            output.writeByte(((Byte)item).byteValue());
94        }
95        else if(item instanceof Character) {
96            output.write(TYPE_CHAR);
97            output.writeChar(((Character)item).charValue());
98        }
99        else if(item instanceof String) {
100            output.write(TYPE_STRING);
101            output.writeUTF(((String)item));
102        }
103        else if(item instanceof Double) {
104            output.write(TYPE_DOUBLE);
105            output.writeDouble(((Double)item).doubleValue());
106        }
107        else if(item instanceof Float) {
108            output.write(TYPE_FLOAT);
109            output.writeFloat(((Float)item).floatValue());
110        }
111        else if(item instanceof Integer) {
112            output.write(TYPE_INT);
113            output.writeInt(((Integer)item).intValue());
114        }
115        else if(item instanceof Long) {
116            output.write(TYPE_LONG);
117            output.writeLong(((Long)item).longValue());
118        }
119        else if(item instanceof Short) {
120            output.write(TYPE_SHORT);
121            output.writeShort(((Short)item).shortValue());
122        }
123        else {
124            output.write(TYPE_NULL);
125            output.write(0);
126        }
127    }
128
129    private static Object readObject(DataInputStream input) throws IOException {
130        int type = input.read();
131        switch(type) {
132            case TYPE_BOOLEAN:
133                return new Boolean(input.readBoolean());
134            case TYPE_BYTE:
135                return new Byte(input.readByte());
136            case TYPE_CHAR:
137                return new Character(input.readChar());
138            case TYPE_STRING:
139                return input.readUTF();
140            case TYPE_DOUBLE:
141                return new Double(input.readDouble());
142            case TYPE_FLOAT:
143                return new Float(input.readFloat());
144            case TYPE_INT:
145                return new Integer(input.readInt());
146            case TYPE_LONG:
147                return new Long(input.readLong());
148            case TYPE_SHORT:
149                return new Short(input.readShort());
150            case TYPE_NULL:
151                return null;
152            default:
153                return null;
154        }
155    }
156
157    public void serialize(DataOutputStream output) throws IOException {
158        output.writeLong(uniqueId);
159        int size = this.size();
160        output.writeInt(size);
161        for(int i=0; i<size; ++i) {
162            writeObject(output, this.elementAt(i));
163        }
164    }
165
166    public void deserialize(DataInputStream input) throws IOException {
167        this.removeAllElements();
168        uniqueId = input.readLong();
169        int size = input.readInt();
170        if(size > MAX_ITEMS) {
171            throw new IOException();
172        }
173       
174        for(int i=0; i<size; i++) {
175            this.addElement(readObject(input));
176        }
177    }
178
179    public long getUniqueId() {
180        return uniqueId;
181    }   
182}
Note: See TracBrowser for help on using the browser.