Forum Stats

  • 3,851,936 Users
  • 2,264,053 Discussions


Opera and content encoding

843807 Member Posts: 46,582
edited Sep 29, 2002 10:19AM in Java Applet Development
i have an applet which fetches and parses xml, via the following method.

private void loadURL(String s) throws Exception {
URL url = new URL(getDocumentBase(),s);
System.out.println("loading "+url);
System.out.println("protocol "+url.getProtocol());
URLConnection conn = url.openConnection();
System.out.println("content encoding : "+conn.getContentEncoding());
System.out.println("content type : "+conn.getContentType());

InputStream istr = conn.getInputStream();
InputStreamReader r = new InputStreamReader(istr);

this works on ie5.5 (internal jvm and Sun plugin), ns4.7, ns6, opera6
(i have jre 1.4)

bug #1 : in opera, both content type and encoding are null
whereas the other browsers correctly report it as text/xml

bug #2 : when the webserver has xml compression turned on, opera (but not the others) returns a compressed stream to the applet.
(this was also reported with ns4.61 jre 1.1.8 on os/2)

so how do i detect this ? if i know it's zipped, i can unzip it, but where is my header info ??



  • 843807
    843807 Member Posts: 46,582
    Hi Adrian,

    Unfortunately I think it is not the only problem you can get with Opera, for instance if you write :
    it always return 0 with Opera !!
    But if you are sure of your file enconding (without making any test on conn.getContentEncoding() for instance) your applet may work anyway, you have to test it. (However you can test how ends your url, testing the extension for instance).
    I think, even the browser has its VM or use a plugin, the browser sends and receives informations to/from the web server and sometimes gives the applet wrong informations
  • 843807
    843807 Member Posts: 46,582
    yeah. my problem is that other browsers unzip the file before i get it, so the content encoding is transparent. i suppose i'll have to manually look at the stram & figure out if it's zipped, then act accordingly.
    if (first_5_bytes != "<?xml") { try to unzip..}


  • 843807
    843807 Member Posts: 46,582
    yes or just a little better :
    if (!firstline.startWith("<?xml")){ try to unzip..}

  • 843807
    843807 Member Posts: 46,582
    this works, but it's crude....
            InputStream istr = conn.getInputStream();
    /* first problem - we need mark/reset to peek at the data */
            InputStream istr1;  
            if (!istr.markSupported())
                istr1 = new BufferedInputStream(istr);
                istr1 = istr;
            byte b[] = new byte[5];
            System.out.println("data '"+(char)b[0]+","+(char)b[1]+","+(char)b[2]+","+(char)b[3]+","+(char)b[4]+"'");
    /* now we can test for zip  */ 
            InputStream istr2;
            int w1 = (((int)b[1] & 0xff) << 8) | b[0];
            if (w1 == GZIPInputStream.GZIP_MAGIC)
                istr2 = new GZIPInputStream(istr1,1024);
                istr2 = istr1;
    /* now read  */ 
            InputStreamReader r = new InputStreamReader(istr2);
    any better ideas ?

This discussion has been closed.