使用 nsIFileInputStream & 时加载的数据被截断nsIConverter输入流

发布于 2024-08-02 20:06:36 字数 1351 浏览 4 评论 0原文

我正在开发一个项目(BrowserIO - 如果您想查看代码并进行操作,请访问 browserio dot googlecode dot com。欢迎帮助!),根据他们的示例,我将 Firefox 的 nsIFileInputStream 与 nsIConverterInputStream 结合使用(https://developer.mozilla.org/en/Code_snippets/File_I %2F%2FO#Simple),但仅加载完整数据的一部分。代码是:

var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(path);
var data = "";

var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
var cstream = Components.classes["@mozilla.org/intl/converter-input-stream;1"].createInstance(Components.interfaces.nsIConverterInputStream);

fstream.init(file, -1, 0, 0);
cstream.init(fstream, "UTF-8", 0, 0); // you can use another encoding here if you wish

var str = {};
cstream.readString(-1, str); // read the whole file and put it in str.value
data = str.value;

cstream.close(); // this closes fstream

如果您想看到此行为,请从 BrowserIO 项目页面查看代码,并使用 Firebug 在 file_io.js 中的 data = str.value; 行设置断点。然后从列表中选择一个文本文件,然后单击“打开”按钮。在 Firebug 中,在监视面板中设置 str.value 的监视。看看文件...它应该被截断,除非它真的很短。

作为参考,上面的代码是 trunk/scripts/file_io.js 中 openFile() 函数的主体。

有人知道这是怎么回事吗?

I'm working on a project (BrowserIO - go to browserio dot googlecode dot com if you want to check out the code and work on it. Help welcome!) in which I'm using Firefox's nsIFileInputStream in tandem with nsIConverterInputStream, per their example (https://developer.mozilla.org/en/Code_snippets/File_I%2F%2FO#Simple), but only a portion of the full data is being loaded. The code is:

var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(path);
var data = "";

var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
var cstream = Components.classes["@mozilla.org/intl/converter-input-stream;1"].createInstance(Components.interfaces.nsIConverterInputStream);

fstream.init(file, -1, 0, 0);
cstream.init(fstream, "UTF-8", 0, 0); // you can use another encoding here if you wish

var str = {};
cstream.readString(-1, str); // read the whole file and put it in str.value
data = str.value;

cstream.close(); // this closes fstream

If you want to see this behavior, checkout the code from the BrowserIO project page, and use Firebug to set a breakpoint at the data = str.value; line in file_io.js. Then select a text file from the list, and click the "Open" button. In Firebug, in the watch panel set a watch for str.value. Look at the file... It should be truncated, unless it's really short.

For reference, the code above is the main body of the openFile() function in trunk/scripts/file_io.js.

Anybody have any clue what's happening with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

゛时过境迁 2024-08-09 20:06:36

请参阅 nsIConverterInputStream;基本上,-1 并不意味着“给我一切”,而是“给我默认数量”,文档声称是 8192。

更一般地说,如果你想耗尽输入流的内容,你必须循环直到它是空的。任何流契约中的任何内容都不能保证调用返回的数据量是流的全部内容;如果需要的话,它甚至可以返回比立即可用的更少的值。

See nsIConverterInputStream; basically, -1 doesn't mean "give me everything" but rather "give me the default amount", which the docs claim is 8192.

More generally, if you want to exhaust the contents of an input stream, you have to loop until it's empty. Nothing in any of the stream contracts guarantees that the amount of data returned by a call is the entirety of the contents of the stream; it could even return less than it has immediately available if it wanted.

就此别过 2024-08-09 20:06:36

我发现了如何在不转换的情况下读取文件,以避免因不知道文件编码类型而出现问题。答案是将 nsIScriptableInputStream 与 nsIFileInputStream 结合使用:

var sstream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
fstream.init(file, 0x01, 0004, 0);
sstream.init(fstream);
data = sstream.read(sstream.available());

I discovered how to do the file read without converting, to avoid issues from not knowing the file encoding type. The answer is to use nsIScriptableInputStream with nsIFileInputStream:

var sstream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
fstream.init(file, 0x01, 0004, 0);
sstream.init(fstream);
data = sstream.read(sstream.available());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文