I'm trying to make the following code work.  It appears to load the data
into in inputStream correctly, as the inputStream.available() ==
data.length (54 in this example).  However, the compressed data is never
sent to my listener.  Instead, it calls onStartRequest() and then
onStopRequest() with status NS_ERROR_FAILURE (generic).

Thanks in advance!

--

var gzip64 =
'H4sIAAAAAAAAAwvJSFVIzCsuTy1SKMlXqMwvLVIoLE0tLsnMz1PILLZSMDQDAGz035IiAAAA';
// base64 encoded gzipped data that = 'The answer to your question is: 16'
var gzip = window.atob(gzip64);
gunzip(gzip);

function gunzip(data) {
        // Store data in an input stream
        var inputStream = 
Components.classes["@mozilla.org/io/string-input-stream;1"].
                createInstance(Components.interfaces.nsIStringInputStream);
        inputStream.setData(data, data.length);
        dd("Input stream has " + inputStream.available() + " available (data is 
length " + data.length + ")");

        // Load input stream onto a pump
        var inputPump = 
Components.classes["@mozilla.org/network/input-stream-pump;1"]
                .createInstance(Components.interfaces.nsIInputStreamPump);
        inputPump.init(inputStream, -1, -1, 0, 0, true);

        // Create a generic stream converter service
        var streamConv = Components.classes["@mozilla.org/streamConverters;1"].
                createInstance(Components.interfaces.nsIStreamConverterService);

        // Create a stream listener to accept gunzip'ed data
        var gzipListener = {
                data: "",
                onStartRequest: function(request, context){
                        dd("gzip onStartRequest() called");
                },
                onDataAvailable: function(request, context, inputStream, 
offset, count){
                        dd("gzip onDataAvailable() called");
                        var inStream = 
Components.classes["@mozilla.org/scriptableinputstream;1"]
                                
.createInstance(Components.interfaces.nsIScriptableInputStream);
                        inStream.init(inputStream);
                        this.data += inStream.read(count);
                },
                onStopRequest: function(request, context, statusCode){
                        dd("gzip onStopRequest() called with status " + 
statusCode);
                        dd("Data is '" + this.data + "'");
                }
        };

        // Create a specific gunzipper with my listener
        var converter = streamConv.asyncConvertData("gzip", "uncompressed", 
gzipListener, null);

        // Set stream converter to read from input pump

        inputPump.asyncRead(converter,null);

        // No way yet to make this actually return data as it's async, but at 
least
        // I'll see the data in the debug if it can get uncompressed
}

_______________________________________________
dev-tech-network mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-network

Reply via email to