1 19 package org.netbeans.mdr.persistence.memoryimpl; 20 21 import org.netbeans.mdr.persistence.MOFID; 22 import org.netbeans.mdr.persistence.Storage; 23 import org.netbeans.mdr.util.IOUtils; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.util.Collection ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 32 37 public class Utils extends Object { 38 public static void write(OutputStream outputStream, Object object, Storage storage) throws IOException { 39 if (object instanceof Map ) { 40 outputStream.write(IOUtils.T_MAP); 41 Map temp = (Map ) object; 42 IOUtils.writeInt(outputStream, temp.size()); 43 Map.Entry key; 44 for (Iterator it = temp.entrySet().iterator(); it.hasNext();) { 45 key = (Map.Entry ) it.next(); 46 write(outputStream, key.getKey(), storage); 47 write(outputStream, key.getValue(), storage); 48 } 49 } else if (object instanceof Collection ) { 50 outputStream.write(IOUtils.T_COLLECTION); 51 Collection col = (Collection ) object; 52 IOUtils.writeInt(outputStream, col.size()); 53 for (Iterator it = col.iterator(); it.hasNext();) { 54 write(outputStream, it.next(), storage); 55 } 56 } else if (object instanceof MOFID) { 57 outputStream.write(IOUtils.T_MOFID); 58 IOUtils.writeMOFID(outputStream, (MOFID)object, storage); 59 } else { 60 IOUtils.write(outputStream, object); 61 } 62 } 63 64 public static Object read(InputStream inputStream, Storage storage) throws IOException { 65 int type = inputStream.read(); 66 switch (type) { 67 case IOUtils.T_MAP: { 68 int size = IOUtils.readInt(inputStream); 69 Map result = new HashMap (size); 70 for (int i = 0; i < size; i++) { 71 result.put(read(inputStream, storage), read(inputStream, storage)); 72 } 73 return result; 74 } 75 case IOUtils.T_COLLECTION: { 76 int size = IOUtils.readInt(inputStream); 77 java.util.List result = new java.util.ArrayList (size); 78 for (int i = 0; i < size; i++) { 79 result.add(read(inputStream, storage)); 80 } 81 return result; 82 } 83 case IOUtils.T_MOFID: { 84 return IOUtils.readMOFID(inputStream, storage); 85 } 86 default: { 87 return IOUtils.read(inputStream, null, null, type); 88 } 89 } 90 } 91 } 92 | Popular Tags |