<div dir="ltr">Hi - <br><br>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. <br>
<br>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.<br>
<br>Has anyone seen such an issue before? Any pointer is appreciated.<br><br>My implementation below...<br><br>yc<br><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">;; afile.ss ;; (load "afile.ss") </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">;; provide read closure over the wrapped port </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">(define (read-in port)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (lambda (bytes)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (let* ((len (bytes-length bytes))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (bytes-in (read-bytes len</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> port)))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (cond ((eof-object? bytes-in) eof)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (else</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (bytes-copy! bytes 0</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> bytes-in)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> len)))))</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">;; provide peek closure over the wrapped port </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">(define (peek-in port)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (lambda (bytes skip progress-evt)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (let* ((len (bytes-length bytes))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (peeked (peek-bytes len</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> skip port)))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (cond ((eof-object? peeked) eof)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (else (bytes-copy! bytes 0</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> peeked)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> len)))))</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">;; provide the close closure over the wrapped port</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">(define (close-in port)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (lambda ()</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (close-input-port port)))</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">;; the function that returns an input port to wrap another input port </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">(define (open-input-afile path)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (let ((port (open-input-file path)))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (make-input-port path</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (read-in port)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (peek-in port)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (close-in port))))</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">;; test <br></span><span style="font-family: courier new,monospace;">(require scheme/port)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">(define path "test-out.txt")</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">(define out (open-output-file path #:exists 'replace))</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">(display "test data\ntest data\ntest data\ntest data\n" out)</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">(close-output-port out)</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">(define in (open-input-afile path))</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">(define out (open-output-bytes))</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">(copy-port in out)</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">(close-input-port in)</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">(get-output-bytes out) ;; a bunch of random data will be appended...</span><br style="font-family: courier new,monospace;">
<br></div>