On Tue, Jan 2, 2018 at 1:13 PM, Henrik Sperre Johansen <henrik.s.johansen@veloxit.no> wrote:
Stephane Ducasse-3 wrote
> Hi Bernhard
>
> Have a look at how guille started to implement File package.
> (Guille is actively converting all the image users of the old
> hierarchy but last time he losts a couple of hours
> because changing epicea and other users of positionable streams and
> sourceArray... at the same time
> was leading to complex situations :).�� But Guille told me that he
> learned and will come back to this.)
>
> He is using ZnCharacterWriteStream and wrapper like
> ZnCrPortableWriteStream.
>
> writeSourceCodeFrom: aStream baseName: baseName isSt: stOrCsFlag
>
> | extension fileName�� outputStream |
> extension := stOrCsFlag ifTrue: ['.st']�� ifFalse: ['.cs'].
> fileName := baseName, extension.
> fileName := FileSystem disk checkName: fileName fixErrors: true.
> outputStream := (File named: fileName) writeStream.
> (ZnCrPortableWriteStream on: (ZnCharacterWriteStream
> on: outputStream
> encoding: 'utf8')) nextPutAll: aStream contents.
>
> outputStream close.
>
> writeSourceCodeFrom: aStream toFileReference: aFileReference
>
>�� �� ��aFileReference writeStreamDo: [ :outputStream |
>�� �� �� �� �� �� �� (ZnCrPortableWriteStream on: (ZnCharacterWriteStream
>�� �� �� �� �� �� �� �� �� on: outputStream
>�� �� �� �� �� �� �� �� �� encoding: 'utf8')) nextPutAll: aStream contents.
>�� �� �� �� �� �� �� �� �� ].
>
> self inform: 'Filed out to: ', String cr, aFileReference basename
>
>
> self inform: 'Filed out to: ', String cr, fileName.
>
> I hope it helps.
>
> Stef
>
> On Sat, Dec 30, 2017 at 1:27 PM, Bernhard Pieber &lt;

> bernhard@

> &gt; wrote:
>> I just saw that the FileStream hierarchy including MultiByteFileStream is
>> deprecated. However, I can't find what file stream class should be used
>> to read a UTF-8 encoded file instead. FileReference>>#readStreamDo: also
>> uses MultiByteFileStream.
>>
>> Can someone please point me to the right code?
>>
>> Bernhard

Maybe it's just me, but I'd a big fan of convenience methods over explicit
stacking when it comes to multi-layer streams.
- It documents the intended/valid ways to stack (such as which streams it
makes sense to wrap in a ZnCrPortableWriteStream, or which streams one
normally would want to #buffered:do:)
- It makes the transition (in terms of conciseness) from "traditional"
filestreams less jarring (provided convenience methods provide sensible
defaults).

Say, for instance, a #withEncoding:do:* on read/writeStream, so instead of
the above, one can do:

fileName := FileSystem disk checkName: fileName fixErrors: true.
(outputStream :=(File named: fileName) writeStream)
�� ��withEncoding: 'utf8'
�� do: [:encodedWriteStream |
�� �� �� encodedWriteStream nextPutAll: aStream contents].
outputStream close.

and
aFileReference writeStreamDo: [ :outputStream |
�� ��outputStream withEncoding: 'utf8' do: [:encodedStream |
�� �� �� encodedStream nextPutAll: aStream contents. ]].
or
input := aFileReference readStreamDo: [ :inputStream |
�� ��inputStream withEncoding: 'utf8' do: [:decodingStream |
�� �� �� decodingStream contents]]

It's more involved than the current multibytestreams, but takes away the
burden of determining the correct stack of ZnStreams for the common case.
For instance, you'd normally want file write streams to also be wrapped by a
buffered stream, and have a single flush at end of withEncoding:do:, while
the same isn't necessary for withEncoding:do: on the base WriteStream class.


It's not just you :).

I was during last sprint (while seeing such idioms appear) adding convenience methods like

writeStreamEncoded: anEncoding
writeStreamDo: aBlock encoded: anEncoding

readStreamEncoded: anEncoding
readStreamDo: aBlock encoded: anEncoding

But my image fantastically crashed, the save image failed and I lost everything...



Cheers,
Henry

*Bad name, I know, but you get the point.



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Developers-f1294837.html




--

������

Guille Polito

Research Engineer

Centre de Recherche en Informatique, Signal et Automatique de Lille

CRIStAL - UMR 9189

French National Center for Scientific Research - http://www.cnrs.fr


Web: http://guillep.github.io

Phone: +33 06 52 70 66 13