1 9 package javolution; 10 11 import java.io.ByteArrayInputStream; 12 import java.io.ByteArrayOutputStream; 13 import java.io.IOException; 14 import java.util.Vector; 15 16 import j2me.io.ObjectInputStream; 17 import j2me.io.ObjectOutputStream; 18 import j2me.io.ObjectInput; 19 import j2me.io.ObjectOutput; 20 import j2me.lang.UnsupportedOperationException; 21 import j2me.nio.ByteBuffer; 22 import javolution.lang.Text; 23 import javolution.lang.TextBuilder; 24 import javolution.util.FastList; 25 import javolution.util.FastMap; 26 import javolution.util.FastSet; 27 import javolution.util.FastTable; 28 import javolution.xml.CharacterData; 29 import javolution.xml.ObjectReader; 30 import javolution.xml.ObjectWriter; 31 import javolution.xml.XmlElement; 32 import javolution.xml.XmlFormat; 33 34 40 final class Perf_Xml extends Javolution implements Runnable { 41 42 private static final int OBJECT_SIZE = 1000; 44 private static final int BYTE_BUFFER_SIZE = 50 * OBJECT_SIZE + 1000; 45 46 49 public void run() { 50 println("/////////////////////////////"); 51 println("// Package: javolution.xml //"); 52 println("/////////////////////////////"); 53 54 Vector v = new Vector(OBJECT_SIZE); 56 for (int i = 0; i < OBJECT_SIZE; i++) { 57 v.addElement("This is the string #" + i); 58 } 59 v.addElement(null); 61 v.addElement(TextBuilder.newInstance().append(Long.MAX_VALUE)); 62 v.addElement(Text.valueOf(Long.MAX_VALUE, 16)); 63 String str = "<<< Some character data >>>"; 64 CharacterData charData = CharacterData.valueOf(Text.valueOf(str)); 65 v.addElement(charData); 66 FastMap fm = new FastMap(); 67 fm.put("ONE", "1"); 68 fm.put("TWO", "2"); 69 fm.put("THREE", "3"); 70 v.addElement(fm); 71 FastList fl = new FastList(); 72 fl.add("FIRST"); 73 fl.add("SECOND"); 74 fl.add("THIRD"); 75 v.addElement(fl); 76 FastSet fs = new FastSet(); 77 fs.add("ALPHA"); 78 fs.add("BETA"); 79 fs.add("ALPHA"); 80 v.addElement(fs); 81 FastTable ft = new FastTable(); 82 ft.add("UN"); 83 ft.add("DEUX"); 84 ft.add("TROIS"); 85 v.addElement(ft); 86 87 XmlFormat vectorXml = new XmlFormat() { 89 public Object preallocate(XmlElement xml) { 90 return new Vector(xml.getAttribute("size", 0)); 91 } 92 93 public void format(Object obj, XmlElement xml) { 94 Vector v = (Vector) obj; 95 xml.setAttribute("size", v.size()); 96 for (int i = 0; i < v.size(); i++) { 97 xml.getContent().add(v.elementAt(i)); 98 } 99 } 100 101 public Object parse(XmlElement xml) { 102 Vector v = (Vector) xml.object(); for (FastList.Node n = xml.getContent().headNode(), 104 end = xml.getContent().tailNode(); 105 (n = n.getNextNode()) != end;) { 106 v.addElement(n.getValue()); 107 } 108 return v; 109 } 110 }; 111 XmlFormat.setInstance(vectorXml, new Vector().getClass()); 112 113 println(""); 114 println("-- Java(TM) Serialization --"); 115 try { 116 ByteArrayOutputStream out = new ByteArrayOutputStream( 117 BYTE_BUFFER_SIZE); 118 ObjectOutput oo = new ObjectOutputStream(out); 119 print("Write Time: "); 120 startTime(); 121 oo.writeObject(v); 122 oo.close(); 123 println(endTime(1)); 124 ByteArrayInputStream in = new ByteArrayInputStream(out 125 .toByteArray()); 126 ObjectInput oi = new ObjectInputStream(in); 127 print("Read Time: "); 128 startTime(); 129 Object readObject = oi.readObject(); 130 oi.close(); 131 println(endTime(1)); 132 if (!v.equals(readObject)) { 133 throw new Error("SERIALIZATION ERROR"); 134 } 135 } catch (UnsupportedOperationException e) { 136 println("NOT SUPPORTED (J2SE 1.4+ build required)"); 137 } catch (Throwable e) { 138 throw new JavolutionError(e); 139 } 140 141 println(""); 142 println("-- XML Serialization (I/O Stream) --"); 143 try { 144 ObjectWriter ow = new ObjectWriter(); 145 ow.setNamespace("", "java.lang"); 146 ByteArrayOutputStream out = new ByteArrayOutputStream( 147 BYTE_BUFFER_SIZE); 148 print("Write Time: "); 149 startTime(); 150 ow.write(v, out); 151 println(endTime(1)); 152 154 ObjectReader or = new ObjectReader(); 155 ByteArrayInputStream in = new ByteArrayInputStream(out 156 .toByteArray()); 157 print("Read Time: "); 158 startTime(); 159 Object readObject = or.read(in); 160 println(endTime(1)); 161 if (!v.equals(readObject)) { 163 throw new Error("SERIALIZATION ERROR"); 164 } 165 } catch (IOException e) { 166 throw new JavolutionError(e); 167 } 168 169 println(""); 170 println("-- XML Serialization (NIO ByteBuffer) --"); 171 try { 172 ObjectWriter ow = new ObjectWriter(); 173 ow.setNamespace("", "java.lang"); 174 ByteBuffer bb = ByteBuffer.allocateDirect(BYTE_BUFFER_SIZE); 175 print("Write Time: "); 176 startTime(); 177 ow.write(v, bb); 178 println(endTime(1)); 179 ObjectReader or = new ObjectReader(); 180 bb.flip(); 181 print("Read Time: "); 182 startTime(); 183 Object readObject = or.read(bb); 184 println(endTime(1)); 185 if (!v.equals(readObject)) { 186 throw new Error("SERIALIZATION ERROR"); 187 } 188 } catch (IOException e) { 189 throw new JavolutionError(e); 190 } 191 println(""); 192 } 193 } | Popular Tags |