3 const PR_RDONLY
= 0x1; // see prio.h
6 var dirSvc
= Components
.classes
["@mozilla.org/file/directory_service;1"]
7 .getService(Components
.interfaces
.nsIProperties
);
8 return dirSvc
.get(key
, Components
.interfaces
.nsILocalFile
);
11 // Does some sanity checks on the stream and returns the number of bytes read
12 // when the checks passed.
13 function test_stream(stream
) {
14 // This test only handles blocking streams; that's desired for file streams
16 do_check_eq(stream
.isNonBlocking(), false);
18 // Wrap it in a binary stream (to avoid wrong results that
19 // scriptablestream would produce with binary content)
20 var binstream
= Components
.classes
['@mozilla.org/binaryinputstream;1']
21 .createInstance(Components
.interfaces
.nsIBinaryInputStream
);
22 binstream
.setInputStream(stream
);
26 do_check_eq(stream
.available(), binstream
.available());
27 var avail
= stream
.available();
28 do_check_neq(avail
, -1);
30 // PR_UINT32_MAX and PR_INT32_MAX; the files we're testing with aren't that
32 do_check_neq(avail
, Math
.pow(2, 32) - 1);
33 do_check_neq(avail
, Math
.pow(2, 31) - 1);
36 // For blocking streams, available() only returns 0 on EOF
37 // Make sure that there is really no data left
38 var could_read
= false;
40 binstream
.readByteArray(1);
43 // We expect the exception, so do nothing here
46 do_throw("Data readable when available indicated EOF!");
50 dump("Trying to read " + avail
+ " bytes\n");
51 // Note: Verification that this does return as much bytes as we asked for is
52 // done in the binarystream implementation
53 var data
= binstream
.readByteArray(avail
);
60 function stream_for_file(file
) {
61 var str
= Components
.classes
["@mozilla.org/network/file-input-stream;1"]
62 .createInstance(Components
.interfaces
.nsIFileInputStream
);
63 str
.init(file
, PR_RDONLY
, 0, 0);
67 function stream_from_channel(file
) {
68 var ios
= Components
.classes
["@mozilla.org/network/io-service;1"]
69 .getService(Components
.interfaces
.nsIIOService
);
70 var uri
= ios
.newFileURI(file
);
71 return ios
.newChannelFromURI(uri
).open();
75 // Get a file and a directory in order to do some testing
76 var file
= getDir("XpcomLib");
77 var len
= file
.fileSize
;
78 do_check_eq(test_stream(stream_for_file(file
)), len
);
79 do_check_eq(test_stream(stream_from_channel(file
)), len
);
80 var dir
= file
.parent
;
81 test_stream(stream_from_channel(dir
)); // Can't do size checking