6 <title>ArrayBuffer stream test
</title>
7 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
8 <link rel=
"stylesheet" type=
"text/css" href=
"/tests/SimpleTest/test.css" />
10 <script type=
"text/javascript">
11 function detachArrayBuffer(ab
)
13 var w
= new Worker("data:application/javascript,");
14 w
.postMessage(ab
, [ab
]);
19 var ab
= new ArrayBuffer(4000);
20 var ta
= new Uint8Array(ab
);
21 ta
[0] = 'a'.charCodeAt(0);
22 ta
[1] = 'b'.charCodeAt(0);
24 const Cc
= SpecialPowers
.Cc
, Ci
= SpecialPowers
.Ci
;
25 var abis
= Cc
["@mozilla.org/io/arraybuffer-input-stream;1"]
26 .createInstance(Ci
.nsIArrayBufferInputStream
);
28 var sis
= Cc
["@mozilla.org/scriptableinputstream;1"]
29 .createInstance(Ci
.nsIScriptableInputStream
);
32 is(sis
.read(1), "", "should read no data from an uninitialized ABIS");
34 abis
.setData(ab
, 0, 256 * 1024);
36 is(sis
.read(1), "a", "should read 'a' after init");
38 detachArrayBuffer(ab
);
40 SpecialPowers
.forceGC();
41 SpecialPowers
.forceGC();
45 is(sis
.read(1), "b", "should read 'b' after detaching buffer");
49 ok(false, "reading from stream should have worked");
52 // A regression test for bug 1265076. Previously, overflowing
53 // the internal buffer from readSegments would cause incorrect
54 // copying. The constant mirrors the value in
55 // ArrayBufferInputStream::readSegments.
57 ab
= new ArrayBuffer(2 * size
);
58 ta
= new Uint8Array(ab
);
61 for (i
= 0; i
< size
; ++i
) {
62 ta
[i
] = 'x'.charCodeAt(0);
64 for (i
= 0; i
< size
; ++i
) {
65 ta
[size
+ i
] = 'y'.charCodeAt(0);
68 abis
= Cc
["@mozilla.org/io/arraybuffer-input-stream;1"]
69 .createInstance(Ci
.nsIArrayBufferInputStream
);
70 abis
.setData(ab
, 0, 2 * size
);
72 sis
= Cc
["@mozilla.org/scriptableinputstream;1"]
73 .createInstance(Ci
.nsIScriptableInputStream
);
76 var result
= sis
.read(2 * size
);
77 is(result
, "x".repeat(size
) + "y".repeat(size
), "correctly read the data");
85 <div id=
"content" style=
"display: none"></div>