1 package com.thoughtworks.xstream.core.util; 2 3 import com.thoughtworks.xstream.converters.ConversionException; 4 import com.thoughtworks.xstream.converters.DataHolder; 5 6 import java.io.IOException ; 7 import java.io.InvalidObjectException ; 8 import java.io.NotActiveException ; 9 import java.io.ObjectInputStream ; 10 import java.io.ObjectInputValidation ; 11 import java.io.ObjectStreamClass ; 12 import java.util.Map ; 13 14 public class CustomObjectInputStream extends ObjectInputStream { 15 16 private StreamCallback callback; 17 18 private static final String DATA_HOLDER_KEY = CustomObjectInputStream.class.getName(); 19 20 public static interface StreamCallback { 21 Object readFromStream() throws IOException ; 22 Map readFieldsFromStream() throws IOException ; 23 void defaultReadObject() throws IOException ; 24 void registerValidation(ObjectInputValidation validation, int priority) throws NotActiveException , InvalidObjectException ; 25 void close() throws IOException ; 26 } 27 28 public static synchronized CustomObjectInputStream getInstance(DataHolder whereFrom, CustomObjectInputStream.StreamCallback callback) { 29 try { 30 CustomObjectInputStream result = (CustomObjectInputStream) whereFrom.get(DATA_HOLDER_KEY); 31 if (result == null) { 32 result = new CustomObjectInputStream(callback); 33 whereFrom.put(DATA_HOLDER_KEY, result); 34 } else { 35 result.setCallback(callback); 36 } 37 return result; 38 } catch (IOException e) { 39 throw new ConversionException("Cannot create CustomObjectStream", e); 40 } 41 } 42 43 49 public CustomObjectInputStream(StreamCallback callback) throws IOException , SecurityException { 50 super(); 51 this.callback = callback; 52 } 53 54 57 public void setCallback(StreamCallback callback) { 58 this.callback = callback; 59 } 60 61 public void defaultReadObject() throws IOException , ClassNotFoundException { 62 callback.defaultReadObject(); 63 } 64 65 protected Object readObjectOverride() throws IOException , ClassNotFoundException { 66 return callback.readFromStream(); 67 } 68 69 public boolean readBoolean() throws IOException { 70 return ((Boolean )callback.readFromStream()).booleanValue(); 71 } 72 73 public byte readByte() throws IOException { 74 return ((Byte )callback.readFromStream()).byteValue(); 75 } 76 77 public int readInt() throws IOException { 78 return ((Integer )callback.readFromStream()).intValue(); 79 } 80 81 public char readChar() throws IOException { 82 return ((Character )callback.readFromStream()).charValue(); 83 } 84 85 public float readFloat() throws IOException { 86 return ((Float )callback.readFromStream()).floatValue(); 87 } 88 89 public double readDouble() throws IOException { 90 return ((Double )callback.readFromStream()).doubleValue(); 91 } 92 93 public long readLong() throws IOException { 94 return ((Long )callback.readFromStream()).longValue(); 95 } 96 97 public short readShort() throws IOException { 98 return ((Short )callback.readFromStream()).shortValue(); 99 } 100 101 public String readUTF() throws IOException { 102 return (String ) callback.readFromStream(); 103 } 104 105 public void readFully(byte[] buf) throws IOException { 106 readFully(buf, 0, buf.length); 107 } 108 109 public void readFully(byte[] buf, int off, int len) throws IOException { 110 byte[] b = (byte[])callback.readFromStream(); 111 for(int i = 0; i < len; i++) { 112 buf[i + off] = b[i]; 113 } 114 } 115 116 public GetField readFields() throws IOException , ClassNotFoundException { 117 return new CustomGetField(callback.readFieldsFromStream()); 118 } 119 120 private class CustomGetField extends GetField { 121 122 private Map fields; 123 124 public CustomGetField(Map fields) { 125 this.fields = fields; 126 } 127 128 public ObjectStreamClass getObjectStreamClass() { 129 throw new UnsupportedOperationException (); 130 } 131 132 private Object get(String name) { 133 return fields.get(name); 134 } 135 136 public boolean defaulted(String name) throws IOException { 137 return !fields.containsKey(name); 138 } 139 140 public byte get(String name, byte val) throws IOException { 141 return defaulted(name) ? val : ((Byte )get(name)).byteValue(); 142 } 143 144 public char get(String name, char val) throws IOException { 145 return defaulted(name) ? val : ((Character )get(name)).charValue(); 146 } 147 148 public double get(String name, double val) throws IOException { 149 return defaulted(name) ? val : ((Double )get(name)).doubleValue(); 150 } 151 152 public float get(String name, float val) throws IOException { 153 return defaulted(name) ? val : ((Float )get(name)).floatValue(); 154 } 155 156 public int get(String name, int val) throws IOException { 157 return defaulted(name) ? val : ((Integer )get(name)).intValue(); 158 } 159 160 public long get(String name, long val) throws IOException { 161 return defaulted(name) ? val : ((Long )get(name)).longValue(); 162 } 163 164 public short get(String name, short val) throws IOException { 165 return defaulted(name) ? val : ((Short )get(name)).shortValue(); 166 } 167 168 public boolean get(String name, boolean val) throws IOException { 169 return defaulted(name) ? val : ((Boolean )get(name)).booleanValue(); 170 } 171 172 public Object get(String name, Object val) throws IOException { 173 return defaulted(name) ? val : get(name); 174 } 175 176 } 177 178 public void registerValidation(ObjectInputValidation validation, int priority) throws NotActiveException , InvalidObjectException { 179 callback.registerValidation(validation, priority); 180 } 181 182 183 184 public int available() throws IOException { 185 throw new UnsupportedOperationException (); 186 } 187 188 public void close() throws IOException { 189 callback.close(); 190 } 191 192 public int readUnsignedByte() throws IOException { 193 throw new UnsupportedOperationException (); 194 } 195 196 public String readLine() throws IOException { 197 throw new UnsupportedOperationException (); 198 } 199 200 public Object readUnshared() throws IOException , ClassNotFoundException { 201 throw new UnsupportedOperationException (); 202 } 203 204 public int readUnsignedShort() throws IOException { 205 throw new UnsupportedOperationException (); 206 } 207 208 public int read() throws IOException { 209 throw new UnsupportedOperationException (); 210 } 211 212 public int read(byte[] buf, int off, int len) throws IOException { 213 throw new UnsupportedOperationException (); 214 } 215 216 public int skipBytes(int len) throws IOException { 217 throw new UnsupportedOperationException (); 218 } 219 220 public int read(byte b[]) throws IOException { 221 throw new UnsupportedOperationException (); 222 } 223 224 public long skip(long n) throws IOException { 225 throw new UnsupportedOperationException (); 226 } 227 228 public void mark(int readlimit) { 229 throw new UnsupportedOperationException (); 230 } 231 232 public void reset() throws IOException { 233 throw new UnsupportedOperationException (); 234 } 235 236 public boolean markSupported() { 237 return false; 238 } 239 240 } 241 | Popular Tags |