KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > persistence > memoryimpl > Utils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Map JavaDoc;
31
32 /**
33  *
34  * @author mmatula
35  * @version
36  */

37 public class Utils extends Object JavaDoc {
38     public static void write(OutputStream JavaDoc outputStream, Object JavaDoc object, Storage storage) throws IOException JavaDoc {
39         if (object instanceof Map JavaDoc) {
40             outputStream.write(IOUtils.T_MAP);
41             Map JavaDoc temp = (Map JavaDoc) object;
42             IOUtils.writeInt(outputStream, temp.size());
43             Map.Entry JavaDoc key;
44             for (Iterator JavaDoc it = temp.entrySet().iterator(); it.hasNext();) {
45                 key = (Map.Entry JavaDoc) it.next();
46                 write(outputStream, key.getKey(), storage);
47                 write(outputStream, key.getValue(), storage);
48             }
49         } else if (object instanceof Collection JavaDoc) {
50             outputStream.write(IOUtils.T_COLLECTION);
51             Collection JavaDoc col = (Collection JavaDoc) object;
52             IOUtils.writeInt(outputStream, col.size());
53             for (Iterator JavaDoc 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 JavaDoc read(InputStream JavaDoc inputStream, Storage storage) throws IOException JavaDoc {
65         int type = inputStream.read();
66         switch (type) {
67             case IOUtils.T_MAP: {
68                 int size = IOUtils.readInt(inputStream);
69                 Map JavaDoc result = new HashMap JavaDoc(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 JavaDoc result = new java.util.ArrayList JavaDoc(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