Closed Bug 292296 Opened 20 years ago Closed 20 years ago

I cannot put into nsIOutputStream unicode characters

Categories

(Core :: Internationalization, defect)

x86
Windows 2000
defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: surkov, Assigned: smontagu)

Details

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.0; ru-RU; rv:1.7.5) Gecko/20041108 Firefox/1.0 (ax)
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8b) Gecko/20050217

Method write() of nsIOutputStream waits string as char* 
PRUint32 write ( char* buf , PRUint32 count ). 
Is it mean I cannot use write() method to write unicode string into stream?

Reproducible: Always

Steps to Reproduce:
var file=Components.classes["@mozilla.org/file/local;1"].createInstance()
			.QueryInterface(Components.interfaces.nsILocalFile);
file.initWithPath("c:\\sample.txt");

if(!file.exists()) file.create(file.NORMAL_FILE_TYPE, null);

var
stream=Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance()
			.QueryInterface(Components.interfaces.nsIFileOutputStream);

stream.init(file, 0x04|0x10, null, null);

var string="В какой кодировке у меня файл?";
stream.write(string, string.length);
Actual Results:  
I cannot read a file.

Expected Results:  
File should be unicode.
example of comment#0 should be saved in utf-8 file before executing.
This is, again, _Internationalization_, not Localization.
Assignee: kairo → smontagu
Component: Localization → Internationalization
QA Contact: mmx_bugzilla → amyy
streams write bytes, not characters. now, the signature of nsIOutputStream does
not allow you writing NUL bytes. You can wrap it with an nsIBinaryOutputStream
and use the methods here:
http://lxr.mozilla.org/seamonkey/source/xpcom/io/nsIBinaryOutputStream.idl#84

You will want to convert the string to some sort of byte array before using
that, see nsIScriptableUnicodeConverter. If you use its convertToInputStream
method, you can avoid using the binaryoutputstream and just use the writeFrom
method of nsIOutputStream.

Maybe we should have some scriptable wrapper for streams that wraps an
outputstream, takes a charset, and allows writing strings, automatically
converting it to the desired character set...
Status: UNCONFIRMED → RESOLVED
Closed: 20 years ago
Resolution: --- → INVALID
(In reply to comment #3)
> If you use its convertToInputStream
> method, you can avoid using the binaryoutputstream and just use the writeFrom
> method of nsIOutputStream.

Where can I find documentation of convertToInputStream() method?
nsILocalFile.writeFrom() method is not implemented
(https://bugzilla.mozilla.org/show_bug.cgi?id=289400)
(In reply to comment #3)

> Maybe we should have some scriptable wrapper for streams that wraps an
> outputstream, takes a charset, and allows writing strings, automatically
> converting it to the desired character set...

Such interface solves my problem. Should I post a new bug with request to
realize this interface or you can do it yourself? 

Though I think it's not right when I'm not able to write unicode string into a
stream by using write() method of nsIFileOutputStream. Maybe is it a better way
to override write() method for nsIFileOutputSteam interface? How do you think?
(In reply to comment #4)
> Where can I find documentation of convertToInputStream() method?

In the IDL file...
http://lxr.mozilla.org/seamonkey/source/intl/uconv/idl/nsIScriptableUConv.idl#90

> nsILocalFile.writeFrom() method is not implemented

Oh, true. You'd have to wrap a buffered stream around the fileoutputstream.


(In reply to comment #5)
> Such interface solves my problem. Should I post a new bug with request to
> realize this interface or you can do it yourself? 

Could you do that?

> Though I think it's not right when I'm not able to write unicode string into a
> stream by using write() method of nsIFileOutputStream. Maybe is it a better way
> to override write() method for nsIFileOutputSteam interface? How do you think?

IDL does not support such overriding...

And why do you not think it's right? Like I said, streams write bytes, not
characters. So you need to convert your characters to bytes before writing them.
How can the stream know which character set you want? And if it could, how would
you write binary data? And it's not like Mozilla is alone here: Java's output
streams do not support writing strings either:
http://java.sun.com/j2se/1.4.2/docs/api/java/io/OutputStream.html
(You'd need to wrap an OutputStreamWriter around the stream)

It is unfortunate that nsIOutputStream takes |string| as an argument instead of
an octet array... (for example, does not allow writing 0 bytes from JS)
>> Such interface solves my problem. Should I post a new bug with request to
>> realize this interface or you can do it yourself? 
>Could you do that?

I filed bug 295047 on that, with a first version of a patch.
You need to log in before you can comment on or make changes to this bug.