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.private io.streams.sequence kernel namespaces
5 sequences sequences.private ;
6 IN: io.streams.byte-array
8 M: byte-vector stream-element-type drop +byte+ ;
10 : <byte-writer> ( encoding -- stream )
11 512 <byte-vector> swap <encoder> ;
13 : with-byte-writer ( encoding quot -- byte-array )
14 [ <byte-writer> ] dip [ output-stream get ] compose with-output-stream*
15 dup encoder? [ stream>> ] when >byte-array ; inline
17 TUPLE: byte-reader { underlying byte-array read-only } { i array-capacity } ;
19 M: byte-reader stream-element-type drop +byte+ ;
21 M: byte-reader stream-read-partial stream-read ;
22 M: byte-reader stream-read sequence-read ;
23 M: byte-reader stream-read1 sequence-read1 ;
24 M: byte-reader stream-read-until sequence-read-until ;
25 M: byte-reader dispose drop ;
27 M: byte-reader stream-tell i>> ;
28 M: byte-reader stream-seek (stream-seek) ;
30 : <byte-reader> ( byte-array encoding -- stream )
31 [ B{ } like 0 byte-reader boa ] dip <decoder> ;
33 : with-byte-reader ( byte-array encoding quot -- )
34 [ <byte-reader> ] dip with-input-stream* ; inline