[plt-scheme] copy-port and custom-port

From: YC (yinso.chen at gmail.com)
Date: Wed Jul 23 14:20:36 EDT 2008

Hi -

I've created custom input port to wrap another input port - and when passing
the port to `copy-port`, besides copying the data inside the wrapped input
port, `copy-port` also appears to copy a bunch of random binary data after
the eof position.

When reading from the port via functions like `read` - it returns eof
properly, but copy-port blows right pass the position and keeps on copying
some random data.  Obviously this is a bug in my simplistic implementation,
but I'm not sure how to find/tackle the problem.

Has anyone seen such an issue before?  Any pointer is appreciated.

My implementation below...


;; afile.ss ;; (load "afile.ss")
;; provide read closure over the wrapped port
(define (read-in port)
  (lambda (bytes)
    (let* ((len (bytes-length bytes))
           (bytes-in (read-bytes len
      (cond ((eof-object? bytes-in) eof)
             (bytes-copy! bytes 0

;; provide peek closure over the wrapped port
(define (peek-in port)
  (lambda (bytes skip progress-evt)
    (let* ((len (bytes-length bytes))
           (peeked (peek-bytes len
                               skip port)))
      (cond ((eof-object? peeked) eof)
            (else (bytes-copy! bytes 0

;; provide the close closure over the wrapped port
(define (close-in port)
  (lambda ()
    (close-input-port port)))

;; the function that returns an input port to wrap another input port
(define (open-input-afile path)
  (let ((port (open-input-file path)))
    (make-input-port path
                     (read-in port)
                     (peek-in port)
                     (close-in port))))

;; test
(require scheme/port)
(define path "test-out.txt")
(define out (open-output-file path #:exists 'replace))

(display "test data\ntest data\ntest data\ntest data\n" out)

(close-output-port out)

(define in (open-input-afile path))

(define out (open-output-bytes))

(copy-port in out)

(close-input-port in)

(get-output-bytes out) ;; a bunch of random data will be appended...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20080723/851a32c8/attachment.html>

Posted on the users mailing list.