[squeak-dev] ByteArray>>at:put:

Bert Freudenberg bert at freudenbergs.de
Thu Apr 26 12:03:34 UTC 2018


On 26 April 2018 at 13:29, Bert Freudenberg <bert at freudenbergs.de> wrote:

> On 26 April 2018 at 11:28, marcel.taeumel <Marcel.Taeumel at hpi.de> wrote:
>
>> Hi, there.
>>
>> I cannot put a ByteString into a ByteArray via a WriteStream anymore:
>>
>> | s |
>> s := WriteStream on: ByteArray new.
>> s nextPutAll: 'hello'.
>>
>> You need to do:
>>
>> | s |
>> s := WriteStream on: ByteArray new.
>> s nextPutAll: 'hello' asByteArray.
>>
>> This breaks code.
>>
>> Best,
>> Marcel
>>
>
> ​Tech​nically this is an expected error - you simply cannot put Characters
> (the elements of a String) into a ByteArray. However, for convenience we do
> support copying Strings into ByteArrays in various places, including
> WriteStream>>nextPutAll:.
>
> However, the test in WriteStream>>nextPutAll: is too strict. It needs to
> be "collection class isBits" instead of "collection isString". This makes
> the code actually match its comment:
>
> WriteStream>>nextPutAll: aCollection
>
> | newEnd |
> (collection class == aCollection class
> or: [ collection class isBits
> and: [ aCollection isString
> and: [ collection class format = aCollection class format ] ] ]) "Let
> Strings with the same field size as collection take the quick route too."
> ifFalse: [ ^ super nextPutAll: aCollection ].
>
> newEnd := position + aCollection size.
> newEnd > writeLimit ifTrue:
> [self growTo: newEnd + 10].
>
> collection replaceFrom: position+1 to: newEnd  with: aCollection
> startingAt: 1.
> position := newEnd.
> ^aCollection​
>
> With that change your example works. I think this was the original intent
> of the code.
>
> Fixed in Collections-bf.787
>

​PS: ​doing it this way ensures that you still get an error when you try to
write a WideString into a ByteArray stream.

- Bert -
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20180426/8318f96a/attachment.html>


More information about the Squeak-dev mailing list