KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > core > util > CustomObjectInputStream


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 JavaDoc;
7 import java.io.InvalidObjectException JavaDoc;
8 import java.io.NotActiveException JavaDoc;
9 import java.io.ObjectInputStream JavaDoc;
10 import java.io.ObjectInputValidation JavaDoc;
11 import java.io.ObjectStreamClass JavaDoc;
12 import java.util.Map JavaDoc;
13
14 public class CustomObjectInputStream extends ObjectInputStream JavaDoc {
15
16     private StreamCallback callback;
17
18     private static final String JavaDoc DATA_HOLDER_KEY = CustomObjectInputStream.class.getName();
19
20     public static interface StreamCallback {
21         Object JavaDoc readFromStream() throws IOException JavaDoc;
22         Map JavaDoc readFieldsFromStream() throws IOException JavaDoc;
23         void defaultReadObject() throws IOException JavaDoc;
24         void registerValidation(ObjectInputValidation JavaDoc validation, int priority) throws NotActiveException JavaDoc, InvalidObjectException JavaDoc;
25         void close() throws IOException JavaDoc;
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 JavaDoc e) {
39             throw new ConversionException("Cannot create CustomObjectStream", e);
40         }
41     }
42
43     /**
44      * Warning, this object is expensive to create (due to functionality inherited from superclass).
45      * Use the static fetch() method instead, wherever possible.
46      *
47      * @see #getInstance(com.thoughtworks.xstream.converters.DataHolder, com.thoughtworks.xstream.core.util.CustomObjectInputStream.StreamCallback)
48      */

49     public CustomObjectInputStream(StreamCallback callback) throws IOException JavaDoc, SecurityException JavaDoc {
50         super();
51         this.callback = callback;
52     }
53
54     /**
55      * Allows the CustomObjectInputStream (which is expensive to create) to be reused.
56      */

57     public void setCallback(StreamCallback callback) {
58         this.callback = callback;
59     }
60
61     public void defaultReadObject() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
62         callback.defaultReadObject();
63     }
64
65     protected Object JavaDoc readObjectOverride() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
66         return callback.readFromStream();
67     }
68
69     public boolean readBoolean() throws IOException JavaDoc {
70         return ((Boolean JavaDoc)callback.readFromStream()).booleanValue();
71     }
72
73     public byte readByte() throws IOException JavaDoc {
74         return ((Byte JavaDoc)callback.readFromStream()).byteValue();
75     }
76
77     public int readInt() throws IOException JavaDoc {
78         return ((Integer JavaDoc)callback.readFromStream()).intValue();
79     }
80
81     public char readChar() throws IOException JavaDoc {
82         return ((Character JavaDoc)callback.readFromStream()).charValue();
83     }
84
85     public float readFloat() throws IOException JavaDoc {
86         return ((Float JavaDoc)callback.readFromStream()).floatValue();
87     }
88
89     public double readDouble() throws IOException JavaDoc {
90         return ((Double JavaDoc)callback.readFromStream()).doubleValue();
91     }
92
93     public long readLong() throws IOException JavaDoc {
94         return ((Long JavaDoc)callback.readFromStream()).longValue();
95     }
96
97     public short readShort() throws IOException JavaDoc {
98         return ((Short JavaDoc)callback.readFromStream()).shortValue();
99     }
100
101     public String JavaDoc readUTF() throws IOException JavaDoc {
102         return (String JavaDoc) callback.readFromStream();
103     }
104
105     public void readFully(byte[] buf) throws IOException JavaDoc {
106         readFully(buf, 0, buf.length);
107     }
108
109     public void readFully(byte[] buf, int off, int len) throws IOException JavaDoc {
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 JavaDoc, ClassNotFoundException JavaDoc {
117         return new CustomGetField(callback.readFieldsFromStream());
118     }
119
120     private class CustomGetField extends GetField {
121
122         private Map JavaDoc fields;
123
124         public CustomGetField(Map JavaDoc fields) {
125             this.fields = fields;
126         }
127
128         public ObjectStreamClass JavaDoc getObjectStreamClass() {
129             throw new UnsupportedOperationException JavaDoc();
130         }
131
132         private Object JavaDoc get(String JavaDoc name) {
133             return fields.get(name);
134         }
135
136         public boolean defaulted(String JavaDoc name) throws IOException JavaDoc {
137             return !fields.containsKey(name);
138         }
139
140         public byte get(String JavaDoc name, byte val) throws IOException JavaDoc {
141             return defaulted(name) ? val : ((Byte JavaDoc)get(name)).byteValue();
142         }
143
144         public char get(String JavaDoc name, char val) throws IOException JavaDoc {
145             return defaulted(name) ? val : ((Character JavaDoc)get(name)).charValue();
146         }
147
148         public double get(String JavaDoc name, double val) throws IOException JavaDoc {
149             return defaulted(name) ? val : ((Double JavaDoc)get(name)).doubleValue();
150         }
151
152         public float get(String JavaDoc name, float val) throws IOException JavaDoc {
153             return defaulted(name) ? val : ((Float JavaDoc)get(name)).floatValue();
154         }
155
156         public int get(String JavaDoc name, int val) throws IOException JavaDoc {
157             return defaulted(name) ? val : ((Integer JavaDoc)get(name)).intValue();
158         }
159
160         public long get(String JavaDoc name, long val) throws IOException JavaDoc {
161             return defaulted(name) ? val : ((Long JavaDoc)get(name)).longValue();
162         }
163
164         public short get(String JavaDoc name, short val) throws IOException JavaDoc {
165             return defaulted(name) ? val : ((Short JavaDoc)get(name)).shortValue();
166         }
167
168         public boolean get(String JavaDoc name, boolean val) throws IOException JavaDoc {
169             return defaulted(name) ? val : ((Boolean JavaDoc)get(name)).booleanValue();
170         }
171
172         public Object JavaDoc get(String JavaDoc name, Object JavaDoc val) throws IOException JavaDoc {
173             return defaulted(name) ? val : get(name);
174         }
175
176     }
177
178     public void registerValidation(ObjectInputValidation JavaDoc validation, int priority) throws NotActiveException JavaDoc, InvalidObjectException JavaDoc {
179         callback.registerValidation(validation, priority);
180     }
181
182     /****** Unsupported methods ******/
183
184     public int available() throws IOException JavaDoc {
185         throw new UnsupportedOperationException JavaDoc();
186     }
187
188     public void close() throws IOException JavaDoc {
189         callback.close();
190     }
191
192     public int readUnsignedByte() throws IOException JavaDoc {
193         throw new UnsupportedOperationException JavaDoc();
194     }
195
196     public String JavaDoc readLine() throws IOException JavaDoc {
197         throw new UnsupportedOperationException JavaDoc();
198     }
199
200     public Object JavaDoc readUnshared() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
201         throw new UnsupportedOperationException JavaDoc();
202     }
203
204     public int readUnsignedShort() throws IOException JavaDoc {
205         throw new UnsupportedOperationException JavaDoc();
206     }
207
208     public int read() throws IOException JavaDoc {
209         throw new UnsupportedOperationException JavaDoc();
210     }
211
212     public int read(byte[] buf, int off, int len) throws IOException JavaDoc {
213         throw new UnsupportedOperationException JavaDoc();
214     }
215
216     public int skipBytes(int len) throws IOException JavaDoc {
217         throw new UnsupportedOperationException JavaDoc();
218     }
219
220     public int read(byte b[]) throws IOException JavaDoc {
221         throw new UnsupportedOperationException JavaDoc();
222     }
223
224     public long skip(long n) throws IOException JavaDoc {
225         throw new UnsupportedOperationException JavaDoc();
226     }
227
228     public void mark(int readlimit) {
229         throw new UnsupportedOperationException JavaDoc();
230     }
231
232     public void reset() throws IOException JavaDoc {
233         throw new UnsupportedOperationException JavaDoc();
234     }
235
236     public boolean markSupported() {
237         return false;
238     }
239
240 }
241
Popular Tags