/*
 * JBoss, Home of Professional Open Source
 *
 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
 * by the @author tags. See the COPYRIGHT.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.netty.handler.codec.serialization;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;
import java.io.StreamCorruptedException;

/**
 * @author The Netty Project (netty-dev@lists.jboss.org)
 * @author Trustin Lee (tlee@redhat.com)
 *
 * @version $Rev$, $Date$
 *
 */
class CompactObjectInputStream extends ObjectInputStream {

    private final ClassLoader classLoader;

    CompactObjectInputStream(InputStream in) throws IOException {
        this(in, null);
    }

    CompactObjectInputStream(InputStream in, ClassLoader classLoader) throws IOException {
        super(in);
        this.classLoader = classLoader;
    }

    @Override
    protected void readStreamHeader() throws IOException,
            StreamCorruptedException {
        int version = readByte() & 0xFF;
        if (version != STREAM_VERSION) {
            throw new StreamCorruptedException(
                    "Unsupported version: " + version);
        }
    }

    @Override
    protected ObjectStreamClass readClassDescriptor()
            throws IOException, ClassNotFoundException {
        int type = read();
        if (type < 0) {
            throw new EOFException();
        }
        switch (type) {
        case CompactObjectOutputStream.TYPE_PRIMITIVE:
            return super.readClassDescriptor();
        case CompactObjectOutputStream.TYPE_NON_PRIMITIVE:
            String className = readUTF();
            Class<?> clazz;
            if (classLoader == null) {
                clazz = Class.forName(
                        className, true,
                        Thread.currentThread().getContextClassLoader());
            } else {
                clazz = Class.forName(className, true, classLoader);
            }
            return ObjectStreamClass.lookup(clazz);
        default:
            throw new StreamCorruptedException(
                    "Unexpected class descriptor type: " + type);
        }
    }

    @Override
    protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
        String name = desc.getName();
        try {
            if (classLoader == null) {
                return Class.forName(
                        name, false,
                        Thread.currentThread().getContextClassLoader());
            } else {
                return Class.forName(name, false, classLoader);
            }
        } catch (ClassNotFoundException ex) {
            return super.resolveClass(desc);
        }
    }
}

@@ -2550,305 +2550,32 @@
 lazz
-;%0A            if (classLoader == null) %7B%0A                clazz = Class.forName(%0A                        className, true,%0A                        Thread.currentThread().getContextClassLoader());%0A            %7D else %7B%0A                clazz = Class.forName(className, true, classLoader);%0A            %7D
+ = loadClass(className);
 %0A   
@@ -2905,17 +2905,22 @@
  String 
-n
+classN
 ame = de
@@ -2963,34 +2963,127 @@
     
-if (classLoader == null) %7B
+return loadClass(className);%0A        %7D catch (ClassNotFoundException ex) %7B%0A            return super.resolveClass(desc);
 %0A   
@@ -3091,98 +3091,234 @@
     
+%7D%0A
     
+%7D%0A%0A
     
-return Class.forName(%0A                        name, false,%0A                       
+private Class%3C?%3E loadClass(String className) throws ClassNotFoundException %7B%0A        Class%3C?%3E clazz;%0A        ClassLoader classLoader = this.classLoader;%0A        if (classLoader == null) %7B%0A            classLoader =
  Thr
@@ -3360,17 +3360,16 @@
 Loader()
-)
 ;%0A      
@@ -3374,107 +3374,127 @@
     
+%7D%0A%0A
     
-%7D else %7B%0A                return Class.forName(name, false, classLoader);%0A            %7D%0A
+    if (classLoader != null) %7B%0A            clazz = classLoader.loadClass(className);%0A        %7D else %7B%0A  
         
 %7D ca
@@ -3493,100 +3493,75 @@
     
-%7D catch (ClassNotFoundException ex) %7B%0A            return super.resolveClass(desc);%0A        %7D
+  clazz = Class.forName(className);%0A        %7D%0A        return clazz;
 %0A   

Fix for NETTY-150 CompactObjectInputStream web start (applet) Class.forName issue * Refactored to reduce code duplication in CompactObjectInputStream * Made sure that class loading doesn't incur security check unnecessarily