michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: */ michael@0: michael@0: function BinaryComparer(file, callback) { michael@0: var fstream = Cc["@mozilla.org/network/file-input-stream;1"]. michael@0: createInstance(Ci.nsIFileInputStream); michael@0: fstream.init(file, -1, 0, 0); michael@0: this.length = file.fileSize; michael@0: this.fileStream = Cc["@mozilla.org/binaryinputstream;1"]. michael@0: createInstance(Ci.nsIBinaryInputStream); michael@0: this.fileStream.setInputStream(fstream); michael@0: this.offset = 0; michael@0: this.callback = callback; michael@0: } michael@0: michael@0: BinaryComparer.prototype = { michael@0: fileStream: null, michael@0: offset: null, michael@0: length: null, michael@0: callback: null, michael@0: michael@0: onStartRequest: function(aRequest, aContext) { michael@0: }, michael@0: michael@0: onStopRequest: function(aRequest, aContext, aStatusCode) { michael@0: this.fileStream.close(); michael@0: do_check_eq(aStatusCode, Components.results.NS_OK); michael@0: do_check_eq(this.offset, this.length); michael@0: this.callback(); michael@0: }, michael@0: michael@0: onDataAvailable: function(aRequest, aContext, aInputStream, aOffset, aCount) { michael@0: var stream = Cc["@mozilla.org/binaryinputstream;1"]. michael@0: createInstance(Ci.nsIBinaryInputStream); michael@0: stream.setInputStream(aInputStream); michael@0: var source, actual; michael@0: for (var i = 0; i < aCount; i++) { michael@0: try { michael@0: source = this.fileStream.read8(); michael@0: } michael@0: catch (e) { michael@0: do_throw("Unable to read from file at offset " + this.offset + " " + e); michael@0: } michael@0: try { michael@0: actual = stream.read8(); michael@0: } michael@0: catch (e) { michael@0: do_throw("Unable to read from converted stream at offset " + this.offset + " " + e); michael@0: } michael@0: if (source != actual) michael@0: do_throw("Invalid value " + actual + " at offset " + this.offset + ", should have been " + source); michael@0: this.offset++; michael@0: } michael@0: } michael@0: } michael@0: michael@0: function comparer_callback() michael@0: { michael@0: do_test_finished(); michael@0: } michael@0: michael@0: function run_test() michael@0: { michael@0: var source = do_get_file(DATA_DIR + "test_bug399727.html"); michael@0: var comparer = new BinaryComparer(do_get_file(DATA_DIR + "test_bug399727.zlib"), michael@0: comparer_callback); michael@0: michael@0: // Prepare the stream converter michael@0: var scs = Cc["@mozilla.org/streamConverters;1"]. michael@0: getService(Ci.nsIStreamConverterService); michael@0: var converter = scs.asyncConvertData("uncompressed", "deflate", comparer, null); michael@0: michael@0: // Open the expected output file michael@0: var fstream = Cc["@mozilla.org/network/file-input-stream;1"]. michael@0: createInstance(Ci.nsIFileInputStream); michael@0: fstream.init(source, -1, 0, 0); michael@0: michael@0: // Set up a pump to push data from the file to the stream converter michael@0: var pump = Cc["@mozilla.org/network/input-stream-pump;1"]. michael@0: createInstance(Ci.nsIInputStreamPump); michael@0: pump.init(fstream, -1, -1, 0, 0, true); michael@0: pump.asyncRead(converter, null); michael@0: do_test_pending(); michael@0: }