FileWriter.write() as iso8859.1 instead of UTF-8

2,339 views
Skip to first unread message

Daniel Holth

unread,
Aug 2, 2011, 9:27:22 AM8/2/11
to phon...@googlegroups.com
I've noticed FileWriter.write() encodes strings as UTF-8 before writing. I'm considering hacking my PhoneGap to encode strings as the considerably more useful iso8859.1; this will allow my program to save binary data.

An alternative would be to implement BlobBuilder per the w3c file writer spec. Any thoughts?

Daniel Holth

Simon MacDonald

unread,
Aug 2, 2011, 12:10:57 PM8/2/11
to phon...@googlegroups.com
If you can figure out a way to pass binary data from the JavaScript
side to the Java side and back again that would be great. Right now
the way things are we are limited to textual data only.

We thought it was weird that the W3C spec for FileReader provides a
encoding parameter when reading but the FileWriter does not provide a
encoding parameter for writing. Right now we just write in the default
encoding of the platform.

Simon Mac Donald
http://hi.im/simonmacdonald

> --
> You received this message because you are subscribed to the Google
> Groups "phonegap" group.
> To post to this group, send email to phon...@googlegroups.com
> To unsubscribe from this group, send email to
> phonegap+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/phonegap?hl=en?hl=en
>
> For more info on PhoneGap or to download the code go to www.phonegap.com
>

Daniel Holth

unread,
Aug 2, 2011, 2:01:39 PM8/2/11
to phon...@googlegroups.com
When I looked at it I thought the FileWriter only accepted blobs in the spec so the encoding has to happen in JavaScript. This is not a problem if you want to use UTF-8.

As far as I understand ISO8859.1 is a foolproof way to handle binary data (expressed as Unicode strings with no symbols greater than 0xff). You just waste half your RAM if your JIT is not unexpectedly clever.

Is something else bad happening at the JavaScript -> Java boundary before we call the native write method?

Daniel Holth

Daniel Holth

unread,
Aug 5, 2011, 4:29:37 PM8/5/11
to phon...@googlegroups.com
I really thought ISO8859.1 was just the first byte of each character in a UCS-2 string. However, I did manage to get binary data (a PDF) from a server into PhoneGap to the filesystem and open it. I've replaced FileUtils.write() with the following, and I'm able to save a PDF.

        byte[] rawData = new byte[data.length()];
    FileOutputStream stream = new FileOutputStream(filename, append);
        for(int i=0; i<data.length(); i++) {
            rawData[i] = (byte)(data.charAt(i) & 0xff);
        }
        stream.write(rawData);

This technique wastes tons of RAM. For a large file it will be necessary to split the download into partial requests with something like Content-Range: bytes 0-65536 and so on until the whole file has been downloaded.

The document is also served with Content-Type: text/plain; charset=x-user-defined, but I'm not sure that is necessary.

Daniel Holth

unread,
Aug 8, 2011, 2:32:23 PM8/8/11
to phon...@googlegroups.com
Something bad does happen before PhoneGap is involved. Some of the bytes returned by XMLHttpRequest (with charset: x-user-defined ) are prefixed by 0xf7. Instead of getting 0xdd you'll get 0xf7dd. I don't know why that happens, but it can be remedied by and-ing each character of the result with 0xff.

capd...@yahoo.es

unread,
May 30, 2012, 11:26:39 AM5/30/12
to phon...@googlegroups.com

I've tried to update the phonegap code, but the source SVN code in:
https://svn.apache.org/repos/asf/incubator/callback/phonegap-android/tags/

( from  http://incubator.apache.org/projects/callback.html )

is from phnogap 1.0.0   and  i am using the actual version,  cordova-1.7.0.jar
I've used  FileUtils.java source and tried to integrate it into a new cordova-1.7.0.jar
but the invention does not work....


05-30 15:11:49.315: E/AndroidRuntime(22000): java.lang.IllegalAccessError: tried to access method org.apache.cordova.FileUtils.getEntry:(Ljava/lang/String;Ljava/lang/String;)Lorg/json/JSONObject; from class org.apache.cordova.FileTransfer

I've tried to decompile FileUtils.class with java decompiler, but the originated source is incomplete....

Does anyone knows how to get the source code from cordova-1.7.0.jar  and arrange this issue?

I need to write binary data as i am constructing a handheld application which gets zipped data!!

I can not believe this prevents me from using phonegap?

No other solution?

please!!!




capd...@yahoo.es

unread,
May 30, 2012, 11:31:53 AM5/30/12
to phon...@googlegroups.com


tried to use cordova-1.7.0.jar and update the FileUtils.java as mentioned here...
but there is only source code for phonegap-1.0.0.jar!!!


the source is here:
http://incubator.apache.org/projects/callback.html

anyone knowing where to get the FileUtils.java from  cordova-1.7.0.jar ?

I cannot belive this prevents me from a full development i was doing, I need to write zipped files from a server!!!

capd...@yahoo.es

unread,
May 30, 2012, 11:32:09 AM5/30/12
to phon...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages