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

  1. /**
  2.  *
  3.  * ObjectOutputStream.java    (c) 1996 Ulrich Gall & Jan Kautz
  4.  *
  5.  */
  6.  
  7. package como.io;
  8.  
  9. import como.util.Debug;
  10. import java.io.*;
  11. import java.awt.*;
  12. import java.util.*;
  13. import como.io.*;
  14. import como.sys.*;
  15. import como.irc.*;
  16. import como.commlet.*;
  17.  
  18. /*
  19.  * This Stream can write any Objects to a given stream, that
  20.  * implement Saveable. It also supports various java-classes
  21.  * (see ObjectInputStream).
  22.  *
  23.  */
  24.  
  25. public class ObjectOutputStream extends DataOutputStream {
  26.  
  27.     public static byte SAVEABLE = 1;
  28.     public static byte INTEGER = 2;
  29.     public static byte STRING = 3;
  30.     public static byte COLOR = 4;
  31.     public static byte HASHTABLE = 5;
  32.     public static byte BOOLEAN = 6;    
  33.     public static byte FONT = 7;
  34.     public static byte DIMENSION = 8;
  35.     public static byte POINT = 9;
  36.     public static byte INTARRAY = 10;
  37.     public static byte BYTEARRAY = 11;
  38.  
  39.     public ObjectOutputStream(OutputStream out) {
  40.         super(out);
  41.         }
  42.  
  43.     /**
  44.      * writes a String to the stream. You don't have to care
  45.      * about the length!
  46.      * @param s the String you want to save.
  47.      */
  48.     public void writeString(String s) throws IOException { // TODO Do some basic Huffman encoding?
  49.         int l = s.length();
  50.         writeInt(l);
  51.         byte[] b = new byte[l];
  52.         s.getBytes(0,l,b,0);
  53.         write(b,0,l);
  54.     }                               
  55.  
  56.     // TODO: writeNumber, schreibt automatisch den kⁿrzesten datentyp
  57.  
  58.     /**
  59.      * writes a Object to the stream. This Object must implement
  60.      * Saveable, or it must be one of the supported classes (see
  61.      * ObjectInputStream)
  62.      * @param o the Object you want to save.
  63.      */
  64.     public void writeObject(Object o) throws IOException {
  65.         if (o instanceof Saveable) {
  66.             writeByte(SAVEABLE);
  67.             writeString(o.getClass().getName());
  68.             ((Saveable)o).save(this);
  69.         }
  70.         else if (o instanceof Boolean) {
  71.             writeByte(BOOLEAN);
  72.             writeBoolean(((Boolean)o).booleanValue());
  73.         }
  74.         else if (o instanceof Integer) {
  75.             writeByte(INTEGER);
  76.             writeInt(((Integer)o).intValue());
  77.         }
  78.         else if (o instanceof String) {
  79.             writeByte(STRING);
  80.             writeString((String)o);
  81.         }
  82.         else if (o instanceof Font) {
  83.             writeByte(FONT);
  84.             Font f = (Font)o;
  85.             writeString(f.getName());
  86.             writeInt(f.getStyle());
  87.             writeInt(f.getSize());
  88.         }
  89.         else if (o instanceof Color) {
  90.             writeByte(COLOR);
  91.             writeByte(((Color)o).getRed());
  92.             writeByte(((Color)o).getGreen());
  93.             writeByte(((Color)o).getBlue());
  94.         }
  95.         else if (o instanceof Point) {
  96.             writeByte(POINT);
  97.             writeInt(((Point)o).x);
  98.             writeInt(((Point)o).y);
  99.             }
  100.         else if (o instanceof Dimension) {
  101.             writeByte(DIMENSION);
  102.             writeInt(((Dimension)o).width);
  103.             writeInt(((Dimension)o).height);
  104.             }
  105.         else if (o instanceof int[]) {
  106.             int[] ar = (int[])o;
  107.             writeByte(INTARRAY);
  108.             writeInt(ar.length);
  109.             int i;
  110.             for (i=0;i<ar.length;i++) {
  111.                 writeInt(ar[i]);
  112.             }
  113.         }
  114.         else if (o instanceof byte[]) {
  115.             byte[] ar = (byte[])o;
  116.             writeByte(BYTEARRAY);
  117.             writeInt(ar.length);
  118.             int i;
  119.             for (i=0;i<ar.length;i++) {
  120.                 writeByte(ar[i]);
  121.             }
  122.         }
  123.         else if (o instanceof Hashtable) {
  124.             writeByte(HASHTABLE);
  125.             Hashtable h = (Hashtable)o;
  126.             writeInt(h.size());
  127.             Enumeration e = h.keys();
  128.             while (e.hasMoreElements()) {
  129.                 Object k = e.nextElement();
  130.                 writeObject(k);
  131.                 writeObject(h.get(k));
  132.             }
  133.         }
  134.     }
  135. }
  136.