home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / in4wjcxu / src / como / io / objectinputstream.java next >
Encoding:
Java Source  |  1996-08-14  |  4.2 KB  |  147 lines

  1. package como.io;
  2.  
  3. /**
  4. * ObjectInputStream.java   
  5. *
  6. * (c) 1996 Jan Kautz & Ulrich Gall
  7. *
  8. */
  9.  
  10. import java.io.*;
  11. import java.util.*;
  12. import java.awt.*;
  13. import como.io.*;
  14. import como.irc.*;
  15. import como.sys.*;
  16. import como.commlet.*;
  17. import como.commlet.draw.*;
  18. import como.commlet.nicnacnoe.*;
  19. import como.commlet.scheduler.*;
  20. import como.util.*;
  21.  
  22. /**
  23. * An ObjectInputStream is used to write Objects to a Stream.
  24. * It can load every Object that implements Saveable and
  25. * which was saved with the ObjectOutputStream!
  26. * You can even recursively load/save Objects. I.e. an object
  27. * may call writeObject( my_object ) in it's save()-method!
  28. *
  29. */
  30. public class ObjectInputStream extends DataInputStream {
  31.  
  32.     public ObjectInputStream(InputStream in) {
  33.         super(in);
  34.     }
  35.  
  36.     /**
  37.      * Reads a String from the ObjectInputStream. You don't have
  38.      * to draw attention to the length. It will be automatically
  39.      * correct!
  40.      * @return the String that was loaded
  41.      */
  42.     public String readString() throws IOException { // TODO Do some basic Huffman encoding?
  43.         int l = readInt();
  44.         byte[] b = new byte[l];
  45.         read(b,0,l);
  46.         return new String(b,0,0,l);
  47.     }                               
  48.  
  49.     // TODO: readNumber, writes automatically the shortest datatype
  50.  
  51.     /**
  52.      * Reads an Object from the ObjectInputStream. It reads
  53.      * every Object that implements Saveable. And some other
  54.      * basic Classes like Hashtable, Integer, byte[], int[],
  55.      * Boolean, Point, Dimension.
  56.      * @return the Object that was loaded
  57.      */
  58.     public Object readObject() throws IOException {
  59.         byte t = readByte();
  60.  
  61.         if (t == ObjectOutputStream.SAVEABLE) {
  62.             String cn = readString();
  63.             Saveable o = null;
  64.  
  65.             // TODO: as soon as loading classes in an applet works, forget this
  66.             // for now, we have to do that instead (since no class-loader is
  67.             // allowed by netscape, which lets you instantiate Objects by name!):
  68.  
  69.             if (cn.equals("como.commlet.draw.GOText")) o = new GOText();
  70.             else if (cn.equals("como.commlet.draw.GOGroup")) o = new GOGroup();
  71.             else if (cn.equals("como.commlet.draw.GOPolygon")) o = new GOPolygon();
  72.             else if (cn.equals("como.commlet.draw.GOOval")) o = new GOOval();
  73.             else if (cn.equals("como.commlet.draw.GORect")) o = new GORect();
  74.             else if (cn.equals("como.commlet.draw.GOImage")) o = new GOImage();
  75.             else if (cn.equals("como.commlet.draw.GOLine")) o = new GOLine();
  76.             else if (cn.equals("como.commlet.nicnacnoe.Piece")) o = new como.commlet.nicnacnoe.Piece();
  77.             else if (cn.equals("como.commlet.scheduler.UserSchedule")) o = new como.commlet.scheduler.UserSchedule();
  78.             else if (cn.equals("como.irc.IrcChan")) o = new IrcChan();
  79.             else if (cn.equals("como.sys.User")) o = (Saveable)new User();
  80.     
  81.             if (o != null) o.load(this) ;
  82.                 else Debug.msg(10,"ObjectInputStream: Cannot instantiate Object of class "+cn);
  83.             return o;
  84.         }
  85.         else if (t == ObjectOutputStream.INTEGER) {
  86.             return (new Integer(readInt()));
  87.         }    
  88.         else if (t == ObjectOutputStream.BOOLEAN) {
  89.             return (new Boolean(readBoolean()));
  90.         }    
  91.         else if (t == ObjectOutputStream.FONT) {
  92.             String name = readString();
  93.             int style = readInt();
  94.             int size = readInt();
  95.             return new Font(name,style,size);
  96.             }
  97.         else if (t == ObjectOutputStream.STRING) {
  98.             return readString();
  99.         }
  100.         else if (t == ObjectOutputStream.POINT) {
  101.             int x = readInt();
  102.             int y = readInt();
  103.             return (new Point(x,y));
  104.         }
  105.         else if (t == ObjectOutputStream.DIMENSION) {
  106.             int w = readInt();
  107.             int h = readInt();
  108.             return (new Dimension(w,h));
  109.         }
  110.         else if (t == ObjectOutputStream.BYTEARRAY) {
  111.             int l = readInt();
  112.             byte[] ar = new byte[l];
  113.             int i;
  114.             for (i=0;i<l;i++) {
  115.                 ar[i] = readByte();
  116.             }
  117.             return ar;
  118.         }
  119.         else if (t == ObjectOutputStream.INTARRAY) {
  120.             int l = readInt();
  121.             int[] ar = new int[l];
  122.             int i;
  123.             for (i=0;i<l;i++) {
  124.                 ar[i] = readInt();
  125.             }
  126.             return ar;
  127.         }
  128.         else if (t == ObjectOutputStream.COLOR) {
  129.             int r = readByte();
  130.             int g = readByte();
  131.             int b = readByte();
  132.             return (new Color(r,g,b));
  133.         }
  134.         else if (t == ObjectOutputStream.HASHTABLE) {
  135.             int size = readInt();
  136.             Hashtable h = new Hashtable(size);
  137.             int i;
  138.             for (i=0;i<size;i++) {
  139.                 h.put(readObject(),readObject());
  140.             }
  141.             return h;
  142.         }
  143.             Debug.msg(100,"ObjectInputStream: Cannot load Object of type #"+t);
  144.         return null;
  145.     }
  146. }
  147.