1 48 49 package com.caucho.hessian.io; 50 51 import java.io.IOException ; 52 import java.lang.reflect.Array ; 53 import java.util.ArrayList ; 54 55 58 public class ArrayDeserializer extends AbstractListDeserializer { 59 private Class _componentType; 60 61 public ArrayDeserializer(Deserializer componentDeserializer) 62 { 63 if (componentDeserializer != null) 64 _componentType = componentDeserializer.getType(); 65 } 66 67 public Class getType() 68 { 69 if (_componentType == null || _componentType.getName() == null) { 70 return Object [].class; 71 } 72 73 try { 74 return Class.forName("[" + getArrayClassName(_componentType)); 75 } catch (ClassNotFoundException e) { 76 return Object [].class; 77 } 78 } 79 80 private static String getArrayClassName(Class cl) 81 { 82 if (cl.isArray()) 83 return "[" + getArrayClassName(cl.getComponentType()); 84 else 85 return "L" + cl.getName() + ";"; 86 } 87 88 91 public Object readList(AbstractHessianInput in, int length) 92 throws IOException 93 { 94 if (length >= 0) { 95 Object []data = createArray(length); 96 97 in.addRef(data); 98 99 if (_componentType != null) { 100 for (int i = 0; i < data.length; i++) 101 data[i] = in.readObject(_componentType); 102 } 103 else { 104 for (int i = 0; i < data.length; i++) 105 data[i] = in.readObject(); 106 } 107 108 in.readListEnd(); 109 110 return data; 111 } 112 else { 113 ArrayList list = new ArrayList (); 114 115 in.addRef(list); 116 117 if (_componentType != null) { 118 while (! in.isEnd()) 119 list.add(in.readObject(_componentType)); 120 } 121 else { 122 while (! in.isEnd()) 123 list.add(in.readObject()); 124 } 125 126 in.readListEnd(); 127 128 Object []data = createArray(list.size()); 129 for (int i = 0; i < data.length; i++) 130 data[i] = list.get(i); 131 132 return data; 133 } 134 } 135 136 139 public Object readLengthList(AbstractHessianInput in, int length) 140 throws IOException 141 { 142 Object []data = createArray(length); 143 144 in.addRef(data); 145 146 if (_componentType != null) { 147 for (int i = 0; i < data.length; i++) 148 data[i] = in.readObject(_componentType); 149 } 150 else { 151 for (int i = 0; i < data.length; i++) 152 data[i] = in.readObject(); 153 } 154 155 return data; 156 } 157 158 protected Object []createArray(int length) 159 { 160 if (_componentType != null) 161 return (Object []) Array.newInstance(_componentType, length); 162 else 163 return new Object [length]; 164 } 165 166 public String toString() 167 { 168 return "ArrayDeserializer[" + _componentType + "]"; 169 } 170 } 171 | Popular Tags |