Open Bug 1353285 Opened 6 years ago Updated 6 months ago

Provide chrome JS helpers for reading UTF-8 as string and writing string as UTF-8 file

Categories

(Toolkit :: General, enhancement)

enhancement
Not set
normal

Tracking

()

Performance Impact none

People

(Reporter: hsivonen, Unassigned)

References

(Depends on 1 open bug, Blocks 3 open bugs)

Details

To facilitate the removal of nsIConverterOutputStream, nsIConverterInputStream and nsIScriptableUnicodeConverter, we should have helper functions available to chrome JS that use TextDecoder and TextEncoder for conversion between JS strings (UTF-16) and bytes (UTF-8) in the context of XPCOM streams.

We should probably have these four functions:

1. Takes an nsIInputStream and reads it to the end synchronously, converts the data from UTF-8 to UTF-16 using TextDecoder and returns the result as a JS string. Marked as "don't use except for legacy code that hasn't been refactored to be async yet".

2. Takes an nsIInputStream and returns a promise. Reads the stream to the end asynchronously, converts the data from UTF-8 to UTF-16 and resolves the promise with the result as a JS string.

3. Takes a JS string and an nsIOutputStream, converts the string from UTF-16 to UTF-8 using TextEncoder and writes the bytes to the stream synchronously. Marked as "don't use except for legacy code that hasn't been refactored to be async yet".

4. Takes a JS string and an nsIOutputStream and returns a promise. Converts the string from UTF-16 to UTF-8 using TextEncoder, writes the bytes to the stream asynchronously and resolves the promise when done.
(In reply to Henri Sivonen (:hsivonen) from comment #0)
> 1. Takes an nsIInputStream and reads it to the end synchronously, converts
> the data from UTF-8 to UTF-16 using TextDecoder and returns the result as a
> JS string. Marked as "don't use except for legacy code that hasn't been
> refactored to be async yet".

Wrap the stream in nsIBinaryInputStream, use readArrayBuffer().

> 2. Takes an nsIInputStream and returns a promise. Reads the stream to the
> end asynchronously, converts the data from UTF-8 to UTF-16 and resolves the
> promise with the result as a JS string.

Depends on bug 1347873, AFAICT.

> 3. Takes a JS string and an nsIOutputStream, converts the string from UTF-16
> to UTF-8 using TextEncoder and writes the bytes to the stream synchronously.
> Marked as "don't use except for legacy code that hasn't been refactored to
> be async yet".

Need to check if writeByteArray on nsIBinaryOutputStream enables this already.

> 4. Takes a JS string and an nsIOutputStream and returns a promise. Converts
> the string from UTF-16 to UTF-8 using TextEncoder, writes the bytes to the
> stream asynchronously and resolves the promise when done.

Wrap as nsIArrayBufferInputStream and use NetUtil::asyncCopy().
Depends on: 1347873
Should these live in NetUtil.jsm?
Flags: needinfo?(continuation)
(In reply to Henri Sivonen (:hsivonen) from comment #2)
> Should these live in NetUtil.jsm?

I really don't know anything about NetUtil.jsm, sorry.
Flags: needinfo?(continuation)
Whiteboard: [qf]
Whiteboard: [qf] → [qf-]
(In reply to Andrew McCreight [:mccr8] from comment #3)
> (In reply to Henri Sivonen (:hsivonen) from comment #2)
> > Should these live in NetUtil.jsm?
> 
> I really don't know anything about NetUtil.jsm, sorry.

OK, based on tweets, I thought you were doing NetUtil.jsm-related things.

bz, should these live in NetUtil.jsm?
Flags: needinfo?(bzbarsky)
I was going to say I have no idea and have nothing to do with this file, then discovered I'm the original author...

So given that, sure, why not.  ;)
Flags: needinfo?(bzbarsky)
Blocks: 1387785
Performance Impact: --- → -
Whiteboard: [qf-]
You need to log in before you can comment on or make changes to this bug.