001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.io;
016    
017    import com.liferay.portal.kernel.util.CharBufferPool;
018    import com.liferay.portal.kernel.util.ClassLoaderObjectInputStream;
019    import com.liferay.portal.kernel.util.ClassLoaderPool;
020    
021    import java.io.IOException;
022    import java.io.InputStream;
023    import java.io.ObjectInputStream;
024    import java.io.Serializable;
025    
026    import java.nio.ByteBuffer;
027    
028    /**
029     * Deserializes data in a ClassLoader-aware manner. This class is the
030     * counterpart of {@link Serializer} for deserialization.
031     *
032     * @author Shuyang Zhou
033     * @see    Serializer
034     */
035    public class Deserializer {
036    
037            public Deserializer(ByteBuffer byteBuffer) {
038                    buffer = byteBuffer.array();
039                    index = byteBuffer.arrayOffset();
040                    limit = index + byteBuffer.remaining();
041            }
042    
043            public boolean readBoolean() {
044                    detectBufferUnderflow(1);
045    
046                    return BigEndianCodec.getBoolean(buffer, index++);
047            }
048    
049            public byte readByte() {
050                    detectBufferUnderflow(1);
051    
052                    return buffer[index++];
053            }
054    
055            public char readChar() {
056                    detectBufferUnderflow(2);
057    
058                    char c = BigEndianCodec.getChar(buffer, index);
059    
060                    index += 2;
061    
062                    return c;
063            }
064    
065            public double readDouble() {
066                    detectBufferUnderflow(8);
067    
068                    double d = BigEndianCodec.getDouble(buffer, index);
069    
070                    index += 8;
071    
072                    return d;
073            }
074    
075            public float readFloat() {
076                    detectBufferUnderflow(4);
077    
078                    float f = BigEndianCodec.getFloat(buffer, index);
079    
080                    index += 4;
081    
082                    return f;
083            }
084    
085            public int readInt() {
086                    detectBufferUnderflow(4);
087    
088                    int i = BigEndianCodec.getInt(buffer, index);
089    
090                    index += 4;
091    
092                    return i;
093            }
094    
095            public long readLong() {
096                    detectBufferUnderflow(8);
097    
098                    long l = BigEndianCodec.getLong(buffer, index);
099    
100                    index += 8;
101    
102                    return l;
103            }
104    
105            public <T extends Serializable> T readObject()
106                    throws ClassNotFoundException {
107    
108                    byte tcByte = buffer[index++];
109    
110                    switch (tcByte) {
111                            case SerializationConstants.TC_BOOLEAN:
112                                    return (T)Boolean.valueOf(readBoolean());
113    
114                            case SerializationConstants.TC_BYTE:
115                                    return (T)Byte.valueOf(readByte());
116    
117                            case SerializationConstants.TC_CHARACTER:
118                                    return (T)Character.valueOf(readChar());
119    
120                            case SerializationConstants.TC_DOUBLE:
121                                    return (T)Double.valueOf(readDouble());
122    
123                            case SerializationConstants.TC_FLOAT:
124                                    return (T)Float.valueOf(readFloat());
125    
126                            case SerializationConstants.TC_INTEGER:
127                                    return (T)Integer.valueOf(readInt());
128    
129                            case SerializationConstants.TC_LONG:
130                                    return (T)Long.valueOf(readLong());
131    
132                            case SerializationConstants.TC_NULL:
133                                    return null;
134    
135                            case SerializationConstants.TC_SHORT:
136                                    return (T)Short.valueOf(readShort());
137    
138                            case SerializationConstants.TC_STRING:
139                                    return (T)readString();
140    
141                            case SerializationConstants.TC_CONTEXT_NAME:
142                                    String contextName = readString();
143    
144                                    ClassLoader classLoader = ClassLoaderPool.getClassLoader(
145                                            contextName);
146    
147                                    try {
148                                            ObjectInputStream objectInpputStream =
149                                                    new ClassLoaderObjectInputStream(
150                                                            new BufferInputStream(), classLoader);
151    
152                                            T t = (T)objectInpputStream.readObject();
153    
154                                            objectInpputStream.close();
155    
156                                            return t;
157                                    }
158                                    catch (IOException ioe) {
159                                            throw new RuntimeException(ioe);
160                                    }
161    
162                            default :
163                                    throw new IllegalStateException("Unkown TC code " + tcByte);
164                    }
165            }
166    
167            public short readShort() {
168                    detectBufferUnderflow(2);
169    
170                    short s = BigEndianCodec.getShort(buffer, index);
171    
172                    index += 2;
173    
174                    return s;
175            }
176    
177            public String readString() {
178                    detectBufferUnderflow(5);
179    
180                    boolean asciiCode = BigEndianCodec.getBoolean(buffer, index++);
181    
182                    int length = BigEndianCodec.getInt(buffer, index);
183    
184                    index += 4;
185    
186                    if (asciiCode) {
187                            detectBufferUnderflow(length);
188    
189                            char[] chars = CharBufferPool.borrow(length);
190    
191                            for (int i = 0; i < length; i++) {
192                                    chars[i] = (char)buffer[index++];
193                            }
194    
195                            return new String(chars, 0, length);
196                    }
197                    else {
198                            detectBufferUnderflow(length * 2);
199    
200                            char[] chars = CharBufferPool.borrow(length);
201    
202                            for (int i = 0; i < length; i++) {
203                                    chars[i] = BigEndianCodec.getChar(buffer, index);
204    
205                                    index += 2;
206                            }
207    
208                            return new String(chars, 0, length);
209                    }
210            }
211    
212            /**
213             * Detects a buffer underflow throwing an {@link
214             * java.lang.IllegalStateException} if the input data is shorter than the
215             * reserved space. This method is final so JIT can perform an inline
216             * expansion.
217             *
218             * @param availableBytes number of bytes available in input buffer
219             */
220            protected final void detectBufferUnderflow(int availableBytes) {
221                    if ((index + availableBytes) > limit) {
222                            throw new IllegalStateException("Buffer underflow");
223                    }
224            }
225    
226            protected byte[] buffer;
227            protected int index;
228            protected int limit;
229    
230            protected class BufferInputStream extends InputStream {
231    
232                    @Override
233                    public int read() {
234                            return buffer[index++];
235                    }
236    
237                    @Override
238                    public int read(byte[] bytes) {
239                            return read(bytes, 0, bytes.length);
240                    }
241    
242                    @Override
243                    public int read(byte[] bytes, int offset, int length) {
244                            int remain = limit - index;
245    
246                            if (remain < length) {
247                                    length = remain;
248                            }
249    
250                            System.arraycopy(buffer, index, bytes, offset, length);
251    
252                            index += length;
253    
254                            return length;
255                    }
256    
257            }
258    
259    }