在Squeak中,一旦块到达就将大量HTTP响应的块写入磁盘

时间:2018-03-21 07:27:01

标签: http download stream smalltalk squeak

我正在尝试从squeak将文件下载到磁盘。 我的方法适用于小文本/ html文件, 但由于缺乏缓冲, 这对于大型二进制文件来说非常慢 https://mirror.racket-lang.org/installers/6.12/racket-6.12-x86_64-win32.exe。 此外,完成后,文件更大(113 MB) 比下载页面上显示的那样(75MB)。

我的代码如下所示:

download: anURL 
    "download a file over HTTP and save it to disk under a name extracted from url."
    | ios name |
    name := ((anURL findTokens: '/') removeLast findTokens: '?') removeFirst.
    ios := FileStream oldFileNamed: name.
    ios  nextPutAll: ((HTTPClient httpGetDocument: anURL) content).
    ios close.
    Transcript show: 'done'; cr.

我已经尝试[bytes = stream next bufSize. bytes printTo: ios]使用contentStream循环在HTTP响应[stream atEnd] whileFalse:中使用固定大小的块,但是在每个块周围用单引号乱码输出文件,以及之后的额外内容块,看起来像流的所有字符,每个单引号。

如何实现对磁盘文件的HTTP响应的缓冲写入? 另外,有没有办法在显示下载进度时发出吱吱声?

2 个答案:

答案 0 :(得分:2)

正如Leandro已经写过这个问题是#binary

您的代码几乎是正确的,我已经冒昧地运行它 - 现在它正确地下载了整个文件:

| ios name anURL |
anURL := ' https://mirror.racket-lang.org/installers/6.12/racket-6.12-x86_64-win32.exe'.
name := ((anURL findTokens: '/') removeLast findTokens: '?') removeFirst.
ios := FileStream newFileNamed: 'C:\Users\user\Downloads\_squeak\', name.
ios binary.
ios  nextPutAll: ((HTTPClient httpGetDocument: anURL) content).
ios close.
Transcript show: 'done'; cr.

关于冻结,我认为问题在于您下载时整个环境的一个线程。这意味着直到您下载整个文件为止,您将无法使用Squeak。

刚刚在Pharo中测试过(更容易安装),下面的代码可以按照您的意愿运行:

ZnClient new
  url: 'https://mirror.racket-lang.org/installers/6.12/racket-6.12-x86_64-win32.exe';
  downloadTo: 'C:\Users\user\Downloads\_squeak'.

答案 1 :(得分:1)

WebResponse类在构建响应内容时,会创建一个足以容纳整个响应的缓冲区,即使对于巨大的响应也是如此!我认为这是由于WebMessage>>#getContentWithProgress:中的代码而发生的。

我尝试将SocketStream的输入WebResponse中的数据直接复制到输出FileStream。 我必须继承WebClientWebResponse,并编写两个方法。 现在,以下代码按要求工作。

| client link |
client := PkWebClient new.
link := 'http://localhost:8000/racket-6.12-x86_64-linux.sh'.
client download: link toFile: '/home/yo/test'.

我已经验证了逐块更新和下载文件的完整性。

我在下面提供了来源。方法streamContentDirectToFile: aFilePathString是以不同方式处理并解决问题的方法。

WebClient subclass: #PkWebClient
    instanceVariableNames: ''
    classVariableNames: ''
    poolDictionaries: ''
    category: 'PK'!
!PkWebClient commentStamp: 'pk 3/28/2018 20:16' prior: 0!
Trying to download http directly to file.!


!PkWebClient methodsFor: 'as yet unclassified' stamp: 'pk 3/29/2018 13:29'!
download: urlString toFile: aFilePathString 
    "Try to download large files sensibly"
    | res |
    res := self httpGet: urlString.
    res := PkWebResponse new copySameFrom: res.
    res streamContentDirectToFile: aFilePathString! !


WebResponse subclass: #PkWebResponse
    instanceVariableNames: ''
    classVariableNames: ''
    poolDictionaries: ''
    category: 'PK'!
!PkWebResponse commentStamp: 'pk 3/28/2018 20:49' prior: 0!
To make getContentwithProgress better.!
]style[(38)f1!


!PkWebResponse methodsFor: 'as yet unclassified' stamp: 'pk 3/29/2018 13:20'!
streamContentDirectToFile: aFilePathString 
    "stream response's content directly to file."
    | buffer ostream |
    stream binary.
    buffer := ByteArray new: 4096.
    ostream := FileStream oldFileNamed: aFilePathString.
    ostream binary.
    [stream atEnd]
        whileFalse: [buffer := stream nextInBuffer: 4096.
            stream receiveAvailableData.
            ostream nextPutAll: buffer].
    stream close.
    ostream close! !
相关问题