home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 139 / dpcs0999.iso / Web / CFserver / data1.cab / Java / netscape / util / Serializer.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-04-12  |  4.6 KB  |  335 lines

  1. package netscape.util;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.FilterOutputStream;
  5. import java.io.IOException;
  6. import java.io.OutputStream;
  7.  
  8. public class Serializer extends FilterOutputStream {
  9.    private static boolean[] unsafeChars = new boolean[127];
  10.    private static int SMALL_STRING_LIMIT = 40;
  11.    private final int BUF_LEN = 128;
  12.    private byte[] buf = new byte[128];
  13.    private int bufIndex;
  14.  
  15.    public Serializer(OutputStream var1) {
  16.       super(var1);
  17.    }
  18.  
  19.    private void flushBuffer() throws IOException {
  20.       if (this.bufIndex > 0) {
  21.          ((FilterOutputStream)this).write(this.buf, 0, this.bufIndex);
  22.          this.bufIndex = 0;
  23.       }
  24.  
  25.    }
  26.  
  27.    final void writeOutput(int var1) throws IOException {
  28.       if (this.bufIndex >= 128) {
  29.          this.flushBuffer();
  30.       }
  31.  
  32.       this.buf[this.bufIndex++] = (byte)var1;
  33.    }
  34.  
  35.    private final void serializeHashtable(Hashtable var1) throws IOException {
  36.       Enumeration var2 = var1.keys();
  37.       this.writeOutput(123);
  38.  
  39.       for(; var2.hasMoreElements(); this.writeOutput(59)) {
  40.          Object var3 = var2.nextElement();
  41.          Object var4 = var1.get(var3);
  42.          if (var3 instanceof String) {
  43.             this.serializeString((String)var3);
  44.          } else {
  45.             this.serializeObjectInternal(var3);
  46.          }
  47.  
  48.          this.writeOutput(61);
  49.          if (var4 instanceof String) {
  50.             this.serializeString((String)var4);
  51.          } else {
  52.             this.serializeObjectInternal(var4);
  53.          }
  54.       }
  55.  
  56.       this.writeOutput(125);
  57.    }
  58.  
  59.    private final void serializeArray(Object[] var1) throws IOException {
  60.       this.writeOutput(91);
  61.       int var3 = 0;
  62.  
  63.       for(int var4 = var1.length; var3 < var4; ++var3) {
  64.          Object var2 = var1[var3];
  65.          if (var2 instanceof String) {
  66.             this.serializeString((String)var2);
  67.          } else {
  68.             this.serializeObjectInternal(var2);
  69.          }
  70.  
  71.          if (var3 < var4 - 1) {
  72.             this.writeOutput(44);
  73.          }
  74.       }
  75.  
  76.       this.writeOutput(93);
  77.    }
  78.  
  79.    private final void serializeVector(Vector var1) throws IOException {
  80.       this.writeOutput(40);
  81.       int var3 = 0;
  82.  
  83.       for(int var4 = var1.count(); var3 < var4; ++var3) {
  84.          Object var2 = var1.elementAt(var3);
  85.          if (var2 instanceof String) {
  86.             this.serializeString((String)var2);
  87.          } else {
  88.             this.serializeObjectInternal(var2);
  89.          }
  90.  
  91.          if (var3 < var4 - 1) {
  92.             this.writeOutput(44);
  93.          }
  94.       }
  95.  
  96.       this.writeOutput(41);
  97.    }
  98.  
  99.    final boolean stringRequiresQuotes(String var1) {
  100.       int var3 = 0;
  101.  
  102.       for(int var4 = var1.length(); var3 < var4; ++var3) {
  103.          char var2 = var1.charAt(var3);
  104.          if (var2 >= 127) {
  105.             return true;
  106.          }
  107.  
  108.          if (unsafeChars[var2]) {
  109.             return true;
  110.          }
  111.       }
  112.  
  113.       return false;
  114.    }
  115.  
  116.    private final boolean stringRequiresQuotes(char[] var1) {
  117.       int var3 = 0;
  118.  
  119.       for(int var4 = var1.length; var3 < var4; ++var3) {
  120.          char var2 = var1[var3];
  121.          if (var2 >= 127) {
  122.             return true;
  123.          }
  124.  
  125.          if (unsafeChars[var2]) {
  126.             return true;
  127.          }
  128.       }
  129.  
  130.       return false;
  131.    }
  132.  
  133.    private final int fourBitToAscii(int var1) {
  134.       return var1 < 10 ? 48 + var1 : 65 + (var1 - 10);
  135.    }
  136.  
  137.    void serializeString(String var1) throws IOException {
  138.       boolean var2 = false;
  139.       boolean var3 = true;
  140.       char[] var7 = null;
  141.       int var5;
  142.       if (var1 != null && (var5 = var1.length()) != 0) {
  143.          if (var5 <= 8) {
  144.             var3 = false;
  145.          } else {
  146.             var3 = true;
  147.             var7 = var1.toCharArray();
  148.          }
  149.  
  150.          if (var5 > SMALL_STRING_LIMIT) {
  151.             var2 = true;
  152.          } else if (var3) {
  153.             var2 = this.stringRequiresQuotes(var7);
  154.          } else {
  155.             var2 = this.stringRequiresQuotes(var1);
  156.          }
  157.  
  158.          if (var2) {
  159.             this.writeOutput(34);
  160.          }
  161.  
  162.          for(int var4 = 0; var4 < var5; ++var4) {
  163.             char var6;
  164.             if (var3) {
  165.                var6 = var7[var4];
  166.             } else {
  167.                var6 = var1.charAt(var4);
  168.             }
  169.  
  170.             if (var6 < 255) {
  171.                if (var6 >= '#' && var6 <= '~' && var6 != '\\') {
  172.                   this.writeOutput(var6);
  173.                } else {
  174.                   switch (var6) {
  175.                      case '\t':
  176.                         this.writeOutput(92);
  177.                         this.writeOutput(116);
  178.                         break;
  179.                      case '\n':
  180.                         this.writeOutput(92);
  181.                         this.writeOutput(110);
  182.                         break;
  183.                      case '\r':
  184.                         this.writeOutput(92);
  185.                         this.writeOutput(114);
  186.                         break;
  187.                      case ' ':
  188.                      case '!':
  189.                         this.writeOutput(var6);
  190.                         break;
  191.                      case '"':
  192.                         this.writeOutput(92);
  193.                         this.writeOutput(34);
  194.                         break;
  195.                      case '\\':
  196.                         this.writeOutput(92);
  197.                         this.writeOutput(92);
  198.                         break;
  199.                      default:
  200.                         this.writeOutput(92);
  201.                         this.writeOutput(48 + (var6 >> 6));
  202.                         this.writeOutput(48 + (var6 >> 3 & 7));
  203.                         this.writeOutput(48 + (var6 & 7));
  204.                   }
  205.                }
  206.             } else {
  207.                this.writeOutput(92);
  208.                this.writeOutput(117);
  209.                this.writeOutput(this.fourBitToAscii(var6 >> 12));
  210.                this.writeOutput(this.fourBitToAscii(var6 >> 8 & 15));
  211.                this.writeOutput(this.fourBitToAscii(var6 >> 4 & 15));
  212.                this.writeOutput(this.fourBitToAscii(var6 & 15));
  213.             }
  214.          }
  215.  
  216.          if (var3) {
  217.             Object var10 = null;
  218.          }
  219.  
  220.          if (var2) {
  221.             this.writeOutput(34);
  222.          }
  223.  
  224.       } else {
  225.          this.writeOutput(34);
  226.          this.writeOutput(34);
  227.       }
  228.    }
  229.  
  230.    final void serializeNull() throws IOException {
  231.       this.writeOutput(64);
  232.    }
  233.  
  234.    private final void serializeObjectInternal(Object var1) throws IOException {
  235.       if (var1 instanceof String) {
  236.          this.serializeString((String)var1);
  237.       } else if (var1 instanceof Hashtable) {
  238.          this.serializeHashtable((Hashtable)var1);
  239.       } else if (var1 instanceof Object[]) {
  240.          this.serializeArray(var1);
  241.       } else if (var1 instanceof Vector) {
  242.          this.serializeVector((Vector)var1);
  243.       } else if (var1 == null) {
  244.          this.serializeNull();
  245.       } else {
  246.          this.serializeString(var1.toString());
  247.       }
  248.    }
  249.  
  250.    public void flush() throws IOException {
  251.       this.flushBuffer();
  252.       super.flush();
  253.    }
  254.  
  255.    public void writeObject(Object var1) throws IOException {
  256.       this.serializeObjectInternal(var1);
  257.    }
  258.  
  259.    public static String serializeObject(Object var0) {
  260.       Object var1 = null;
  261.       String var6;
  262.       if (var0 == null) {
  263.          var6 = null;
  264.       } else {
  265.          ByteArrayOutputStream var2 = new ByteArrayOutputStream(256);
  266.          Serializer var3 = new Serializer(var2);
  267.  
  268.          try {
  269.             var3.writeObject(var0);
  270.             var3.flush();
  271.          } catch (IOException var5) {
  272.          }
  273.  
  274.          var6 = var2.toString();
  275.  
  276.          try {
  277.             ((FilterOutputStream)var3).close();
  278.             ((OutputStream)var2).close();
  279.          } catch (IOException var4) {
  280.          }
  281.  
  282.          Object var7 = null;
  283.          Object var8 = null;
  284.       }
  285.  
  286.       return var6;
  287.    }
  288.  
  289.    public static boolean writeObject(OutputStream var0, Object var1) {
  290.       try {
  291.          Serializer var2 = new Serializer(var0);
  292.          var2.writeObject(var1);
  293.          var2.flush();
  294.          return true;
  295.       } catch (IOException var3) {
  296.          return false;
  297.       }
  298.    }
  299.  
  300.    static {
  301.       for(int var0 = 0; var0 < 32; ++var0) {
  302.          unsafeChars[var0] = true;
  303.       }
  304.  
  305.       unsafeChars[32] = true;
  306.       unsafeChars[34] = true;
  307.       unsafeChars[91] = true;
  308.       unsafeChars[93] = true;
  309.       unsafeChars[44] = true;
  310.       unsafeChars[40] = true;
  311.       unsafeChars[41] = true;
  312.       unsafeChars[123] = true;
  313.       unsafeChars[125] = true;
  314.       unsafeChars[61] = true;
  315.       unsafeChars[59] = true;
  316.       unsafeChars[47] = true;
  317.       unsafeChars[64] = true;
  318.       unsafeChars[33] = true;
  319.       unsafeChars[35] = true;
  320.       unsafeChars[36] = true;
  321.       unsafeChars[37] = true;
  322.       unsafeChars[38] = true;
  323.       unsafeChars[39] = true;
  324.       unsafeChars[58] = true;
  325.       unsafeChars[60] = true;
  326.       unsafeChars[62] = true;
  327.       unsafeChars[63] = true;
  328.       unsafeChars[92] = true;
  329.       unsafeChars[94] = true;
  330.       unsafeChars[96] = true;
  331.       unsafeChars[124] = true;
  332.       unsafeChars[126] = true;
  333.    }
  334. }
  335.