1 ! Copyright (C) 2008, 2009 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors byte-arrays byte-vectors destructors io
4 io.encodings io.streams.sequence kernel math namespaces
5 sequences sequences.private ;
6 IN: io.streams.byte-array
8 INSTANCE: byte-vector output-stream
9 M: byte-vector stream-element-type drop +byte+ ; inline
10 M: byte-vector stream-tell length ; inline
12 : <byte-writer> ( encoding -- stream )
13 512 <byte-vector> swap <encoder> ; inline
15 : with-byte-writer ( encoding quot -- byte-array )
16 [ <byte-writer> ] dip [ with-output-stream* ] 2keep drop
17 dup encoder? [ stream>> ] when >byte-array ; inline
19 TUPLE: byte-reader { underlying byte-array read-only } { i array-capacity } ;
20 INSTANCE: byte-reader input-stream
22 M: byte-reader stream-element-type drop +byte+ ; inline
24 M: byte-reader stream-read-unsafe sequence-read-unsafe ;
25 M: byte-reader stream-read1 sequence-read1 ;
26 M: byte-reader stream-read-until sequence-read-until ;
27 M: byte-reader dispose drop ;
29 M: byte-reader stream-tell i>> ;
30 M: byte-reader stream-seek sequence-seek ;
31 M: byte-reader stream-seekable? drop t ; inline
32 M: byte-reader stream-length underlying>> length ; inline
34 : <byte-reader> ( byte-array encoding -- stream )
35 [ B{ } like 0 byte-reader boa ] dip <decoder> ;
37 : with-byte-reader ( byte-array encoding quot -- )
38 [ <byte-reader> ] dip with-input-stream* ; inline