1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.codec.serialization;
17
18 import java.io.EOFException;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.ObjectInputStream;
22 import java.io.ObjectStreamClass;
23 import java.io.StreamCorruptedException;
24
25 class CompactObjectInputStream extends ObjectInputStream {
26
27 private final ClassResolver classResolver;
28
29 CompactObjectInputStream(InputStream in, ClassResolver classResolver) throws IOException {
30 super(in);
31 if (classResolver == null) {
32 throw new NullPointerException("classResolver");
33 }
34 this.classResolver = classResolver;
35 }
36
37 @Override
38 protected void readStreamHeader() throws IOException {
39 int version = readByte() & 0xFF;
40 if (version != STREAM_VERSION) {
41 throw new StreamCorruptedException(
42 "Unsupported version: " + version);
43 }
44 }
45
46 @Override
47 protected ObjectStreamClass readClassDescriptor()
48 throws IOException, ClassNotFoundException {
49 int type = read();
50 if (type < 0) {
51 throw new EOFException();
52 }
53 switch (type) {
54 case CompactObjectOutputStream.TYPE_FAT_DESCRIPTOR:
55 return super.readClassDescriptor();
56 case CompactObjectOutputStream.TYPE_THIN_DESCRIPTOR:
57 String className = readUTF();
58 Class<?> clazz = classResolver.resolve(className);
59 ObjectStreamClass streamClass = ObjectStreamClass.lookup(clazz);
60 if (streamClass == null) {
61
62
63
64
65
66
67
68
69 streamClass = ObjectStreamClass.lookupAny(clazz);
70 }
71 return streamClass;
72 default:
73 throw new StreamCorruptedException(
74 "Unexpected class descriptor type: " + type);
75 }
76 }
77
78 @Override
79 protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
80 Class<?> clazz;
81 try {
82 clazz = classResolver.resolve(desc.getName());
83 } catch (ClassNotFoundException ex) {
84 clazz = super.resolveClass(desc);
85 }
86
87 return clazz;
88 }
89
90 }