Project

General

Profile

Download (2.71 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 *  Copyright Terracotta, Inc.
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

    
17
package net.sf.ehcache.util;
18

    
19
import java.io.IOException;
20
import java.io.InputStream;
21
import java.io.ObjectInputStream;
22
import java.io.ObjectStreamClass;
23
import java.util.HashMap;
24

    
25
/**
26
 * Note by AM (2022-06-15): this is an exact copy of the same class in ehcache (v2.x)
27
 *     but with correct handling for primitive types.
28
 *     This class should be removed when upgrading to ehcache 3.x as it seems not to exist in
29
 *     this version anymore.
30
 *
31
 *     For further information see #10075 and #10077
32
 *
33
 * =========================================
34
 * Original javadoc:
35
 *
36
 * ObjectInputStream that uses a supplied classloader when resolving classes
37
 *
38
 * @author teck
39
 */
40
public class PreferredLoaderObjectInputStream extends ObjectInputStream {
41

    
42
    private final ClassLoader loader;
43

    
44
    /**
45
     * Constructor
46
     *
47
     * @param in
48
     * @throws IOException
49
     */
50
    public PreferredLoaderObjectInputStream(InputStream in, ClassLoader loader) throws IOException {
51
        super(in);
52
        this.loader = loader;
53
    }
54

    
55
// ************** REPLACED METHOD ********************************/
56

    
57
    @Override
58
    protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
59

    
60
        try {
61
            return Class.forName(desc.getName(), false, loader);
62
        } catch (ClassNotFoundException ex) {
63
            Class<?> cl = primClasses.get(desc.getName());
64
            if (cl != null) {
65
                return cl;
66
            } else {
67
                throw ex;
68
            }
69
        }
70
    }
71
    //copied from
72
    /** table mapping primitive type names to corresponding class objects */
73
    private static final HashMap<String, Class<?>> primClasses
74
        = new HashMap<>(8, 1.0F);
75
    static {
76
        primClasses.put("boolean", boolean.class);
77
        primClasses.put("byte", byte.class);
78
        primClasses.put("char", char.class);
79
        primClasses.put("short", short.class);
80
        primClasses.put("int", int.class);
81
        primClasses.put("long", long.class);
82
        primClasses.put("float", float.class);
83
        primClasses.put("double", double.class);
84
        primClasses.put("void", void.class);
85
    }
86

    
87
}
    (1-1/1)