Javascript中的二进制I / O. [英] Binary I/O in Javascript

查看:86
本文介绍了Javascript中的二进制I / O.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




有没有人编写成功操作二进制文件数据的代码

使用Javascript?


它可能---并且在进行I / O的情况下,将---使用浏览器 -

特定功能(ActiveX / COM与Internet Explorer,XPCOM / XPConnect

使用Mozilla / Firefox)。


我正在编写客户端代码,它将生成二进制数据,用于通过OBJECT元素生成

a GIF文件。 (GIF是一个线的图像和

点在双轴图上。)


为了做一些测试,看看OBJECT是怎样的元素执行,我是试图读取二进制文件数据的
(在Firefox中使用XPConnect函数)。


问题是普通的Javascript和这些浏览器 - 特定的函数

不会相互结合(例如,它们不能被操作

作为字符串,而这些东西的read()函数返回字符串为

好​​)。这是我所知道的。日期发布在各个

新闻组上的主题得到评论说,如果那些创建文件

I / O库在客户端代码中的文本文件I / O也可能使用它可以操作I / O中的二进制数据,因为不返回

授权对文件系统的任何访问,而不是文本或文本系统

二进制访问。


如果我在这里得不到答案,我会把它带到JScript和XPCOM

组,我希望后者比前者更多。如果它不能在MSIE中完成
,我想我应该忘记它(并尝试

编写一个类似的插件)。除非我听说全世界不到10万人使用的MSIE浏览器是




Has anyone written code that successfully manipulates binary file data
using Javascript?

It might---and in the case of doing I/O, will---make use of browser-
specific functions (ActiveX/COM with Internet Explorer, XPCOM/XPConnect
with Mozilla/Firefox).

I am writing client-side code that will generate binary data for producing
a GIF file through an OBJECT element. (The GIF is an image of a line and
points on a two-axis plot.)

In order to do some tests to see how the OBJECT element performs, I am
trying to read in binary file data (using XPConnect functions in Firefox).

The problem is that plain Javascript and these browser-specific functions
will not marry with one another (for instance, they cannot be manipulated
as strings, and the read() functions of these things return strings as
well). This is from what little I know. Dated posted in various
newsgroups on the subject get comments saying that if those creating file
I/O libraries in client-side code for text file I/O might as well make it
possible for manipulating binary data in I/O, since the point of no return
is authorizing any access to the file system, and not whether it is text or
binary access.

If I don''t get an answer here, I will take this to the JScript and XPCOM
groups, where I can hope for more from the latter than the former. If it
can''t be done in MSIE though, I suppose I should forget it (and try to
write a plugin of some kind instead). Unless I hear that MSIE browsers are
being used by less than 100,000 people around the world.

推荐答案



Patient Guy写道:

Patient Guy wrote:
有没有人使用Javascript编写成功操作二进制文件数据的代码?

它可能---并且在做I / O的情况下,将---利用浏览器特定的功能(带有Internet Explorer的ActiveX / COM,带有Mozilla / Firefox的XPCOM / XPConnect)。
我正在编写客户端代码,它将生成二进制数据,用于通过OBJECT元素生成GIF文件。 (GIF是一个线条的图像,并且在两轴图上有点。)

为了做一些测试来看看OBJECT元素的表现如何,我是
尝试读取二进制文件数据(在Firefox中使用XPConnect函数)。

问题是普通的Javascript和这些特定于浏览器的函数不会相互结合(例如,它们不能被操作为字符串,而这些东西的read()函数返回字符串
。这是我所知道的。日期发布在各个新闻组的主题上得到评论说,如果那些在文本文件I / O的客户端代码中创建文件的I / O库也可能使它成为可能的在I / O中操作二进制数据,因为不返回
授权对文件系统的任何访问,而不是文本或二进制访问。

如果我不会在这里得到答案,我会把它带到JScript和XPCOM
组,我希望后者比前者更多。如果它不能在MSIE中完成,我想我应该忘记它(并尝试编写某种类型的插件)。除非我听说世界上不到10万人使用MSIE浏览器。
Has anyone written code that successfully manipulates binary file data
using Javascript?

It might---and in the case of doing I/O, will---make use of browser-
specific functions (ActiveX/COM with Internet Explorer, XPCOM/XPConnect
with Mozilla/Firefox).

I am writing client-side code that will generate binary data for producing
a GIF file through an OBJECT element. (The GIF is an image of a line and
points on a two-axis plot.)

In order to do some tests to see how the OBJECT element performs, I am
trying to read in binary file data (using XPConnect functions in Firefox).

The problem is that plain Javascript and these browser-specific functions
will not marry with one another (for instance, they cannot be manipulated
as strings, and the read() functions of these things return strings as
well). This is from what little I know. Dated posted in various
newsgroups on the subject get comments saying that if those creating file
I/O libraries in client-side code for text file I/O might as well make it
possible for manipulating binary data in I/O, since the point of no return
is authorizing any access to the file system, and not whether it is text or
binary access.

If I don''t get an answer here, I will take this to the JScript and XPCOM
groups, where I can hope for more from the latter than the former. If it
can''t be done in MSIE though, I suppose I should forget it (and try to
write a plugin of some kind instead). Unless I hear that MSIE browsers are
being used by less than 100,000 people around the world.




我正在将我的jsFileManager作为通用FSO / XPConnect包装。

我真的很想在本周结束之前完成Gesko部分,如果我能够在我的主要工作后能够保持迟到的话。 IE部分已经完成。


简单来说 - 是的,它是完全可能的通用方式(尽管

内部机制与黑白不同,加上buggy

作为微软方面的街头狗至少)。


jsFileManager将是免费开放的库,所以如果你有一些

燃烧的问题 - 我很高兴尝试回答。



I''m doing my jsFileManager as universal FSO/XPConnect wrapper.
I''d really like to finish up the Gesko part by this week-end if I''ll be
able to keep staying late after my main job. IE part is already done.

Briefly - yes, it is perfectly possible in the universal way (despite
the internal mechanics is as different as black and white, plus buggy
as a street dog from the Microsoft side at least).

jsFileManager will be free open library any way so if you have some
burning questions - I''m glad to try to answer.


作为插件:

实际上我的第一个版本计划是文本文件读/写

加上lounch应用程序。二进制数据读/写可以完成

另外因为它真的不是一个挑战。


文件系统中的任何文件都是二进制的,意思是它包含
在某些序列中分配的
字节 - 无论是文本文件还是

图片或应用程序 - 都没关系。因此,您可以将任何文件作为文本或二进制文件读取



TextStream仅在使用内部标记生成器时与ByteStream不同。

此标记生成器TextStream采用不同的换行符和

特殊字符序列(如果不是ASCII编码)。另外阅读从

一个系统换行到另一个系统换行。

所以要处理ByteStream你需要按块来读/写数据
字节(比如说每个块80个字节)不是行。并且你必须确保

编码设置为基于ASCII以避免字节转换由

tokenizer。这都是字节I / O的真正传说。

As an addon:
Actually my first realease planned to be text files reading/writing
plus lounch applications. Binary data reading/writing can be done
additionally as it really not a challenge.

Any file in the file system is binary in meaning that it consists of
bytes allocated in some sequences - either it''s a text file or a
picture or an application - it doesn''t matter. So you can read any file
as text or as binary.
TextStream differs from ByteStream only in usage of internal tokenizer.
This tokenizer makes TextStream tread differently line breaks and
special char sequences (if not ASCII encoding). Also reading goes from
one system line break to another.
So to handle ByteStream you simply need to read/write data by blocks of
bytes (say 80 bytes per block) not by lines. And you have to ensure
that the encoding set to base ASCII to avoid bytes transformations by
tokenizer. This is all real lore of byte I/O.


Patient Guy写道:
Patient Guy wrote:
有没有人写过代码使用Javascript成功操作二进制文件数据?
它可能---并且在做I / O的情况下,将---利用浏览器特定的功能(ActiveX / COM与Internet Explorer,XPCOM / XPConnect
与Mozilla / Firefox)。
Has anyone written code that successfully manipulates binary file data
using Javascript? It might---and in the case of doing I/O, will---make use of browser-
specific functions (ActiveX/COM with Internet Explorer, XPCOM/XPConnect
with Mozilla/Firefox).



[snip]


您当然可以粗略地操纵二进制数据,使用

Javascript字符串。 Javascript字符串格式是Unicode对,我相信。即每个字符可以保存一个0000到FFFF的值,所以二进制流可以表示为一个序列,其中

更高的对是00,字节是更低,即0000,0001,0002

.... 00FF,或每对字节编码为Unicode对,

eg 0001,0203,0304,... FEFF。后一种情况对于

奇数编号的流是有问题的。


然而诀窍是从文件中取出二进制代码
$ b $首先是String对象的b,无论是否有效,或者

有效。


我一直在试验自己,到目前为止只有Javascript没有

似乎就够了。


INTERNET EXPLORER


有两个ActiveX对象要考虑: -


(a)Scripting.FileSystemObject


通过Javascript,似乎无法使用它来保存

字符串包含二进制流。如果编码设置为ASCII,则会为80-A0范围内的char代码抛出

错误,您尝试将
写入文件。如果编码是Unicode,那么生成的文件是

前缀为FF FE,它只适用于偶数编号的流。


你可以用它来加载二进制到字符串中,但是你需要再次使用
监视字符80-A0,因为它们在加载期间被转换为一些其他的unicode字符。这是一贯的,

所以一旦你知道转换,即80-> 2030等,你可以很容易地反转它。


然而,奇怪的是,在VBScript中,将编码设置为ASCII,您可以使用Scripting.FileSystemObject实际写入二进制文件
。我认为这个

是因为VBScript有其他数据类型,这些类型对于ActiveX对象来说更可接受。


所以你可以采用VBScript和Javascript的混合来获得一种

二进制i / o。


(a)ADODB.Stream

这提供了完整的二进制文件系统访问。不过我认为在Windows的更高版本中默认禁用了
。无论如何,如果你想从字符串中将二进制数据写入
,那么它在Javascript中又有一个限制:WriteText方法再次失败并且

80-A0范围内的字符。


(c)XML数据类型


要探索的第三个方面是设置一个XML节点数据类型为bin.hex或

bin.base64。您可以将String转换为base64或hex,并使用该编码字符串设置

nodeTypedValue。这对于

HTTPRequests更有用,但转换为base64和hex非常慢。当你随后下注nodeTypedValue时,它将返回一个包含二进制文件的Variant字节

缓冲区。在Javascript中无用,但可能是

可用于ADODB.Stream或VBScript。


MOZILLA


我是不熟悉XPCOM组件 - 请参阅 www.xulplanet.com

但它们可能有一些可编写脚本的输入和输出流。再次

我们不知道他们将如何与Javascript String类型进行交互。


Julian


[snip]

You can certainly manipulate binary data in a rough fashion, using a
Javascript String. A Javascript String format is in Unicode pairs I
believe. I.e. each character can hold a value from 0000 to FFFF, so
the binary stream could be represented either as a sequence where the
higher pair is 00, and the byte is the lower, i.e. 0000, 0001, 0002
.... 00FF, or each pair of bytes is encoded in to the Unicode pairs,
e.g. 0001, 0203, 0304, ... FEFF. The latter case is problematic for
odd numbered streams.

The trick however is getting the binary codes from files into and out
of the String object in the first place, whether at all, or
efficiently.

I have been experimenting myself, and so far Javascript alone does not
seem to be enough.

INTERNET EXPLORER

There are two ActiveX objects to consider:-

(a) Scripting.FileSystemObject

Through Javascript, it seems to be impossible to use this to save
strings containing a binary stream. If encoding is set to ASCII, an
error will be thrown for char codes in the range 80 - A0 which you try
to write to a file. If encoding is Unicode, then the resulting file is
prefixed with FF FE, and it only works for even numbered streams.

You can use it to load binary into Strings, but you need again to
watch for characters 80-A0, as they are converted during the load
process to some other unicode characters. This is consistently done,
so once you know the conversion, i.e. 80->2030 etc, you can easily
reverse it.

However, oddly, in VBScript, with encoding set to ASCII you can
actually write binary using Scripting.FileSystemObject. I think this
is because VBScript has other data types which are more acceptable to
the ActiveX object.

So you can adopt a mixture of VBScript and Javascript to get a kind of
binary i/o.

(a) ADODB.Stream

This gives full binary file system access. I think however it is
disabled by default on later versions of windows. In any event, it has
one limitation again in Javascript if you want to write binary data
from a String to the stream: the WriteText method again fails with
characters in the range 80-A0.

(c) XML dataType

A third area to explore is setting an XML node data type to bin.hex or
bin.base64. You could convert a String to base64 or hex, and set the
nodeTypedValue with that encoded string. This is more useful for
HTTPRequests, but conversion to base64 and hex is very slow. When you
subsequently bet the nodeTypedValue it will return a Variant byte
buffer containing the binary. Useless in Javascript, but might be
useable with ADODB.Stream or VBScript.

MOZILLA

I am not as familiar with the XPCOM components - see www.xulplanet.com,
but they may have some scriptable input and output streams. Again
don''t know how they would interact with the Javascript String type.

Julian


这篇关于Javascript中的二进制I / O.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆