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.ObjectOutputStream ; 8 import java.io.ObjectOutput ; 9 import java.util.Map ; 10 import java.util.HashMap ; 11 12 public class CustomObjectOutputStream extends ObjectOutputStream { 13 14 private StreamCallback callback; 15 private FastStack customFields = new FastStack(1); 16 17 private static final String 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 e) { 30 throw new ConversionException("Cannot create CustomObjectStream", e); 31 } 32 } 33 34 public static interface StreamCallback { 35 void writeToStream(Object object) throws IOException ; 36 void writeFieldsToStream(Map fields) throws IOException ; 37 void defaultWriteObject() throws IOException ; 38 void flush() throws IOException ; 39 void close() throws IOException ; 40 } 41 42 48 public CustomObjectOutputStream(StreamCallback callback) throws IOException , SecurityException { 49 this.callback = callback; 50 } 51 52 55 public void setCallback(StreamCallback callback) { 56 this.callback = callback; 57 } 58 59 60 61 public void defaultWriteObject() throws IOException { 62 callback.defaultWriteObject(); 63 } 64 65 protected void writeObjectOverride(Object obj) throws IOException { 66 callback.writeToStream(obj); 67 } 68 69 public void writeBoolean(boolean val) throws IOException { 70 callback.writeToStream(new Boolean (val)); 71 } 72 73 public void writeByte(int val) throws IOException { 74 callback.writeToStream(new Byte ((byte) val)); 75 } 76 77 public void writeInt(int val) throws IOException { 78 callback.writeToStream(new Integer (val)); 79 } 80 81 public void writeChar(int val) throws IOException { 82 callback.writeToStream(new Character ((char)val)); 83 } 84 85 public void writeDouble(double val) throws IOException { 86 callback.writeToStream(new Double (val)); 87 } 88 89 public void writeFloat(float val) throws IOException { 90 callback.writeToStream(new Float (val)); 91 } 92 93 public void writeLong(long val) throws IOException { 94 callback.writeToStream(new Long (val)); 95 } 96 97 public void writeShort(int val) throws IOException { 98 callback.writeToStream(new Short ((short) val)); 99 } 100 101 public void write(byte[] buf) throws IOException { 102 callback.writeToStream(buf); 103 } 104 105 public void writeChars(String str) throws IOException { 106 callback.writeToStream(str.toCharArray()); 107 } 108 109 public void writeUTF(String str) throws IOException { 110 callback.writeToStream(str); 111 } 112 113 public void write(int val) throws IOException { 114 callback.writeToStream(new Byte ((byte) val)); 115 } 116 117 public void write(byte[] buf, int off, int len) throws IOException { 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 { 126 callback.flush(); 127 } 128 129 public void close() throws IOException { 130 callback.close(); 131 } 132 133 public PutField putFields() throws IOException { 134 CustomPutField result = new CustomPutField(); 135 customFields.push(result); 136 return result; 137 } 138 139 public void writeFields() throws IOException { 140 CustomPutField customPutField = (CustomPutField) customFields.pop(); 141 callback.writeFieldsToStream(customPutField.asMap()); 142 } 143 144 private class CustomPutField extends PutField { 145 146 private final Map fields = new OrderRetainingMap(); 147 148 public Map asMap() { 149 return fields; 150 } 151 152 public void write(ObjectOutput out) throws IOException { 153 callback.writeToStream(asMap()); 154 } 155 156 public void put(String name, Object val) { 157 fields.put(name, val); 158 } 159 160 public void put(String name, byte val) { 161 put(name, new Byte (val)); 162 } 163 164 public void put(String name, char val) { 165 put(name, new Character (val)); 166 } 167 168 public void put(String name, double val) { 169 put(name, new Double (val)); 170 } 171 172 public void put(String name, float val) { 173 put(name, new Float (val)); 174 } 175 176 public void put(String name, int val) { 177 put(name, new Integer (val)); 178 } 179 180 public void put(String name, long val) { 181 put(name, new Long (val)); 182 } 183 184 public void put(String name, short val) { 185 put(name, new Short (val)); 186 } 187 188 public void put(String name, boolean val) { 189 put(name, new Boolean (val)); 190 } 191 192 } 193 194 195 196 public void reset() throws IOException { 197 throw new UnsupportedOperationException (); 198 } 199 200 public void useProtocolVersion(int version) throws IOException { 201 throw new UnsupportedOperationException (); 202 } 203 204 public void writeBytes(String str) throws IOException { 205 throw new UnsupportedOperationException (); 206 } 207 208 public void writeUnshared(Object obj) throws IOException { 209 throw new UnsupportedOperationException (); 210 } 211 212 } 213 | Popular Tags |