copy stream into file: eof error

This is a discussion on copy stream into file: eof error within the lisp forums in Programming Languages category; Wanting to check out the clsql mailing list (let ((stream (drakma:http-request "http://lists.b9.com/pipermail/ clsql/2008-August.txt.gz" :want-stream t))) (with-open-file (f "/tmp/mails.gz" :direction utput :element- type '(unsigned-byte 8) :if-exists :supersede :if-does-not- exist :create) (loop for c = (read-byte stream) while c do (write-byte c f))) (close stream)) Which should write the http://lists.b9.com/pipermail/clsql/2008-August.txt.gz conten into the file /tmp/mails.gz Or so I thought. I've got an end of file error. Doesn't while c handle eof? I finally let drakma:http-request return an array and wrote that array into a file, no problem there. However I'm wondering how to copy a stream into a file i) correctly and ...

Go Back   Application Development Forum > Programming Languages > lisp

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-22-2008, 06:01 PM
olivier.buechel@gmail.com
Guest
 
Default copy stream into file: eof error

Wanting to check out the clsql mailing list
(let ((stream (drakma:http-request "http://lists.b9.com/pipermail/
clsql/2008-August.txt.gz"
:want-stream t)))
(with-open-file (f "/tmp/mails.gz" :direction utput :element-
type '(unsigned-byte 8) :if-exists :supersede :if-does-not-
exist :create)
(loop for c = (read-byte stream)
while c do
(write-byte c f)))
(close stream))

Which should write the http://lists.b9.com/pipermail/clsql/2008-August.txt.gz
conten into the file /tmp/mails.gz
Or so I thought.
I've got an end of file error. Doesn't
while c
handle eof?
I finally let drakma:http-request return an array and wrote that array
into a file, no problem there.
However I'm wondering how to copy a stream into a file
i) correctly and
ii) possible faster than byte by byte
Reply With Quote
  #2  
Old 08-22-2008, 06:30 PM
Thomas A. Russ
Guest
 
Default Re: copy stream into file: eof error

olivier.buechel@gmail.com writes:

> (loop for c = (read-byte stream)
> while c do
> (write-byte c f)))
> (close stream))

....
> I've got an end of file error. Doesn't
> while c
> handle eof?


Well, yes. But the default handling of EOF by READ-BYTE is to signal an
error. See

<http://www.lispworks.com/documentation/HyperSpec/Body/f_rd_by.htm>

What you want is

(read-byte stream nil nil)

One rationale for that is that in the normal case, the return value
from READ-BYTE will always be a byte value.

> I finally let drakma:http-request return an array and wrote that array
> into a file, no problem there.
> However I'm wondering how to copy a stream into a file
> i) correctly and


a) Use the suggestion above
b) Use HANDLER-CASE to handle the EOF condition.

> ii) possible faster than byte by byte


use READ-SEQUENCE and WRITE-SEQUENCE


--
Thomas A. Russ, USC/Information Sciences Institute
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 02:03 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.