Instance of ByteArray did not understand #isByteString in ZnUTF8Encoder

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Instance of ByteArray did not understand #isByteString in ZnUTF8Encoder

hernanmd
Hi there,

In Pharo 7.0 I've encountered an error related with the new streams changes (https://github.com/pharo-open-documentation/pharo-wiki/blob/master/Migration/MigrationToPharo7.md) while downloading a zip file. Problem is ZnUTF8Encoder expects a String, but my code which worked in Pharo 6 provides a ByteArray. I've isolated it in a reproducible way:

| webClient resp |
webClient := ZnClient new.
UIManager default informUserDuring: [ :bar |
    bar label: 'Downloading resources for ' , self class printString.
    [ webClient
        signalProgress: true;
        get: 'https://github.com/biosmalltalk/biopharo/raw/develop/test_files/BioSmalltalkTestFiles.zip' ]
    on: HTTPProgress , ZnEntityTooLarge
    do: [ : ex |
        (ex isKindOf: ZnEntityTooLarge)
            ifTrue: [ ex resume ]
            ifFalse: [
                | progress |
                progress := ex.
                progress isEmpty
                    ifFalse: [
                        bar current: progress percentage.
                        progress total ifNotNil: [ :aTotalNumber | bar label: 'Downloading ' ] ].
                progress resume ] ] ].
(resp := webClient response) isSuccess
        ifTrue: [ 'output2.zip' asFileReference writeStreamDo: [ : stream | stream nextPutAll: resp contents ] ]
        ifFalse: [ self error: 'Cannot download resource files' ].

I also tried using #writeOn: on the response, but that also wrote the response header:

self response writeOn: 'test1.zip' asFileReference writeStream.
self response writeOn: 'test2.zip' asFileReference binaryWriteStream.


HTTP/1.1 200 OK
Via: 1.1 varnish
Strict-Transport-Security: max-age=31536000
...

PK    ¸  D               BioSmalltalkTestFiles/PK ½U?+*8!"   K  /   BioSmalltalkTestFiles/A4A7DS6V01S-Alignment.xmlíœks›H †¿à ¯èà ‡©à "’¹_  ¦: 0 R$`ðjSS.,a›

Any suggestion?

Cheers,

Hernán



Reply | Threaded
Open this post in threaded view
|

Re: Instance of ByteArray did not understand #isByteString in ZnUTF8Encoder

Sven Van Caekenberghe-2
Hmm, the following all work for me (in Pharo 7.0.1 on macOS):

ZnClient new
        url: 'https://github.com/biosmalltalk/biopharo/raw/develop/test_files/BioSmalltalkTestFiles.zip';
        get;
        yourself.
       
ZnClient new
        url: 'https://github.com/biosmalltalk/biopharo/raw/develop/test_files/BioSmalltalkTestFiles.zip';
        get;
        contents.
       
ZnClient new
        get: 'https://github.com/biosmalltalk/biopharo/raw/develop/test_files/BioSmalltalkTestFiles.zip'.
       
ZnClient new
        url: 'https://github.com/biosmalltalk/biopharo/raw/develop/test_files/BioSmalltalkTestFiles.zip';
        downloadTo: '/tmp'.

$ ls -lah /tmp/BioSmalltalkTestFiles.zip
-rw-r--r--@ 1 sven  wheel   5.3M Jan 26 22:57 /tmp/BioSmalltalkTestFiles.zip

$ file /tmp/BioSmalltalkTestFiles.zip
/tmp/BioSmalltalkTestFiles.zip: Zip archive data, at least v2.0 to extract

> On 26 Jan 2019, at 20:36, Hernán Morales Durand <[hidden email]> wrote:
>
> Hi there,
>
> In Pharo 7.0 I've encountered an error related with the new streams changes (https://github.com/pharo-open-documentation/pharo-wiki/blob/master/Migration/MigrationToPharo7.md) while downloading a zip file. Problem is ZnUTF8Encoder expects a String, but my code which worked in Pharo 6 provides a ByteArray. I've isolated it in a reproducible way:
>
> | webClient resp |
> webClient := ZnClient new.
> UIManager default informUserDuring: [ :bar |
>     bar label: 'Downloading resources for ' , self class printString.
>     [ webClient
>         signalProgress: true;
>         get: 'https://github.com/biosmalltalk/biopharo/raw/develop/test_files/BioSmalltalkTestFiles.zip' ]
>     on: HTTPProgress , ZnEntityTooLarge
>     do: [ : ex |
>         (ex isKindOf: ZnEntityTooLarge)
>             ifTrue: [ ex resume ]
>             ifFalse: [
>                 | progress |
>                 progress := ex.
>                 progress isEmpty
>                     ifFalse: [
>                         bar current: progress percentage.
>                         progress total ifNotNil: [ :aTotalNumber | bar label: 'Downloading ' ] ].
>                 progress resume ] ] ].
> (resp := webClient response) isSuccess
>         ifTrue: [ 'output2.zip' asFileReference writeStreamDo: [ : stream | stream nextPutAll: resp contents ] ]
>         ifFalse: [ self error: 'Cannot download resource files' ].
>
> I also tried using #writeOn: on the response, but that also wrote the response header:
>
> self response writeOn: 'test1.zip' asFileReference writeStream.
> self response writeOn: 'test2.zip' asFileReference binaryWriteStream.
>
>
> HTTP/1.1 200 OK
> Via: 1.1 varnish
> Strict-Transport-Security: max-age=31536000
> ...
>
> PK    Â¸  D               BioSmalltalkTestFiles/PK ½U?+*8!"  K  /   BioSmalltalkTestFiles/A4A7DS6V01S-Alignment.xmlíœks›H †¿à ¯èà ‡©à "’¹_  ¦: 0 R$`ðjSS.,a›
>
> Any suggestion?
>
> Cheers,
>
> Hernán
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Instance of ByteArray did not understand #isByteString in ZnUTF8Encoder

Sven Van Caekenberghe-2
BTW, ZTimezone in ZTimestamp does something similar to what I guess you want to do:

downloadFallbackZoneinfoDataset
        "Download a fallback copy of the zoneinfo dataset and return its location.
        See #fallbackZoneinfoDatasetURL for more info and a warning"
       
        | zipLocation zoneinfoLocation |
        zipLocation := self fallbackZoneinfoDatasetURL file asFileReference.
        zipLocation ensureDelete.
        ZnClient new
                url: self fallbackZoneinfoDatasetURL;
                downloadTo: zipLocation;
                close.
        zoneinfoLocation := zipLocation parent / 'zoneinfo'.
        zoneinfoLocation ensureDeleteAll.
        ZipArchive new
                readFrom: zipLocation;
                extractAllTo: zoneinfoLocation parent;
                close.
        ^ zoneinfoLocation

where

fallbackZoneinfoDatasetURL
        "The URL to the ZIP archive zoneinfo.zip which is offered as a fallback for
        systems (such as Windows) that do no have their own copy of this data.
        Note that it is highly recommended that you use such a OS maintained dataset,
        as this data is changed (being added to) each year."
       
        ^ 'https://github.com/svenvc/ztimestamp/raw/master/rsrc/zoneinfo.zip' asUrl

> On 26 Jan 2019, at 23:00, Sven Van Caekenberghe <[hidden email]> wrote:
>
> Hmm, the following all work for me (in Pharo 7.0.1 on macOS):
>
> ZnClient new
> url: 'https://github.com/biosmalltalk/biopharo/raw/develop/test_files/BioSmalltalkTestFiles.zip';
> get;
> yourself.
>
> ZnClient new
> url: 'https://github.com/biosmalltalk/biopharo/raw/develop/test_files/BioSmalltalkTestFiles.zip';
> get;
> contents.
>
> ZnClient new
> get: 'https://github.com/biosmalltalk/biopharo/raw/develop/test_files/BioSmalltalkTestFiles.zip'.
>
> ZnClient new
> url: 'https://github.com/biosmalltalk/biopharo/raw/develop/test_files/BioSmalltalkTestFiles.zip';
> downloadTo: '/tmp'.
>
> $ ls -lah /tmp/BioSmalltalkTestFiles.zip
> -rw-r--r--@ 1 sven  wheel   5.3M Jan 26 22:57 /tmp/BioSmalltalkTestFiles.zip
>
> $ file /tmp/BioSmalltalkTestFiles.zip
> /tmp/BioSmalltalkTestFiles.zip: Zip archive data, at least v2.0 to extract
>
>> On 26 Jan 2019, at 20:36, Hernán Morales Durand <[hidden email]> wrote:
>>
>> Hi there,
>>
>> In Pharo 7.0 I've encountered an error related with the new streams changes (https://github.com/pharo-open-documentation/pharo-wiki/blob/master/Migration/MigrationToPharo7.md) while downloading a zip file. Problem is ZnUTF8Encoder expects a String, but my code which worked in Pharo 6 provides a ByteArray. I've isolated it in a reproducible way:
>>
>> | webClient resp |
>> webClient := ZnClient new.
>> UIManager default informUserDuring: [ :bar |
>>    bar label: 'Downloading resources for ' , self class printString.
>>    [ webClient
>>        signalProgress: true;
>>        get: 'https://github.com/biosmalltalk/biopharo/raw/develop/test_files/BioSmalltalkTestFiles.zip' ]
>>    on: HTTPProgress , ZnEntityTooLarge
>>    do: [ : ex |
>>        (ex isKindOf: ZnEntityTooLarge)
>>            ifTrue: [ ex resume ]
>>            ifFalse: [
>>                | progress |
>>                progress := ex.
>>                progress isEmpty
>>                    ifFalse: [
>>                        bar current: progress percentage.
>>                        progress total ifNotNil: [ :aTotalNumber | bar label: 'Downloading ' ] ].
>>                progress resume ] ] ].
>> (resp := webClient response) isSuccess
>>        ifTrue: [ 'output2.zip' asFileReference writeStreamDo: [ : stream | stream nextPutAll: resp contents ] ]
>>        ifFalse: [ self error: 'Cannot download resource files' ].
>>
>> I also tried using #writeOn: on the response, but that also wrote the response header:
>>
>> self response writeOn: 'test1.zip' asFileReference writeStream.
>> self response writeOn: 'test2.zip' asFileReference binaryWriteStream.
>>
>>
>> HTTP/1.1 200 OK
>> Via: 1.1 varnish
>> Strict-Transport-Security: max-age=31536000
>> ...
>>
>> PK    Â¸  D               BioSmalltalkTestFiles/PK ½U?+*8!"  K  /   BioSmalltalkTestFiles/A4A7DS6V01S-Alignment.xmlíœks›H †¿à ¯èà ‡©à "’¹_  ¦: 0 R$`ðjSS.,a›
>>
>> Any suggestion?
>>
>> Cheers,
>>
>> Hernán
>>
>>
>>
>


Reply | Threaded
Open this post in threaded view
|

Re: Instance of ByteArray did not understand #isByteString in ZnUTF8Encoder

hernanmd
Hi Sven,

Yes, your examples work in Pharo 7, however I also wanted a progress bar, and two exception handlers:

1) Resume when my files are too large (ZnEntityTooLarge).
2) When download failed display an error message.

Now for 1) I consider to wrap the whole download code - because I don't like #isKindOf: - into this:

ZnMaximumEntitySize value: someMaxSize during: [ ... ].

I changed my code to use #downloadTo:
Thank you for the suggestion!!

Cheers,

Hernán

El sáb., 26 ene. 2019 a las 19:05, Sven Van Caekenberghe (<[hidden email]>) escribió:
BTW, ZTimezone in ZTimestamp does something similar to what I guess you want to do:

downloadFallbackZoneinfoDataset
        "Download a fallback copy of the zoneinfo dataset and return its location.
        See #fallbackZoneinfoDatasetURL for more info and a warning"

        | zipLocation zoneinfoLocation |
        zipLocation := self fallbackZoneinfoDatasetURL file asFileReference.
        zipLocation ensureDelete.
        ZnClient new
                url: self fallbackZoneinfoDatasetURL;
                downloadTo: zipLocation;
                close.
        zoneinfoLocation := zipLocation parent / 'zoneinfo'.
        zoneinfoLocation ensureDeleteAll.
        ZipArchive new
                readFrom: zipLocation;
                extractAllTo: zoneinfoLocation parent;
                close.
        ^ zoneinfoLocation

where

fallbackZoneinfoDatasetURL
        "The URL to the ZIP archive zoneinfo.zip which is offered as a fallback for
        systems (such as Windows) that do no have their own copy of this data.
        Note that it is highly recommended that you use such a OS maintained dataset,
        as this data is changed (being added to) each year."

        ^ 'https://github.com/svenvc/ztimestamp/raw/master/rsrc/zoneinfo.zip' asUrl

> On 26 Jan 2019, at 23:00, Sven Van Caekenberghe <[hidden email]> wrote:
>
> Hmm, the following all work for me (in Pharo 7.0.1 on macOS):
>
> ZnClient new
>       url: 'https://github.com/biosmalltalk/biopharo/raw/develop/test_files/BioSmalltalkTestFiles.zip';
>       get;
>       yourself.
>       
> ZnClient new
>       url: 'https://github.com/biosmalltalk/biopharo/raw/develop/test_files/BioSmalltalkTestFiles.zip';
>       get;
>       contents.
>       
> ZnClient new
>       get: 'https://github.com/biosmalltalk/biopharo/raw/develop/test_files/BioSmalltalkTestFiles.zip'.
>       
> ZnClient new
>       url: 'https://github.com/biosmalltalk/biopharo/raw/develop/test_files/BioSmalltalkTestFiles.zip';
>       downloadTo: '/tmp'.
>
> $ ls -lah /tmp/BioSmalltalkTestFiles.zip
> -rw-r--r--@ 1 sven  wheel   5.3M Jan 26 22:57 /tmp/BioSmalltalkTestFiles.zip
>
> $ file /tmp/BioSmalltalkTestFiles.zip
> /tmp/BioSmalltalkTestFiles.zip: Zip archive data, at least v2.0 to extract
>
>> On 26 Jan 2019, at 20:36, Hernán Morales Durand <[hidden email]> wrote:
>>
>> Hi there,
>>
>> In Pharo 7.0 I've encountered an error related with the new streams changes (https://github.com/pharo-open-documentation/pharo-wiki/blob/master/Migration/MigrationToPharo7.md) while downloading a zip file. Problem is ZnUTF8Encoder expects a String, but my code which worked in Pharo 6 provides a ByteArray. I've isolated it in a reproducible way:
>>
>> | webClient resp |
>> webClient := ZnClient new.
>> UIManager default informUserDuring: [ :bar |
>>    bar label: 'Downloading resources for ' , self class printString.
>>    [ webClient
>>        signalProgress: true;
>>        get: 'https://github.com/biosmalltalk/biopharo/raw/develop/test_files/BioSmalltalkTestFiles.zip' ]
>>    on: HTTPProgress , ZnEntityTooLarge
>>    do: [ : ex |
>>        (ex isKindOf: ZnEntityTooLarge)
>>            ifTrue: [ ex resume ]
>>            ifFalse: [
>>                | progress |
>>                progress := ex.
>>                progress isEmpty
>>                    ifFalse: [
>>                        bar current: progress percentage.
>>                        progress total ifNotNil: [ :aTotalNumber | bar label: 'Downloading ' ] ].
>>                progress resume ] ] ].
>> (resp := webClient response) isSuccess
>>        ifTrue: [ 'output2.zip' asFileReference writeStreamDo: [ : stream | stream nextPutAll: resp contents ] ]
>>        ifFalse: [ self error: 'Cannot download resource files' ].
>>
>> I also tried using #writeOn: on the response, but that also wrote the response header:
>>
>> self response writeOn: 'test1.zip' asFileReference writeStream.
>> self response writeOn: 'test2.zip' asFileReference binaryWriteStream.
>>
>>
>> HTTP/1.1 200 OK
>> Via: 1.1 varnish
>> Strict-Transport-Security: max-age=31536000
>> ...
>>
>> PK    ¸  D               BioSmalltalkTestFiles/PK ½U?+*8!"   K  /   BioSmalltalkTestFiles/A4A7DS6V01S-Alignment.xmlíœks›H †¿à ¯èà ‡©à "’¹_  ¦: 0 R$`ðjSS.,a›
>>
>> Any suggestion?
>>
>> Cheers,
>>
>> Hernán
>>
>>
>>
>


Reply | Threaded
Open this post in threaded view
|

Re: Instance of ByteArray did not understand #isByteString in ZnUTF8Encoder

Sven Van Caekenberghe-2


> On 27 Jan 2019, at 00:20, Hernán Morales Durand <[hidden email]> wrote:
>
> Hi Sven,
>
> Yes, your examples work in Pharo 7, however I also wanted a progress bar, and two exception handlers:
>
> 1) Resume when my files are too large (ZnEntityTooLarge).
> 2) When download failed display an error message.
>
> Now for 1) I consider to wrap the whole download code - because I don't like #isKindOf: - into this:
>
> ZnMaximumEntitySize value: someMaxSize during: [ ... ].

There is also an accessor #maximumEntitySize:

Because ZnMaximumEntitySize will go away for a more general mechanism.

> I changed my code to use #downloadTo:
> Thank you for the suggestion!!
>
> Cheers,
>
> Hernán
>
> El sáb., 26 ene. 2019 a las 19:05, Sven Van Caekenberghe (<[hidden email]>) escribió:
> BTW, ZTimezone in ZTimestamp does something similar to what I guess you want to do:
>
> downloadFallbackZoneinfoDataset
>         "Download a fallback copy of the zoneinfo dataset and return its location.
>         See #fallbackZoneinfoDatasetURL for more info and a warning"
>
>         | zipLocation zoneinfoLocation |
>         zipLocation := self fallbackZoneinfoDatasetURL file asFileReference.
>         zipLocation ensureDelete.
>         ZnClient new
>                 url: self fallbackZoneinfoDatasetURL;
>                 downloadTo: zipLocation;
>                 close.
>         zoneinfoLocation := zipLocation parent / 'zoneinfo'.
>         zoneinfoLocation ensureDeleteAll.
>         ZipArchive new
>                 readFrom: zipLocation;
>                 extractAllTo: zoneinfoLocation parent;
>                 close.
>         ^ zoneinfoLocation
>
> where
>
> fallbackZoneinfoDatasetURL
>         "The URL to the ZIP archive zoneinfo.zip which is offered as a fallback for
>         systems (such as Windows) that do no have their own copy of this data.
>         Note that it is highly recommended that you use such a OS maintained dataset,
>         as this data is changed (being added to) each year."
>
>         ^ 'https://github.com/svenvc/ztimestamp/raw/master/rsrc/zoneinfo.zip' asUrl
>
> > On 26 Jan 2019, at 23:00, Sven Van Caekenberghe <[hidden email]> wrote:
> >
> > Hmm, the following all work for me (in Pharo 7.0.1 on macOS):
> >
> > ZnClient new
> >       url: 'https://github.com/biosmalltalk/biopharo/raw/develop/test_files/BioSmalltalkTestFiles.zip';
> >       get;
> >       yourself.
> >      
> > ZnClient new
> >       url: 'https://github.com/biosmalltalk/biopharo/raw/develop/test_files/BioSmalltalkTestFiles.zip';
> >       get;
> >       contents.
> >      
> > ZnClient new
> >       get: 'https://github.com/biosmalltalk/biopharo/raw/develop/test_files/BioSmalltalkTestFiles.zip'.
> >      
> > ZnClient new
> >       url: 'https://github.com/biosmalltalk/biopharo/raw/develop/test_files/BioSmalltalkTestFiles.zip';
> >       downloadTo: '/tmp'.
> >
> > $ ls -lah /tmp/BioSmalltalkTestFiles.zip
> > -rw-r--r--@ 1 sven  wheel   5.3M Jan 26 22:57 /tmp/BioSmalltalkTestFiles.zip
> >
> > $ file /tmp/BioSmalltalkTestFiles.zip
> > /tmp/BioSmalltalkTestFiles.zip: Zip archive data, at least v2.0 to extract
> >
> >> On 26 Jan 2019, at 20:36, Hernán Morales Durand <[hidden email]> wrote:
> >>
> >> Hi there,
> >>
> >> In Pharo 7.0 I've encountered an error related with the new streams changes (https://github.com/pharo-open-documentation/pharo-wiki/blob/master/Migration/MigrationToPharo7.md) while downloading a zip file. Problem is ZnUTF8Encoder expects a String, but my code which worked in Pharo 6 provides a ByteArray. I've isolated it in a reproducible way:
> >>
> >> | webClient resp |
> >> webClient := ZnClient new.
> >> UIManager default informUserDuring: [ :bar |
> >>    bar label: 'Downloading resources for ' , self class printString.
> >>    [ webClient
> >>        signalProgress: true;
> >>        get: 'https://github.com/biosmalltalk/biopharo/raw/develop/test_files/BioSmalltalkTestFiles.zip' ]
> >>    on: HTTPProgress , ZnEntityTooLarge
> >>    do: [ : ex |
> >>        (ex isKindOf: ZnEntityTooLarge)
> >>            ifTrue: [ ex resume ]
> >>            ifFalse: [
> >>                | progress |
> >>                progress := ex.
> >>                progress isEmpty
> >>                    ifFalse: [
> >>                        bar current: progress percentage.
> >>                        progress total ifNotNil: [ :aTotalNumber | bar label: 'Downloading ' ] ].
> >>                progress resume ] ] ].
> >> (resp := webClient response) isSuccess
> >>        ifTrue: [ 'output2.zip' asFileReference writeStreamDo: [ : stream | stream nextPutAll: resp contents ] ]
> >>        ifFalse: [ self error: 'Cannot download resource files' ].
> >>
> >> I also tried using #writeOn: on the response, but that also wrote the response header:
> >>
> >> self response writeOn: 'test1.zip' asFileReference writeStream.
> >> self response writeOn: 'test2.zip' asFileReference binaryWriteStream.
> >>
> >>
> >> HTTP/1.1 200 OK
> >> Via: 1.1 varnish
> >> Strict-Transport-Security: max-age=31536000
> >> ...
> >>
> >> PK    Â¸  D               BioSmalltalkTestFiles/PK ½U?+*8!"  K  /   BioSmalltalkTestFiles/A4A7DS6V01S-Alignment.xmlíœks›H †¿à ¯èà ‡©à "’¹_  ¦: 0 R$`ðjSS.,a›
> >>
> >> Any suggestion?
> >>
> >> Cheers,
> >>
> >> Hernán
> >>
> >>
> >>
> >
>
>