home *** CD-ROM | disk | FTP | other *** search
/ Online Today 2000 January / Onto0100.iso / pc / JAVA / MSJAVX86.EXE / xmldso.cab / XML4IE3.cab / com / ms / xml / util / XMLInputStream.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-10-10  |  3.7 KB  |  269 lines

  1. package com.ms.xml.util;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.io.OutputStream;
  8.  
  9. public class XMLInputStream extends InputStream {
  10.    static final int INPUTSR = 1;
  11.    static final int UCS2 = 2;
  12.    static final int ASCII = 3;
  13.    static final int INPUTSR_POP = 4;
  14.    static final int UCS2_POP = 5;
  15.    static final int ASCII_POP = 6;
  16.    private int[] next = new int[4];
  17.    private int index = -1;
  18.    // $FF: renamed from: in java.io.InputStream
  19.    private InputStream field_0;
  20.    private InputStreamReader insr;
  21.    private int pos;
  22.    private String encoding;
  23.    private boolean littleendian;
  24.    private boolean byteOrderMark;
  25.    private int readState;
  26.    boolean jdk11;
  27.  
  28.    public XMLInputStream(InputStream var1) {
  29.       String var2 = System.getProperty("java.version");
  30.       this.jdk11 = var2.equals("1.1");
  31.       this.littleendian = false;
  32.       this.byteOrderMark = false;
  33.       this.encoding = "UTF-8";
  34.       this.field_0 = (InputStream)var1;
  35.       this.insr = null;
  36.       this.readState = 3;
  37.       boolean var3 = false;
  38.  
  39.       try {
  40.          char var4 = (char)((InputStream)var1).read();
  41.          char var5 = (char)((InputStream)var1).read();
  42.          char var6 = (char)((InputStream)var1).read();
  43.          char var7 = (char)((InputStream)var1).read();
  44.          if (var4 == 254 && var5 == 255 && var6 == 0 && var7 == '<') {
  45.             this.littleendian = false;
  46.             this.byteOrderMark = true;
  47.             this.readState = 5;
  48.             this.encoding = "UCS-2";
  49.          } else if (var4 == 255 && var5 == 254 && var6 == '<' && var7 == 0) {
  50.             this.littleendian = true;
  51.             this.byteOrderMark = true;
  52.             this.readState = 5;
  53.             this.encoding = "UCS-2";
  54.             this.field_0 = new ByteSwapInputStream((InputStream)var1);
  55.          } else if (var4 == 0 && var5 == '<' && var6 == 0 && var7 == '?') {
  56.             this.littleendian = false;
  57.             this.readState = 5;
  58.             this.encoding = "UCS-2";
  59.          } else if (var4 == '<' && var5 == 0 && var6 == '?' && var7 == 0) {
  60.             this.littleendian = true;
  61.             this.readState = 5;
  62.             this.encoding = "UCS-2";
  63.             this.field_0 = new ByteSwapInputStream((InputStream)var1);
  64.          } else if (var4 == '<' && var5 == '?' && Character.toUpperCase(var6) == 'X' && Character.toUpperCase(var7) == 'M') {
  65.             this.readState = 6;
  66.             this.encoding = "ASCII";
  67.          } else if (var4 == 0 && var5 == 0 && var6 == 0 && var7 == '<') {
  68.             this.readState = 6;
  69.             this.encoding = "UCS-4";
  70.          } else if (var4 == '<' && var5 == 0 && var6 == 0 && var7 == 0) {
  71.             this.readState = 6;
  72.             this.encoding = "UCS-4";
  73.          } else if (var4 == 0 && var5 == 0 && var6 == '<' && var7 == 0) {
  74.             this.readState = 6;
  75.             this.encoding = "UCS-4";
  76.          } else if (var4 == 0 && var5 == '<' && var6 == 0 && var7 == 0) {
  77.             this.readState = 6;
  78.             this.encoding = "UCS-4";
  79.          } else if (var4 == 'L' && var5 == 'o' && var6 == 231 && var7 == 212) {
  80.             this.readState = 6;
  81.             this.encoding = "EBCDIC";
  82.          } else {
  83.             var3 = true;
  84.          }
  85.  
  86.          if (!this.encoding.equals("UCS-2")) {
  87.             this.push(var7);
  88.             this.push(var6);
  89.             this.push(var5);
  90.             this.push(var4);
  91.          } else if (this.littleendian) {
  92.             this.push(var6);
  93.             this.push(var7);
  94.             if (!this.byteOrderMark) {
  95.                this.push(var4);
  96.                this.push(var5);
  97.             }
  98.          } else {
  99.             this.push(var7);
  100.             this.push(var6);
  101.             if (!this.byteOrderMark) {
  102.                this.push(var5);
  103.                this.push(var4);
  104.             }
  105.          }
  106.       } catch (IOException var9) {
  107.          var3 = true;
  108.       }
  109.  
  110.       this.pos = -1;
  111.       if (var3) {
  112.          try {
  113.             if (!this.jdk11) {
  114.                throw new IOException("Readers not supported in JDK 1.0");
  115.             }
  116.  
  117.             if (!((InputStream)var1).markSupported()) {
  118.                var1 = new BufferedInputStream((InputStream)var1);
  119.                this.field_0 = (InputStream)var1;
  120.             }
  121.  
  122.             ((InputStream)var1).mark(4096);
  123.             this.insr = new InputStreamReader((InputStream)var1, "UTF8");
  124.             this.readState = 4;
  125.             this.encoding = "UTF-8";
  126.             return;
  127.          } catch (IOException var8) {
  128.             this.readState = 6;
  129.             this.encoding = "ASCII";
  130.          }
  131.       }
  132.  
  133.    }
  134.  
  135.    public void setEncoding(String var1) throws IOException {
  136.       this.insr = null;
  137.       if (!var1.equalsIgnoreCase("ISO-10646-UCS-2") && !var1.equalsIgnoreCase("UCS-2")) {
  138.          String var2;
  139.          if (var1.equalsIgnoreCase("Shift_JIS")) {
  140.             var2 = "SJIS";
  141.          } else if (var1.equalsIgnoreCase("ISO-8859-1")) {
  142.             var2 = "8859_1";
  143.          } else {
  144.             if (var1.equalsIgnoreCase("ISO-10646-UCS-4")) {
  145.                throw new IOException("UCS-4 not yet supported");
  146.             }
  147.  
  148.             if (var1.equalsIgnoreCase("UTF-8")) {
  149.                var2 = "UTF8";
  150.             } else {
  151.                var2 = var1;
  152.             }
  153.          }
  154.  
  155.          if (!this.encoding.equalsIgnoreCase("ASCII") && !this.encoding.equalsIgnoreCase("UTF-8")) {
  156.             throw new IOException("Illegal Change of Encoding");
  157.          } else {
  158.             try {
  159.                if (this.jdk11) {
  160.                   if (this.pos != -1) {
  161.                      this.field_0.reset();
  162.                      this.field_0.skip((long)(this.pos + 1));
  163.                   }
  164.  
  165.                   this.insr = new InputStreamReader(this.field_0, var2);
  166.                   this.readState = 1;
  167.                   this.encoding = var1;
  168.                } else {
  169.                   throw new IOException(var2 + " is not supported by your Java Virtual Machine." + "  Try installing the latest VM from http://www.microsoft.com/java/download.htm");
  170.                }
  171.             } catch (Exception var3) {
  172.                throw new IOException("Unrecognized encoding: " + var1);
  173.             }
  174.          }
  175.       } else if (!this.encoding.equalsIgnoreCase("UCS-2")) {
  176.          throw new IOException("Illegal Change of Encoding");
  177.       } else {
  178.          this.readState = 2;
  179.          this.encoding = "UCS-2";
  180.       }
  181.    }
  182.  
  183.    public int read() throws IOException {
  184.       switch (this.readState) {
  185.          case 1:
  186.             ++this.pos;
  187.             return this.insr.read();
  188.          case 2:
  189.             int var3 = this.field_0.read();
  190.             if (var3 == -1) {
  191.                return -1;
  192.             }
  193.  
  194.             int var4 = this.field_0.read();
  195.             return var3 << 8 | var4;
  196.          case 3:
  197.             return this.field_0.read();
  198.          case 4:
  199.             if (this.index >= 0) {
  200.                int[] var6 = this.next;
  201.                int var9 = this.index;
  202.                this.index = var9 + -1;
  203.                return var6[var9];
  204.             }
  205.  
  206.             this.readState = 1;
  207.             return this.read();
  208.          case 5:
  209.             int var1;
  210.             if (this.index >= 0) {
  211.                int[] var10000 = this.next;
  212.                int var10003 = this.index;
  213.                this.index = var10003 + -1;
  214.                var1 = var10000[var10003];
  215.             } else {
  216.                this.readState = 2;
  217.                var1 = this.field_0.read();
  218.             }
  219.  
  220.             if (var1 == -1) {
  221.                return -1;
  222.             }
  223.  
  224.             int var2;
  225.             if (this.index >= 0) {
  226.                int[] var5 = this.next;
  227.                int var8 = this.index;
  228.                this.index = var8 + -1;
  229.                var2 = var5[var8];
  230.             } else {
  231.                this.readState = 2;
  232.                var2 = this.field_0.read();
  233.             }
  234.  
  235.             return var1 << 8 | var2;
  236.          case 6:
  237.          default:
  238.             if (this.index >= 0) {
  239.                int[] var7 = this.next;
  240.                int var10 = this.index;
  241.                this.index = var10 + -1;
  242.                return var7[var10];
  243.             } else {
  244.                this.readState = 3;
  245.                return this.field_0.read();
  246.             }
  247.       }
  248.    }
  249.  
  250.    private void push(char var1) {
  251.       if (this.index == 3) {
  252.          System.exit(0);
  253.       }
  254.  
  255.       this.next[++this.index] = var1;
  256.    }
  257.  
  258.    public XMLOutputStream createOutputStream(OutputStream var1) {
  259.       XMLOutputStream var2 = new XMLOutputStream(var1);
  260.  
  261.       try {
  262.          var2.setEncoding(this.encoding, this.littleendian, this.byteOrderMark);
  263.       } catch (IOException var3) {
  264.       }
  265.  
  266.       return var2;
  267.    }
  268. }
  269.