using class jsio::File

39 views
Skip to first unread message

Anshuman S. Rawat

unread,
Jun 18, 2012, 5:56:36 AM6/18/12
to jslibs
Hi,

I have been trying to use jsio::File to read to local file and facing
problem. My code is really simple. Here it is:

var sockdata='';
try
{
var FQfilename = _this.zoneFileBasePath + '\\' +filename;
var file = new File(FQfilename);
if (file.exist)
{
file.open(File.RDONLY);
sockdata = file.read();
file.close();
}
}
catch(ex)
{
throw new Error('Unknown exception:' + ex);
}

The file exists and it enters the if condition but does not read
anything (sockdata is null).
What am I missing here?

Thanks,
Anshuman

soubok

unread,
Jun 18, 2012, 8:36:47 AM6/18/12
to jsl...@googlegroups.com
Hi,

Your code seems correct.
Can you tell me which version of jslibs you are using (use print(host._sourceId)), the exact version of your OS and the size of the file you are trying to read.
Note that in the recent version of jslibs the return value of read() (and many other functions) is not string anymore, but an ArrayBuffer object.
You can convert ArrayBuffer (binary data) into JavaScript string using the stringify() function.

note 1:
  I recommend you to replace '\\' with directorySeparator.

note 2:
  file.open(File.RDONLY); 
  sockdata = stringify(file.read());
  file.close();
 can be replaced with:
  sockdata = stringify(file.content);

I keep trying to understand where the read() function may return null...

Franck.

Anshuman S. Rawat

unread,
Jun 18, 2012, 8:57:48 AM6/18/12
to jslibs
Hi,

Thanks for the response. I do not have exact version of jslibs as of
now but it should be a recent version as while trying to print
contents it printed something like "arrarybuffer object". OS is WinXP
SP3. Files are of the order 100KB - 140KB.

Is file size going to be a problem?

>
> *note 1:*
>   I recommend you to replace '\\' with directorySeparator.
>
> *note 2:*
>   file.open(File.RDONLY);
>   sockdata = stringify(file.read());
>   file.close();
>  can be replaced with:
>   sockdata = stringify(file.content);

I will try this. I think lack of stringify was the problem.

>
> I keep trying to understand where the read() function may return null...
>

Sorry for lack for correct terminology. I was not sure what it
returned as it printed out what I described above. I assumed it was
either null or empty (i am new to both jslibs and javascript).

> Franck.

Thanks for the help. Will update on the result.

-Anshuman

soubok

unread,
Jun 18, 2012, 10:21:20 AM6/18/12
to jsl...@googlegroups.com
On Mon, Jun 18, 2012 at 2:57 PM, Anshuman S. Rawat <rawat.a...@gmail.com> wrote:
Hi,

Thanks for the response. I do not have exact version of jslibs as of
now but it should be a recent version as while trying to print
contents it printed something like "arrarybuffer object". OS is WinXP
SP3. Files are of the order 100KB - 140KB.

Is file size going to be a problem?

no, but I have not tried jslibs with very big files (>2GB)
 

>
> *note 1:*
>   I recommend you to replace '\\' with directorySeparator.
>
> *note 2:*
>   file.open(File.RDONLY);
>   sockdata = stringify(file.read());
>   file.close();
>  can be replaced with:
>   sockdata = stringify(file.content);

I will try this. I think lack of stringify was the problem.

>
> I keep trying to understand where the read() function may return null...
>

Sorry for lack for correct terminology. I was not sure what it
returned as it printed out what I described above. I assumed it was
either null or empty (i am new to both jslibs and javascript).

print() function tries to convert its argument into printable data (when possible). This includes ArrayBuffers, TypedArrays, primitive values (like string, number, boolean, ...) and finally it calls toString() or valueOf() against the argument (if it an object) to obtain its string representing. 

 

> Franck.

Thanks for the help. Will update on the result.

-Anshuman

Anshuman S. Rawat

unread,
Jun 18, 2012, 11:04:14 AM6/18/12
to jslibs
Hi,

On Jun 18, 5:36 pm, soubok <sou...@gmail.com> wrote:
>
> *note 1:*
>   I recommend you to replace '\\' with directorySeparator.
>
> *note 2:*
>   file.open(File.RDONLY);
>   sockdata = stringify(file.read());
>   file.close();
>  can be replaced with:
>   sockdata = stringify(file.content);

the above didn't work as it couldn't find stringify() so I tried this
instead:

sockdata = JSON.stringify(file.content);

which crashed the application every time. So I reverted to this:

file.open(File.RDONLY);
var content = file.read();
file.close();
sockdata = JSON.stringify(content);

This put sockdata as '{}' (those are not file contents). Then i found
a function to do the same:

function buffToString(arbuff)
{
var str=new Array();
var buff = new Uint8Array( arbuff );
var index =0;
for ( index=0; index< buff.length ;index++)
{
str[index] = String.fromCharCode(buff[index]);
}
return str.join("");
}

Problem is it looses the newline chars.

I should have mentioned this before but I am trying to use this inside
spidermonkey 1.8.5 running inside OpenVxi engine. And I am trying to
use timezoneJS implementation of date/time (which uses jsio) to parse
through timezone files.

Thanks,
Anshuman

soubok

unread,
Jun 18, 2012, 1:40:47 PM6/18/12
to jsl...@googlegroups.com
On Mon, Jun 18, 2012 at 5:04 PM, Anshuman S. Rawat <rawat.a...@gmail.com> wrote:
Hi,

On Jun 18, 5:36 pm, soubok <sou...@gmail.com> wrote:
>
> *note 1:*
>   I recommend you to replace '\\' with directorySeparator.
>
> *note 2:*
>   file.open(File.RDONLY);
>   sockdata = stringify(file.read());
>   file.close();
>  can be replaced with:
>   sockdata = stringify(file.content);

the above didn't work as it couldn't find stringify() so I tried this
instead:

sockdata = JSON.stringify(file.content);

JSON.stringify() is the function that transforms JavaScript value into a string.
The stringify() function I refereed belongs to the jslibs global namespace.

 

which crashed the application every time. So I reverted to this:

file.open(File.RDONLY);
 var content = file.read();
 file.close();
 sockdata = JSON.stringify(content);

This put sockdata as '{}' (those are not file contents). Then i found
a function to do the same:

function buffToString(arbuff)
{
var str=new Array();
var buff = new Uint8Array( arbuff );
var index =0;
for ( index=0; index< buff.length ;index++)
{
 str[index] = String.fromCharCode(buff[index]);
 }
return str.join("");
}

Problem is it looses the newline chars.

I should have mentioned this before but I am trying to use this inside
spidermonkey 1.8.5 running inside OpenVxi engine. And I am trying to
use timezoneJS implementation of date/time (which uses jsio) to parse
through timezone files.

I cannot see any reference to jslibs in the OpenVxi project.
Can you give me more details about your link between jslibs and OpenVxi ?

Thanks
Franck.

 

Thanks,

Anshuman S. Rawat

unread,
Jun 19, 2012, 2:01:09 AM6/19/12
to jslibs
On Jun 18, 10:40 pm, soubok <sou...@gmail.com> wrote:

> JSON.stringify() is the function that transforms JavaScript value into a
> string.
> The stringify() function I refereed belongs to the jslibs global namespace.
>

Ok. Thanks for that info. Now I have to figure out how to access it.

>
>
> I cannot see any reference to jslibs in the OpenVxi project.
> Can you give me more details about your link between jslibs and OpenVxi ?
>

Sorry, you won't find any reference there. OpenVxi is not being
maintained by anyone but we are trying to use it all the same. It uses
spidermonkey to provide an ECMAscript interface (VXML has scripting
support). We are trying to use jsio for network IO and file IO within
this interface. Earlier we were using class jsio::Socket to fetch
Olson timezone files from our server. We succeeded but there was a lot
of delay. Now I am trying to find out if there's a way to read these
from a local directory, for which I am using class jsio::File.

I hope that explains the connection.

Thanks,
Anshuman


> Thanks
> Franck.
Reply all
Reply to author
Forward
0 new messages