From: Pisin Bootvong
Subject: How to make an in memory input/output stream?
Date: 
Message-ID: <1129719010.155234.316900@g47g2000cwa.googlegroups.com>
Hi,

I am trying to write some binary data.
Now I want to have an easy way to test the data I wrote to the stream.

Knowing (with-output-to-string) and (with-input-from-string) I am
thinking of something along this style, but with element-type as 'byte.

(let ((io (make-buffer)))
  (write-the-binary (buffer-output-stream io))
  (test-the-binary (buffer-input-stream io)))

However, in Hypersepc there is no way to create an output stream that
write to memory. Is there any portable way to do this? I know that I
could write to a temp file and read back, but it doesn't seems like the
clean way to do it.

Regards,

From: Duane Rettig
Subject: Re: How to make an in memory input/output stream?
Date: 
Message-ID: <4u0fdo78x.fsf@franz.com>
"Pisin Bootvong" <··········@gmail.com> writes:

> Hi,
>
> I am trying to write some binary data.
> Now I want to have an easy way to test the data I wrote to the stream.
>
> Knowing (with-output-to-string) and (with-input-from-string) I am
> thinking of something along this style, but with element-type as 'byte.
>
> (let ((io (make-buffer)))
>   (write-the-binary (buffer-output-stream io))
>   (test-the-binary (buffer-input-stream io)))
>
> However, in Hypersepc there is no way to create an output stream that
> write to memory. Is there any portable way to do this? I know that I
> could write to a temp file and read back, but it doesn't seems like the
> clean way to do it.

Normally, CL only provides you with with-output-to-string as a direct
way to write to memory.  However, if you have a Lisp that has implemented
simple-streams, you can also use with-output-to-buffer as well:

http://www.franz.com/support/documentation/7.0/doc/operators/excl/with-output-to-buffer.htm

It is similar to w-o-t-s, except that the buffer needs no fill pointer.

For example:

CL-USER(1): (defparameter x (make-array 10 :element-type '(unsigned-byte 8)))
X
CL-USER(2): (with-output-to-buffer (st x) (format st "Hello!"))
NIL
CL-USER(3): x
#(72 101 108 108 111 33 0 0 0 0)
CL-USER(4): 

You can also do things more directly if you wish, using
make-buffer-output-stream and get-output-stream-buffer:

http://www.franz.com/support/documentation/7.0/doc/operators/excl/make-buffer-output-stream.htm
http://www.franz.com/support/documentation/7.0/doc/operators/excl/get-output-stream-buffer.htm

-- 
Duane Rettig    ·····@franz.com    Franz Inc.  http://www.franz.com/
555 12th St., Suite 1450               http://www.555citycenter.com/
Oakland, Ca. 94607        Phone: (510) 452-2000; Fax: (510) 452-0182   
From: Lars Brinkhoff
Subject: Re: How to make an in memory input/output stream?
Date: 
Message-ID: <85k6g9bq9i.fsf@junk.nocrew.org>
"Pisin Bootvong" <··········@gmail.com> writes:
> I am trying to write some binary data.  Now I want to have an easy
> way to test the data I wrote to the stream.  Knowing
> (with-output-to-string) and (with-input-from-string) I am thinking
> of something along this style, but with element-type as 'byte.

Would it be ok to write to an adjustable vector?

  (defun make-buffer (&optional (initial-size 1024))
    (make-array initial-size
                :element-type '(unsigned-byte 8)
                :fill-pointer 0
                :adjustable t))

  (defun write-byte-to-buffer (byte buffer)
    (vector-push-extend byte buffer))

  (defun write-buffer-to-stream (buffer stream)
    (write-sequence buffer stream))