source: trunk/LogicMail/src/org/logicprobe/LogicMail/util/SerializableVector.java @ 601

Revision 601, 7.4 KB checked in by octorian, 2 years ago (diff)

Fix for some FindBugs issues

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.DataInput;
35import java.io.DataOutput;
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    private int hashcode = -1;
48
49    final private static int TYPE_NULL    = 0;
50    final private static int TYPE_BOOLEAN = 1;
51    final private static int TYPE_BYTE    = 2;
52    final private static int TYPE_CHAR    = 3;
53    final private static int TYPE_STRING  = 4;
54    final private static int TYPE_DOUBLE  = 5;
55    final private static int TYPE_FLOAT   = 6;
56    final private static int TYPE_INT     = 7;
57    final private static int TYPE_LONG    = 8;
58    final private static int TYPE_SHORT   = 9;
59
60    final private static int MAX_ITEMS = 1000;
61
62    private long uniqueId;
63
64    /**
65     * Creates a new instance of SerializableVector.
66     * This class only supports vectors containing objects which
67     * wrap the various primitive types supported by {@link DataOutput}
68     * and {@link DataInput}.
69     */
70    public SerializableVector() {
71        super();
72        uniqueId = UniqueIdGenerator.getInstance().getUniqueId();
73    }
74
75    /**
76     * Creates a new instance of SerializableVector.
77     * This class only supports vectors containing objects which
78     * wrap the various primitive types supported by {@link DataOutput}
79     * and {@link DataInput}.
80     *
81     * @param initialCapacity Initial capacity of the vector.
82     */
83    public SerializableVector(int initialCapacity) {
84        super(initialCapacity);
85        uniqueId = UniqueIdGenerator.getInstance().getUniqueId();
86    }
87
88    private static void writeObject(DataOutput output, Object item) throws IOException {
89        if(item instanceof Boolean) {
90            output.writeInt(TYPE_BOOLEAN);
91            output.writeBoolean(((Boolean)item).booleanValue());
92        }
93        else if(item instanceof Byte) {
94            output.writeInt(TYPE_BYTE);
95            output.writeByte(((Byte)item).byteValue());
96        }
97        else if(item instanceof Character) {
98            output.writeInt(TYPE_CHAR);
99            output.writeChar(((Character)item).charValue());
100        }
101        else if(item instanceof String) {
102            output.writeInt(TYPE_STRING);
103            output.writeUTF(((String)item));
104        }
105        else if(item instanceof Double) {
106            output.writeInt(TYPE_DOUBLE);
107            output.writeDouble(((Double)item).doubleValue());
108        }
109        else if(item instanceof Float) {
110            output.writeInt(TYPE_FLOAT);
111            output.writeFloat(((Float)item).floatValue());
112        }
113        else if(item instanceof Integer) {
114            output.writeInt(TYPE_INT);
115            output.writeInt(((Integer)item).intValue());
116        }
117        else if(item instanceof Long) {
118            output.writeInt(TYPE_LONG);
119            output.writeLong(((Long)item).longValue());
120        }
121        else if(item instanceof Short) {
122            output.writeInt(TYPE_SHORT);
123            output.writeShort(((Short)item).shortValue());
124        }
125        else {
126            output.writeInt(TYPE_NULL);
127            output.write(0);
128        }
129    }
130
131    private static Object readObject(DataInput input) throws IOException {
132        int type = input.readInt();
133        switch(type) {
134        case TYPE_BOOLEAN:
135            return new Boolean(input.readBoolean());
136        case TYPE_BYTE:
137            return new Byte(input.readByte());
138        case TYPE_CHAR:
139            return new Character(input.readChar());
140        case TYPE_STRING:
141            return input.readUTF();
142        case TYPE_DOUBLE:
143            return new Double(input.readDouble());
144        case TYPE_FLOAT:
145            return new Float(input.readFloat());
146        case TYPE_INT:
147            return new Integer(input.readInt());
148        case TYPE_LONG:
149            return new Long(input.readLong());
150        case TYPE_SHORT:
151            return new Short(input.readShort());
152        case TYPE_NULL:
153            return null;
154        default:
155            return null;
156        }
157    }
158
159    public void serialize(DataOutput output) throws IOException {
160        output.writeLong(uniqueId);
161        int size = this.size();
162        output.writeInt(size);
163        for(int i=0; i<size; ++i) {
164            writeObject(output, this.elementAt(i));
165        }
166    }
167
168    public void deserialize(DataInput input) throws IOException {
169        this.removeAllElements();
170        uniqueId = input.readLong();
171        int size = input.readInt();
172        if(size > MAX_ITEMS) {
173            throw new IOException();
174        }
175
176        for(int i=0; i<size; i++) {
177            this.addElement(readObject(input));
178        }
179    }
180
181    public long getUniqueId() {
182        return uniqueId;
183    }
184
185    /* (non-Javadoc)
186     * @see java.lang.Object#hashCode()
187     */
188    public int hashCode() {
189        if(hashcode == -1) {
190            hashcode = 31 * 1 + (int) (uniqueId ^ (uniqueId >>> 32));
191        }
192        return hashcode;
193    }
194
195    /* (non-Javadoc)
196     * @see java.lang.Object#equals(java.lang.Object)
197     */
198    public boolean equals(Object obj) {
199        if (this == obj) {
200            return true;
201        }
202        if (obj == null) {
203            return false;
204        }
205        if (getClass() != obj.getClass()) {
206            return false;
207        }
208        SerializableVector other = (SerializableVector) obj;
209        if (uniqueId != other.uniqueId) {
210            return false;
211        }
212        return true;
213    }
214}
Note: See TracBrowser for help on using the repository browser.