1 9 10 11 18 19 package JSX; 20 import java.io.*; import java.util.*; 25 public class ObjOut extends ObjectOutputStream { 26 final boolean DEBUG = false; 27 29 XMLSerialize s; public ObjOut(PrintWriter out) throws IOException { 32 super(); 33 s = new XMLSerialize(out); 34 s.setArg(new Object [] {this}); } 37 public ObjOut(PrintWriter out, Config cfg) throws IOException { 38 super(); 39 s = new XMLSerialize(out, cfg); 40 s.setArg(new Object [] {this}); } 43 44 public ObjOut(Writer out) throws IOException { 46 this(new PrintWriter(out, true)); } 48 public ObjOut(Writer out, boolean autoFlush) throws IOException { 49 this(new PrintWriter(out, autoFlush)); 50 } 51 public ObjOut(Writer out, boolean autoFlush, Config cfg) throws IOException { 52 this(new PrintWriter(out, autoFlush), cfg); 53 } 54 public ObjOut(boolean areAliasesUsed, Writer out) throws IOException { this(out, true); s.areAliasesUsed = areAliasesUsed; 57 } 58 public ObjOut(boolean areAliasesUsed, Writer out, Config cfg) throws IOException { this(out, true, cfg); s.areAliasesUsed = areAliasesUsed; 61 } 62 63 public ObjOut(OutputStream out, boolean autoFlush) throws IOException { 64 this(new OutputStreamWriter(out), autoFlush); } 66 public ObjOut(OutputStream out, boolean autoFlush, Config cfg) throws IOException { 67 this(new OutputStreamWriter(out), autoFlush, cfg); } 69 70 public ObjOut(boolean areAliasesUsed, OutputStream out) throws IOException { this(out, true); s.areAliasesUsed = areAliasesUsed; 74 } 75 public ObjOut(OutputStream out) throws IOException { this(out, true); } 78 public ObjOut(OutputStream out, Config cfg) throws IOException { this(out, true, cfg); } 81 83 84 public ObjOut(boolean areAliasesUsed) throws IOException, SecurityException { 86 this(System.out); s.areAliasesUsed = areAliasesUsed; 88 } 89 public ObjOut(boolean areAliasesUsed, Config cfg) throws IOException, SecurityException { 90 this(System.out, cfg); s.areAliasesUsed = areAliasesUsed; 92 } 93 94 public ObjOut() throws IOException, SecurityException { 95 this(System.out); } 97 public ObjOut(Config cfg) throws IOException, SecurityException { 98 this(System.out, cfg); } 100 101 102 103 104 public void reset() { s.reset(); } 105 public void flush() { s.flush(); } 106 public void close() { s.close(); } 107 108 protected void writeObjectOverride(Object obj) throws IOException { 109 if (DEBUG) System.err.println("writeObject: " +obj); 111 s.serialize(obj); s.flush(); 113 } 117 public void defaultWriteObject() throws IOException { 119 if (DEBUG) System.err.println("defaultWriteObject() - need to pass in"); 120 s.defaultWriteObject(); } 122 123 129 PutField currentPutField = null; 135 public PutField putFields() { 136 if (currentPutField!=null) { if (DEBUG) System.err.println("about to overwrite the currentPutField... need a stack"); 138 142 } 143 return currentPutField = this.s.new PutFieldImpl(); } 147 public void writeFields() throws IOException { currentPutField.write(this); 153 currentPutField = null; if (DEBUG) System.err.println("writeFields() fields and values buffered."); 155 } 156 157 158 public void write(byte[] b) throws IOException { 159 writeObject(b); } 161 162 public void write(byte[] b, int off, int len) throws IOException { 163 byte[] b2 = new byte[len]; 164 System.arraycopy(b, off, b2, 0, len); 165 writeObject(b2); } 167 public void write(int b) throws IOException { s.writePrim(b+""); 169 } 170 public void writeInt(int data) throws IOException { 171 s.writePrim(data+""); 172 } 173 public void writeFloat(float data) throws IOException { 174 s.writePrim(data+""); 175 } 176 public void writeBoolean(boolean data) throws IOException { 177 s.writePrim(data+""); 178 } 179 public void writeByte(int data) throws IOException { 180 s.writePrim(data+""); 181 } 182 public void writeBytes(String data) throws IOException { 183 writeObject(data); } 187 public void writeChar(int data) throws IOException { s.writePrim(data+""); } 196 public void writeChars(String data) throws IOException { 197 System.err.println("writeChars() is not properly implemented"); 198 writeObject(data); } 202 public void writeDouble(double data) throws IOException { 203 s.writePrim(data+""); 204 } 205 public void writeLong(long data) throws IOException { 212 s.writePrim(data+""); 213 } 214 public void writeShort(int data) throws IOException { s.writePrim(data+""); 216 } 217 225 public void writeUTF(String data) throws IOException { 226 s.writePrim(data+""); 228 231 } 232 static class Non_method_innerclassTest {} 233 234 235 244 public void setFormatted(boolean formatted) { 245 s.setFormatted(formatted); 246 } 247 248 249 public static void main(String [] args) throws Exception { 250 Config c = new Config().aliasID(false); ObjOut out = new ObjOut(c); 252 Object obj; 253 if (args.length>0) { 254 Class clazz = Class.forName(args[0]); 255 java.lang.reflect.Constructor cons = clazz.getDeclaredConstructor(new Class [] {}); 258 cons.setAccessible(true); obj = cons.newInstance(new Object [] {}); } else { 261 obj = new MyTyreClass("wheels on my car", 262 new Wheel("Michelin", new MyDate(31,10,1997)), 263 new Wheel("Michelin", new MyDate(31,10,1997)), 264 new Wheel("Michelin", new MyDate(2,3,1999)), 265 new Wheel("Michelin", new MyDate(14,9,2000)) 266 ); 267 } 268 274 out.writeObject(obj); } 276 } 277 | Popular Tags |