From: Adam Warner
Subject: Vector streams
Date: 
Message-ID: <alcku6$1oiup6$1@ID-105510.news.dfncis.de>
Hi all,

What ways are there to create a non-file/pipe/socket binary stream (on
CLISP)? I'm currently simulating one by:

1. Setting a binary faithful character encoding (iso-8859-1 and Unix
newlines).

2. Writing bytes to a with-output-to-string character stream using
(write-char (code-char byte)).

3. Finally using CLISP's convert-string-to-bytes to convert the faithfully
reproduced binary data to a byte vector representation.

What I'm looking for is a with-output-to-vector style command that will
allow me to write binary data to a fill-adjustable vector with
element-type (unsigned-byte 8).

The only candidate appears to be a Gray stream, specifically a
FUNDAMENTAL-BINARY-OUTPUT-STREAM. I'd appreciate if anyone could show me
how to code a Gray stream. I'm hitting the fact that I've yet to
understand the CLOS: http://clisp.sourceforge.net/impnotes/clos-stream.html

I haven't been able to locate any example code. I don't think CLISP
supports simple streams so that's not an option.

Many thanks,
Adam
From: Adam Warner
Subject: Re: Vector streams
Date: 
Message-ID: <alf908$1o0qfe$1@ID-105510.news.dfncis.de>
To follow up:

> What ways are there to create a non-file/pipe/socket binary stream (on
> CLISP)? I'm currently simulating one by:
> 
> 1. Setting a binary faithful character encoding (iso-8859-1 and Unix
> newlines).
> 
> 2. Writing bytes to a with-output-to-string character stream using
> (write-char (code-char byte)).
> 
> 3. Finally using CLISP's convert-string-to-bytes to convert the
> faithfully reproduced binary data to a byte vector representation.

One good answer (thanks Paul and Sam) is to bypass streams by using the
function VECTOR-PUSH-EXTEND.

And BTW if you need to write a string with 8-bit characters to a vector
that has an unsigned 8-bit element type, MAP works a treat:

(setf vector (make-array 1000 :adjustable t :fill-pointer 0
  :element-type '(unsigned-byte 8)))

(setf string "this string only contains 8-bit characters because the
character encoding is UTF-8 and only ASCII characters appear in the
string")

(map nil #'(lambda (x) (vector-push-extend (char-code x) vector)) string)

[4]> vector
#(116 104 105 115 32 115 116 114 105 110 103 32 111 110 108 121 32 99 111
  110 116 97 105 110 115 32 56 45 98 105 116 32 99 104 97 114 97 99 116
  101 114 115 32 98 101 99 97 117 115 101 32 116 104 101 10 99 104 97 114
  97 99 116 101 114 32 101 110 99 111 100 105 110 103 32 105 115 32 85 84
  70 45 56 32 97 110 100 32 111 110 108 121 32 65 83 67 73 73 32 99 104 97
  114 97 99 116 101 114 115 32 97 112 112 101 97 114 32 105 110 32 116 104
  101 10 115 116 114 105 110 103)

Regards,
Adam