byte-streams not working

13 views
Skip to first unread message

arun

unread,
Feb 5, 2011, 8:21:13 AM2/5/11
to mozilla-labs-jetpack
I need to read a file in chunks from local file system.I used text-
streams which always returns empty string.I used byte-stream which
throws error shown below.Can't make sense what is the issue.I have
pasted sample code here http://pastebin.com/siGh9Vbm


Error: Error reading from stream: [Exception... "'JavaScript component
does not
have a method named: "available"' when calling method:
[nsIInputStream::availabl
e]" nsresult: "0x80570030
(NS_ERROR_XPC_JSOBJECT_HAS_NO_FUNCTION_NAMED)" locat
ion: "JS frame :: resource://jid0-gsou2rxu0ucznub4ermuc69s5zi-api-utils-lib/secu
rable-module.js -> resource://jid0-gsou2rxu0ucznub4ermuc69s5zi-api-utils-lib/byt
e-streams.js :: ByteReader_read :: line 66" data: no]
[Exception... "'Error: The stream is closed and cannot be used.' when
calling me
thod: [nsIInputStream::close]" nsresult: "0x8057001c
(NS_ERROR_XPC_JS_THREW_JS_
OBJECT)" location: "JS frame :: resource://jid0-gsou2rxu0ucznub4ermuc69s5zi-api
-utils-lib/securable-module.js -> resource://jid0-gsou2rxu0ucznub4ermuc69s5zi-ap
i-utils-lib/byte-streams.js :: StreamManager_unload :: line 130"
data: no] (und
efined:130)

Drew Willcoxon

unread,
Feb 7, 2011, 8:33:28 PM2/7/11
to mozilla-la...@googlegroups.com
file.open() returns a ByteReader or TextReader depending on its second
argument. In your sample code, you're passing a TextReader to the
ByteReader constructor and a TextReader to the TextReader constructor.
You don't need to use these constructors, which take XPCOM streams
(nsIInputStreams), if you're using file.open().

https://jetpack.mozillalabs.com/sdk/1.0b2/docs/#module/api-utils/file

This should work:

var file = require("file");
var path = "c:\\test.txt";
readBytes(path);
readText(path);

function readBytes(filePath) {
var byteReader = file.open(filePath, "b");
console.log("readBytes:\n" + byteReader.read() + "\n");
byteReader.close();
}

function readText(filePath) {
var textReader = file.open(filePath);
console.log("readText:\n" + textReader.read() + "\n");
textReader.close();
}

Drew

Reply all
Reply to author
Forward
0 new messages