KickJava   Java API By Example, From Geeks To Geeks.

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


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.ObjectOutputStream JavaDoc;
8 import java.io.ObjectOutput JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.HashMap JavaDoc;
11
12 public class CustomObjectOutputStream extends ObjectOutputStream JavaDoc {
13
14     private StreamCallback callback;
15     private FastStack customFields = new FastStack(1);
16
17     private static final String JavaDoc DATA_HOLDER_KEY = CustomObjectOutputStream.class.getName();
18
19     public static synchronized CustomObjectOutputStream getInstance(DataHolder whereFrom, StreamCallback callback) {
20         try {
21             CustomObjectOutputStream result = (CustomObjectOutputStream) whereFrom.get(DATA_HOLDER_KEY);
22             if (result == null) {
23                 result = new CustomObjectOutputStream(callback);
24                 whereFrom.put(DATA_HOLDER_KEY, result);
25             } else {
26                 result.setCallback(callback);
27             }
28             return result;
29         } catch (IOException JavaDoc e) {
30             throw new ConversionException("Cannot create CustomObjectStream", e);
31         }
32     }
33
34     public static interface StreamCallback {
35         void writeToStream(Object JavaDoc object) throws IOException JavaDoc;
36         void writeFieldsToStream(Map JavaDoc fields) throws IOException JavaDoc;
37         void defaultWriteObject() throws IOException JavaDoc;
38         void flush() throws IOException JavaDoc;
39         void close() throws IOException JavaDoc;
40     }
41
42     /**
43      * Warning, this object is expensive to create (due to functionality inherited from superclass).
44      * Use the static fetch() method instead, wherever possible.
45      *
46      * @see #getInstance(com.thoughtworks.xstream.converters.DataHolder, com.thoughtworks.xstream.core.util.CustomObjectOutputStream.StreamCallback)
47      */

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

55     public void setCallback(StreamCallback callback) {
56         this.callback = callback;
57     }
58
59     /*** Methods to delegate to callback ***/
60
61     public void defaultWriteObject() throws IOException JavaDoc {
62         callback.defaultWriteObject();
63     }
64
65     protected void writeObjectOverride(Object JavaDoc obj) throws IOException JavaDoc {
66         callback.writeToStream(obj);
67     }
68
69     public void writeBoolean(boolean val) throws IOException JavaDoc {
70         callback.writeToStream(new Boolean JavaDoc(val));
71     }
72
73     public void writeByte(int val) throws IOException JavaDoc {
74         callback.writeToStream(new Byte JavaDoc((byte) val));
75     }
76
77     public void writeInt(int val) throws IOException JavaDoc {
78         callback.writeToStream(new Integer JavaDoc(val));
79     }
80
81     public void writeChar(int val) throws IOException JavaDoc {
82         callback.writeToStream(new Character JavaDoc((char)val));
83     }
84
85     public void writeDouble(double val) throws IOException JavaDoc {
86         callback.writeToStream(new Double JavaDoc(val));
87     }
88
89     public void writeFloat(float val) throws IOException JavaDoc {
90         callback.writeToStream(new Float JavaDoc(val));
91     }
92
93     public void writeLong(long val) throws IOException JavaDoc {
94         callback.writeToStream(new Long JavaDoc(val));
95     }
96
97     public void writeShort(int val) throws IOException JavaDoc {
98         callback.writeToStream(new Short JavaDoc((short) val));
99     }
100
101     public void write(byte[] buf) throws IOException JavaDoc {
102         callback.writeToStream(buf);
103     }
104
105     public void writeChars(String JavaDoc str) throws IOException JavaDoc {
106         callback.writeToStream(str.toCharArray());
107     }
108
109     public void writeUTF(String JavaDoc str) throws IOException JavaDoc {
110         callback.writeToStream(str);
111     }
112
113     public void write(int val) throws IOException JavaDoc {
114         callback.writeToStream(new Byte JavaDoc((byte) val));
115     }
116
117     public void write(byte[] buf, int off, int len) throws IOException JavaDoc {
118         byte[] b = new byte[len];
119         for(int i = 0; i < len; i++) {
120             b[i] = buf[i + off];
121         }
122         callback.writeToStream(b);
123     }
124
125     public void flush() throws IOException JavaDoc {
126         callback.flush();
127     }
128
129     public void close() throws IOException JavaDoc {
130         callback.close();
131     }
132
133     public PutField putFields() throws IOException JavaDoc {
134         CustomPutField result = new CustomPutField();
135         customFields.push(result);
136         return result;
137     }
138
139     public void writeFields() throws IOException JavaDoc {
140         CustomPutField customPutField = (CustomPutField) customFields.pop();
141         callback.writeFieldsToStream(customPutField.asMap());
142     }
143
144     private class CustomPutField extends PutField {
145
146         private final Map JavaDoc fields = new OrderRetainingMap();
147
148         public Map JavaDoc asMap() {
149             return fields;
150         }
151
152         public void write(ObjectOutput JavaDoc out) throws IOException JavaDoc {
153             callback.writeToStream(asMap());
154         }
155
156         public void put(String JavaDoc name, Object JavaDoc val) {
157             fields.put(name, val);
158         }
159
160         public void put(String JavaDoc name, byte val) {
161             put(name, new Byte JavaDoc(val));
162         }
163
164         public void put(String JavaDoc name, char val) {
165             put(name, new Character JavaDoc(val));
166         }
167
168         public void put(String JavaDoc name, double val) {
169             put(name, new Double JavaDoc(val));
170         }
171
172         public void put(String JavaDoc name, float val) {
173             put(name, new Float JavaDoc(val));
174         }
175
176         public void put(String JavaDoc name, int val) {
177             put(name, new Integer JavaDoc(val));
178         }
179
180         public void put(String JavaDoc name, long val) {
181             put(name, new Long JavaDoc(val));
182         }
183
184         public void put(String JavaDoc name, short val) {
185             put(name, new Short JavaDoc(val));
186         }
187
188         public void put(String JavaDoc name, boolean val) {
189             put(name, new Boolean JavaDoc(val));
190         }
191
192     }
193
194     /****** Unsupported methods ******/
195
196     public void reset() throws IOException JavaDoc {
197         throw new UnsupportedOperationException JavaDoc();
198     }
199
200     public void useProtocolVersion(int version) throws IOException JavaDoc {
201         throw new UnsupportedOperationException JavaDoc();
202     }
203
204     public void writeBytes(String JavaDoc str) throws IOException JavaDoc {
205         throw new UnsupportedOperationException JavaDoc();
206     }
207
208     public void writeUnshared(Object JavaDoc obj) throws IOException JavaDoc {
209         throw new UnsupportedOperationException JavaDoc();
210     }
211
212 }
213
Popular Tags